blob: afd099727aea0174f8011fbaec01061b84822115 [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
WANG Cong54df2cf2017-08-04 21:31:42 -0700107static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
108 struct nlmsghdr *n, struct tcf_proto *tp,
109 unsigned long fh, bool unicast, bool *last);
110
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200111static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
112 struct nlmsghdr *n,
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200113 struct tcf_chain *chain, int event)
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200114{
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200115 struct tcf_proto *tp;
116
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200117 for (tp = rtnl_dereference(chain->filter_chain);
118 tp; tp = rtnl_dereference(tp->next))
Roman Mashak19a8bb22016-11-22 20:57:04 -0500119 tfilter_notify(net, oskb, n, tp, 0, event, false);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* Select new prio value from the range, managed by kernel. */
123
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800124static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800126 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000129 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jiri Pirko79619732017-05-17 11:07:58 +0200131 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Jiri Pirko33a48922017-02-09 14:38:57 +0100134static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200135 u32 prio, u32 parent, struct Qdisc *q,
Jiri Pirko5bc17012017-05-17 11:08:01 +0200136 struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100137{
138 struct tcf_proto *tp;
139 int err;
140
141 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
142 if (!tp)
143 return ERR_PTR(-ENOBUFS);
144
145 err = -ENOENT;
146 tp->ops = tcf_proto_lookup_ops(kind);
147 if (!tp->ops) {
148#ifdef CONFIG_MODULES
149 rtnl_unlock();
150 request_module("cls_%s", kind);
151 rtnl_lock();
152 tp->ops = tcf_proto_lookup_ops(kind);
153 /* We dropped the RTNL semaphore in order to perform
154 * the module load. So, even if we succeeded in loading
155 * the module we have to replay the request. We indicate
156 * this using -EAGAIN.
157 */
158 if (tp->ops) {
159 module_put(tp->ops->owner);
160 err = -EAGAIN;
161 } else {
162 err = -ENOENT;
163 }
164 goto errout;
165#endif
166 }
167 tp->classify = tp->ops->classify;
168 tp->protocol = protocol;
169 tp->prio = prio;
170 tp->classid = parent;
171 tp->q = q;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200172 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100173
174 err = tp->ops->init(tp);
175 if (err) {
176 module_put(tp->ops->owner);
177 goto errout;
178 }
179 return tp;
180
181errout:
182 kfree(tp);
183 return ERR_PTR(err);
184}
185
WANG Cong763dbf62017-04-19 14:21:21 -0700186static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100187{
WANG Cong763dbf62017-04-19 14:21:21 -0700188 tp->ops->destroy(tp);
189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100191}
192
Jiri Pirko5bc17012017-05-17 11:08:01 +0200193static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
194 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200195{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200196 struct tcf_chain *chain;
197
198 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
199 if (!chain)
200 return NULL;
201 list_add_tail(&chain->list, &block->chain_list);
202 chain->block = block;
203 chain->index = chain_index;
204 chain->refcnt = 1;
205 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200206}
207
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200208static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100209{
210 struct tcf_proto *tp;
211
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200212 if (*chain->p_filter_chain)
213 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200214 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
215 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700216 tcf_proto_destroy(tp);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100217 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200218}
219
220static void tcf_chain_destroy(struct tcf_chain *chain)
221{
222 list_del(&chain->list);
223 tcf_chain_flush(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200224 kfree(chain);
225}
226
WANG Cong367a8ce2017-05-23 09:42:37 -0700227struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
228 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200229{
230 struct tcf_chain *chain;
231
232 list_for_each_entry(chain, &block->chain_list, list) {
233 if (chain->index == chain_index) {
234 chain->refcnt++;
235 return chain;
236 }
237 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700238 if (create)
239 return tcf_chain_create(block, chain_index);
240 else
241 return NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200242}
243EXPORT_SYMBOL(tcf_chain_get);
244
245void tcf_chain_put(struct tcf_chain *chain)
246{
247 /* Destroy unused chain, with exception of chain 0, which is the
248 * default one and has to be always present.
249 */
250 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
251 tcf_chain_destroy(chain);
252}
253EXPORT_SYMBOL(tcf_chain_put);
254
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200255static void
256tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
257 struct tcf_proto __rcu **p_filter_chain)
258{
259 chain->p_filter_chain = p_filter_chain;
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100260}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200261
262int tcf_block_get(struct tcf_block **p_block,
263 struct tcf_proto __rcu **p_filter_chain)
264{
265 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200266 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200267 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200268
269 if (!block)
270 return -ENOMEM;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200271 INIT_LIST_HEAD(&block->chain_list);
272 /* Create chain 0 by default, it has to be always present. */
273 chain = tcf_chain_create(block, 0);
274 if (!chain) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200275 err = -ENOMEM;
276 goto err_chain_create;
277 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200278 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200279 *p_block = block;
280 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200281
282err_chain_create:
283 kfree(block);
284 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200285}
286EXPORT_SYMBOL(tcf_block_get);
287
288void tcf_block_put(struct tcf_block *block)
289{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200290 struct tcf_chain *chain, *tmp;
291
Jiri Pirko6529eab2017-05-17 11:07:55 +0200292 if (!block)
293 return;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200294
295 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
296 tcf_chain_destroy(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200297 kfree(block);
298}
299EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100300
Jiri Pirko87d83092017-05-17 11:07:54 +0200301/* Main classifier routine: scans classifier chain attached
302 * to this qdisc, (optionally) tests for protocol and asks
303 * specific classifiers.
304 */
305int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
306 struct tcf_result *res, bool compat_mode)
307{
308 __be16 protocol = tc_skb_protocol(skb);
309#ifdef CONFIG_NET_CLS_ACT
310 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200311 const struct tcf_proto *orig_tp = tp;
312 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200313 int limit = 0;
314
315reclassify:
316#endif
317 for (; tp; tp = rcu_dereference_bh(tp->next)) {
318 int err;
319
320 if (tp->protocol != protocol &&
321 tp->protocol != htons(ETH_P_ALL))
322 continue;
323
324 err = tp->classify(skb, tp, res);
325#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200326 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200327 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200328 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200329 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200330 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200331 goto reset;
332 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200333#endif
334 if (err >= 0)
335 return err;
336 }
337
338 return TC_ACT_UNSPEC; /* signal: continue lookup */
339#ifdef CONFIG_NET_CLS_ACT
340reset:
341 if (unlikely(limit++ >= max_reclassify_loop)) {
342 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
343 tp->q->ops->id, tp->prio & 0xffff,
344 ntohs(tp->protocol));
345 return TC_ACT_SHOT;
346 }
347
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200348 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200349 protocol = tc_skb_protocol(skb);
350 goto reclassify;
351#endif
352}
353EXPORT_SYMBOL(tcf_classify);
354
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200355struct tcf_chain_info {
356 struct tcf_proto __rcu **pprev;
357 struct tcf_proto __rcu *next;
358};
359
360static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
361{
362 return rtnl_dereference(*chain_info->pprev);
363}
364
365static void tcf_chain_tp_insert(struct tcf_chain *chain,
366 struct tcf_chain_info *chain_info,
367 struct tcf_proto *tp)
368{
369 if (chain->p_filter_chain &&
370 *chain_info->pprev == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200371 rcu_assign_pointer(*chain->p_filter_chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200372 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
373 rcu_assign_pointer(*chain_info->pprev, tp);
374}
375
376static void tcf_chain_tp_remove(struct tcf_chain *chain,
377 struct tcf_chain_info *chain_info,
378 struct tcf_proto *tp)
379{
380 struct tcf_proto *next = rtnl_dereference(chain_info->next);
381
382 if (chain->p_filter_chain && tp == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200383 RCU_INIT_POINTER(*chain->p_filter_chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200384 RCU_INIT_POINTER(*chain_info->pprev, next);
385}
386
387static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
388 struct tcf_chain_info *chain_info,
389 u32 protocol, u32 prio,
390 bool prio_allocate)
391{
392 struct tcf_proto **pprev;
393 struct tcf_proto *tp;
394
395 /* Check the chain for existence of proto-tcf with this priority */
396 for (pprev = &chain->filter_chain;
397 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
398 if (tp->prio >= prio) {
399 if (tp->prio == prio) {
400 if (prio_allocate ||
401 (tp->protocol != protocol && protocol))
402 return ERR_PTR(-EINVAL);
403 } else {
404 tp = NULL;
405 }
406 break;
407 }
408 }
409 chain_info->pprev = pprev;
410 chain_info->next = tp ? tp->next : NULL;
411 return tp;
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* Add/change/delete/get a filter node */
415
David Ahernc21ef3e2017-04-16 09:48:24 -0700416static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
417 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900419 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800420 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct tcmsg *t;
422 u32 protocol;
423 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200424 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200426 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct net_device *dev;
428 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200429 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200430 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200431 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800433 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long cl;
435 unsigned long fh;
436 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100437 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400439 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400440 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000441 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100444 tp_created = 0;
445
David Ahernc21ef3e2017-04-16 09:48:24 -0700446 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000447 if (err < 0)
448 return err;
449
David S. Miller942b8162012-06-26 21:48:50 -0700450 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 protocol = TC_H_MIN(t->tcm_info);
452 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200453 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 parent = t->tcm_parent;
455 cl = 0;
456
457 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200458 switch (n->nlmsg_type) {
459 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200460 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200461 return -ENOENT;
462 break;
463 case RTM_NEWTFILTER:
464 /* If no priority is provided by the user,
465 * we allocate one.
466 */
467 if (n->nlmsg_flags & NLM_F_CREATE) {
468 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200469 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200470 break;
471 }
472 /* fall-through */
473 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
478 /* Find head of filter chain. */
479
480 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000481 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800482 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -ENODEV;
484
485 /* Find qdisc */
486 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000487 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800489 } else {
490 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
491 if (q == NULL)
492 return -EINVAL;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000496 cops = q->ops->cl_ops;
497 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EINVAL;
499
Jiri Pirko6529eab2017-05-17 11:07:55 +0200500 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000501 return -EOPNOTSUPP;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Do we search for filter, attached to class? */
504 if (TC_H_MIN(parent)) {
505 cl = cops->get(q, parent);
506 if (cl == 0)
507 return -ENOENT;
508 }
509
510 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200511 block = cops->tcf_block(q, cl);
512 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100513 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100515 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200516
517 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
518 if (chain_index > TC_ACT_EXT_VAL_MASK) {
519 err = -EINVAL;
520 goto errout;
521 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700522 chain = tcf_chain_get(block, chain_index,
523 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200524 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700525 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200526 goto errout;
527 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200528
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200529 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
530 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200531 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200532 err = 0;
533 goto errout;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200536 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
537 prio, prio_allocate);
538 if (IS_ERR(tp)) {
539 err = PTR_ERR(tp);
540 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
543 if (tp == NULL) {
544 /* Proto-tcf does not exist, create new one */
545
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100546 if (tca[TCA_KIND] == NULL || !protocol) {
547 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000551 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100552 !(n->nlmsg_flags & NLM_F_CREATE)) {
553 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200557 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200558 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jiri Pirko33a48922017-02-09 14:38:57 +0100560 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200561 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100562 if (IS_ERR(tp)) {
563 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto errout;
565 }
Minoru Usui12186be2009-06-02 02:17:34 -0700566 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100567 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
568 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 fh = tp->ops->get(tp, t->tcm_handle);
573
574 if (fh == 0) {
575 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200576 tcf_chain_tp_remove(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700577 tfilter_notify(net, skb, n, tp, fh,
578 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700579 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 err = 0;
581 goto errout;
582 }
583
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800584 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100585 !(n->nlmsg_flags & NLM_F_CREATE)) {
586 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700590 bool last;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900593 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700594 if (n->nlmsg_flags & NLM_F_EXCL) {
595 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700596 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100597 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 case RTM_DELTFILTER:
WANG Cong54df2cf2017-08-04 21:31:42 -0700602 err = tfilter_del_notify(net, skb, n, tp, fh, false,
603 &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100604 if (err)
605 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -0700606 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200607 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700608 tcf_proto_destroy(tp);
609 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100610 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 case RTM_GETTFILTER:
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400612 err = tfilter_notify(net, skb, n, tp, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700613 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto errout;
615 default:
616 err = -EINVAL;
617 goto errout;
618 }
619 }
620
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700621 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
622 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700623 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200624 if (tp_created)
625 tcf_chain_tp_insert(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700626 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700627 } else {
628 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700629 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200633 if (chain)
634 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (cl)
636 cops->put(q, cl);
637 if (err == -EAGAIN)
638 /* Replay the request. */
639 goto replay;
640 return err;
641}
642
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400643static int tcf_fill_node(struct net *net, struct sk_buff *skb,
644 struct tcf_proto *tp, unsigned long fh, u32 portid,
645 u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct tcmsg *tcm;
648 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700649 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric W. Biederman15e47302012-09-07 20:12:54 +0000651 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
David S. Miller942b8162012-06-26 21:48:50 -0700652 if (!nlh)
653 goto out_nlmsg_trim;
654 tcm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700656 tcm->tcm__pad1 = 0;
Jiri Pirkoad61df92009-10-08 01:21:46 -0700657 tcm->tcm__pad2 = 0;
David S. Miller5ce2d482008-07-08 17:06:30 -0700658 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 tcm->tcm_parent = tp->classid;
660 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
David S. Miller1b34ec42012-03-29 05:11:39 -0400661 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
662 goto nla_put_failure;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200663 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
664 goto nla_put_failure;
WANG Cong54df2cf2017-08-04 21:31:42 -0700665 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 tcm->tcm_handle = 0;
WANG Cong54df2cf2017-08-04 21:31:42 -0700667 } else {
WANG Cong832d1d52014-01-09 16:14:01 -0800668 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800669 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700671 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return skb->len;
673
David S. Miller942b8162012-06-26 21:48:50 -0700674out_nlmsg_trim:
Patrick McHardyadd93b62008-01-22 22:11:33 -0800675nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700676 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -1;
678}
679
Tom Goff7316ae82010-03-19 15:40:13 +0000680static int tfilter_notify(struct net *net, struct sk_buff *oskb,
681 struct nlmsghdr *n, struct tcf_proto *tp,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700682 unsigned long fh, int event, bool unicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct sk_buff *skb;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000685 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
688 if (!skb)
689 return -ENOBUFS;
690
Roman Mashak30a391a2016-11-16 17:16:10 -0500691 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
692 n->nlmsg_flags, event) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree_skb(skb);
694 return -EINVAL;
695 }
696
Eric Dumazetfa59b272016-10-09 20:25:55 -0700697 if (unicast)
698 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
699
Eric W. Biederman15e47302012-09-07 20:12:54 +0000700 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800701 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
WANG Cong54df2cf2017-08-04 21:31:42 -0700704static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
705 struct nlmsghdr *n, struct tcf_proto *tp,
706 unsigned long fh, bool unicast, bool *last)
707{
708 struct sk_buff *skb;
709 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
710 int err;
711
712 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
713 if (!skb)
714 return -ENOBUFS;
715
716 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
717 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
718 kfree_skb(skb);
719 return -EINVAL;
720 }
721
722 err = tp->ops->delete(tp, fh, last);
723 if (err) {
724 kfree_skb(skb);
725 return err;
726 }
727
728 if (unicast)
729 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
730
731 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
732 n->nlmsg_flags & NLM_F_ECHO);
733}
734
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800735struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct tcf_walker w;
737 struct sk_buff *skb;
738 struct netlink_callback *cb;
739};
740
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800741static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
742 struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800744 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800745 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
WANG Cong832d1d52014-01-09 16:14:01 -0800747 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400748 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
749 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Jiri Pirko5bc17012017-05-17 11:08:01 +0200752static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200753 struct netlink_callback *cb,
754 long index_start, long *p_index)
755{
756 struct net *net = sock_net(skb->sk);
757 struct tcmsg *tcm = nlmsg_data(cb->nlh);
758 struct tcf_dump_args arg;
759 struct tcf_proto *tp;
760
761 for (tp = rtnl_dereference(chain->filter_chain);
762 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
763 if (*p_index < index_start)
764 continue;
765 if (TC_H_MAJ(tcm->tcm_info) &&
766 TC_H_MAJ(tcm->tcm_info) != tp->prio)
767 continue;
768 if (TC_H_MIN(tcm->tcm_info) &&
769 TC_H_MIN(tcm->tcm_info) != tp->protocol)
770 continue;
771 if (*p_index > index_start)
772 memset(&cb->args[1], 0,
773 sizeof(cb->args) - sizeof(cb->args[0]));
774 if (cb->args[1] == 0) {
775 if (tcf_fill_node(net, skb, tp, 0,
776 NETLINK_CB(cb->skb).portid,
777 cb->nlh->nlmsg_seq, NLM_F_MULTI,
778 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200779 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200780
781 cb->args[1] = 1;
782 }
783 if (!tp->ops->walk)
784 continue;
785 arg.w.fn = tcf_node_dump;
786 arg.skb = skb;
787 arg.cb = cb;
788 arg.w.stop = 0;
789 arg.w.skip = cb->args[1] - 1;
790 arg.w.count = 0;
791 tp->ops->walk(tp, &arg.w);
792 cb->args[1] = arg.w.count + 1;
793 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200794 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200795 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200796 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200797}
798
Eric Dumazetbd27a872009-11-05 20:57:26 -0800799/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
801{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900802 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200803 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct net_device *dev;
805 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200806 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200807 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700808 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800810 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200811 long index_start;
812 long index;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200813 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Hong zhi guo573ce262013-03-27 06:47:04 +0000815 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200817
818 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
819 if (err)
820 return err;
821
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000822 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
823 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return skb->len;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!tcm->tcm_parent)
Patrick McHardyaf356af2009-09-04 06:41:18 +0000827 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 else
829 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
830 if (!q)
831 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000832 cops = q->ops->cl_ops;
833 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto errout;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200835 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000836 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (TC_H_MIN(tcm->tcm_parent)) {
838 cl = cops->get(q, tcm->tcm_parent);
839 if (cl == 0)
840 goto errout;
841 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200842 block = cops->tcf_block(q, cl);
843 if (!block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto errout;
845
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200846 index_start = cb->args[0];
847 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200848
849 list_for_each_entry(chain, &block->chain_list, list) {
850 if (tca[TCA_CHAIN] &&
851 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
852 continue;
853 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
854 break;
855 }
856
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200857 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859errout:
860 if (cl)
861 cops->put(q, cl);
862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return skb->len;
864}
865
WANG Cong18d02642014-09-25 10:26:37 -0700866void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700869 LIST_HEAD(actions);
870
871 tcf_exts_to_list(exts, &actions);
872 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
873 kfree(exts->actions);
874 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#endif
876}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800877EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000879int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400880 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#ifdef CONFIG_NET_CLS_ACT
883 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct tc_action *act;
885
WANG Cong5da57f42013-12-15 20:15:07 -0800886 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200887 act = tcf_action_init_1(net, tp, tb[exts->police],
888 rate_tlv, "police", ovr,
889 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800890 if (IS_ERR(act))
891 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
WANG Cong33be6272013-12-15 20:15:05 -0800893 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700894 exts->actions[0] = act;
895 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800896 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700897 LIST_HEAD(actions);
898 int err, i = 0;
899
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200900 err = tcf_action_init(net, tp, tb[exts->action],
901 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400902 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800903 if (err)
904 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700905 list_for_each_entry(act, &actions, list)
906 exts->actions[i++] = act;
907 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#else
WANG Cong5da57f42013-12-15 20:15:07 -0800911 if ((exts->action && tb[exts->action]) ||
912 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EOPNOTSUPP;
914#endif
915
916 return 0;
917}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800918EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200920void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700923 struct tcf_exts old = *dst;
924
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200925 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -0700926 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#endif
928}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800929EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
WANG Cong22dc13c2016-08-13 22:35:00 -0700931#ifdef CONFIG_NET_CLS_ACT
932static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
933{
934 if (exts->nr_actions == 0)
935 return NULL;
936 else
937 return exts->actions[0];
938}
939#endif
WANG Cong33be6272013-12-15 20:15:05 -0800940
WANG Cong5da57f42013-12-15 20:15:07 -0800941int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700944 struct nlattr *nest;
945
Jiri Pirko978dfd82017-08-04 14:29:03 +0200946 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * again for backward compatible mode - we want
949 * to work with both old and new modes of entering
950 * tc data even if iproute2 was newer - jhs
951 */
WANG Cong33be6272013-12-15 20:15:05 -0800952 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700953 LIST_HEAD(actions);
954
WANG Cong5da57f42013-12-15 20:15:07 -0800955 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800956 if (nest == NULL)
957 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700958
959 tcf_exts_to_list(exts, &actions);
960 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800961 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800962 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800963 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800964 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800965 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500966 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800967 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800968 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800969 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800970 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -0700974
975nla_put_failure:
976 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -0700978#else
979 return 0;
980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800982EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800984
WANG Cong5da57f42013-12-15 20:15:07 -0800985int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800988 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +0100989 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800990 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800994EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200996int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
997 struct net_device **hw_dev)
998{
999#ifdef CONFIG_NET_CLS_ACT
1000 const struct tc_action *a;
1001 LIST_HEAD(actions);
1002
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +02001003 if (!tcf_exts_has_actions(exts))
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +02001004 return -EINVAL;
1005
1006 tcf_exts_to_list(exts, &actions);
1007 list_for_each_entry(a, &actions, list) {
1008 if (a->ops->get_dev) {
1009 a->ops->get_dev(a, dev_net(dev), hw_dev);
1010 break;
1011 }
1012 }
1013 if (*hw_dev)
1014 return 0;
1015#endif
1016 return -EOPNOTSUPP;
1017}
1018EXPORT_SYMBOL(tcf_exts_get_dev);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static int __init tc_filter_init(void)
1021{
Greg Rosec7ac8672011-06-10 01:27:09 +00001022 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
1023 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
Thomas Graf82623c02007-03-22 11:56:22 -07001024 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Greg Rosec7ac8672011-06-10 01:27:09 +00001025 tc_dump_tfilter, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028}
1029
1030subsys_initcall(tc_filter_init);