blob: 2e8e87fd9d978a12262ec4fc77fc293da3214f05 [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,
Jiri Pirko69d78ef2017-10-13 14:00:57 +0200244 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200245{
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 Pirko855319b2017-10-13 14:00:58 +0200260 block->net = qdisc_net(q);
Jiri Pirko69d78ef2017-10-13 14:00:57 +0200261 block->q = q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200262 *p_block = block;
263 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200264
265err_chain_create:
266 kfree(block);
267 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200268}
269EXPORT_SYMBOL(tcf_block_get);
270
271void tcf_block_put(struct tcf_block *block)
272{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200273 struct tcf_chain *chain, *tmp;
274
Jiri Pirko6529eab2017-05-17 11:07:55 +0200275 if (!block)
276 return;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200277
Cong Wange2ef7542017-09-11 16:33:31 -0700278 /* XXX: Standalone actions are not allowed to jump to any chain, and
279 * bound actions should be all removed after flushing. However,
Cong Wang1697c4b2017-09-11 16:33:32 -0700280 * filters are destroyed in RCU callbacks, we have to hold the chains
281 * first, otherwise we would always race with RCU callbacks on this list
282 * without proper locking.
Cong Wange2ef7542017-09-11 16:33:31 -0700283 */
Cong Wange2ef7542017-09-11 16:33:31 -0700284
Cong Wang1697c4b2017-09-11 16:33:32 -0700285 /* Wait for existing RCU callbacks to cool down. */
286 rcu_barrier();
287
288 /* Hold a refcnt for all chains, except 0, in case they are gone. */
289 list_for_each_entry(chain, &block->chain_list, list)
290 if (chain->index)
291 tcf_chain_hold(chain);
292
293 /* No race on the list, because no chain could be destroyed. */
294 list_for_each_entry(chain, &block->chain_list, list)
295 tcf_chain_flush(chain);
296
297 /* Wait for RCU callbacks to release the reference count. */
298 rcu_barrier();
299
300 /* At this point, all the chains should have refcnt == 1. */
Cong Wange2ef7542017-09-11 16:33:31 -0700301 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
302 tcf_chain_put(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200303 kfree(block);
304}
305EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100306
Jiri Pirko87d83092017-05-17 11:07:54 +0200307/* Main classifier routine: scans classifier chain attached
308 * to this qdisc, (optionally) tests for protocol and asks
309 * specific classifiers.
310 */
311int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
312 struct tcf_result *res, bool compat_mode)
313{
314 __be16 protocol = tc_skb_protocol(skb);
315#ifdef CONFIG_NET_CLS_ACT
316 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200317 const struct tcf_proto *orig_tp = tp;
318 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200319 int limit = 0;
320
321reclassify:
322#endif
323 for (; tp; tp = rcu_dereference_bh(tp->next)) {
324 int err;
325
326 if (tp->protocol != protocol &&
327 tp->protocol != htons(ETH_P_ALL))
328 continue;
329
330 err = tp->classify(skb, tp, res);
331#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200332 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200333 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200334 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200335 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200336 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200337 goto reset;
338 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200339#endif
340 if (err >= 0)
341 return err;
342 }
343
344 return TC_ACT_UNSPEC; /* signal: continue lookup */
345#ifdef CONFIG_NET_CLS_ACT
346reset:
347 if (unlikely(limit++ >= max_reclassify_loop)) {
348 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
349 tp->q->ops->id, tp->prio & 0xffff,
350 ntohs(tp->protocol));
351 return TC_ACT_SHOT;
352 }
353
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200354 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200355 protocol = tc_skb_protocol(skb);
356 goto reclassify;
357#endif
358}
359EXPORT_SYMBOL(tcf_classify);
360
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200361struct tcf_chain_info {
362 struct tcf_proto __rcu **pprev;
363 struct tcf_proto __rcu *next;
364};
365
366static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
367{
368 return rtnl_dereference(*chain_info->pprev);
369}
370
371static void tcf_chain_tp_insert(struct tcf_chain *chain,
372 struct tcf_chain_info *chain_info,
373 struct tcf_proto *tp)
374{
375 if (chain->p_filter_chain &&
376 *chain_info->pprev == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200377 rcu_assign_pointer(*chain->p_filter_chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200378 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
379 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700380 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200381}
382
383static void tcf_chain_tp_remove(struct tcf_chain *chain,
384 struct tcf_chain_info *chain_info,
385 struct tcf_proto *tp)
386{
387 struct tcf_proto *next = rtnl_dereference(chain_info->next);
388
389 if (chain->p_filter_chain && tp == chain->filter_chain)
Jiri Pirko31efcc22017-05-20 15:01:31 +0200390 RCU_INIT_POINTER(*chain->p_filter_chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200391 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700392 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200393}
394
395static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
396 struct tcf_chain_info *chain_info,
397 u32 protocol, u32 prio,
398 bool prio_allocate)
399{
400 struct tcf_proto **pprev;
401 struct tcf_proto *tp;
402
403 /* Check the chain for existence of proto-tcf with this priority */
404 for (pprev = &chain->filter_chain;
405 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
406 if (tp->prio >= prio) {
407 if (tp->prio == prio) {
408 if (prio_allocate ||
409 (tp->protocol != protocol && protocol))
410 return ERR_PTR(-EINVAL);
411 } else {
412 tp = NULL;
413 }
414 break;
415 }
416 }
417 chain_info->pprev = pprev;
418 chain_info->next = tp ? tp->next : NULL;
419 return tp;
420}
421
WANG Cong71203712017-08-07 15:26:50 -0700422static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200423 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
424 void *fh, u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700425{
426 struct tcmsg *tcm;
427 struct nlmsghdr *nlh;
428 unsigned char *b = skb_tail_pointer(skb);
429
430 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
431 if (!nlh)
432 goto out_nlmsg_trim;
433 tcm = nlmsg_data(nlh);
434 tcm->tcm_family = AF_UNSPEC;
435 tcm->tcm__pad1 = 0;
436 tcm->tcm__pad2 = 0;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200437 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
438 tcm->tcm_parent = parent;
WANG Cong71203712017-08-07 15:26:50 -0700439 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
440 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
441 goto nla_put_failure;
442 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
443 goto nla_put_failure;
444 if (!fh) {
445 tcm->tcm_handle = 0;
446 } else {
447 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
448 goto nla_put_failure;
449 }
450 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
451 return skb->len;
452
453out_nlmsg_trim:
454nla_put_failure:
455 nlmsg_trim(skb, b);
456 return -1;
457}
458
459static int tfilter_notify(struct net *net, struct sk_buff *oskb,
460 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200461 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700462 void *fh, int event, bool unicast)
463{
464 struct sk_buff *skb;
465 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
466
467 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
468 if (!skb)
469 return -ENOBUFS;
470
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200471 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700472 n->nlmsg_flags, event) <= 0) {
473 kfree_skb(skb);
474 return -EINVAL;
475 }
476
477 if (unicast)
478 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
479
480 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
481 n->nlmsg_flags & NLM_F_ECHO);
482}
483
484static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
485 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200486 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700487 void *fh, bool unicast, bool *last)
488{
489 struct sk_buff *skb;
490 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
491 int err;
492
493 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
494 if (!skb)
495 return -ENOBUFS;
496
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200497 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700498 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
499 kfree_skb(skb);
500 return -EINVAL;
501 }
502
503 err = tp->ops->delete(tp, fh, last);
504 if (err) {
505 kfree_skb(skb);
506 return err;
507 }
508
509 if (unicast)
510 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
511
512 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
513 n->nlmsg_flags & NLM_F_ECHO);
514}
515
516static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200517 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700518 struct nlmsghdr *n,
519 struct tcf_chain *chain, int event)
520{
521 struct tcf_proto *tp;
522
523 for (tp = rtnl_dereference(chain->filter_chain);
524 tp; tp = rtnl_dereference(tp->next))
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200525 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/* Add/change/delete/get a filter node */
529
David Ahernc21ef3e2017-04-16 09:48:24 -0700530static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
531 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900533 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800534 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct tcmsg *t;
536 u32 protocol;
537 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200538 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200540 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct net_device *dev;
542 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200543 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200544 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200545 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800547 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -0700549 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100551 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400553 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400554 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000555 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100558 tp_created = 0;
559
David Ahernc21ef3e2017-04-16 09:48:24 -0700560 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000561 if (err < 0)
562 return err;
563
David S. Miller942b8162012-06-26 21:48:50 -0700564 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 protocol = TC_H_MIN(t->tcm_info);
566 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200567 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 parent = t->tcm_parent;
569 cl = 0;
570
571 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200572 switch (n->nlmsg_type) {
573 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200574 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200575 return -ENOENT;
576 break;
577 case RTM_NEWTFILTER:
578 /* If no priority is provided by the user,
579 * we allocate one.
580 */
581 if (n->nlmsg_flags & NLM_F_CREATE) {
582 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200583 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200584 break;
585 }
586 /* fall-through */
587 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 /* Find head of filter chain. */
593
594 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000595 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800596 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -ENODEV;
598
599 /* Find qdisc */
600 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000601 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800603 } else {
604 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
605 if (q == NULL)
606 return -EINVAL;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000610 cops = q->ops->cl_ops;
611 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -EINVAL;
613
Jiri Pirko6529eab2017-05-17 11:07:55 +0200614 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000615 return -EOPNOTSUPP;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Do we search for filter, attached to class? */
618 if (TC_H_MIN(parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700619 cl = cops->find(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (cl == 0)
621 return -ENOENT;
622 }
623
624 /* And the last stroke */
Jiri Pirko6529eab2017-05-17 11:07:55 +0200625 block = cops->tcf_block(q, cl);
626 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100627 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100629 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200630
631 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
632 if (chain_index > TC_ACT_EXT_VAL_MASK) {
633 err = -EINVAL;
634 goto errout;
635 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700636 chain = tcf_chain_get(block, chain_index,
637 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200638 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700639 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200640 goto errout;
641 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200642
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200643 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200644 tfilter_notify_chain(net, skb, q, parent, n,
645 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200646 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200647 err = 0;
648 goto errout;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200651 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
652 prio, prio_allocate);
653 if (IS_ERR(tp)) {
654 err = PTR_ERR(tp);
655 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 if (tp == NULL) {
659 /* Proto-tcf does not exist, create new one */
660
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100661 if (tca[TCA_KIND] == NULL || !protocol) {
662 err = -EINVAL;
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
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000666 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100667 !(n->nlmsg_flags & NLM_F_CREATE)) {
668 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200672 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200673 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jiri Pirko33a48922017-02-09 14:38:57 +0100675 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200676 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100677 if (IS_ERR(tp)) {
678 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto errout;
680 }
Minoru Usui12186be2009-06-02 02:17:34 -0700681 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100682 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
683 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 fh = tp->ops->get(tp, t->tcm_handle);
688
WANG Cong8113c092017-08-04 21:31:43 -0700689 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200691 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200692 tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700693 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700694 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 err = 0;
696 goto errout;
697 }
698
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800699 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100700 !(n->nlmsg_flags & NLM_F_CREATE)) {
701 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700705 bool last;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900708 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700709 if (n->nlmsg_flags & NLM_F_EXCL) {
710 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700711 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100712 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 case RTM_DELTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200717 err = tfilter_del_notify(net, skb, n, tp, q, parent,
718 fh, false, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100719 if (err)
720 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -0700721 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200722 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700723 tcf_proto_destroy(tp);
724 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100725 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case RTM_GETTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200727 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700728 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto errout;
730 default:
731 err = -EINVAL;
732 goto errout;
733 }
734 }
735
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700736 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
737 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700738 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200739 if (tp_created)
740 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200741 tfilter_notify(net, skb, n, tp, q, parent, fh,
742 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700743 } else {
744 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700745 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200749 if (chain)
750 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (err == -EAGAIN)
752 /* Replay the request. */
753 goto replay;
754 return err;
755}
756
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800757struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct tcf_walker w;
759 struct sk_buff *skb;
760 struct netlink_callback *cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200761 struct Qdisc *q;
762 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};
764
WANG Cong8113c092017-08-04 21:31:43 -0700765static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800767 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800768 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200770 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
771 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400772 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
773 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200776static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
777 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200778 long index_start, long *p_index)
779{
780 struct net *net = sock_net(skb->sk);
781 struct tcmsg *tcm = nlmsg_data(cb->nlh);
782 struct tcf_dump_args arg;
783 struct tcf_proto *tp;
784
785 for (tp = rtnl_dereference(chain->filter_chain);
786 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
787 if (*p_index < index_start)
788 continue;
789 if (TC_H_MAJ(tcm->tcm_info) &&
790 TC_H_MAJ(tcm->tcm_info) != tp->prio)
791 continue;
792 if (TC_H_MIN(tcm->tcm_info) &&
793 TC_H_MIN(tcm->tcm_info) != tp->protocol)
794 continue;
795 if (*p_index > index_start)
796 memset(&cb->args[1], 0,
797 sizeof(cb->args) - sizeof(cb->args[0]));
798 if (cb->args[1] == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200799 if (tcf_fill_node(net, skb, tp, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200800 NETLINK_CB(cb->skb).portid,
801 cb->nlh->nlmsg_seq, NLM_F_MULTI,
802 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200803 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200804
805 cb->args[1] = 1;
806 }
807 if (!tp->ops->walk)
808 continue;
809 arg.w.fn = tcf_node_dump;
810 arg.skb = skb;
811 arg.cb = cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200812 arg.q = q;
813 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200814 arg.w.stop = 0;
815 arg.w.skip = cb->args[1] - 1;
816 arg.w.count = 0;
817 tp->ops->walk(tp, &arg.w);
818 cb->args[1] = arg.w.count + 1;
819 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200820 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200821 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200822 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200823}
824
Eric Dumazetbd27a872009-11-05 20:57:26 -0800825/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
827{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900828 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200829 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct net_device *dev;
831 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200832 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200833 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -0700834 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800836 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200837 long index_start;
838 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200839 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200840 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Hong zhi guo573ce262013-03-27 06:47:04 +0000842 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200844
845 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
846 if (err)
847 return err;
848
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000849 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
850 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return skb->len;
852
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200853 parent = tcm->tcm_parent;
854 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000855 q = dev->qdisc;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200856 parent = q->handle;
857 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!q)
861 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000862 cops = q->ops->cl_ops;
863 if (!cops)
WANG Cong143976c2017-08-24 16:51:29 -0700864 goto out;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200865 if (!cops->tcf_block)
WANG Cong143976c2017-08-24 16:51:29 -0700866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (TC_H_MIN(tcm->tcm_parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700868 cl = cops->find(q, tcm->tcm_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (cl == 0)
WANG Cong143976c2017-08-24 16:51:29 -0700870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200872 block = cops->tcf_block(q, cl);
873 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -0700874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200876 index_start = cb->args[0];
877 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200878
879 list_for_each_entry(chain, &block->chain_list, list) {
880 if (tca[TCA_CHAIN] &&
881 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
882 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200883 if (!tcf_chain_dump(chain, q, parent, skb, cb,
884 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +0200885 break;
886 }
887
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200888 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return skb->len;
892}
893
WANG Cong18d02642014-09-25 10:26:37 -0700894void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700897 LIST_HEAD(actions);
898
899 tcf_exts_to_list(exts, &actions);
900 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
901 kfree(exts->actions);
902 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#endif
904}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800905EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000907int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400908 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#ifdef CONFIG_NET_CLS_ACT
911 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct tc_action *act;
913
WANG Cong5da57f42013-12-15 20:15:07 -0800914 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200915 act = tcf_action_init_1(net, tp, tb[exts->police],
916 rate_tlv, "police", ovr,
917 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800918 if (IS_ERR(act))
919 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
WANG Cong33be6272013-12-15 20:15:05 -0800921 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -0700922 exts->actions[0] = act;
923 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -0800924 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700925 LIST_HEAD(actions);
926 int err, i = 0;
927
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200928 err = tcf_action_init(net, tp, tb[exts->action],
929 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400930 &actions);
WANG Cong33be6272013-12-15 20:15:05 -0800931 if (err)
932 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -0700933 list_for_each_entry(act, &actions, list)
934 exts->actions[i++] = act;
935 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#else
WANG Cong5da57f42013-12-15 20:15:07 -0800939 if ((exts->action && tb[exts->action]) ||
940 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -EOPNOTSUPP;
942#endif
943
944 return 0;
945}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800946EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200948void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -0700951 struct tcf_exts old = *dst;
952
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200953 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -0700954 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#endif
956}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800957EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
WANG Cong22dc13c2016-08-13 22:35:00 -0700959#ifdef CONFIG_NET_CLS_ACT
960static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
961{
962 if (exts->nr_actions == 0)
963 return NULL;
964 else
965 return exts->actions[0];
966}
967#endif
WANG Cong33be6272013-12-15 20:15:05 -0800968
WANG Cong5da57f42013-12-15 20:15:07 -0800969int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -0700972 struct nlattr *nest;
973
Jiri Pirko978dfd82017-08-04 14:29:03 +0200974 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * again for backward compatible mode - we want
977 * to work with both old and new modes of entering
978 * tc data even if iproute2 was newer - jhs
979 */
WANG Cong33be6272013-12-15 20:15:05 -0800980 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -0700981 LIST_HEAD(actions);
982
WANG Cong5da57f42013-12-15 20:15:07 -0800983 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800984 if (nest == NULL)
985 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -0700986
987 tcf_exts_to_list(exts, &actions);
988 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800989 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800990 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -0800991 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -0800992 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -0800993 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500994 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800995 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -0800996 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800997 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800998 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001002
1003nla_put_failure:
1004 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001006#else
1007 return 0;
1008#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001010EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001012
WANG Cong5da57f42013-12-15 20:15:07 -08001013int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001016 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001017 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001018 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#endif
1020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001022EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Jiri Pirko717503b2017-10-11 09:41:09 +02001024static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1025 enum tc_setup_type type,
1026 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001027{
1028 int ok_count = 0;
1029#ifdef CONFIG_NET_CLS_ACT
1030 const struct tc_action *a;
1031 struct net_device *dev;
1032 LIST_HEAD(actions);
1033 int ret;
1034
1035 if (!tcf_exts_has_actions(exts))
1036 return 0;
1037
1038 tcf_exts_to_list(exts, &actions);
1039 list_for_each_entry(a, &actions, list) {
1040 if (!a->ops->get_dev)
1041 continue;
1042 dev = a->ops->get_dev(a);
1043 if (!dev || !tc_can_offload(dev))
1044 continue;
1045 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1046 if (ret < 0)
1047 return ret;
1048 ok_count += ret;
1049 }
1050#endif
1051 return ok_count;
1052}
Jiri Pirko717503b2017-10-11 09:41:09 +02001053
1054int tc_setup_cb_call(struct tcf_exts *exts, enum tc_setup_type type,
1055 void *type_data, bool err_stop)
1056{
1057 return tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1058}
1059EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static int __init tc_filter_init(void)
1062{
Florian Westphalb97bac62017-08-09 20:41:48 +02001063 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1064 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001065 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001066 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 0;
1069}
1070
1071subsys_initcall(tc_filter_init);