blob: 89fbb35bc666c251ba9fde582763c886921912c4 [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
Jiri Pirko5bc17012017-05-17 11:08:01 +0200223struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index)
224{
225 struct tcf_chain *chain;
226
227 list_for_each_entry(chain, &block->chain_list, list) {
228 if (chain->index == chain_index) {
229 chain->refcnt++;
230 return chain;
231 }
232 }
233 return tcf_chain_create(block, chain_index);
234}
235EXPORT_SYMBOL(tcf_chain_get);
236
237void tcf_chain_put(struct tcf_chain *chain)
238{
239 /* Destroy unused chain, with exception of chain 0, which is the
240 * default one and has to be always present.
241 */
242 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
243 tcf_chain_destroy(chain);
244}
245EXPORT_SYMBOL(tcf_chain_put);
246
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200247static void
248tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
249 struct tcf_proto __rcu **p_filter_chain)
250{
251 chain->p_filter_chain = p_filter_chain;
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100252}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200253
254int tcf_block_get(struct tcf_block **p_block,
255 struct tcf_proto __rcu **p_filter_chain)
256{
257 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200258 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200259 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200260
261 if (!block)
262 return -ENOMEM;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200263 INIT_LIST_HEAD(&block->chain_list);
264 /* Create chain 0 by default, it has to be always present. */
265 chain = tcf_chain_create(block, 0);
266 if (!chain) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200267 err = -ENOMEM;
268 goto err_chain_create;
269 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200270 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200271 *p_block = block;
272 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200273
274err_chain_create:
275 kfree(block);
276 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200277}
278EXPORT_SYMBOL(tcf_block_get);
279
280void tcf_block_put(struct tcf_block *block)
281{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200282 struct tcf_chain *chain, *tmp;
283
Jiri Pirko6529eab2017-05-17 11:07:55 +0200284 if (!block)
285 return;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200286
287 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
288 tcf_chain_destroy(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200289 kfree(block);
290}
291EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100292
Jiri Pirko87d83092017-05-17 11:07:54 +0200293/* Main classifier routine: scans classifier chain attached
294 * to this qdisc, (optionally) tests for protocol and asks
295 * specific classifiers.
296 */
297int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
298 struct tcf_result *res, bool compat_mode)
299{
300 __be16 protocol = tc_skb_protocol(skb);
301#ifdef CONFIG_NET_CLS_ACT
302 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200303 const struct tcf_proto *orig_tp = tp;
304 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200305 int limit = 0;
306
307reclassify:
308#endif
309 for (; tp; tp = rcu_dereference_bh(tp->next)) {
310 int err;
311
312 if (tp->protocol != protocol &&
313 tp->protocol != htons(ETH_P_ALL))
314 continue;
315
316 err = tp->classify(skb, tp, res);
317#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200318 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200319 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200320 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200321 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200322 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200323 goto reset;
324 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200325#endif
326 if (err >= 0)
327 return err;
328 }
329
330 return TC_ACT_UNSPEC; /* signal: continue lookup */
331#ifdef CONFIG_NET_CLS_ACT
332reset:
333 if (unlikely(limit++ >= max_reclassify_loop)) {
334 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
335 tp->q->ops->id, tp->prio & 0xffff,
336 ntohs(tp->protocol));
337 return TC_ACT_SHOT;
338 }
339
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200340 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200341 protocol = tc_skb_protocol(skb);
342 goto reclassify;
343#endif
344}
345EXPORT_SYMBOL(tcf_classify);
346
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200347struct tcf_chain_info {
348 struct tcf_proto __rcu **pprev;
349 struct tcf_proto __rcu *next;
350};
351
352static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
353{
354 return rtnl_dereference(*chain_info->pprev);
355}
356
357static void tcf_chain_tp_insert(struct tcf_chain *chain,
358 struct tcf_chain_info *chain_info,
359 struct tcf_proto *tp)
360{
361 if (chain->p_filter_chain &&
362 *chain_info->pprev == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200363 rcu_assign_pointer(*chain->p_filter_chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200364 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
365 rcu_assign_pointer(*chain_info->pprev, tp);
366}
367
368static void tcf_chain_tp_remove(struct tcf_chain *chain,
369 struct tcf_chain_info *chain_info,
370 struct tcf_proto *tp)
371{
372 struct tcf_proto *next = rtnl_dereference(chain_info->next);
373
374 if (chain->p_filter_chain && tp == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200375 RCU_INIT_POINTER(*chain->p_filter_chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200376 RCU_INIT_POINTER(*chain_info->pprev, next);
377}
378
379static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
380 struct tcf_chain_info *chain_info,
381 u32 protocol, u32 prio,
382 bool prio_allocate)
383{
384 struct tcf_proto **pprev;
385 struct tcf_proto *tp;
386
387 /* Check the chain for existence of proto-tcf with this priority */
388 for (pprev = &chain->filter_chain;
389 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
390 if (tp->prio >= prio) {
391 if (tp->prio == prio) {
392 if (prio_allocate ||
393 (tp->protocol != protocol && protocol))
394 return ERR_PTR(-EINVAL);
395 } else {
396 tp = NULL;
397 }
398 break;
399 }
400 }
401 chain_info->pprev = pprev;
402 chain_info->next = tp ? tp->next : NULL;
403 return tp;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* Add/change/delete/get a filter node */
407
David Ahernc21ef3e2017-04-16 09:48:24 -0700408static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
409 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900411 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800412 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct tcmsg *t;
414 u32 protocol;
415 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200416 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200418 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct net_device *dev;
420 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200421 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200422 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200423 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800425 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long cl;
427 unsigned long fh;
428 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100429 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400431 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400432 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000433 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100436 tp_created = 0;
437
David Ahernc21ef3e2017-04-16 09:48:24 -0700438 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000439 if (err < 0)
440 return err;
441
David S. Miller942b8162012-06-26 21:48:50 -0700442 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 protocol = TC_H_MIN(t->tcm_info);
444 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200445 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 parent = t->tcm_parent;
447 cl = 0;
448
449 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200450 switch (n->nlmsg_type) {
451 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200452 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200453 return -ENOENT;
454 break;
455 case RTM_NEWTFILTER:
456 /* If no priority is provided by the user,
457 * we allocate one.
458 */
459 if (n->nlmsg_flags & NLM_F_CREATE) {
460 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200461 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200462 break;
463 }
464 /* fall-through */
465 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 /* Find head of filter chain. */
471
472 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000473 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800474 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -ENODEV;
476
477 /* Find qdisc */
478 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000479 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800481 } else {
482 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
483 if (q == NULL)
484 return -EINVAL;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000488 cops = q->ops->cl_ops;
489 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EINVAL;
491
Jiri Pirko6529eab2017-05-17 11:07:55 +0200492 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000493 return -EOPNOTSUPP;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Do we search for filter, attached to class? */
496 if (TC_H_MIN(parent)) {
497 cl = cops->get(q, parent);
498 if (cl == 0)
499 return -ENOENT;
500 }
501
502 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200503 block = cops->tcf_block(q, cl);
504 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100505 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100507 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200508
509 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
510 if (chain_index > TC_ACT_EXT_VAL_MASK) {
511 err = -EINVAL;
512 goto errout;
513 }
514 chain = tcf_chain_get(block, chain_index);
515 if (!chain) {
516 err = -ENOMEM;
517 goto errout;
518 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200519
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200520 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
521 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200522 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200523 err = 0;
524 goto errout;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200527 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
528 prio, prio_allocate);
529 if (IS_ERR(tp)) {
530 err = PTR_ERR(tp);
531 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
534 if (tp == NULL) {
535 /* Proto-tcf does not exist, create new one */
536
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100537 if (tca[TCA_KIND] == NULL || !protocol) {
538 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000542 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100543 !(n->nlmsg_flags & NLM_F_CREATE)) {
544 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200548 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200549 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jiri Pirko33a48922017-02-09 14:38:57 +0100551 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200552 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100553 if (IS_ERR(tp)) {
554 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto errout;
556 }
Minoru Usui12186be2009-06-02 02:17:34 -0700557 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100558 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
559 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 fh = tp->ops->get(tp, t->tcm_handle);
564
565 if (fh == 0) {
566 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200567 tcf_chain_tp_remove(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700568 tfilter_notify(net, skb, n, tp, fh,
569 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700570 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 err = 0;
572 goto errout;
573 }
574
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800575 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100576 !(n->nlmsg_flags & NLM_F_CREATE)) {
577 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700581 bool last;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900584 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700585 if (n->nlmsg_flags & NLM_F_EXCL) {
586 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700587 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100588 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 case RTM_DELTFILTER:
WANG Cong763dbf62017-04-19 14:21:21 -0700593 err = tp->ops->delete(tp, fh, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100594 if (err)
595 goto errout;
Jiri Pirko40c81b22017-02-09 14:39:00 +0100596 tfilter_notify(net, skb, n, tp, t->tcm_handle,
597 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700598 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200599 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700600 tcf_proto_destroy(tp);
601 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100602 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 case RTM_GETTFILTER:
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400604 err = tfilter_notify(net, skb, n, tp, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700605 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto errout;
607 default:
608 err = -EINVAL;
609 goto errout;
610 }
611 }
612
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700613 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
614 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700615 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200616 if (tp_created)
617 tcf_chain_tp_insert(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700618 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700619 } else {
620 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700621 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200625 if (chain)
626 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (cl)
628 cops->put(q, cl);
629 if (err == -EAGAIN)
630 /* Replay the request. */
631 goto replay;
632 return err;
633}
634
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400635static int tcf_fill_node(struct net *net, struct sk_buff *skb,
636 struct tcf_proto *tp, unsigned long fh, u32 portid,
637 u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct tcmsg *tcm;
640 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700641 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Eric W. Biederman15e47302012-09-07 20:12:54 +0000643 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
David S. Miller942b8162012-06-26 21:48:50 -0700644 if (!nlh)
645 goto out_nlmsg_trim;
646 tcm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700648 tcm->tcm__pad1 = 0;
Jiri Pirkoad61df92009-10-08 01:21:46 -0700649 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700650 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 tcm->tcm_parent = tp->classid;
652 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
David S. Miller1b34ec42012-03-29 05:11:39 -0400653 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
654 goto nla_put_failure;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200655 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
656 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tcm->tcm_handle = fh;
658 if (RTM_DELTFILTER != event) {
659 tcm->tcm_handle = 0;
WANG Cong832d1d52014-01-09 16:14:01 -0800660 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800661 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700663 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return skb->len;
665
David S. Miller942b8162012-06-26 21:48:50 -0700666out_nlmsg_trim:
Patrick McHardyadd93b62008-01-22 22:11:33 -0800667nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700668 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -1;
670}
671
Tom Goff7316ae82010-03-19 15:40:13 +0000672static int tfilter_notify(struct net *net, struct sk_buff *oskb,
673 struct nlmsghdr *n, struct tcf_proto *tp,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700674 unsigned long fh, int event, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct sk_buff *skb;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000677 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
680 if (!skb)
681 return -ENOBUFS;
682
Roman Mashak30a391a2016-11-16 17:16:10 -0500683 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
684 n->nlmsg_flags, event) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 kfree_skb(skb);
686 return -EINVAL;
687 }
688
Eric Dumazetfa59b272016-10-09 20:25:55 -0700689 if (unicast)
690 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
691
Eric W. Biederman15e47302012-09-07 20:12:54 +0000692 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800693 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800696struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct tcf_walker w;
698 struct sk_buff *skb;
699 struct netlink_callback *cb;
700};
701
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800702static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
703 struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800705 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800706 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
WANG Cong832d1d52014-01-09 16:14:01 -0800708 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400709 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
710 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Jiri Pirko5bc17012017-05-17 11:08:01 +0200713static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200714 struct netlink_callback *cb,
715 long index_start, long *p_index)
716{
717 struct net *net = sock_net(skb->sk);
718 struct tcmsg *tcm = nlmsg_data(cb->nlh);
719 struct tcf_dump_args arg;
720 struct tcf_proto *tp;
721
722 for (tp = rtnl_dereference(chain->filter_chain);
723 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
724 if (*p_index < index_start)
725 continue;
726 if (TC_H_MAJ(tcm->tcm_info) &&
727 TC_H_MAJ(tcm->tcm_info) != tp->prio)
728 continue;
729 if (TC_H_MIN(tcm->tcm_info) &&
730 TC_H_MIN(tcm->tcm_info) != tp->protocol)
731 continue;
732 if (*p_index > index_start)
733 memset(&cb->args[1], 0,
734 sizeof(cb->args) - sizeof(cb->args[0]));
735 if (cb->args[1] == 0) {
736 if (tcf_fill_node(net, skb, tp, 0,
737 NETLINK_CB(cb->skb).portid,
738 cb->nlh->nlmsg_seq, NLM_F_MULTI,
739 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200740 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200741
742 cb->args[1] = 1;
743 }
744 if (!tp->ops->walk)
745 continue;
746 arg.w.fn = tcf_node_dump;
747 arg.skb = skb;
748 arg.cb = cb;
749 arg.w.stop = 0;
750 arg.w.skip = cb->args[1] - 1;
751 arg.w.count = 0;
752 tp->ops->walk(tp, &arg.w);
753 cb->args[1] = arg.w.count + 1;
754 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200755 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200756 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200757 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200758}
759
Eric Dumazetbd27a872009-11-05 20:57:26 -0800760/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
762{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900763 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200764 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct net_device *dev;
766 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200767 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200768 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700769 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800771 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200772 long index_start;
773 long index;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200774 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Hong zhi guo573ce262013-03-27 06:47:04 +0000776 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200778
779 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
780 if (err)
781 return err;
782
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000783 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
784 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return skb->len;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!tcm->tcm_parent)
Patrick McHardyaf356af2009-09-04 06:41:18 +0000788 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 else
790 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
791 if (!q)
792 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000793 cops = q->ops->cl_ops;
794 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto errout;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200796 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000797 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (TC_H_MIN(tcm->tcm_parent)) {
799 cl = cops->get(q, tcm->tcm_parent);
800 if (cl == 0)
801 goto errout;
802 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200803 block = cops->tcf_block(q, cl);
804 if (!block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto errout;
806
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200807 index_start = cb->args[0];
808 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200809
810 list_for_each_entry(chain, &block->chain_list, list) {
811 if (tca[TCA_CHAIN] &&
812 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
813 continue;
814 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
815 break;
816 }
817
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200818 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820errout:
821 if (cl)
822 cops->put(q, cl);
823out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return skb->len;
825}
826
WANG Cong18d02642014-09-25 10:26:37 -0700827void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700830 LIST_HEAD(actions);
831
832 tcf_exts_to_list(exts, &actions);
833 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
834 kfree(exts->actions);
835 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#endif
837}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800838EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000840int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400841 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#ifdef CONFIG_NET_CLS_ACT
844 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct tc_action *act;
846
WANG Cong5da57f42013-12-15 20:15:07 -0800847 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200848 act = tcf_action_init_1(net, tp, tb[exts->police],
849 rate_tlv, "police", ovr,
850 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800851 if (IS_ERR(act))
852 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
WANG Cong33be6272013-12-15 20:15:05 -0800854 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700855 exts->actions[0] = act;
856 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800857 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700858 LIST_HEAD(actions);
859 int err, i = 0;
860
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200861 err = tcf_action_init(net, tp, tb[exts->action],
862 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400863 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800864 if (err)
865 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700866 list_for_each_entry(act, &actions, list)
867 exts->actions[i++] = act;
868 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#else
WANG Cong5da57f42013-12-15 20:15:07 -0800872 if ((exts->action && tb[exts->action]) ||
873 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return -EOPNOTSUPP;
875#endif
876
877 return 0;
878}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800879EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800881void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
882 struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700885 struct tcf_exts old = *dst;
886
Cong Wanga49eb422014-04-25 13:55:30 -0700887 tcf_tree_lock(tp);
WANG Cong22dc13c2016-08-13 22:35:00 -0700888 dst->nr_actions = src->nr_actions;
889 dst->actions = src->actions;
WANG Cong5301e3e2014-10-06 17:21:54 -0700890 dst->type = src->type;
Cong Wanga49eb422014-04-25 13:55:30 -0700891 tcf_tree_unlock(tp);
WANG Cong22dc13c2016-08-13 22:35:00 -0700892
893 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#endif
895}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800896EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
WANG Cong22dc13c2016-08-13 22:35:00 -0700898#ifdef CONFIG_NET_CLS_ACT
899static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
900{
901 if (exts->nr_actions == 0)
902 return NULL;
903 else
904 return exts->actions[0];
905}
906#endif
WANG Cong33be6272013-12-15 20:15:05 -0800907
WANG Cong5da57f42013-12-15 20:15:07 -0800908int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700911 struct nlattr *nest;
912
WANG Cong22dc13c2016-08-13 22:35:00 -0700913 if (exts->action && exts->nr_actions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * again for backward compatible mode - we want
916 * to work with both old and new modes of entering
917 * tc data even if iproute2 was newer - jhs
918 */
WANG Cong33be6272013-12-15 20:15:05 -0800919 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700920 LIST_HEAD(actions);
921
WANG Cong5da57f42013-12-15 20:15:07 -0800922 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800923 if (nest == NULL)
924 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700925
926 tcf_exts_to_list(exts, &actions);
927 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800928 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800929 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800930 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800931 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800932 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500933 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800934 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800935 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800936 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800937 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -0700941
942nla_put_failure:
943 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -0700945#else
946 return 0;
947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800949EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800951
WANG Cong5da57f42013-12-15 20:15:07 -0800952int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800955 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +0100956 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800957 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#endif
959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800961EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200963int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
964 struct net_device **hw_dev)
965{
966#ifdef CONFIG_NET_CLS_ACT
967 const struct tc_action *a;
968 LIST_HEAD(actions);
969
970 if (tc_no_actions(exts))
971 return -EINVAL;
972
973 tcf_exts_to_list(exts, &actions);
974 list_for_each_entry(a, &actions, list) {
975 if (a->ops->get_dev) {
976 a->ops->get_dev(a, dev_net(dev), hw_dev);
977 break;
978 }
979 }
980 if (*hw_dev)
981 return 0;
982#endif
983 return -EOPNOTSUPP;
984}
985EXPORT_SYMBOL(tcf_exts_get_dev);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987static int __init tc_filter_init(void)
988{
Greg Rosec7ac8672011-06-10 01:27:09 +0000989 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
990 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
Thomas Graf82623c02007-03-22 11:56:22 -0700991 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Greg Rosec7ac8672011-06-10 01:27:09 +0000992 tc_dump_tfilter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
997subsys_initcall(tc_filter_init);