blob: e655221c654e16d51b8bf4eac5fab29108d58110 [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>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080026#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.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
80int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81{
WANG Cong36272872013-12-15 20:15:11 -080082 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int rc = -ENOENT;
84
Daniel Borkmannc78e1742015-05-20 17:13:33 +020085 /* Wait for outstanding call_rcu()s, if any, from a
86 * tcf_proto_ops's destroy() handler.
87 */
88 rcu_barrier();
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -080091 list_for_each_entry(t, &tcf_proto_base, head) {
92 if (t == ops) {
93 list_del(&t->head);
94 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -080096 }
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 write_unlock(&cls_mod_lock);
99 return rc;
100}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800101EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Tom Goff7316ae82010-03-19 15:40:13 +0000103static int tfilter_notify(struct net *net, struct sk_buff *oskb,
104 struct nlmsghdr *n, struct tcf_proto *tp,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700105 unsigned long fh, int event, bool unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200107static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
108 struct nlmsghdr *n,
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200109 struct tcf_chain *chain, int event)
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200110{
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200111 struct tcf_proto *tp;
112
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200113 for (tp = rtnl_dereference(chain->filter_chain);
114 tp; tp = rtnl_dereference(tp->next))
Roman Mashak19a8bb22016-11-22 20:57:04 -0500115 tfilter_notify(net, oskb, n, tp, 0, event, false);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* Select new prio value from the range, managed by kernel. */
119
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800120static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800122 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000125 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jiri Pirko79619732017-05-17 11:07:58 +0200127 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jiri Pirko33a48922017-02-09 14:38:57 +0100130static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200131 u32 prio, u32 parent, struct Qdisc *q,
Jiri Pirko5bc17012017-05-17 11:08:01 +0200132 struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100133{
134 struct tcf_proto *tp;
135 int err;
136
137 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138 if (!tp)
139 return ERR_PTR(-ENOBUFS);
140
141 err = -ENOENT;
142 tp->ops = tcf_proto_lookup_ops(kind);
143 if (!tp->ops) {
144#ifdef CONFIG_MODULES
145 rtnl_unlock();
146 request_module("cls_%s", kind);
147 rtnl_lock();
148 tp->ops = tcf_proto_lookup_ops(kind);
149 /* We dropped the RTNL semaphore in order to perform
150 * the module load. So, even if we succeeded in loading
151 * the module we have to replay the request. We indicate
152 * this using -EAGAIN.
153 */
154 if (tp->ops) {
155 module_put(tp->ops->owner);
156 err = -EAGAIN;
157 } else {
158 err = -ENOENT;
159 }
160 goto errout;
161#endif
162 }
163 tp->classify = tp->ops->classify;
164 tp->protocol = protocol;
165 tp->prio = prio;
166 tp->classid = parent;
167 tp->q = q;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200168 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100169
170 err = tp->ops->init(tp);
171 if (err) {
172 module_put(tp->ops->owner);
173 goto errout;
174 }
175 return tp;
176
177errout:
178 kfree(tp);
179 return ERR_PTR(err);
180}
181
WANG Cong763dbf62017-04-19 14:21:21 -0700182static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100183{
WANG Cong763dbf62017-04-19 14:21:21 -0700184 tp->ops->destroy(tp);
185 module_put(tp->ops->owner);
186 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100187}
188
Jiri Pirko5bc17012017-05-17 11:08:01 +0200189static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
190 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200191{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200192 struct tcf_chain *chain;
193
194 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
195 if (!chain)
196 return NULL;
197 list_add_tail(&chain->list, &block->chain_list);
198 chain->block = block;
199 chain->index = chain_index;
200 chain->refcnt = 1;
201 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200202}
203
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200204static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100205{
206 struct tcf_proto *tp;
207
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200208 if (*chain->p_filter_chain)
209 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200210 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700212 tcf_proto_destroy(tp);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100213 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200214}
215
216static void tcf_chain_destroy(struct tcf_chain *chain)
217{
218 list_del(&chain->list);
219 tcf_chain_flush(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200220 kfree(chain);
221}
222
WANG Cong367a8ce2017-05-23 09:42:37 -0700223struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
224 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200225{
226 struct tcf_chain *chain;
227
228 list_for_each_entry(chain, &block->chain_list, list) {
229 if (chain->index == chain_index) {
230 chain->refcnt++;
231 return chain;
232 }
233 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700234 if (create)
235 return tcf_chain_create(block, chain_index);
236 else
237 return NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200238}
239EXPORT_SYMBOL(tcf_chain_get);
240
241void tcf_chain_put(struct tcf_chain *chain)
242{
243 /* Destroy unused chain, with exception of chain 0, which is the
244 * default one and has to be always present.
245 */
246 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
247 tcf_chain_destroy(chain);
248}
249EXPORT_SYMBOL(tcf_chain_put);
250
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200251static void
252tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
253 struct tcf_proto __rcu **p_filter_chain)
254{
255 chain->p_filter_chain = p_filter_chain;
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100256}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200257
258int tcf_block_get(struct tcf_block **p_block,
259 struct tcf_proto __rcu **p_filter_chain)
260{
261 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200262 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200263 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200264
265 if (!block)
266 return -ENOMEM;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200267 INIT_LIST_HEAD(&block->chain_list);
268 /* Create chain 0 by default, it has to be always present. */
269 chain = tcf_chain_create(block, 0);
270 if (!chain) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200271 err = -ENOMEM;
272 goto err_chain_create;
273 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200274 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200275 *p_block = block;
276 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200277
278err_chain_create:
279 kfree(block);
280 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200281}
282EXPORT_SYMBOL(tcf_block_get);
283
284void tcf_block_put(struct tcf_block *block)
285{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200286 struct tcf_chain *chain, *tmp;
287
Jiri Pirko6529eab2017-05-17 11:07:55 +0200288 if (!block)
289 return;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200290
291 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
292 tcf_chain_destroy(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200293 kfree(block);
294}
295EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100296
Jiri Pirko87d83092017-05-17 11:07:54 +0200297/* Main classifier routine: scans classifier chain attached
298 * to this qdisc, (optionally) tests for protocol and asks
299 * specific classifiers.
300 */
301int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
302 struct tcf_result *res, bool compat_mode)
303{
304 __be16 protocol = tc_skb_protocol(skb);
305#ifdef CONFIG_NET_CLS_ACT
306 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200307 const struct tcf_proto *orig_tp = tp;
308 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200309 int limit = 0;
310
311reclassify:
312#endif
313 for (; tp; tp = rcu_dereference_bh(tp->next)) {
314 int err;
315
316 if (tp->protocol != protocol &&
317 tp->protocol != htons(ETH_P_ALL))
318 continue;
319
320 err = tp->classify(skb, tp, res);
321#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200322 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200323 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200324 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200325 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200326 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200327 goto reset;
328 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200329#endif
330 if (err >= 0)
331 return err;
332 }
333
334 return TC_ACT_UNSPEC; /* signal: continue lookup */
335#ifdef CONFIG_NET_CLS_ACT
336reset:
337 if (unlikely(limit++ >= max_reclassify_loop)) {
338 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
339 tp->q->ops->id, tp->prio & 0xffff,
340 ntohs(tp->protocol));
341 return TC_ACT_SHOT;
342 }
343
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200344 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200345 protocol = tc_skb_protocol(skb);
346 goto reclassify;
347#endif
348}
349EXPORT_SYMBOL(tcf_classify);
350
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200351struct tcf_chain_info {
352 struct tcf_proto __rcu **pprev;
353 struct tcf_proto __rcu *next;
354};
355
356static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
357{
358 return rtnl_dereference(*chain_info->pprev);
359}
360
361static void tcf_chain_tp_insert(struct tcf_chain *chain,
362 struct tcf_chain_info *chain_info,
363 struct tcf_proto *tp)
364{
365 if (chain->p_filter_chain &&
366 *chain_info->pprev == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200367 rcu_assign_pointer(*chain->p_filter_chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200368 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
369 rcu_assign_pointer(*chain_info->pprev, tp);
370}
371
372static void tcf_chain_tp_remove(struct tcf_chain *chain,
373 struct tcf_chain_info *chain_info,
374 struct tcf_proto *tp)
375{
376 struct tcf_proto *next = rtnl_dereference(chain_info->next);
377
378 if (chain->p_filter_chain && tp == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200379 RCU_INIT_POINTER(*chain->p_filter_chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200380 RCU_INIT_POINTER(*chain_info->pprev, next);
381}
382
383static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
384 struct tcf_chain_info *chain_info,
385 u32 protocol, u32 prio,
386 bool prio_allocate)
387{
388 struct tcf_proto **pprev;
389 struct tcf_proto *tp;
390
391 /* Check the chain for existence of proto-tcf with this priority */
392 for (pprev = &chain->filter_chain;
393 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
394 if (tp->prio >= prio) {
395 if (tp->prio == prio) {
396 if (prio_allocate ||
397 (tp->protocol != protocol && protocol))
398 return ERR_PTR(-EINVAL);
399 } else {
400 tp = NULL;
401 }
402 break;
403 }
404 }
405 chain_info->pprev = pprev;
406 chain_info->next = tp ? tp->next : NULL;
407 return tp;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* Add/change/delete/get a filter node */
411
David Ahernc21ef3e2017-04-16 09:48:24 -0700412static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
413 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900415 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800416 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct tcmsg *t;
418 u32 protocol;
419 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200420 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200422 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct net_device *dev;
424 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200425 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200426 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200427 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800429 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long cl;
431 unsigned long fh;
432 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100433 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400435 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400436 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000437 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100440 tp_created = 0;
441
David Ahernc21ef3e2017-04-16 09:48:24 -0700442 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000443 if (err < 0)
444 return err;
445
David S. Miller942b8162012-06-26 21:48:50 -0700446 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 protocol = TC_H_MIN(t->tcm_info);
448 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200449 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 parent = t->tcm_parent;
451 cl = 0;
452
453 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200454 switch (n->nlmsg_type) {
455 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200456 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200457 return -ENOENT;
458 break;
459 case RTM_NEWTFILTER:
460 /* If no priority is provided by the user,
461 * we allocate one.
462 */
463 if (n->nlmsg_flags & NLM_F_CREATE) {
464 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200465 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200466 break;
467 }
468 /* fall-through */
469 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 /* Find head of filter chain. */
475
476 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000477 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800478 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -ENODEV;
480
481 /* Find qdisc */
482 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000483 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800485 } else {
486 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
487 if (q == NULL)
488 return -EINVAL;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000492 cops = q->ops->cl_ops;
493 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
495
Jiri Pirko6529eab2017-05-17 11:07:55 +0200496 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000497 return -EOPNOTSUPP;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Do we search for filter, attached to class? */
500 if (TC_H_MIN(parent)) {
501 cl = cops->get(q, parent);
502 if (cl == 0)
503 return -ENOENT;
504 }
505
506 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200507 block = cops->tcf_block(q, cl);
508 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100509 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100511 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200512
513 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
514 if (chain_index > TC_ACT_EXT_VAL_MASK) {
515 err = -EINVAL;
516 goto errout;
517 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700518 chain = tcf_chain_get(block, chain_index,
519 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200520 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700521 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200522 goto errout;
523 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200524
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200525 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
526 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200527 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200528 err = 0;
529 goto errout;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200532 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
533 prio, prio_allocate);
534 if (IS_ERR(tp)) {
535 err = PTR_ERR(tp);
536 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 if (tp == NULL) {
540 /* Proto-tcf does not exist, create new one */
541
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100542 if (tca[TCA_KIND] == NULL || !protocol) {
543 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000547 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100548 !(n->nlmsg_flags & NLM_F_CREATE)) {
549 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200553 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200554 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jiri Pirko33a48922017-02-09 14:38:57 +0100556 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200557 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100558 if (IS_ERR(tp)) {
559 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto errout;
561 }
Minoru Usui12186be2009-06-02 02:17:34 -0700562 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100563 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
564 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 fh = tp->ops->get(tp, t->tcm_handle);
569
570 if (fh == 0) {
571 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200572 tcf_chain_tp_remove(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700573 tfilter_notify(net, skb, n, tp, fh,
574 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700575 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 err = 0;
577 goto errout;
578 }
579
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800580 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100581 !(n->nlmsg_flags & NLM_F_CREATE)) {
582 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700586 bool last;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900589 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700590 if (n->nlmsg_flags & NLM_F_EXCL) {
591 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700592 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100593 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 case RTM_DELTFILTER:
WANG Cong763dbf62017-04-19 14:21:21 -0700598 err = tp->ops->delete(tp, fh, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100599 if (err)
600 goto errout;
Jiri Pirko40c81b22017-02-09 14:39:00 +0100601 tfilter_notify(net, skb, n, tp, t->tcm_handle,
602 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700603 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200604 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700605 tcf_proto_destroy(tp);
606 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100607 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 case RTM_GETTFILTER:
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400609 err = tfilter_notify(net, skb, n, tp, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700610 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto errout;
612 default:
613 err = -EINVAL;
614 goto errout;
615 }
616 }
617
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700618 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
619 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700620 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200621 if (tp_created)
622 tcf_chain_tp_insert(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700623 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700624 } else {
625 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700626 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200630 if (chain)
631 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (cl)
633 cops->put(q, cl);
634 if (err == -EAGAIN)
635 /* Replay the request. */
636 goto replay;
637 return err;
638}
639
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400640static int tcf_fill_node(struct net *net, struct sk_buff *skb,
641 struct tcf_proto *tp, unsigned long fh, u32 portid,
642 u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct tcmsg *tcm;
645 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700646 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Eric W. Biederman15e47302012-09-07 20:12:54 +0000648 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
David S. Miller942b8162012-06-26 21:48:50 -0700649 if (!nlh)
650 goto out_nlmsg_trim;
651 tcm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700653 tcm->tcm__pad1 = 0;
Jiri Pirkoad61df92009-10-08 01:21:46 -0700654 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700655 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 tcm->tcm_parent = tp->classid;
657 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
David S. Miller1b34ec42012-03-29 05:11:39 -0400658 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
659 goto nla_put_failure;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200660 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
661 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tcm->tcm_handle = fh;
663 if (RTM_DELTFILTER != event) {
664 tcm->tcm_handle = 0;
WANG Cong832d1d52014-01-09 16:14:01 -0800665 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800666 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700668 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return skb->len;
670
David S. Miller942b8162012-06-26 21:48:50 -0700671out_nlmsg_trim:
Patrick McHardyadd93b62008-01-22 22:11:33 -0800672nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700673 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -1;
675}
676
Tom Goff7316ae82010-03-19 15:40:13 +0000677static int tfilter_notify(struct net *net, struct sk_buff *oskb,
678 struct nlmsghdr *n, struct tcf_proto *tp,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700679 unsigned long fh, int event, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct sk_buff *skb;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000682 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
685 if (!skb)
686 return -ENOBUFS;
687
Roman Mashak30a391a2016-11-16 17:16:10 -0500688 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
689 n->nlmsg_flags, event) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree_skb(skb);
691 return -EINVAL;
692 }
693
Eric Dumazetfa59b272016-10-09 20:25:55 -0700694 if (unicast)
695 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
696
Eric W. Biederman15e47302012-09-07 20:12:54 +0000697 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800698 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800701struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct tcf_walker w;
703 struct sk_buff *skb;
704 struct netlink_callback *cb;
705};
706
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800707static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
708 struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800710 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800711 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
WANG Cong832d1d52014-01-09 16:14:01 -0800713 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400714 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
715 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Jiri Pirko5bc17012017-05-17 11:08:01 +0200718static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200719 struct netlink_callback *cb,
720 long index_start, long *p_index)
721{
722 struct net *net = sock_net(skb->sk);
723 struct tcmsg *tcm = nlmsg_data(cb->nlh);
724 struct tcf_dump_args arg;
725 struct tcf_proto *tp;
726
727 for (tp = rtnl_dereference(chain->filter_chain);
728 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
729 if (*p_index < index_start)
730 continue;
731 if (TC_H_MAJ(tcm->tcm_info) &&
732 TC_H_MAJ(tcm->tcm_info) != tp->prio)
733 continue;
734 if (TC_H_MIN(tcm->tcm_info) &&
735 TC_H_MIN(tcm->tcm_info) != tp->protocol)
736 continue;
737 if (*p_index > index_start)
738 memset(&cb->args[1], 0,
739 sizeof(cb->args) - sizeof(cb->args[0]));
740 if (cb->args[1] == 0) {
741 if (tcf_fill_node(net, skb, tp, 0,
742 NETLINK_CB(cb->skb).portid,
743 cb->nlh->nlmsg_seq, NLM_F_MULTI,
744 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200745 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200746
747 cb->args[1] = 1;
748 }
749 if (!tp->ops->walk)
750 continue;
751 arg.w.fn = tcf_node_dump;
752 arg.skb = skb;
753 arg.cb = cb;
754 arg.w.stop = 0;
755 arg.w.skip = cb->args[1] - 1;
756 arg.w.count = 0;
757 tp->ops->walk(tp, &arg.w);
758 cb->args[1] = arg.w.count + 1;
759 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200760 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200761 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200762 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200763}
764
Eric Dumazetbd27a872009-11-05 20:57:26 -0800765/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
767{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900768 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200769 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct net_device *dev;
771 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200772 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200773 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700774 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800776 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200777 long index_start;
778 long index;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200779 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Hong zhi guo573ce262013-03-27 06:47:04 +0000781 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200783
784 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
785 if (err)
786 return err;
787
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000788 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
789 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return skb->len;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!tcm->tcm_parent)
Patrick McHardyaf356af2009-09-04 06:41:18 +0000793 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 else
795 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
796 if (!q)
797 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000798 cops = q->ops->cl_ops;
799 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto errout;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200801 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000802 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (TC_H_MIN(tcm->tcm_parent)) {
804 cl = cops->get(q, tcm->tcm_parent);
805 if (cl == 0)
806 goto errout;
807 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200808 block = cops->tcf_block(q, cl);
809 if (!block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto errout;
811
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200812 index_start = cb->args[0];
813 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200814
815 list_for_each_entry(chain, &block->chain_list, list) {
816 if (tca[TCA_CHAIN] &&
817 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
818 continue;
819 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
820 break;
821 }
822
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200823 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825errout:
826 if (cl)
827 cops->put(q, cl);
828out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return skb->len;
830}
831
WANG Cong18d02642014-09-25 10:26:37 -0700832void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700835 LIST_HEAD(actions);
836
837 tcf_exts_to_list(exts, &actions);
838 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
839 kfree(exts->actions);
840 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841#endif
842}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800843EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000845int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400846 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#ifdef CONFIG_NET_CLS_ACT
849 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct tc_action *act;
851
WANG Cong5da57f42013-12-15 20:15:07 -0800852 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200853 act = tcf_action_init_1(net, tp, tb[exts->police],
854 rate_tlv, "police", ovr,
855 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800856 if (IS_ERR(act))
857 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
WANG Cong33be6272013-12-15 20:15:05 -0800859 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700860 exts->actions[0] = act;
861 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800862 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700863 LIST_HEAD(actions);
864 int err, i = 0;
865
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200866 err = tcf_action_init(net, tp, tb[exts->action],
867 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400868 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800869 if (err)
870 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700871 list_for_each_entry(act, &actions, list)
872 exts->actions[i++] = act;
873 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#else
WANG Cong5da57f42013-12-15 20:15:07 -0800877 if ((exts->action && tb[exts->action]) ||
878 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EOPNOTSUPP;
880#endif
881
882 return 0;
883}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800884EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200886void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700889 struct tcf_exts old = *dst;
890
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200891 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -0700892 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
894}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800895EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
WANG Cong22dc13c2016-08-13 22:35:00 -0700897#ifdef CONFIG_NET_CLS_ACT
898static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
899{
900 if (exts->nr_actions == 0)
901 return NULL;
902 else
903 return exts->actions[0];
904}
905#endif
WANG Cong33be6272013-12-15 20:15:05 -0800906
WANG Cong5da57f42013-12-15 20:15:07 -0800907int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700910 struct nlattr *nest;
911
Jiri Pirko978dfd82017-08-04 14:29:03 +0200912 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * again for backward compatible mode - we want
915 * to work with both old and new modes of entering
916 * tc data even if iproute2 was newer - jhs
917 */
WANG Cong33be6272013-12-15 20:15:05 -0800918 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700919 LIST_HEAD(actions);
920
WANG Cong5da57f42013-12-15 20:15:07 -0800921 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800922 if (nest == NULL)
923 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700924
925 tcf_exts_to_list(exts, &actions);
926 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800927 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800928 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800929 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800930 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800931 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500932 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800933 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800934 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800935 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800936 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -0700940
941nla_put_failure:
942 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -0700944#else
945 return 0;
946#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800948EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800950
WANG Cong5da57f42013-12-15 20:15:07 -0800951int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800954 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +0100955 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800956 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#endif
958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800960EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200962int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
963 struct net_device **hw_dev)
964{
965#ifdef CONFIG_NET_CLS_ACT
966 const struct tc_action *a;
967 LIST_HEAD(actions);
968
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200969 if (!tcf_exts_has_actions(exts))
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200970 return -EINVAL;
971
972 tcf_exts_to_list(exts, &actions);
973 list_for_each_entry(a, &actions, list) {
974 if (a->ops->get_dev) {
975 a->ops->get_dev(a, dev_net(dev), hw_dev);
976 break;
977 }
978 }
979 if (*hw_dev)
980 return 0;
981#endif
982 return -EOPNOTSUPP;
983}
984EXPORT_SYMBOL(tcf_exts_get_dev);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static int __init tc_filter_init(void)
987{
Greg Rosec7ac8672011-06-10 01:27:09 +0000988 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
989 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
Thomas Graf82623c02007-03-22 11:56:22 -0700990 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Greg Rosec7ac8672011-06-10 01:27:09 +0000991 tc_dump_tfilter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 0;
994}
995
996subsys_initcall(tc_filter_init);