blob: 63aa2ea5f00c4ae17c1918428a1c395c49b87416 [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,
132 struct tcf_block *block)
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 Pirko6529eab2017-05-17 11:07:55 +0200168 tp->block = block;
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 Pirko2190d1d2017-05-17 11:07:59 +0200189static struct tcf_chain *tcf_chain_create(void)
190{
191 return kzalloc(sizeof(struct tcf_chain), GFP_KERNEL);
192}
193
194static void tcf_chain_destroy(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100195{
196 struct tcf_proto *tp;
197
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200198 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
199 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700200 tcf_proto_destroy(tp);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100201 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200202 kfree(chain);
203}
204
205static void
206tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
207 struct tcf_proto __rcu **p_filter_chain)
208{
209 chain->p_filter_chain = p_filter_chain;
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100210}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200211
212int tcf_block_get(struct tcf_block **p_block,
213 struct tcf_proto __rcu **p_filter_chain)
214{
215 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200216 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200217
218 if (!block)
219 return -ENOMEM;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200220 block->chain = tcf_chain_create();
221 if (!block->chain) {
222 err = -ENOMEM;
223 goto err_chain_create;
224 }
225 tcf_chain_filter_chain_ptr_set(block->chain, p_filter_chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200226 *p_block = block;
227 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200228
229err_chain_create:
230 kfree(block);
231 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200232}
233EXPORT_SYMBOL(tcf_block_get);
234
235void tcf_block_put(struct tcf_block *block)
236{
237 if (!block)
238 return;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200239 tcf_chain_destroy(block->chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200240 kfree(block);
241}
242EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100243
Jiri Pirko87d83092017-05-17 11:07:54 +0200244/* Main classifier routine: scans classifier chain attached
245 * to this qdisc, (optionally) tests for protocol and asks
246 * specific classifiers.
247 */
248int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
249 struct tcf_result *res, bool compat_mode)
250{
251 __be16 protocol = tc_skb_protocol(skb);
252#ifdef CONFIG_NET_CLS_ACT
253 const int max_reclassify_loop = 4;
254 const struct tcf_proto *old_tp = tp;
255 int limit = 0;
256
257reclassify:
258#endif
259 for (; tp; tp = rcu_dereference_bh(tp->next)) {
260 int err;
261
262 if (tp->protocol != protocol &&
263 tp->protocol != htons(ETH_P_ALL))
264 continue;
265
266 err = tp->classify(skb, tp, res);
267#ifdef CONFIG_NET_CLS_ACT
268 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode))
269 goto reset;
270#endif
271 if (err >= 0)
272 return err;
273 }
274
275 return TC_ACT_UNSPEC; /* signal: continue lookup */
276#ifdef CONFIG_NET_CLS_ACT
277reset:
278 if (unlikely(limit++ >= max_reclassify_loop)) {
279 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
280 tp->q->ops->id, tp->prio & 0xffff,
281 ntohs(tp->protocol));
282 return TC_ACT_SHOT;
283 }
284
285 tp = old_tp;
286 protocol = tc_skb_protocol(skb);
287 goto reclassify;
288#endif
289}
290EXPORT_SYMBOL(tcf_classify);
291
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200292struct tcf_chain_info {
293 struct tcf_proto __rcu **pprev;
294 struct tcf_proto __rcu *next;
295};
296
297static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
298{
299 return rtnl_dereference(*chain_info->pprev);
300}
301
302static void tcf_chain_tp_insert(struct tcf_chain *chain,
303 struct tcf_chain_info *chain_info,
304 struct tcf_proto *tp)
305{
306 if (chain->p_filter_chain &&
307 *chain_info->pprev == chain->filter_chain)
308 *chain->p_filter_chain = tp;
309 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
310 rcu_assign_pointer(*chain_info->pprev, tp);
311}
312
313static void tcf_chain_tp_remove(struct tcf_chain *chain,
314 struct tcf_chain_info *chain_info,
315 struct tcf_proto *tp)
316{
317 struct tcf_proto *next = rtnl_dereference(chain_info->next);
318
319 if (chain->p_filter_chain && tp == chain->filter_chain)
320 *chain->p_filter_chain = next;
321 RCU_INIT_POINTER(*chain_info->pprev, next);
322}
323
324static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
325 struct tcf_chain_info *chain_info,
326 u32 protocol, u32 prio,
327 bool prio_allocate)
328{
329 struct tcf_proto **pprev;
330 struct tcf_proto *tp;
331
332 /* Check the chain for existence of proto-tcf with this priority */
333 for (pprev = &chain->filter_chain;
334 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
335 if (tp->prio >= prio) {
336 if (tp->prio == prio) {
337 if (prio_allocate ||
338 (tp->protocol != protocol && protocol))
339 return ERR_PTR(-EINVAL);
340 } else {
341 tp = NULL;
342 }
343 break;
344 }
345 }
346 chain_info->pprev = pprev;
347 chain_info->next = tp ? tp->next : NULL;
348 return tp;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* Add/change/delete/get a filter node */
352
David Ahernc21ef3e2017-04-16 09:48:24 -0700353static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
354 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900356 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800357 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct tcmsg *t;
359 u32 protocol;
360 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200361 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 u32 parent;
363 struct net_device *dev;
364 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200365 struct tcf_chain_info chain_info;
366 struct tcf_chain *chain;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200367 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800369 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long cl;
371 unsigned long fh;
372 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100373 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400375 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400376 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000377 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100380 tp_created = 0;
381
David Ahernc21ef3e2017-04-16 09:48:24 -0700382 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000383 if (err < 0)
384 return err;
385
David S. Miller942b8162012-06-26 21:48:50 -0700386 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 protocol = TC_H_MIN(t->tcm_info);
388 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200389 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 parent = t->tcm_parent;
391 cl = 0;
392
393 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200394 switch (n->nlmsg_type) {
395 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200396 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200397 return -ENOENT;
398 break;
399 case RTM_NEWTFILTER:
400 /* If no priority is provided by the user,
401 * we allocate one.
402 */
403 if (n->nlmsg_flags & NLM_F_CREATE) {
404 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200405 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200406 break;
407 }
408 /* fall-through */
409 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 /* Find head of filter chain. */
415
416 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000417 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800418 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -ENODEV;
420
421 /* Find qdisc */
422 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000423 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800425 } else {
426 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
427 if (q == NULL)
428 return -EINVAL;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000432 cops = q->ops->cl_ops;
433 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EINVAL;
435
Jiri Pirko6529eab2017-05-17 11:07:55 +0200436 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000437 return -EOPNOTSUPP;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Do we search for filter, attached to class? */
440 if (TC_H_MIN(parent)) {
441 cl = cops->get(q, parent);
442 if (cl == 0)
443 return -ENOENT;
444 }
445
446 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200447 block = cops->tcf_block(q, cl);
448 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100449 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100451 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200452 chain = block->chain;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200453
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200454 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
455 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
Jiri Pirkofbe9c5b2017-05-17 11:07:56 +0200456 tcf_chain_destroy(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200457 err = 0;
458 goto errout;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200461 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
462 prio, prio_allocate);
463 if (IS_ERR(tp)) {
464 err = PTR_ERR(tp);
465 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 if (tp == NULL) {
469 /* Proto-tcf does not exist, create new one */
470
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100471 if (tca[TCA_KIND] == NULL || !protocol) {
472 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000476 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100477 !(n->nlmsg_flags & NLM_F_CREATE)) {
478 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200482 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200483 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jiri Pirko33a48922017-02-09 14:38:57 +0100485 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200486 protocol, prio, parent, q, block);
Jiri Pirko33a48922017-02-09 14:38:57 +0100487 if (IS_ERR(tp)) {
488 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto errout;
490 }
Minoru Usui12186be2009-06-02 02:17:34 -0700491 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100492 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
493 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 fh = tp->ops->get(tp, t->tcm_handle);
498
499 if (fh == 0) {
500 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200501 tcf_chain_tp_remove(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700502 tfilter_notify(net, skb, n, tp, fh,
503 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700504 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 err = 0;
506 goto errout;
507 }
508
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800509 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100510 !(n->nlmsg_flags & NLM_F_CREATE)) {
511 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700515 bool last;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900518 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700519 if (n->nlmsg_flags & NLM_F_EXCL) {
520 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700521 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100522 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526 case RTM_DELTFILTER:
WANG Cong763dbf62017-04-19 14:21:21 -0700527 err = tp->ops->delete(tp, fh, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100528 if (err)
529 goto errout;
Jiri Pirko40c81b22017-02-09 14:39:00 +0100530 tfilter_notify(net, skb, n, tp, t->tcm_handle,
531 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700532 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200533 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700534 tcf_proto_destroy(tp);
535 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100536 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 case RTM_GETTFILTER:
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400538 err = tfilter_notify(net, skb, n, tp, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700539 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto errout;
541 default:
542 err = -EINVAL;
543 goto errout;
544 }
545 }
546
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700547 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
548 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700549 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200550 if (tp_created)
551 tcf_chain_tp_insert(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700552 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700553 } else {
554 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700555 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558errout:
559 if (cl)
560 cops->put(q, cl);
561 if (err == -EAGAIN)
562 /* Replay the request. */
563 goto replay;
564 return err;
565}
566
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400567static int tcf_fill_node(struct net *net, struct sk_buff *skb,
568 struct tcf_proto *tp, unsigned long fh, u32 portid,
569 u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct tcmsg *tcm;
572 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700573 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric W. Biederman15e47302012-09-07 20:12:54 +0000575 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
David S. Miller942b8162012-06-26 21:48:50 -0700576 if (!nlh)
577 goto out_nlmsg_trim;
578 tcm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700580 tcm->tcm__pad1 = 0;
Jiri Pirkoad61df92009-10-08 01:21:46 -0700581 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700582 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 tcm->tcm_parent = tp->classid;
584 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
David S. Miller1b34ec42012-03-29 05:11:39 -0400585 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
586 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 tcm->tcm_handle = fh;
588 if (RTM_DELTFILTER != event) {
589 tcm->tcm_handle = 0;
WANG Cong832d1d52014-01-09 16:14:01 -0800590 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800591 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700593 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return skb->len;
595
David S. Miller942b8162012-06-26 21:48:50 -0700596out_nlmsg_trim:
Patrick McHardyadd93b62008-01-22 22:11:33 -0800597nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700598 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -1;
600}
601
Tom Goff7316ae82010-03-19 15:40:13 +0000602static int tfilter_notify(struct net *net, struct sk_buff *oskb,
603 struct nlmsghdr *n, struct tcf_proto *tp,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700604 unsigned long fh, int event, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct sk_buff *skb;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000607 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
610 if (!skb)
611 return -ENOBUFS;
612
Roman Mashak30a391a2016-11-16 17:16:10 -0500613 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
614 n->nlmsg_flags, event) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 kfree_skb(skb);
616 return -EINVAL;
617 }
618
Eric Dumazetfa59b272016-10-09 20:25:55 -0700619 if (unicast)
620 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
621
Eric W. Biederman15e47302012-09-07 20:12:54 +0000622 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800623 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800626struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct tcf_walker w;
628 struct sk_buff *skb;
629 struct netlink_callback *cb;
630};
631
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800632static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
633 struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800635 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800636 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
WANG Cong832d1d52014-01-09 16:14:01 -0800638 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400639 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
640 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200643static void tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
644 struct netlink_callback *cb,
645 long index_start, long *p_index)
646{
647 struct net *net = sock_net(skb->sk);
648 struct tcmsg *tcm = nlmsg_data(cb->nlh);
649 struct tcf_dump_args arg;
650 struct tcf_proto *tp;
651
652 for (tp = rtnl_dereference(chain->filter_chain);
653 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
654 if (*p_index < index_start)
655 continue;
656 if (TC_H_MAJ(tcm->tcm_info) &&
657 TC_H_MAJ(tcm->tcm_info) != tp->prio)
658 continue;
659 if (TC_H_MIN(tcm->tcm_info) &&
660 TC_H_MIN(tcm->tcm_info) != tp->protocol)
661 continue;
662 if (*p_index > index_start)
663 memset(&cb->args[1], 0,
664 sizeof(cb->args) - sizeof(cb->args[0]));
665 if (cb->args[1] == 0) {
666 if (tcf_fill_node(net, skb, tp, 0,
667 NETLINK_CB(cb->skb).portid,
668 cb->nlh->nlmsg_seq, NLM_F_MULTI,
669 RTM_NEWTFILTER) <= 0)
670 break;
671
672 cb->args[1] = 1;
673 }
674 if (!tp->ops->walk)
675 continue;
676 arg.w.fn = tcf_node_dump;
677 arg.skb = skb;
678 arg.cb = cb;
679 arg.w.stop = 0;
680 arg.w.skip = cb->args[1] - 1;
681 arg.w.count = 0;
682 tp->ops->walk(tp, &arg.w);
683 cb->args[1] = arg.w.count + 1;
684 if (arg.w.stop)
685 break;
686 }
687}
688
Eric Dumazetbd27a872009-11-05 20:57:26 -0800689/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
691{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900692 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct net_device *dev;
694 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200695 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200696 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700697 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800699 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200700 long index_start;
701 long index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Hong zhi guo573ce262013-03-27 06:47:04 +0000703 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return skb->len;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000705 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
706 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return skb->len;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!tcm->tcm_parent)
Patrick McHardyaf356af2009-09-04 06:41:18 +0000710 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 else
712 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
713 if (!q)
714 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000715 cops = q->ops->cl_ops;
716 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto errout;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200718 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000719 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (TC_H_MIN(tcm->tcm_parent)) {
721 cl = cops->get(q, tcm->tcm_parent);
722 if (cl == 0)
723 goto errout;
724 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200725 block = cops->tcf_block(q, cl);
726 if (!block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto errout;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200728 chain = block->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200730 index_start = cb->args[0];
731 index = 0;
732 tcf_chain_dump(chain, skb, cb, index_start, &index);
733 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735errout:
736 if (cl)
737 cops->put(q, cl);
738out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return skb->len;
740}
741
WANG Cong18d02642014-09-25 10:26:37 -0700742void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700745 LIST_HEAD(actions);
746
747 tcf_exts_to_list(exts, &actions);
748 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
749 kfree(exts->actions);
750 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#endif
752}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800753EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000755int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400756 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#ifdef CONFIG_NET_CLS_ACT
759 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct tc_action *act;
761
WANG Cong5da57f42013-12-15 20:15:07 -0800762 if (exts->police && tb[exts->police]) {
763 act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400764 "police", ovr, TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800765 if (IS_ERR(act))
766 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
WANG Cong33be6272013-12-15 20:15:05 -0800768 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700769 exts->actions[0] = act;
770 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800771 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700772 LIST_HEAD(actions);
773 int err, i = 0;
774
WANG Cong5da57f42013-12-15 20:15:07 -0800775 err = tcf_action_init(net, tb[exts->action], rate_tlv,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400776 NULL, ovr, TCA_ACT_BIND,
777 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800778 if (err)
779 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700780 list_for_each_entry(act, &actions, list)
781 exts->actions[i++] = act;
782 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#else
WANG Cong5da57f42013-12-15 20:15:07 -0800786 if ((exts->action && tb[exts->action]) ||
787 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EOPNOTSUPP;
789#endif
790
791 return 0;
792}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800793EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800795void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
796 struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700799 struct tcf_exts old = *dst;
800
Cong Wanga49eb422014-04-25 13:55:30 -0700801 tcf_tree_lock(tp);
WANG Cong22dc13c2016-08-13 22:35:00 -0700802 dst->nr_actions = src->nr_actions;
803 dst->actions = src->actions;
WANG Cong5301e3e2014-10-06 17:21:54 -0700804 dst->type = src->type;
Cong Wanga49eb422014-04-25 13:55:30 -0700805 tcf_tree_unlock(tp);
WANG Cong22dc13c2016-08-13 22:35:00 -0700806
807 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#endif
809}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800810EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
WANG Cong22dc13c2016-08-13 22:35:00 -0700812#ifdef CONFIG_NET_CLS_ACT
813static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
814{
815 if (exts->nr_actions == 0)
816 return NULL;
817 else
818 return exts->actions[0];
819}
820#endif
WANG Cong33be6272013-12-15 20:15:05 -0800821
WANG Cong5da57f42013-12-15 20:15:07 -0800822int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700825 struct nlattr *nest;
826
WANG Cong22dc13c2016-08-13 22:35:00 -0700827 if (exts->action && exts->nr_actions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /*
829 * again for backward compatible mode - we want
830 * to work with both old and new modes of entering
831 * tc data even if iproute2 was newer - jhs
832 */
WANG Cong33be6272013-12-15 20:15:05 -0800833 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700834 LIST_HEAD(actions);
835
WANG Cong5da57f42013-12-15 20:15:07 -0800836 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800837 if (nest == NULL)
838 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700839
840 tcf_exts_to_list(exts, &actions);
841 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800842 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800843 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800844 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800845 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800846 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500847 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800848 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800849 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800850 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800851 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -0700855
856nla_put_failure:
857 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -0700859#else
860 return 0;
861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800863EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800865
WANG Cong5da57f42013-12-15 20:15:07 -0800866int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800869 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +0100870 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800871 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#endif
873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800875EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200877int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
878 struct net_device **hw_dev)
879{
880#ifdef CONFIG_NET_CLS_ACT
881 const struct tc_action *a;
882 LIST_HEAD(actions);
883
884 if (tc_no_actions(exts))
885 return -EINVAL;
886
887 tcf_exts_to_list(exts, &actions);
888 list_for_each_entry(a, &actions, list) {
889 if (a->ops->get_dev) {
890 a->ops->get_dev(a, dev_net(dev), hw_dev);
891 break;
892 }
893 }
894 if (*hw_dev)
895 return 0;
896#endif
897 return -EOPNOTSUPP;
898}
899EXPORT_SYMBOL(tcf_exts_get_dev);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int __init tc_filter_init(void)
902{
Greg Rosec7ac8672011-06-10 01:27:09 +0000903 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
904 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
Thomas Graf82623c02007-03-22 11:56:22 -0700905 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Greg Rosec7ac8672011-06-10 01:27:09 +0000906 tc_dump_tfilter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
909}
910
911subsys_initcall(tc_filter_init);