blob: e20aad1987b8b77fa03ef94614b47670959e16b4 [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 Pirkof34e8bf2018-07-23 09:23:04 +020042static 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
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020060static const struct tcf_proto_ops *
61tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
62{
63 const struct tcf_proto_ops *ops;
64
65 ops = __tcf_proto_lookup_ops(kind);
66 if (ops)
67 return ops;
68#ifdef CONFIG_MODULES
69 rtnl_unlock();
70 request_module("cls_%s", kind);
71 rtnl_lock();
72 ops = __tcf_proto_lookup_ops(kind);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
76 * this using -EAGAIN.
77 */
78 if (ops) {
79 module_put(ops->owner);
80 return ERR_PTR(-EAGAIN);
81 }
82#endif
83 NL_SET_ERR_MSG(extack, "TC classifier not found");
84 return ERR_PTR(-ENOENT);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Register(unregister) new classifier type */
88
89int register_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
WANG Cong36272872013-12-15 20:15:11 -080091 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int rc = -EEXIST;
93
94 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080095 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!strcmp(ops->kind, t->kind))
97 goto out;
98
WANG Cong36272872013-12-15 20:15:11 -080099 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 rc = 0;
101out:
102 write_unlock(&cls_mod_lock);
103 return rc;
104}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800105EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Cong Wang7aa00452017-10-26 18:24:28 -0700107static struct workqueue_struct *tc_filter_wq;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
110{
WANG Cong36272872013-12-15 20:15:11 -0800111 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int rc = -ENOENT;
113
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
116 */
117 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700118 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800121 list_for_each_entry(t, &tcf_proto_base, head) {
122 if (t == ops) {
123 list_del(&t->head);
124 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800126 }
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 write_unlock(&cls_mod_lock);
129 return rc;
130}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800131EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Cong Wangaaa908f2018-05-23 15:26:53 -0700133bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700134{
Cong Wangaaa908f2018-05-23 15:26:53 -0700135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700137}
138EXPORT_SYMBOL(tcf_queue_work);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Select new prio value from the range, managed by kernel. */
141
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800142static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000147 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jiri Pirko79619732017-05-17 11:07:58 +0200149 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jiri Pirko33a48922017-02-09 14:38:57 +0100152static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100155{
156 struct tcf_proto *tp;
157 int err;
158
159 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
160 if (!tp)
161 return ERR_PTR(-ENOBUFS);
162
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200166 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100167 }
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
170 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200171 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100172
173 err = tp->ops->init(tp);
174 if (err) {
175 module_put(tp->ops->owner);
176 goto errout;
177 }
178 return tp;
179
180errout:
181 kfree(tp);
182 return ERR_PTR(err);
183}
184
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800185static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100187{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800188 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100191}
192
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100193struct tcf_filter_chain_list_item {
194 struct list_head list;
195 tcf_chain_head_change_t *chain_head_change;
196 void *chain_head_change_priv;
197};
198
Jiri Pirko5bc17012017-05-17 11:08:01 +0200199static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
200 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200201{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200202 struct tcf_chain *chain;
203
204 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
205 if (!chain)
206 return NULL;
207 list_add_tail(&chain->list, &block->chain_list);
208 chain->block = block;
209 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700210 chain->refcnt = 1;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200211 if (!chain->index)
212 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200213 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200214}
215
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100216static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
217 struct tcf_proto *tp_head)
218{
219 if (item->chain_head_change)
220 item->chain_head_change(tp_head, item->chain_head_change_priv);
221}
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200222
223static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100225{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100226 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200227 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100228
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200229 if (chain->index)
230 return;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100232 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100233}
234
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200235static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100236{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100237 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100238
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200239 tcf_chain0_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100240 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200241 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800242 tcf_proto_destroy(tp, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100243 tp = rtnl_dereference(chain->filter_chain);
244 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100245 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200246}
247
248static void tcf_chain_destroy(struct tcf_chain *chain)
249{
Cong Wangefbf7892017-12-04 10:48:18 -0800250 struct tcf_block *block = chain->block;
251
Cong Wange2ef7542017-09-11 16:33:31 -0700252 list_del(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200253 if (!chain->index)
254 block->chain0.chain = NULL;
Cong Wange2ef7542017-09-11 16:33:31 -0700255 kfree(chain);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200256 if (list_empty(&block->chain_list) && block->refcnt == 0)
Cong Wangefbf7892017-12-04 10:48:18 -0800257 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700258}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200259
Cong Wange2ef7542017-09-11 16:33:31 -0700260static void tcf_chain_hold(struct tcf_chain *chain)
261{
262 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200263}
264
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200265static void tcf_chain_hold_by_act(struct tcf_chain *chain)
266{
267 ++chain->action_refcnt;
268}
269
270static void tcf_chain_release_by_act(struct tcf_chain *chain)
271{
272 --chain->action_refcnt;
273}
274
275static bool tcf_chain_is_zombie(struct tcf_chain *chain)
276{
277 /* In case all the references are action references, this
278 * chain is a zombie and should not be listed in the chain
279 * dump list.
280 */
281 return chain->refcnt == chain->action_refcnt;
282}
283
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200284static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
285 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200286{
287 struct tcf_chain *chain;
288
289 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200290 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700291 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200292 }
293 return NULL;
294}
295
296static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
297 u32 seq, u16 flags, int event, bool unicast);
298
299struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
300 bool create)
301{
302 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
303
304 if (chain) {
305 tcf_chain_hold(chain);
306 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200307 }
Jiri Pirko80532382017-09-06 13:14:19 +0200308
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200309 if (!create)
310 return NULL;
311 chain = tcf_chain_create(block, chain_index);
312 if (!chain)
313 return NULL;
314 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
315 RTM_NEWCHAIN, false);
316 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200317}
318EXPORT_SYMBOL(tcf_chain_get);
319
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200320struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
321{
322 struct tcf_chain *chain = tcf_chain_get(block, chain_index, true);
323
324 tcf_chain_hold_by_act(chain);
325 return chain;
326}
327EXPORT_SYMBOL(tcf_chain_get_by_act);
328
Jiri Pirko9f407f12018-07-23 09:23:07 +0200329static void tc_chain_tmplt_del(struct tcf_chain *chain);
330
Jiri Pirko5bc17012017-05-17 11:08:01 +0200331void tcf_chain_put(struct tcf_chain *chain)
332{
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200333 if (--chain->refcnt == 0) {
334 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200335 tc_chain_tmplt_del(chain);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200336 tcf_chain_destroy(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200337 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200338}
339EXPORT_SYMBOL(tcf_chain_put);
340
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200341void tcf_chain_put_by_act(struct tcf_chain *chain)
342{
343 tcf_chain_release_by_act(chain);
344 tcf_chain_put(chain);
345}
346EXPORT_SYMBOL(tcf_chain_put_by_act);
347
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200348static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
349{
350 if (chain->explicitly_created)
351 tcf_chain_put(chain);
352}
353
Jiri Pirkocaa72602018-01-17 11:46:50 +0100354static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200355{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100356 return block->offloadcnt;
357}
358
359static int tcf_block_offload_cmd(struct tcf_block *block,
360 struct net_device *dev,
361 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700362 enum tc_block_command command,
363 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100364{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200365 struct tc_block_offload bo = {};
366
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200367 bo.command = command;
368 bo.binder_type = ei->binder_type;
369 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700370 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100371 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200372}
373
Jiri Pirkocaa72602018-01-17 11:46:50 +0100374static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700375 struct tcf_block_ext_info *ei,
376 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200377{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100378 struct net_device *dev = q->dev_queue->dev;
379 int err;
380
381 if (!dev->netdev_ops->ndo_setup_tc)
382 goto no_offload_dev_inc;
383
384 /* If tc offload feature is disabled and the block we try to bind
385 * to already has some offloaded filters, forbid to bind.
386 */
John Hurley60513bd2018-06-25 14:30:04 -0700387 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
388 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100389 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700390 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100391
John Hurley60513bd2018-06-25 14:30:04 -0700392 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100393 if (err == -EOPNOTSUPP)
394 goto no_offload_dev_inc;
395 return err;
396
397no_offload_dev_inc:
398 if (tcf_block_offload_in_use(block))
399 return -EOPNOTSUPP;
400 block->nooffloaddevcnt++;
401 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200402}
403
404static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
405 struct tcf_block_ext_info *ei)
406{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100407 struct net_device *dev = q->dev_queue->dev;
408 int err;
409
410 if (!dev->netdev_ops->ndo_setup_tc)
411 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700412 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100413 if (err == -EOPNOTSUPP)
414 goto no_offload_dev_dec;
415 return;
416
417no_offload_dev_dec:
418 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200419}
420
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100421static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200422tcf_chain0_head_change_cb_add(struct tcf_block *block,
423 struct tcf_block_ext_info *ei,
424 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100425{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200426 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100427 struct tcf_filter_chain_list_item *item;
428
429 item = kmalloc(sizeof(*item), GFP_KERNEL);
430 if (!item) {
431 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
432 return -ENOMEM;
433 }
434 item->chain_head_change = ei->chain_head_change;
435 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200436 if (chain0 && chain0->filter_chain)
437 tcf_chain_head_change_item(item, chain0->filter_chain);
438 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100439 return 0;
440}
441
442static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200443tcf_chain0_head_change_cb_del(struct tcf_block *block,
444 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100445{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200446 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100447 struct tcf_filter_chain_list_item *item;
448
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200449 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100450 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
451 (item->chain_head_change == ei->chain_head_change &&
452 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200453 if (chain0)
454 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100455 list_del(&item->list);
456 kfree(item);
457 return;
458 }
459 }
460 WARN_ON(1);
461}
462
Jiri Pirko48617382018-01-17 11:46:46 +0100463struct tcf_net {
464 struct idr idr;
465};
466
467static unsigned int tcf_net_id;
468
469static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100470 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100471{
Jiri Pirko48617382018-01-17 11:46:46 +0100472 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100473
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100474 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
475 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100476}
477
Jiri Pirko48617382018-01-17 11:46:46 +0100478static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200479{
Jiri Pirko48617382018-01-17 11:46:46 +0100480 struct tcf_net *tn = net_generic(net, tcf_net_id);
481
Matthew Wilcox9c160942017-11-28 09:48:43 -0500482 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100483}
484
485static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100486 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100487 struct netlink_ext_ack *extack)
488{
489 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200490
Jiri Pirko48617382018-01-17 11:46:46 +0100491 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500492 if (!block) {
493 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100494 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500495 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200496 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200497 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100498 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200499 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200500
Jiri Pirko48617382018-01-17 11:46:46 +0100501 block->refcnt = 1;
502 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100503 block->index = block_index;
504
505 /* Don't store q pointer for blocks which are shared */
506 if (!tcf_block_shared(block))
507 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100508 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100509}
510
511static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
512{
513 struct tcf_net *tn = net_generic(net, tcf_net_id);
514
Matthew Wilcox322d8842017-11-28 10:01:24 -0500515 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100516}
517
Vlad Buslovc431f892018-05-31 09:52:53 +0300518/* Find tcf block.
519 * Set q, parent, cl when appropriate.
520 */
521
522static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
523 u32 *parent, unsigned long *cl,
524 int ifindex, u32 block_index,
525 struct netlink_ext_ack *extack)
526{
527 struct tcf_block *block;
528
529 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
530 block = tcf_block_lookup(net, block_index);
531 if (!block) {
532 NL_SET_ERR_MSG(extack, "Block of given index was not found");
533 return ERR_PTR(-EINVAL);
534 }
535 } else {
536 const struct Qdisc_class_ops *cops;
537 struct net_device *dev;
538
539 /* Find link */
540 dev = __dev_get_by_index(net, ifindex);
541 if (!dev)
542 return ERR_PTR(-ENODEV);
543
544 /* Find qdisc */
545 if (!*parent) {
546 *q = dev->qdisc;
547 *parent = (*q)->handle;
548 } else {
549 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
550 if (!*q) {
551 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
552 return ERR_PTR(-EINVAL);
553 }
554 }
555
556 /* Is it classful? */
557 cops = (*q)->ops->cl_ops;
558 if (!cops) {
559 NL_SET_ERR_MSG(extack, "Qdisc not classful");
560 return ERR_PTR(-EINVAL);
561 }
562
563 if (!cops->tcf_block) {
564 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
565 return ERR_PTR(-EOPNOTSUPP);
566 }
567
568 /* Do we search for filter, attached to class? */
569 if (TC_H_MIN(*parent)) {
570 *cl = cops->find(*q, *parent);
571 if (*cl == 0) {
572 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
573 return ERR_PTR(-ENOENT);
574 }
575 }
576
577 /* And the last stroke */
578 block = cops->tcf_block(*q, *cl, extack);
579 if (!block)
580 return ERR_PTR(-EINVAL);
581 if (tcf_block_shared(block)) {
582 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
583 return ERR_PTR(-EOPNOTSUPP);
584 }
585 }
586
587 return block;
588}
589
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100590struct tcf_block_owner_item {
591 struct list_head list;
592 struct Qdisc *q;
593 enum tcf_block_binder_type binder_type;
594};
595
596static void
597tcf_block_owner_netif_keep_dst(struct tcf_block *block,
598 struct Qdisc *q,
599 enum tcf_block_binder_type binder_type)
600{
601 if (block->keep_dst &&
602 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
603 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
604 netif_keep_dst(qdisc_dev(q));
605}
606
607void tcf_block_netif_keep_dst(struct tcf_block *block)
608{
609 struct tcf_block_owner_item *item;
610
611 block->keep_dst = true;
612 list_for_each_entry(item, &block->owner_list, list)
613 tcf_block_owner_netif_keep_dst(block, item->q,
614 item->binder_type);
615}
616EXPORT_SYMBOL(tcf_block_netif_keep_dst);
617
618static int tcf_block_owner_add(struct tcf_block *block,
619 struct Qdisc *q,
620 enum tcf_block_binder_type binder_type)
621{
622 struct tcf_block_owner_item *item;
623
624 item = kmalloc(sizeof(*item), GFP_KERNEL);
625 if (!item)
626 return -ENOMEM;
627 item->q = q;
628 item->binder_type = binder_type;
629 list_add(&item->list, &block->owner_list);
630 return 0;
631}
632
633static void tcf_block_owner_del(struct tcf_block *block,
634 struct Qdisc *q,
635 enum tcf_block_binder_type binder_type)
636{
637 struct tcf_block_owner_item *item;
638
639 list_for_each_entry(item, &block->owner_list, list) {
640 if (item->q == q && item->binder_type == binder_type) {
641 list_del(&item->list);
642 kfree(item);
643 return;
644 }
645 }
646 WARN_ON(1);
647}
648
Jiri Pirko48617382018-01-17 11:46:46 +0100649int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
650 struct tcf_block_ext_info *ei,
651 struct netlink_ext_ack *extack)
652{
653 struct net *net = qdisc_net(q);
654 struct tcf_block *block = NULL;
655 bool created = false;
656 int err;
657
658 if (ei->block_index) {
659 /* block_index not 0 means the shared block is requested */
660 block = tcf_block_lookup(net, ei->block_index);
661 if (block)
662 block->refcnt++;
663 }
664
665 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100666 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100667 if (IS_ERR(block))
668 return PTR_ERR(block);
669 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100670 if (tcf_block_shared(block)) {
671 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100672 if (err)
673 goto err_block_insert;
674 }
675 }
676
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100677 err = tcf_block_owner_add(block, q, ei->binder_type);
678 if (err)
679 goto err_block_owner_add;
680
681 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
682
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200683 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100684 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200685 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100686
John Hurley60513bd2018-06-25 14:30:04 -0700687 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100688 if (err)
689 goto err_block_offload_bind;
690
Jiri Pirko6529eab2017-05-17 11:07:55 +0200691 *p_block = block;
692 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200693
Jiri Pirkocaa72602018-01-17 11:46:50 +0100694err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200695 tcf_chain0_head_change_cb_del(block, ei);
696err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100697 tcf_block_owner_del(block, q, ei->binder_type);
698err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100699 if (created) {
700 if (tcf_block_shared(block))
701 tcf_block_remove(block, net);
702err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100703 kfree(block);
704 } else {
705 block->refcnt--;
706 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200707 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200708}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200709EXPORT_SYMBOL(tcf_block_get_ext);
710
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100711static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
712{
713 struct tcf_proto __rcu **p_filter_chain = priv;
714
715 rcu_assign_pointer(*p_filter_chain, tp_head);
716}
717
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200718int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500719 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
720 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200721{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100722 struct tcf_block_ext_info ei = {
723 .chain_head_change = tcf_chain_head_change_dflt,
724 .chain_head_change_priv = p_filter_chain,
725 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200726
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100727 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500728 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200729}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200730EXPORT_SYMBOL(tcf_block_get);
731
Cong Wang7aa00452017-10-26 18:24:28 -0700732/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100733 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700734 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100735void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900736 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700737{
Cong Wangefbf7892017-12-04 10:48:18 -0800738 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700739
David S. Millerc30abd52017-12-16 22:11:55 -0500740 if (!block)
741 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200742 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100743 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100744
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200745 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100746 if (tcf_block_shared(block))
747 tcf_block_remove(block, block->net);
748
749 /* Hold a refcnt for all chains, so that they don't disappear
750 * while we are iterating.
751 */
752 list_for_each_entry(chain, &block->chain_list, list)
753 tcf_chain_hold(chain);
754
755 list_for_each_entry(chain, &block->chain_list, list)
756 tcf_chain_flush(chain);
757 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700758
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100759 tcf_block_offload_unbind(block, q, ei);
760
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200761 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100762 /* At this point, all the chains should have refcnt >= 1. */
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200763 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
764 tcf_chain_put_explicitly_created(chain);
Jiri Pirko48617382018-01-17 11:46:46 +0100765 tcf_chain_put(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200766 }
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100767
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200768 block->refcnt--;
769 if (list_empty(&block->chain_list))
770 kfree(block);
Jiri Pirko48617382018-01-17 11:46:46 +0100771 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200772}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200773EXPORT_SYMBOL(tcf_block_put_ext);
774
775void tcf_block_put(struct tcf_block *block)
776{
777 struct tcf_block_ext_info ei = {0, };
778
Jiri Pirko4853f122017-12-21 13:13:59 +0100779 if (!block)
780 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100781 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200782}
David S. Millere1ea2f92017-10-30 14:10:01 +0900783
Jiri Pirko6529eab2017-05-17 11:07:55 +0200784EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100785
Jiri Pirkoacb67442017-10-19 15:50:31 +0200786struct tcf_block_cb {
787 struct list_head list;
788 tc_setup_cb_t *cb;
789 void *cb_ident;
790 void *cb_priv;
791 unsigned int refcnt;
792};
793
794void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
795{
796 return block_cb->cb_priv;
797}
798EXPORT_SYMBOL(tcf_block_cb_priv);
799
800struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
801 tc_setup_cb_t *cb, void *cb_ident)
802{ struct tcf_block_cb *block_cb;
803
804 list_for_each_entry(block_cb, &block->cb_list, list)
805 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
806 return block_cb;
807 return NULL;
808}
809EXPORT_SYMBOL(tcf_block_cb_lookup);
810
811void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
812{
813 block_cb->refcnt++;
814}
815EXPORT_SYMBOL(tcf_block_cb_incref);
816
817unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
818{
819 return --block_cb->refcnt;
820}
821EXPORT_SYMBOL(tcf_block_cb_decref);
822
John Hurley32636742018-06-25 14:30:10 -0700823static int
824tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
825 void *cb_priv, bool add, bool offload_in_use,
826 struct netlink_ext_ack *extack)
827{
828 struct tcf_chain *chain;
829 struct tcf_proto *tp;
830 int err;
831
832 list_for_each_entry(chain, &block->chain_list, list) {
833 for (tp = rtnl_dereference(chain->filter_chain); tp;
834 tp = rtnl_dereference(tp->next)) {
835 if (tp->ops->reoffload) {
836 err = tp->ops->reoffload(tp, add, cb, cb_priv,
837 extack);
838 if (err && add)
839 goto err_playback_remove;
840 } else if (add && offload_in_use) {
841 err = -EOPNOTSUPP;
842 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
843 goto err_playback_remove;
844 }
845 }
846 }
847
848 return 0;
849
850err_playback_remove:
851 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
852 extack);
853 return err;
854}
855
Jiri Pirkoacb67442017-10-19 15:50:31 +0200856struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
857 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700858 void *cb_priv,
859 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200860{
861 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700862 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200863
John Hurley32636742018-06-25 14:30:10 -0700864 /* Replay any already present rules */
865 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
866 tcf_block_offload_in_use(block),
867 extack);
868 if (err)
869 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100870
Jiri Pirkoacb67442017-10-19 15:50:31 +0200871 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
872 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100873 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200874 block_cb->cb = cb;
875 block_cb->cb_ident = cb_ident;
876 block_cb->cb_priv = cb_priv;
877 list_add(&block_cb->list, &block->cb_list);
878 return block_cb;
879}
880EXPORT_SYMBOL(__tcf_block_cb_register);
881
882int tcf_block_cb_register(struct tcf_block *block,
883 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700884 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200885{
886 struct tcf_block_cb *block_cb;
887
John Hurley60513bd2018-06-25 14:30:04 -0700888 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
889 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500890 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200891}
892EXPORT_SYMBOL(tcf_block_cb_register);
893
John Hurley32636742018-06-25 14:30:10 -0700894void __tcf_block_cb_unregister(struct tcf_block *block,
895 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200896{
John Hurley32636742018-06-25 14:30:10 -0700897 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
898 false, tcf_block_offload_in_use(block),
899 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200900 list_del(&block_cb->list);
901 kfree(block_cb);
902}
903EXPORT_SYMBOL(__tcf_block_cb_unregister);
904
905void tcf_block_cb_unregister(struct tcf_block *block,
906 tc_setup_cb_t *cb, void *cb_ident)
907{
908 struct tcf_block_cb *block_cb;
909
910 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
911 if (!block_cb)
912 return;
John Hurley32636742018-06-25 14:30:10 -0700913 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200914}
915EXPORT_SYMBOL(tcf_block_cb_unregister);
916
917static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
918 void *type_data, bool err_stop)
919{
920 struct tcf_block_cb *block_cb;
921 int ok_count = 0;
922 int err;
923
David S. Miller9a99dc12018-06-06 13:55:47 -0400924 /* Make sure all netdevs sharing this block are offload-capable. */
925 if (block->nooffloaddevcnt && err_stop)
926 return -EOPNOTSUPP;
927
Jiri Pirkoacb67442017-10-19 15:50:31 +0200928 list_for_each_entry(block_cb, &block->cb_list, list) {
929 err = block_cb->cb(type, type_data, block_cb->cb_priv);
930 if (err) {
931 if (err_stop)
932 return err;
933 } else {
934 ok_count++;
935 }
936 }
937 return ok_count;
938}
939
Jiri Pirko87d83092017-05-17 11:07:54 +0200940/* Main classifier routine: scans classifier chain attached
941 * to this qdisc, (optionally) tests for protocol and asks
942 * specific classifiers.
943 */
944int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
945 struct tcf_result *res, bool compat_mode)
946{
947 __be16 protocol = tc_skb_protocol(skb);
948#ifdef CONFIG_NET_CLS_ACT
949 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200950 const struct tcf_proto *orig_tp = tp;
951 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200952 int limit = 0;
953
954reclassify:
955#endif
956 for (; tp; tp = rcu_dereference_bh(tp->next)) {
957 int err;
958
959 if (tp->protocol != protocol &&
960 tp->protocol != htons(ETH_P_ALL))
961 continue;
962
963 err = tp->classify(skb, tp, res);
964#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200965 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200966 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200967 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200968 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200969 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200970 goto reset;
971 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200972#endif
973 if (err >= 0)
974 return err;
975 }
976
977 return TC_ACT_UNSPEC; /* signal: continue lookup */
978#ifdef CONFIG_NET_CLS_ACT
979reset:
980 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100981 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
982 tp->chain->block->index,
983 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200984 ntohs(tp->protocol));
985 return TC_ACT_SHOT;
986 }
987
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200988 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200989 protocol = tc_skb_protocol(skb);
990 goto reclassify;
991#endif
992}
993EXPORT_SYMBOL(tcf_classify);
994
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200995struct tcf_chain_info {
996 struct tcf_proto __rcu **pprev;
997 struct tcf_proto __rcu *next;
998};
999
1000static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1001{
1002 return rtnl_dereference(*chain_info->pprev);
1003}
1004
1005static void tcf_chain_tp_insert(struct tcf_chain *chain,
1006 struct tcf_chain_info *chain_info,
1007 struct tcf_proto *tp)
1008{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001009 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001010 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001011 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1012 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001013 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001014}
1015
1016static void tcf_chain_tp_remove(struct tcf_chain *chain,
1017 struct tcf_chain_info *chain_info,
1018 struct tcf_proto *tp)
1019{
1020 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1021
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001022 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001023 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001024 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001025 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001026}
1027
1028static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1029 struct tcf_chain_info *chain_info,
1030 u32 protocol, u32 prio,
1031 bool prio_allocate)
1032{
1033 struct tcf_proto **pprev;
1034 struct tcf_proto *tp;
1035
1036 /* Check the chain for existence of proto-tcf with this priority */
1037 for (pprev = &chain->filter_chain;
1038 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1039 if (tp->prio >= prio) {
1040 if (tp->prio == prio) {
1041 if (prio_allocate ||
1042 (tp->protocol != protocol && protocol))
1043 return ERR_PTR(-EINVAL);
1044 } else {
1045 tp = NULL;
1046 }
1047 break;
1048 }
1049 }
1050 chain_info->pprev = pprev;
1051 chain_info->next = tp ? tp->next : NULL;
1052 return tp;
1053}
1054
WANG Cong71203712017-08-07 15:26:50 -07001055static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001056 struct tcf_proto *tp, struct tcf_block *block,
1057 struct Qdisc *q, u32 parent, void *fh,
1058 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001059{
1060 struct tcmsg *tcm;
1061 struct nlmsghdr *nlh;
1062 unsigned char *b = skb_tail_pointer(skb);
1063
1064 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1065 if (!nlh)
1066 goto out_nlmsg_trim;
1067 tcm = nlmsg_data(nlh);
1068 tcm->tcm_family = AF_UNSPEC;
1069 tcm->tcm__pad1 = 0;
1070 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001071 if (q) {
1072 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1073 tcm->tcm_parent = parent;
1074 } else {
1075 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1076 tcm->tcm_block_index = block->index;
1077 }
WANG Cong71203712017-08-07 15:26:50 -07001078 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1079 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1080 goto nla_put_failure;
1081 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1082 goto nla_put_failure;
1083 if (!fh) {
1084 tcm->tcm_handle = 0;
1085 } else {
1086 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1087 goto nla_put_failure;
1088 }
1089 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1090 return skb->len;
1091
1092out_nlmsg_trim:
1093nla_put_failure:
1094 nlmsg_trim(skb, b);
1095 return -1;
1096}
1097
1098static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1099 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001100 struct tcf_block *block, struct Qdisc *q,
1101 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001102{
1103 struct sk_buff *skb;
1104 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1105
1106 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1107 if (!skb)
1108 return -ENOBUFS;
1109
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001110 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1111 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001112 kfree_skb(skb);
1113 return -EINVAL;
1114 }
1115
1116 if (unicast)
1117 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1118
1119 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1120 n->nlmsg_flags & NLM_F_ECHO);
1121}
1122
1123static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1124 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001125 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001126 u32 parent, void *fh, bool unicast, bool *last,
1127 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001128{
1129 struct sk_buff *skb;
1130 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1131 int err;
1132
1133 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1134 if (!skb)
1135 return -ENOBUFS;
1136
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001137 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1138 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001139 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001140 kfree_skb(skb);
1141 return -EINVAL;
1142 }
1143
Alexander Aring571acf22018-01-18 11:20:53 -05001144 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001145 if (err) {
1146 kfree_skb(skb);
1147 return err;
1148 }
1149
1150 if (unicast)
1151 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1152
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001153 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1154 n->nlmsg_flags & NLM_F_ECHO);
1155 if (err < 0)
1156 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1157 return err;
WANG Cong71203712017-08-07 15:26:50 -07001158}
1159
1160static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001161 struct tcf_block *block, struct Qdisc *q,
1162 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001163 struct tcf_chain *chain, int event)
1164{
1165 struct tcf_proto *tp;
1166
1167 for (tp = rtnl_dereference(chain->filter_chain);
1168 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001169 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001170 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001171}
1172
Vlad Buslovc431f892018-05-31 09:52:53 +03001173static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001174 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001176 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001177 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct tcmsg *t;
1179 u32 protocol;
1180 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001181 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001183 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001184 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001185 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001186 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001187 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001190 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001192 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Vlad Buslovc431f892018-05-31 09:52:53 +03001194 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001195 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001198 tp_created = 0;
1199
David Ahernc21ef3e2017-04-16 09:48:24 -07001200 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001201 if (err < 0)
1202 return err;
1203
David S. Miller942b8162012-06-26 21:48:50 -07001204 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 protocol = TC_H_MIN(t->tcm_info);
1206 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001207 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 parent = t->tcm_parent;
1209 cl = 0;
1210
1211 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001212 /* If no priority is provided by the user,
1213 * we allocate one.
1214 */
1215 if (n->nlmsg_flags & NLM_F_CREATE) {
1216 prio = TC_H_MAKE(0x80000000U, 0U);
1217 prio_allocate = true;
1218 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001219 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
1224 /* Find head of filter chain. */
1225
Vlad Buslovc431f892018-05-31 09:52:53 +03001226 block = tcf_block_find(net, &q, &parent, &cl,
1227 t->tcm_ifindex, t->tcm_block_index, extack);
1228 if (IS_ERR(block)) {
1229 err = PTR_ERR(block);
1230 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001231 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001232
1233 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1234 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001235 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001236 err = -EINVAL;
1237 goto errout;
1238 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001239 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001240 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001241 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001242 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001243 goto errout;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001246 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1247 prio, prio_allocate);
1248 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001249 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001250 err = PTR_ERR(tp);
1251 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 if (tp == NULL) {
1255 /* Proto-tcf does not exist, create new one */
1256
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001257 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001258 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001259 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Vlad Buslovc431f892018-05-31 09:52:53 +03001263 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001264 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 +01001265 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001269 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001270 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jiri Pirko33a48922017-02-09 14:38:57 +01001272 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001273 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001274 if (IS_ERR(tp)) {
1275 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto errout;
1277 }
Minoru Usui12186be2009-06-02 02:17:34 -07001278 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001279 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001280 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001281 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 fh = tp->ops->get(tp, t->tcm_handle);
1286
WANG Cong8113c092017-08-04 21:31:43 -07001287 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001288 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001289 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 +01001290 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001292 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001293 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1294 NL_SET_ERR_MSG(extack, "Filter already exists");
1295 err = -EEXIST;
1296 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Jiri Pirko9f407f12018-07-23 09:23:07 +02001299 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1300 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1301 err = -EINVAL;
1302 goto errout;
1303 }
1304
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001305 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001306 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1307 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001308 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001309 if (tp_created)
1310 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001311 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001312 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001313 } else {
1314 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001315 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001319 if (chain)
1320 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (err == -EAGAIN)
1322 /* Replay the request. */
1323 goto replay;
1324 return err;
1325}
1326
Vlad Buslovc431f892018-05-31 09:52:53 +03001327static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1328 struct netlink_ext_ack *extack)
1329{
1330 struct net *net = sock_net(skb->sk);
1331 struct nlattr *tca[TCA_MAX + 1];
1332 struct tcmsg *t;
1333 u32 protocol;
1334 u32 prio;
1335 u32 parent;
1336 u32 chain_index;
1337 struct Qdisc *q = NULL;
1338 struct tcf_chain_info chain_info;
1339 struct tcf_chain *chain = NULL;
1340 struct tcf_block *block;
1341 struct tcf_proto *tp = NULL;
1342 unsigned long cl = 0;
1343 void *fh = NULL;
1344 int err;
1345
1346 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1347 return -EPERM;
1348
1349 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1350 if (err < 0)
1351 return err;
1352
1353 t = nlmsg_data(n);
1354 protocol = TC_H_MIN(t->tcm_info);
1355 prio = TC_H_MAJ(t->tcm_info);
1356 parent = t->tcm_parent;
1357
1358 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1359 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1360 return -ENOENT;
1361 }
1362
1363 /* Find head of filter chain. */
1364
1365 block = tcf_block_find(net, &q, &parent, &cl,
1366 t->tcm_ifindex, t->tcm_block_index, extack);
1367 if (IS_ERR(block)) {
1368 err = PTR_ERR(block);
1369 goto errout;
1370 }
1371
1372 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1373 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1374 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1375 err = -EINVAL;
1376 goto errout;
1377 }
1378 chain = tcf_chain_get(block, chain_index, false);
1379 if (!chain) {
1380 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1381 err = -EINVAL;
1382 goto errout;
1383 }
1384
1385 if (prio == 0) {
1386 tfilter_notify_chain(net, skb, block, q, parent, n,
1387 chain, RTM_DELTFILTER);
1388 tcf_chain_flush(chain);
1389 err = 0;
1390 goto errout;
1391 }
1392
1393 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1394 prio, false);
1395 if (!tp || IS_ERR(tp)) {
1396 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001397 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001398 goto errout;
1399 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1400 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1401 err = -EINVAL;
1402 goto errout;
1403 }
1404
1405 fh = tp->ops->get(tp, t->tcm_handle);
1406
1407 if (!fh) {
1408 if (t->tcm_handle == 0) {
1409 tcf_chain_tp_remove(chain, &chain_info, tp);
1410 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1411 RTM_DELTFILTER, false);
1412 tcf_proto_destroy(tp, extack);
1413 err = 0;
1414 } else {
1415 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1416 err = -ENOENT;
1417 }
1418 } else {
1419 bool last;
1420
1421 err = tfilter_del_notify(net, skb, n, tp, block,
1422 q, parent, fh, false, &last,
1423 extack);
1424 if (err)
1425 goto errout;
1426 if (last) {
1427 tcf_chain_tp_remove(chain, &chain_info, tp);
1428 tcf_proto_destroy(tp, extack);
1429 }
1430 }
1431
1432errout:
1433 if (chain)
1434 tcf_chain_put(chain);
1435 return err;
1436}
1437
1438static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1439 struct netlink_ext_ack *extack)
1440{
1441 struct net *net = sock_net(skb->sk);
1442 struct nlattr *tca[TCA_MAX + 1];
1443 struct tcmsg *t;
1444 u32 protocol;
1445 u32 prio;
1446 u32 parent;
1447 u32 chain_index;
1448 struct Qdisc *q = NULL;
1449 struct tcf_chain_info chain_info;
1450 struct tcf_chain *chain = NULL;
1451 struct tcf_block *block;
1452 struct tcf_proto *tp = NULL;
1453 unsigned long cl = 0;
1454 void *fh = NULL;
1455 int err;
1456
1457 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1458 if (err < 0)
1459 return err;
1460
1461 t = nlmsg_data(n);
1462 protocol = TC_H_MIN(t->tcm_info);
1463 prio = TC_H_MAJ(t->tcm_info);
1464 parent = t->tcm_parent;
1465
1466 if (prio == 0) {
1467 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1468 return -ENOENT;
1469 }
1470
1471 /* Find head of filter chain. */
1472
1473 block = tcf_block_find(net, &q, &parent, &cl,
1474 t->tcm_ifindex, t->tcm_block_index, extack);
1475 if (IS_ERR(block)) {
1476 err = PTR_ERR(block);
1477 goto errout;
1478 }
1479
1480 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1481 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1482 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1483 err = -EINVAL;
1484 goto errout;
1485 }
1486 chain = tcf_chain_get(block, chain_index, false);
1487 if (!chain) {
1488 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1489 err = -EINVAL;
1490 goto errout;
1491 }
1492
1493 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1494 prio, false);
1495 if (!tp || IS_ERR(tp)) {
1496 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001497 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001498 goto errout;
1499 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1500 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1501 err = -EINVAL;
1502 goto errout;
1503 }
1504
1505 fh = tp->ops->get(tp, t->tcm_handle);
1506
1507 if (!fh) {
1508 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1509 err = -ENOENT;
1510 } else {
1511 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1512 fh, RTM_NEWTFILTER, true);
1513 if (err < 0)
1514 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1515 }
1516
1517errout:
1518 if (chain)
1519 tcf_chain_put(chain);
1520 return err;
1521}
1522
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001523struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct tcf_walker w;
1525 struct sk_buff *skb;
1526 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001527 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001528 struct Qdisc *q;
1529 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530};
1531
WANG Cong8113c092017-08-04 21:31:43 -07001532static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001534 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001535 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001537 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001538 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001539 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1540 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001543static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1544 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001545 long index_start, long *p_index)
1546{
1547 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001548 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001549 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1550 struct tcf_dump_args arg;
1551 struct tcf_proto *tp;
1552
1553 for (tp = rtnl_dereference(chain->filter_chain);
1554 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1555 if (*p_index < index_start)
1556 continue;
1557 if (TC_H_MAJ(tcm->tcm_info) &&
1558 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1559 continue;
1560 if (TC_H_MIN(tcm->tcm_info) &&
1561 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1562 continue;
1563 if (*p_index > index_start)
1564 memset(&cb->args[1], 0,
1565 sizeof(cb->args) - sizeof(cb->args[0]));
1566 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001567 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001568 NETLINK_CB(cb->skb).portid,
1569 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1570 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001571 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001572
1573 cb->args[1] = 1;
1574 }
1575 if (!tp->ops->walk)
1576 continue;
1577 arg.w.fn = tcf_node_dump;
1578 arg.skb = skb;
1579 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001580 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001581 arg.q = q;
1582 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001583 arg.w.stop = 0;
1584 arg.w.skip = cb->args[1] - 1;
1585 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001586 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001587 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001588 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001589 cb->args[1] = arg.w.count + 1;
1590 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001591 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001592 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001593 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001594}
1595
Eric Dumazetbd27a872009-11-05 20:57:26 -08001596/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1598{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001599 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001600 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001601 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001602 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001603 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001604 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001605 long index_start;
1606 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001607 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001608 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Hong zhi guo573ce262013-03-27 06:47:04 +00001610 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001612
1613 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1614 if (err)
1615 return err;
1616
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001617 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1618 block = tcf_block_lookup(net, tcm->tcm_block_index);
1619 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001620 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001621 /* If we work with block index, q is NULL and parent value
1622 * will never be used in the following code. The check
1623 * in tcf_fill_node prevents it. However, compiler does not
1624 * see that far, so set parent to zero to silence the warning
1625 * about parent being uninitialized.
1626 */
1627 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001628 } else {
1629 const struct Qdisc_class_ops *cops;
1630 struct net_device *dev;
1631 unsigned long cl = 0;
1632
1633 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1634 if (!dev)
1635 return skb->len;
1636
1637 parent = tcm->tcm_parent;
1638 if (!parent) {
1639 q = dev->qdisc;
1640 parent = q->handle;
1641 } else {
1642 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1643 }
1644 if (!q)
1645 goto out;
1646 cops = q->ops->cl_ops;
1647 if (!cops)
1648 goto out;
1649 if (!cops->tcf_block)
1650 goto out;
1651 if (TC_H_MIN(tcm->tcm_parent)) {
1652 cl = cops->find(q, tcm->tcm_parent);
1653 if (cl == 0)
1654 goto out;
1655 }
1656 block = cops->tcf_block(q, cl, NULL);
1657 if (!block)
1658 goto out;
1659 if (tcf_block_shared(block))
1660 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001663 index_start = cb->args[0];
1664 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001665
1666 list_for_each_entry(chain, &block->chain_list, list) {
1667 if (tca[TCA_CHAIN] &&
1668 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1669 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001670 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001671 index_start, &index)) {
1672 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001673 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001674 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001675 }
1676
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001677 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001680 /* If we did no progress, the error (EMSGSIZE) is real */
1681 if (skb->len == 0 && err)
1682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return skb->len;
1684}
1685
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001686static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1687 struct sk_buff *skb, struct tcf_block *block,
1688 u32 portid, u32 seq, u16 flags, int event)
1689{
1690 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001691 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001692 struct nlmsghdr *nlh;
1693 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001694 void *priv;
1695
1696 ops = chain->tmplt_ops;
1697 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001698
1699 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1700 if (!nlh)
1701 goto out_nlmsg_trim;
1702 tcm = nlmsg_data(nlh);
1703 tcm->tcm_family = AF_UNSPEC;
1704 tcm->tcm__pad1 = 0;
1705 tcm->tcm__pad2 = 0;
1706 tcm->tcm_handle = 0;
1707 if (block->q) {
1708 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1709 tcm->tcm_parent = block->q->handle;
1710 } else {
1711 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1712 tcm->tcm_block_index = block->index;
1713 }
1714
1715 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1716 goto nla_put_failure;
1717
Jiri Pirko9f407f12018-07-23 09:23:07 +02001718 if (ops) {
1719 if (nla_put_string(skb, TCA_KIND, ops->kind))
1720 goto nla_put_failure;
1721 if (ops->tmplt_dump(skb, net, priv) < 0)
1722 goto nla_put_failure;
1723 }
1724
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001725 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1726 return skb->len;
1727
1728out_nlmsg_trim:
1729nla_put_failure:
1730 nlmsg_trim(skb, b);
1731 return -EMSGSIZE;
1732}
1733
1734static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1735 u32 seq, u16 flags, int event, bool unicast)
1736{
1737 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1738 struct tcf_block *block = chain->block;
1739 struct net *net = block->net;
1740 struct sk_buff *skb;
1741
1742 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1743 if (!skb)
1744 return -ENOBUFS;
1745
1746 if (tc_chain_fill_node(chain, net, skb, block, portid,
1747 seq, flags, event) <= 0) {
1748 kfree_skb(skb);
1749 return -EINVAL;
1750 }
1751
1752 if (unicast)
1753 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1754
1755 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1756}
1757
Jiri Pirko9f407f12018-07-23 09:23:07 +02001758static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1759 struct nlattr **tca,
1760 struct netlink_ext_ack *extack)
1761{
1762 const struct tcf_proto_ops *ops;
1763 void *tmplt_priv;
1764
1765 /* If kind is not set, user did not specify template. */
1766 if (!tca[TCA_KIND])
1767 return 0;
1768
1769 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1770 if (IS_ERR(ops))
1771 return PTR_ERR(ops);
1772 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1773 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1774 return -EOPNOTSUPP;
1775 }
1776
1777 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1778 if (IS_ERR(tmplt_priv)) {
1779 module_put(ops->owner);
1780 return PTR_ERR(tmplt_priv);
1781 }
1782 chain->tmplt_ops = ops;
1783 chain->tmplt_priv = tmplt_priv;
1784 return 0;
1785}
1786
1787static void tc_chain_tmplt_del(struct tcf_chain *chain)
1788{
1789 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1790
1791 /* If template ops are set, no work to do for us. */
1792 if (!ops)
1793 return;
1794
1795 ops->tmplt_destroy(chain->tmplt_priv);
1796 module_put(ops->owner);
1797}
1798
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001799/* Add/delete/get a chain */
1800
1801static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1802 struct netlink_ext_ack *extack)
1803{
1804 struct net *net = sock_net(skb->sk);
1805 struct nlattr *tca[TCA_MAX + 1];
1806 struct tcmsg *t;
1807 u32 parent;
1808 u32 chain_index;
1809 struct Qdisc *q = NULL;
1810 struct tcf_chain *chain = NULL;
1811 struct tcf_block *block;
1812 unsigned long cl;
1813 int err;
1814
1815 if (n->nlmsg_type != RTM_GETCHAIN &&
1816 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1817 return -EPERM;
1818
1819replay:
1820 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1821 if (err < 0)
1822 return err;
1823
1824 t = nlmsg_data(n);
1825 parent = t->tcm_parent;
1826 cl = 0;
1827
1828 block = tcf_block_find(net, &q, &parent, &cl,
1829 t->tcm_ifindex, t->tcm_block_index, extack);
1830 if (IS_ERR(block))
1831 return PTR_ERR(block);
1832
1833 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1834 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1835 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1836 return -EINVAL;
1837 }
1838 chain = tcf_chain_lookup(block, chain_index);
1839 if (n->nlmsg_type == RTM_NEWCHAIN) {
1840 if (chain) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001841 if (tcf_chain_is_zombie(chain)) {
1842 /* The chain exists only because there is
1843 * some action referencing it, meaning it
1844 * is a zombie.
1845 */
1846 tcf_chain_hold(chain);
1847 } else {
1848 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1849 return -EEXIST;
1850 }
1851 } else {
1852 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1853 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1854 return -ENOENT;
1855 }
1856 chain = tcf_chain_create(block, chain_index);
1857 if (!chain) {
1858 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1859 return -ENOMEM;
1860 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001861 }
1862 } else {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001863 if (!chain || tcf_chain_is_zombie(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001864 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1865 return -EINVAL;
1866 }
1867 tcf_chain_hold(chain);
1868 }
1869
1870 switch (n->nlmsg_type) {
1871 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001872 err = tc_chain_tmplt_add(chain, net, tca, extack);
1873 if (err)
1874 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001875 /* In case the chain was successfully added, take a reference
1876 * to the chain. This ensures that an empty chain
1877 * does not disappear at the end of this function.
1878 */
1879 tcf_chain_hold(chain);
1880 chain->explicitly_created = true;
1881 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1882 RTM_NEWCHAIN, false);
1883 break;
1884 case RTM_DELCHAIN:
1885 /* Flush the chain first as the user requested chain removal. */
1886 tcf_chain_flush(chain);
1887 /* In case the chain was successfully deleted, put a reference
1888 * to the chain previously taken during addition.
1889 */
1890 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001891 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001892 break;
1893 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001894 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1895 n->nlmsg_seq, n->nlmsg_type, true);
1896 if (err < 0)
1897 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1898 break;
1899 default:
1900 err = -EOPNOTSUPP;
1901 NL_SET_ERR_MSG(extack, "Unsupported message type");
1902 goto errout;
1903 }
1904
1905errout:
1906 tcf_chain_put(chain);
1907 if (err == -EAGAIN)
1908 /* Replay the request. */
1909 goto replay;
1910 return err;
1911}
1912
1913/* called with RTNL */
1914static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1915{
1916 struct net *net = sock_net(skb->sk);
1917 struct nlattr *tca[TCA_MAX + 1];
1918 struct Qdisc *q = NULL;
1919 struct tcf_block *block;
1920 struct tcf_chain *chain;
1921 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1922 long index_start;
1923 long index;
1924 u32 parent;
1925 int err;
1926
1927 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1928 return skb->len;
1929
1930 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1931 if (err)
1932 return err;
1933
1934 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1935 block = tcf_block_lookup(net, tcm->tcm_block_index);
1936 if (!block)
1937 goto out;
1938 /* If we work with block index, q is NULL and parent value
1939 * will never be used in the following code. The check
1940 * in tcf_fill_node prevents it. However, compiler does not
1941 * see that far, so set parent to zero to silence the warning
1942 * about parent being uninitialized.
1943 */
1944 parent = 0;
1945 } else {
1946 const struct Qdisc_class_ops *cops;
1947 struct net_device *dev;
1948 unsigned long cl = 0;
1949
1950 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1951 if (!dev)
1952 return skb->len;
1953
1954 parent = tcm->tcm_parent;
1955 if (!parent) {
1956 q = dev->qdisc;
1957 parent = q->handle;
1958 } else {
1959 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1960 }
1961 if (!q)
1962 goto out;
1963 cops = q->ops->cl_ops;
1964 if (!cops)
1965 goto out;
1966 if (!cops->tcf_block)
1967 goto out;
1968 if (TC_H_MIN(tcm->tcm_parent)) {
1969 cl = cops->find(q, tcm->tcm_parent);
1970 if (cl == 0)
1971 goto out;
1972 }
1973 block = cops->tcf_block(q, cl, NULL);
1974 if (!block)
1975 goto out;
1976 if (tcf_block_shared(block))
1977 q = NULL;
1978 }
1979
1980 index_start = cb->args[0];
1981 index = 0;
1982
1983 list_for_each_entry(chain, &block->chain_list, list) {
1984 if ((tca[TCA_CHAIN] &&
1985 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
1986 continue;
1987 if (index < index_start) {
1988 index++;
1989 continue;
1990 }
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001991 if (tcf_chain_is_zombie(chain))
1992 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001993 err = tc_chain_fill_node(chain, net, skb, block,
1994 NETLINK_CB(cb->skb).portid,
1995 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1996 RTM_NEWCHAIN);
1997 if (err <= 0)
1998 break;
1999 index++;
2000 }
2001
2002 cb->args[0] = index;
2003
2004out:
2005 /* If we did no progress, the error (EMSGSIZE) is real */
2006 if (skb->len == 0 && err)
2007 return err;
2008 return skb->len;
2009}
2010
WANG Cong18d02642014-09-25 10:26:37 -07002011void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002014 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002015 kfree(exts->actions);
2016 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017#endif
2018}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002019EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002021int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002022 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2023 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#ifdef CONFIG_NET_CLS_ACT
2026 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002028 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
WANG Cong5da57f42013-12-15 20:15:07 -08002030 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002031 act = tcf_action_init_1(net, tp, tb[exts->police],
2032 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002033 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002034 if (IS_ERR(act))
2035 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
WANG Cong33be6272013-12-15 20:15:05 -08002037 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002038 exts->actions[0] = act;
2039 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002040 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002041 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002042
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002043 err = tcf_action_init(net, tp, tb[exts->action],
2044 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002045 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002046 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002047 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002048 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002049 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Cong Wange4b95c42017-11-06 13:47:19 -08002051 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053#else
WANG Cong5da57f42013-12-15 20:15:07 -08002054 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002055 (exts->police && tb[exts->police])) {
2056 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#endif
2060
2061 return 0;
2062}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002063EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002065void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002068 struct tcf_exts old = *dst;
2069
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002070 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002071 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072#endif
2073}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002074EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
WANG Cong22dc13c2016-08-13 22:35:00 -07002076#ifdef CONFIG_NET_CLS_ACT
2077static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2078{
2079 if (exts->nr_actions == 0)
2080 return NULL;
2081 else
2082 return exts->actions[0];
2083}
2084#endif
WANG Cong33be6272013-12-15 20:15:05 -08002085
WANG Cong5da57f42013-12-15 20:15:07 -08002086int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002089 struct nlattr *nest;
2090
Jiri Pirko978dfd82017-08-04 14:29:03 +02002091 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /*
2093 * again for backward compatible mode - we want
2094 * to work with both old and new modes of entering
2095 * tc data even if iproute2 was newer - jhs
2096 */
WANG Cong33be6272013-12-15 20:15:05 -08002097 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002098 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002099 if (nest == NULL)
2100 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002101
Vlad Buslov90b73b72018-07-05 17:24:33 +03002102 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002103 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002104 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002105 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002106 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002107 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002108 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002109 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002110 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002111 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002112 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002116
2117nla_put_failure:
2118 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002120#else
2121 return 0;
2122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002124EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002126
WANG Cong5da57f42013-12-15 20:15:07 -08002127int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002130 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002131 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002132 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133#endif
2134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002136EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Jiri Pirko717503b2017-10-11 09:41:09 +02002138static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2139 enum tc_setup_type type,
2140 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002141{
2142 int ok_count = 0;
2143#ifdef CONFIG_NET_CLS_ACT
2144 const struct tc_action *a;
2145 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002146 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002147
2148 if (!tcf_exts_has_actions(exts))
2149 return 0;
2150
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002151 for (i = 0; i < exts->nr_actions; i++) {
2152 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002153 if (!a->ops->get_dev)
2154 continue;
2155 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002156 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002157 continue;
2158 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
2159 if (ret < 0)
2160 return ret;
2161 ok_count += ret;
2162 }
2163#endif
2164 return ok_count;
2165}
Jiri Pirko717503b2017-10-11 09:41:09 +02002166
Jiri Pirko208c0f42017-10-19 15:50:32 +02002167int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2168 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002169{
David S. Miller9a99dc12018-06-06 13:55:47 -04002170 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002171 int ret;
2172
David S. Miller9a99dc12018-06-06 13:55:47 -04002173 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2174 if (ret < 0)
2175 return ret;
2176 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002177
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002178 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002179 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002180 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2181 if (ret < 0)
2182 return ret;
2183 ok_count += ret;
2184
2185 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002186}
2187EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002188
Jiri Pirko48617382018-01-17 11:46:46 +01002189static __net_init int tcf_net_init(struct net *net)
2190{
2191 struct tcf_net *tn = net_generic(net, tcf_net_id);
2192
2193 idr_init(&tn->idr);
2194 return 0;
2195}
2196
2197static void __net_exit tcf_net_exit(struct net *net)
2198{
2199 struct tcf_net *tn = net_generic(net, tcf_net_id);
2200
2201 idr_destroy(&tn->idr);
2202}
2203
2204static struct pernet_operations tcf_net_ops = {
2205 .init = tcf_net_init,
2206 .exit = tcf_net_exit,
2207 .id = &tcf_net_id,
2208 .size = sizeof(struct tcf_net),
2209};
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211static int __init tc_filter_init(void)
2212{
Jiri Pirko48617382018-01-17 11:46:46 +01002213 int err;
2214
Cong Wang7aa00452017-10-26 18:24:28 -07002215 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2216 if (!tc_filter_wq)
2217 return -ENOMEM;
2218
Jiri Pirko48617382018-01-17 11:46:46 +01002219 err = register_pernet_subsys(&tcf_net_ops);
2220 if (err)
2221 goto err_register_pernet_subsys;
2222
Vlad Buslovc431f892018-05-31 09:52:53 +03002223 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2224 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2225 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002226 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002227 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2228 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2229 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2230 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002233
2234err_register_pernet_subsys:
2235 destroy_workqueue(tc_filter_wq);
2236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239subsys_initcall(tc_filter_init);