blob: 0b2219adf520b33a471dd3b8e2fdf9f46271c3f9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* Select new prio value from the range, managed by kernel. */
104
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800105static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800107 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000110 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jiri Pirko79619732017-05-17 11:07:58 +0200112 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jiri Pirko33a48922017-02-09 14:38:57 +0100115static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200116 u32 prio, u32 parent, struct Qdisc *q,
Jiri Pirko5bc17012017-05-17 11:08:01 +0200117 struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100118{
119 struct tcf_proto *tp;
120 int err;
121
122 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
123 if (!tp)
124 return ERR_PTR(-ENOBUFS);
125
126 err = -ENOENT;
127 tp->ops = tcf_proto_lookup_ops(kind);
128 if (!tp->ops) {
129#ifdef CONFIG_MODULES
130 rtnl_unlock();
131 request_module("cls_%s", kind);
132 rtnl_lock();
133 tp->ops = tcf_proto_lookup_ops(kind);
134 /* We dropped the RTNL semaphore in order to perform
135 * the module load. So, even if we succeeded in loading
136 * the module we have to replay the request. We indicate
137 * this using -EAGAIN.
138 */
139 if (tp->ops) {
140 module_put(tp->ops->owner);
141 err = -EAGAIN;
142 } else {
143 err = -ENOENT;
144 }
145 goto errout;
146#endif
147 }
148 tp->classify = tp->ops->classify;
149 tp->protocol = protocol;
150 tp->prio = prio;
151 tp->classid = parent;
152 tp->q = q;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200153 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100154
155 err = tp->ops->init(tp);
156 if (err) {
157 module_put(tp->ops->owner);
158 goto errout;
159 }
160 return tp;
161
162errout:
163 kfree(tp);
164 return ERR_PTR(err);
165}
166
WANG Cong763dbf62017-04-19 14:21:21 -0700167static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100168{
WANG Cong763dbf62017-04-19 14:21:21 -0700169 tp->ops->destroy(tp);
170 module_put(tp->ops->owner);
171 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100172}
173
Jiri Pirko5bc17012017-05-17 11:08:01 +0200174static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
175 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200176{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200177 struct tcf_chain *chain;
178
179 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
180 if (!chain)
181 return NULL;
182 list_add_tail(&chain->list, &block->chain_list);
183 chain->block = block;
184 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700185 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200186 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200187}
188
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200189static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100190{
191 struct tcf_proto *tp;
192
Jiri Pirkoacc8b312017-08-18 10:10:43 +0200193 if (chain->p_filter_chain)
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200194 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200195 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
196 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Cong Wange2ef7542017-09-11 16:33:31 -0700197 tcf_chain_put(chain);
WANG Cong763dbf62017-04-19 14:21:21 -0700198 tcf_proto_destroy(tp);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100199 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200200}
201
202static void tcf_chain_destroy(struct tcf_chain *chain)
203{
Cong Wange2ef7542017-09-11 16:33:31 -0700204 list_del(&chain->list);
205 kfree(chain);
206}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200207
Cong Wange2ef7542017-09-11 16:33:31 -0700208static void tcf_chain_hold(struct tcf_chain *chain)
209{
210 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200211}
212
WANG Cong367a8ce2017-05-23 09:42:37 -0700213struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
214 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200215{
216 struct tcf_chain *chain;
217
218 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700219 if (chain->index == chain_index) {
220 tcf_chain_hold(chain);
221 return chain;
222 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200223 }
Jiri Pirko80532382017-09-06 13:14:19 +0200224
Cong Wange2ef7542017-09-11 16:33:31 -0700225 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200226}
227EXPORT_SYMBOL(tcf_chain_get);
228
229void tcf_chain_put(struct tcf_chain *chain)
230{
Cong Wange2ef7542017-09-11 16:33:31 -0700231 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200232 tcf_chain_destroy(chain);
233}
234EXPORT_SYMBOL(tcf_chain_put);
235
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200236static void
237tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
238 struct tcf_proto __rcu **p_filter_chain)
239{
240 chain->p_filter_chain = p_filter_chain;
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100241}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200242
243int tcf_block_get(struct tcf_block **p_block,
244 struct tcf_proto __rcu **p_filter_chain)
245{
246 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200247 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200248 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200249
250 if (!block)
251 return -ENOMEM;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200252 INIT_LIST_HEAD(&block->chain_list);
253 /* Create chain 0 by default, it has to be always present. */
254 chain = tcf_chain_create(block, 0);
255 if (!chain) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200256 err = -ENOMEM;
257 goto err_chain_create;
258 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200259 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200260 *p_block = block;
261 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200262
263err_chain_create:
264 kfree(block);
265 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200266}
267EXPORT_SYMBOL(tcf_block_get);
268
269void tcf_block_put(struct tcf_block *block)
270{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200271 struct tcf_chain *chain, *tmp;
272
Jiri Pirko6529eab2017-05-17 11:07:55 +0200273 if (!block)
274 return;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200275
Cong Wange2ef7542017-09-11 16:33:31 -0700276 /* XXX: Standalone actions are not allowed to jump to any chain, and
277 * bound actions should be all removed after flushing. However,
Cong Wang1697c4b2017-09-11 16:33:32 -0700278 * filters are destroyed in RCU callbacks, we have to hold the chains
279 * first, otherwise we would always race with RCU callbacks on this list
280 * without proper locking.
Cong Wange2ef7542017-09-11 16:33:31 -0700281 */
Cong Wange2ef7542017-09-11 16:33:31 -0700282
Cong Wang1697c4b2017-09-11 16:33:32 -0700283 /* Wait for existing RCU callbacks to cool down. */
284 rcu_barrier();
285
286 /* Hold a refcnt for all chains, except 0, in case they are gone. */
287 list_for_each_entry(chain, &block->chain_list, list)
288 if (chain->index)
289 tcf_chain_hold(chain);
290
291 /* No race on the list, because no chain could be destroyed. */
292 list_for_each_entry(chain, &block->chain_list, list)
293 tcf_chain_flush(chain);
294
295 /* Wait for RCU callbacks to release the reference count. */
296 rcu_barrier();
297
298 /* At this point, all the chains should have refcnt == 1. */
Cong Wange2ef7542017-09-11 16:33:31 -0700299 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
300 tcf_chain_put(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200301 kfree(block);
302}
303EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100304
Jiri Pirko87d83092017-05-17 11:07:54 +0200305/* Main classifier routine: scans classifier chain attached
306 * to this qdisc, (optionally) tests for protocol and asks
307 * specific classifiers.
308 */
309int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
310 struct tcf_result *res, bool compat_mode)
311{
312 __be16 protocol = tc_skb_protocol(skb);
313#ifdef CONFIG_NET_CLS_ACT
314 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200315 const struct tcf_proto *orig_tp = tp;
316 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200317 int limit = 0;
318
319reclassify:
320#endif
321 for (; tp; tp = rcu_dereference_bh(tp->next)) {
322 int err;
323
324 if (tp->protocol != protocol &&
325 tp->protocol != htons(ETH_P_ALL))
326 continue;
327
328 err = tp->classify(skb, tp, res);
329#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200330 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200331 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200332 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200333 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200334 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200335 goto reset;
336 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200337#endif
338 if (err >= 0)
339 return err;
340 }
341
342 return TC_ACT_UNSPEC; /* signal: continue lookup */
343#ifdef CONFIG_NET_CLS_ACT
344reset:
345 if (unlikely(limit++ >= max_reclassify_loop)) {
346 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
347 tp->q->ops->id, tp->prio & 0xffff,
348 ntohs(tp->protocol));
349 return TC_ACT_SHOT;
350 }
351
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200352 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200353 protocol = tc_skb_protocol(skb);
354 goto reclassify;
355#endif
356}
357EXPORT_SYMBOL(tcf_classify);
358
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200359struct tcf_chain_info {
360 struct tcf_proto __rcu **pprev;
361 struct tcf_proto __rcu *next;
362};
363
364static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
365{
366 return rtnl_dereference(*chain_info->pprev);
367}
368
369static void tcf_chain_tp_insert(struct tcf_chain *chain,
370 struct tcf_chain_info *chain_info,
371 struct tcf_proto *tp)
372{
373 if (chain->p_filter_chain &&
374 *chain_info->pprev == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200375 rcu_assign_pointer(*chain->p_filter_chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200376 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
377 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700378 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200379}
380
381static void tcf_chain_tp_remove(struct tcf_chain *chain,
382 struct tcf_chain_info *chain_info,
383 struct tcf_proto *tp)
384{
385 struct tcf_proto *next = rtnl_dereference(chain_info->next);
386
387 if (chain->p_filter_chain && tp == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200388 RCU_INIT_POINTER(*chain->p_filter_chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200389 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700390 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200391}
392
393static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
394 struct tcf_chain_info *chain_info,
395 u32 protocol, u32 prio,
396 bool prio_allocate)
397{
398 struct tcf_proto **pprev;
399 struct tcf_proto *tp;
400
401 /* Check the chain for existence of proto-tcf with this priority */
402 for (pprev = &chain->filter_chain;
403 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
404 if (tp->prio >= prio) {
405 if (tp->prio == prio) {
406 if (prio_allocate ||
407 (tp->protocol != protocol && protocol))
408 return ERR_PTR(-EINVAL);
409 } else {
410 tp = NULL;
411 }
412 break;
413 }
414 }
415 chain_info->pprev = pprev;
416 chain_info->next = tp ? tp->next : NULL;
417 return tp;
418}
419
WANG Cong71203712017-08-07 15:26:50 -0700420static int tcf_fill_node(struct net *net, struct sk_buff *skb,
421 struct tcf_proto *tp, void *fh, u32 portid,
422 u32 seq, u16 flags, int event)
423{
424 struct tcmsg *tcm;
425 struct nlmsghdr *nlh;
426 unsigned char *b = skb_tail_pointer(skb);
427
428 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
429 if (!nlh)
430 goto out_nlmsg_trim;
431 tcm = nlmsg_data(nlh);
432 tcm->tcm_family = AF_UNSPEC;
433 tcm->tcm__pad1 = 0;
434 tcm->tcm__pad2 = 0;
435 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
436 tcm->tcm_parent = tp->classid;
437 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
438 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
439 goto nla_put_failure;
440 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
441 goto nla_put_failure;
442 if (!fh) {
443 tcm->tcm_handle = 0;
444 } else {
445 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
446 goto nla_put_failure;
447 }
448 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
449 return skb->len;
450
451out_nlmsg_trim:
452nla_put_failure:
453 nlmsg_trim(skb, b);
454 return -1;
455}
456
457static int tfilter_notify(struct net *net, struct sk_buff *oskb,
458 struct nlmsghdr *n, struct tcf_proto *tp,
459 void *fh, int event, bool unicast)
460{
461 struct sk_buff *skb;
462 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
463
464 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
465 if (!skb)
466 return -ENOBUFS;
467
468 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
469 n->nlmsg_flags, event) <= 0) {
470 kfree_skb(skb);
471 return -EINVAL;
472 }
473
474 if (unicast)
475 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
476
477 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
478 n->nlmsg_flags & NLM_F_ECHO);
479}
480
481static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
482 struct nlmsghdr *n, struct tcf_proto *tp,
483 void *fh, bool unicast, bool *last)
484{
485 struct sk_buff *skb;
486 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
487 int err;
488
489 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
490 if (!skb)
491 return -ENOBUFS;
492
493 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
494 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
495 kfree_skb(skb);
496 return -EINVAL;
497 }
498
499 err = tp->ops->delete(tp, fh, last);
500 if (err) {
501 kfree_skb(skb);
502 return err;
503 }
504
505 if (unicast)
506 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
507
508 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
509 n->nlmsg_flags & NLM_F_ECHO);
510}
511
512static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
513 struct nlmsghdr *n,
514 struct tcf_chain *chain, int event)
515{
516 struct tcf_proto *tp;
517
518 for (tp = rtnl_dereference(chain->filter_chain);
519 tp; tp = rtnl_dereference(tp->next))
520 tfilter_notify(net, oskb, n, tp, 0, event, false);
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/* Add/change/delete/get a filter node */
524
David Ahernc21ef3e2017-04-16 09:48:24 -0700525static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
526 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900528 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800529 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct tcmsg *t;
531 u32 protocol;
532 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200533 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200535 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct net_device *dev;
537 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200538 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200539 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200540 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800542 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -0700544 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100546 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400548 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400549 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000550 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100553 tp_created = 0;
554
David Ahernc21ef3e2017-04-16 09:48:24 -0700555 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000556 if (err < 0)
557 return err;
558
David S. Miller942b8162012-06-26 21:48:50 -0700559 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 protocol = TC_H_MIN(t->tcm_info);
561 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200562 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 parent = t->tcm_parent;
564 cl = 0;
565
566 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200567 switch (n->nlmsg_type) {
568 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200569 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200570 return -ENOENT;
571 break;
572 case RTM_NEWTFILTER:
573 /* If no priority is provided by the user,
574 * we allocate one.
575 */
576 if (n->nlmsg_flags & NLM_F_CREATE) {
577 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200578 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200579 break;
580 }
581 /* fall-through */
582 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 /* Find head of filter chain. */
588
589 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000590 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800591 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -ENODEV;
593
594 /* Find qdisc */
595 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000596 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800598 } else {
599 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
600 if (q == NULL)
601 return -EINVAL;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000605 cops = q->ops->cl_ops;
606 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return -EINVAL;
608
Jiri Pirko6529eab2017-05-17 11:07:55 +0200609 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000610 return -EOPNOTSUPP;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Do we search for filter, attached to class? */
613 if (TC_H_MIN(parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700614 cl = cops->find(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (cl == 0)
616 return -ENOENT;
617 }
618
619 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200620 block = cops->tcf_block(q, cl);
621 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100622 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100624 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200625
626 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
627 if (chain_index > TC_ACT_EXT_VAL_MASK) {
628 err = -EINVAL;
629 goto errout;
630 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700631 chain = tcf_chain_get(block, chain_index,
632 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200633 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700634 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200635 goto errout;
636 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200637
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200638 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
639 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200640 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200641 err = 0;
642 goto errout;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200645 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
646 prio, prio_allocate);
647 if (IS_ERR(tp)) {
648 err = PTR_ERR(tp);
649 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 if (tp == NULL) {
653 /* Proto-tcf does not exist, create new one */
654
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100655 if (tca[TCA_KIND] == NULL || !protocol) {
656 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000660 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100661 !(n->nlmsg_flags & NLM_F_CREATE)) {
662 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200666 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200667 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Jiri Pirko33a48922017-02-09 14:38:57 +0100669 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200670 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100671 if (IS_ERR(tp)) {
672 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto errout;
674 }
Minoru Usui12186be2009-06-02 02:17:34 -0700675 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100676 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
677 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 fh = tp->ops->get(tp, t->tcm_handle);
682
WANG Cong8113c092017-08-04 21:31:43 -0700683 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200685 tcf_chain_tp_remove(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700686 tfilter_notify(net, skb, n, tp, fh,
687 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700688 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 err = 0;
690 goto errout;
691 }
692
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800693 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100694 !(n->nlmsg_flags & NLM_F_CREATE)) {
695 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700699 bool last;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900702 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700703 if (n->nlmsg_flags & NLM_F_EXCL) {
704 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700705 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100706 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 case RTM_DELTFILTER:
WANG Cong54df2cf2017-08-04 21:31:42 -0700711 err = tfilter_del_notify(net, skb, n, tp, fh, false,
712 &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100713 if (err)
714 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -0700715 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200716 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700717 tcf_proto_destroy(tp);
718 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100719 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 case RTM_GETTFILTER:
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400721 err = tfilter_notify(net, skb, n, tp, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700722 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto errout;
724 default:
725 err = -EINVAL;
726 goto errout;
727 }
728 }
729
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700730 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
731 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700732 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200733 if (tp_created)
734 tcf_chain_tp_insert(chain, &chain_info, tp);
Eric Dumazetfa59b272016-10-09 20:25:55 -0700735 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700736 } else {
737 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700738 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200742 if (chain)
743 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (err == -EAGAIN)
745 /* Replay the request. */
746 goto replay;
747 return err;
748}
749
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800750struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct tcf_walker w;
752 struct sk_buff *skb;
753 struct netlink_callback *cb;
754};
755
WANG Cong8113c092017-08-04 21:31:43 -0700756static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800758 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800759 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
WANG Cong832d1d52014-01-09 16:14:01 -0800761 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400762 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
763 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Jiri Pirko5bc17012017-05-17 11:08:01 +0200766static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200767 struct netlink_callback *cb,
768 long index_start, long *p_index)
769{
770 struct net *net = sock_net(skb->sk);
771 struct tcmsg *tcm = nlmsg_data(cb->nlh);
772 struct tcf_dump_args arg;
773 struct tcf_proto *tp;
774
775 for (tp = rtnl_dereference(chain->filter_chain);
776 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
777 if (*p_index < index_start)
778 continue;
779 if (TC_H_MAJ(tcm->tcm_info) &&
780 TC_H_MAJ(tcm->tcm_info) != tp->prio)
781 continue;
782 if (TC_H_MIN(tcm->tcm_info) &&
783 TC_H_MIN(tcm->tcm_info) != tp->protocol)
784 continue;
785 if (*p_index > index_start)
786 memset(&cb->args[1], 0,
787 sizeof(cb->args) - sizeof(cb->args[0]));
788 if (cb->args[1] == 0) {
789 if (tcf_fill_node(net, skb, tp, 0,
790 NETLINK_CB(cb->skb).portid,
791 cb->nlh->nlmsg_seq, NLM_F_MULTI,
792 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200793 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200794
795 cb->args[1] = 1;
796 }
797 if (!tp->ops->walk)
798 continue;
799 arg.w.fn = tcf_node_dump;
800 arg.skb = skb;
801 arg.cb = cb;
802 arg.w.stop = 0;
803 arg.w.skip = cb->args[1] - 1;
804 arg.w.count = 0;
805 tp->ops->walk(tp, &arg.w);
806 cb->args[1] = arg.w.count + 1;
807 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200808 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200809 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200810 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200811}
812
Eric Dumazetbd27a872009-11-05 20:57:26 -0800813/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
815{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900816 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200817 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct net_device *dev;
819 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200820 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200821 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700822 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800824 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200825 long index_start;
826 long index;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200827 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Hong zhi guo573ce262013-03-27 06:47:04 +0000829 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200831
832 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
833 if (err)
834 return err;
835
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000836 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
837 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return skb->len;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!tcm->tcm_parent)
Patrick McHardyaf356af2009-09-04 06:41:18 +0000841 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else
843 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
844 if (!q)
845 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000846 cops = q->ops->cl_ops;
847 if (!cops)
WANG Cong143976c2017-08-24 16:51:29 -0700848 goto out;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200849 if (!cops->tcf_block)
WANG Cong143976c2017-08-24 16:51:29 -0700850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (TC_H_MIN(tcm->tcm_parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700852 cl = cops->find(q, tcm->tcm_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (cl == 0)
WANG Cong143976c2017-08-24 16:51:29 -0700854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200856 block = cops->tcf_block(q, cl);
857 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -0700858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200860 index_start = cb->args[0];
861 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200862
863 list_for_each_entry(chain, &block->chain_list, list) {
864 if (tca[TCA_CHAIN] &&
865 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
866 continue;
867 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
868 break;
869 }
870
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200871 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return skb->len;
875}
876
WANG Cong18d02642014-09-25 10:26:37 -0700877void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700880 LIST_HEAD(actions);
881
882 tcf_exts_to_list(exts, &actions);
883 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
884 kfree(exts->actions);
885 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#endif
887}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800888EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000890int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400891 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#ifdef CONFIG_NET_CLS_ACT
894 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct tc_action *act;
896
WANG Cong5da57f42013-12-15 20:15:07 -0800897 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200898 act = tcf_action_init_1(net, tp, tb[exts->police],
899 rate_tlv, "police", ovr,
900 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800901 if (IS_ERR(act))
902 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
WANG Cong33be6272013-12-15 20:15:05 -0800904 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700905 exts->actions[0] = act;
906 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800907 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700908 LIST_HEAD(actions);
909 int err, i = 0;
910
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200911 err = tcf_action_init(net, tp, tb[exts->action],
912 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400913 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800914 if (err)
915 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700916 list_for_each_entry(act, &actions, list)
917 exts->actions[i++] = act;
918 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#else
WANG Cong5da57f42013-12-15 20:15:07 -0800922 if ((exts->action && tb[exts->action]) ||
923 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -EOPNOTSUPP;
925#endif
926
927 return 0;
928}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800929EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200931void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700934 struct tcf_exts old = *dst;
935
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200936 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -0700937 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#endif
939}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800940EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
WANG Cong22dc13c2016-08-13 22:35:00 -0700942#ifdef CONFIG_NET_CLS_ACT
943static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
944{
945 if (exts->nr_actions == 0)
946 return NULL;
947 else
948 return exts->actions[0];
949}
950#endif
WANG Cong33be6272013-12-15 20:15:05 -0800951
WANG Cong5da57f42013-12-15 20:15:07 -0800952int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700955 struct nlattr *nest;
956
Jiri Pirko978dfd82017-08-04 14:29:03 +0200957 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /*
959 * again for backward compatible mode - we want
960 * to work with both old and new modes of entering
961 * tc data even if iproute2 was newer - jhs
962 */
WANG Cong33be6272013-12-15 20:15:05 -0800963 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700964 LIST_HEAD(actions);
965
WANG Cong5da57f42013-12-15 20:15:07 -0800966 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800967 if (nest == NULL)
968 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700969
970 tcf_exts_to_list(exts, &actions);
971 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800972 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800973 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800974 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800975 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800976 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500977 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800978 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800979 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800980 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800981 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -0700985
986nla_put_failure:
987 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -0700989#else
990 return 0;
991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800993EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800995
WANG Cong5da57f42013-12-15 20:15:07 -0800996int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800999 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001000 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001001 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002#endif
1003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001005EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +02001007int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
1008 struct net_device **hw_dev)
1009{
1010#ifdef CONFIG_NET_CLS_ACT
1011 const struct tc_action *a;
1012 LIST_HEAD(actions);
1013
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +02001014 if (!tcf_exts_has_actions(exts))
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +02001015 return -EINVAL;
1016
1017 tcf_exts_to_list(exts, &actions);
1018 list_for_each_entry(a, &actions, list) {
1019 if (a->ops->get_dev) {
1020 a->ops->get_dev(a, dev_net(dev), hw_dev);
1021 break;
1022 }
1023 }
1024 if (*hw_dev)
1025 return 0;
1026#endif
1027 return -EOPNOTSUPP;
1028}
1029EXPORT_SYMBOL(tcf_exts_get_dev);
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static int __init tc_filter_init(void)
1032{
Florian Westphalb97bac62017-08-09 20:41:48 +02001033 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1034 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001035 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001036 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039}
1040
1041subsys_initcall(tc_filter_init);