]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/cls_api.c
Merge tag 'powerpc-4.13-8' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-artful-kernel.git] / net / sched / cls_api.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
1da177e4 20#include <linux/string.h>
1da177e4 21#include <linux/errno.h>
33a48927 22#include <linux/err.h>
1da177e4 23#include <linux/skbuff.h>
1da177e4
LT
24#include <linux/init.h>
25#include <linux/kmod.h>
ab27cfb8 26#include <linux/err.h>
5a0e3ad6 27#include <linux/slab.h>
b854272b
DL
28#include <net/net_namespace.h>
29#include <net/sock.h>
dc5fc579 30#include <net/netlink.h>
1da177e4
LT
31#include <net/pkt_sched.h>
32#include <net/pkt_cls.h>
33
1da177e4 34/* The list of all installed classifier types */
36272874 35static LIST_HEAD(tcf_proto_base);
1da177e4
LT
36
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
33a48927 42static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
1da177e4 43{
dcd76081 44 const struct tcf_proto_ops *t, *res = NULL;
1da177e4
LT
45
46 if (kind) {
47 read_lock(&cls_mod_lock);
36272874 48 list_for_each_entry(t, &tcf_proto_base, head) {
33a48927 49 if (strcmp(kind, t->kind) == 0) {
dcd76081
ED
50 if (try_module_get(t->owner))
51 res = t;
1da177e4
LT
52 break;
53 }
54 }
55 read_unlock(&cls_mod_lock);
56 }
dcd76081 57 return res;
1da177e4
LT
58}
59
60/* Register(unregister) new classifier type */
61
62int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63{
36272874 64 struct tcf_proto_ops *t;
1da177e4
LT
65 int rc = -EEXIST;
66
67 write_lock(&cls_mod_lock);
36272874 68 list_for_each_entry(t, &tcf_proto_base, head)
1da177e4
LT
69 if (!strcmp(ops->kind, t->kind))
70 goto out;
71
36272874 72 list_add_tail(&ops->head, &tcf_proto_base);
1da177e4
LT
73 rc = 0;
74out:
75 write_unlock(&cls_mod_lock);
76 return rc;
77}
aa767bfe 78EXPORT_SYMBOL(register_tcf_proto_ops);
1da177e4
LT
79
80int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81{
36272874 82 struct tcf_proto_ops *t;
1da177e4
LT
83 int rc = -ENOENT;
84
c78e1746
DB
85 /* Wait for outstanding call_rcu()s, if any, from a
86 * tcf_proto_ops's destroy() handler.
87 */
88 rcu_barrier();
89
1da177e4 90 write_lock(&cls_mod_lock);
dcd76081
ED
91 list_for_each_entry(t, &tcf_proto_base, head) {
92 if (t == ops) {
93 list_del(&t->head);
94 rc = 0;
1da177e4 95 break;
dcd76081
ED
96 }
97 }
1da177e4
LT
98 write_unlock(&cls_mod_lock);
99 return rc;
100}
aa767bfe 101EXPORT_SYMBOL(unregister_tcf_proto_ops);
1da177e4 102
7316ae88
TG
103static int tfilter_notify(struct net *net, struct sk_buff *oskb,
104 struct nlmsghdr *n, struct tcf_proto *tp,
fa59b27c 105 unsigned long fh, int event, bool unicast);
1da177e4 106
ea7f8277
DB
107static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
108 struct nlmsghdr *n,
2190d1d0 109 struct tcf_chain *chain, int event)
ea7f8277 110{
ea7f8277
DB
111 struct tcf_proto *tp;
112
2190d1d0
JP
113 for (tp = rtnl_dereference(chain->filter_chain);
114 tp; tp = rtnl_dereference(tp->next))
19a8bb28 115 tfilter_notify(net, oskb, n, tp, 0, event, false);
ea7f8277 116}
1da177e4
LT
117
118/* Select new prio value from the range, managed by kernel. */
119
aa767bfe 120static inline u32 tcf_auto_prio(struct tcf_proto *tp)
1da177e4 121{
aa767bfe 122 u32 first = TC_H_MAKE(0xC0000000U, 0U);
1da177e4
LT
123
124 if (tp)
cc7ec456 125 first = tp->prio - 1;
1da177e4 126
7961973a 127 return TC_H_MAJ(first);
1da177e4
LT
128}
129
33a48927 130static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
6529eaba 131 u32 prio, u32 parent, struct Qdisc *q,
5bc17018 132 struct tcf_chain *chain)
33a48927
JP
133{
134 struct tcf_proto *tp;
135 int err;
136
137 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138 if (!tp)
139 return ERR_PTR(-ENOBUFS);
140
141 err = -ENOENT;
142 tp->ops = tcf_proto_lookup_ops(kind);
143 if (!tp->ops) {
144#ifdef CONFIG_MODULES
145 rtnl_unlock();
146 request_module("cls_%s", kind);
147 rtnl_lock();
148 tp->ops = tcf_proto_lookup_ops(kind);
149 /* We dropped the RTNL semaphore in order to perform
150 * the module load. So, even if we succeeded in loading
151 * the module we have to replay the request. We indicate
152 * this using -EAGAIN.
153 */
154 if (tp->ops) {
155 module_put(tp->ops->owner);
156 err = -EAGAIN;
157 } else {
158 err = -ENOENT;
159 }
160 goto errout;
161#endif
162 }
163 tp->classify = tp->ops->classify;
164 tp->protocol = protocol;
165 tp->prio = prio;
166 tp->classid = parent;
167 tp->q = q;
5bc17018 168 tp->chain = chain;
33a48927
JP
169
170 err = tp->ops->init(tp);
171 if (err) {
172 module_put(tp->ops->owner);
173 goto errout;
174 }
175 return tp;
176
177errout:
178 kfree(tp);
179 return ERR_PTR(err);
180}
181
763dbf63 182static void tcf_proto_destroy(struct tcf_proto *tp)
cf1facda 183{
763dbf63
WC
184 tp->ops->destroy(tp);
185 module_put(tp->ops->owner);
186 kfree_rcu(tp, rcu);
cf1facda
JP
187}
188
5bc17018
JP
189static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
190 u32 chain_index)
2190d1d0 191{
5bc17018
JP
192 struct tcf_chain *chain;
193
194 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
195 if (!chain)
196 return NULL;
197 list_add_tail(&chain->list, &block->chain_list);
198 chain->block = block;
199 chain->index = chain_index;
200 chain->refcnt = 1;
201 return chain;
2190d1d0
JP
202}
203
f93e1cdc 204static void tcf_chain_flush(struct tcf_chain *chain)
cf1facda
JP
205{
206 struct tcf_proto *tp;
207
acc8b316 208 if (chain->p_filter_chain)
f93e1cdc 209 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
2190d1d0
JP
210 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
763dbf63 212 tcf_proto_destroy(tp);
cf1facda 213 }
f93e1cdc
JP
214}
215
216static void tcf_chain_destroy(struct tcf_chain *chain)
217{
218 list_del(&chain->list);
219 tcf_chain_flush(chain);
2190d1d0
JP
220 kfree(chain);
221}
222
367a8ce8
WC
223struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
224 bool create)
5bc17018
JP
225{
226 struct tcf_chain *chain;
227
228 list_for_each_entry(chain, &block->chain_list, list) {
229 if (chain->index == chain_index) {
230 chain->refcnt++;
231 return chain;
232 }
233 }
367a8ce8
WC
234 if (create)
235 return tcf_chain_create(block, chain_index);
236 else
237 return NULL;
5bc17018
JP
238}
239EXPORT_SYMBOL(tcf_chain_get);
240
241void tcf_chain_put(struct tcf_chain *chain)
242{
243 /* Destroy unused chain, with exception of chain 0, which is the
244 * default one and has to be always present.
245 */
246 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
247 tcf_chain_destroy(chain);
248}
249EXPORT_SYMBOL(tcf_chain_put);
250
2190d1d0
JP
251static void
252tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
253 struct tcf_proto __rcu **p_filter_chain)
254{
255 chain->p_filter_chain = p_filter_chain;
cf1facda 256}
6529eaba
JP
257
258int tcf_block_get(struct tcf_block **p_block,
259 struct tcf_proto __rcu **p_filter_chain)
260{
261 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
5bc17018 262 struct tcf_chain *chain;
2190d1d0 263 int err;
6529eaba
JP
264
265 if (!block)
266 return -ENOMEM;
5bc17018
JP
267 INIT_LIST_HEAD(&block->chain_list);
268 /* Create chain 0 by default, it has to be always present. */
269 chain = tcf_chain_create(block, 0);
270 if (!chain) {
2190d1d0
JP
271 err = -ENOMEM;
272 goto err_chain_create;
273 }
5bc17018 274 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
6529eaba
JP
275 *p_block = block;
276 return 0;
2190d1d0
JP
277
278err_chain_create:
279 kfree(block);
280 return err;
6529eaba
JP
281}
282EXPORT_SYMBOL(tcf_block_get);
283
284void tcf_block_put(struct tcf_block *block)
285{
5bc17018
JP
286 struct tcf_chain *chain, *tmp;
287
6529eaba
JP
288 if (!block)
289 return;
5bc17018
JP
290
291 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
292 tcf_chain_destroy(chain);
6529eaba
JP
293 kfree(block);
294}
295EXPORT_SYMBOL(tcf_block_put);
cf1facda 296
87d83093
JP
297/* Main classifier routine: scans classifier chain attached
298 * to this qdisc, (optionally) tests for protocol and asks
299 * specific classifiers.
300 */
301int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
302 struct tcf_result *res, bool compat_mode)
303{
304 __be16 protocol = tc_skb_protocol(skb);
305#ifdef CONFIG_NET_CLS_ACT
306 const int max_reclassify_loop = 4;
ee538dce
JP
307 const struct tcf_proto *orig_tp = tp;
308 const struct tcf_proto *first_tp;
87d83093
JP
309 int limit = 0;
310
311reclassify:
312#endif
313 for (; tp; tp = rcu_dereference_bh(tp->next)) {
314 int err;
315
316 if (tp->protocol != protocol &&
317 tp->protocol != htons(ETH_P_ALL))
318 continue;
319
320 err = tp->classify(skb, tp, res);
321#ifdef CONFIG_NET_CLS_ACT
db50514f 322 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
ee538dce 323 first_tp = orig_tp;
87d83093 324 goto reset;
db50514f 325 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
ee538dce 326 first_tp = res->goto_tp;
db50514f
JP
327 goto reset;
328 }
87d83093
JP
329#endif
330 if (err >= 0)
331 return err;
332 }
333
334 return TC_ACT_UNSPEC; /* signal: continue lookup */
335#ifdef CONFIG_NET_CLS_ACT
336reset:
337 if (unlikely(limit++ >= max_reclassify_loop)) {
338 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
339 tp->q->ops->id, tp->prio & 0xffff,
340 ntohs(tp->protocol));
341 return TC_ACT_SHOT;
342 }
343
ee538dce 344 tp = first_tp;
87d83093
JP
345 protocol = tc_skb_protocol(skb);
346 goto reclassify;
347#endif
348}
349EXPORT_SYMBOL(tcf_classify);
350
2190d1d0
JP
351struct tcf_chain_info {
352 struct tcf_proto __rcu **pprev;
353 struct tcf_proto __rcu *next;
354};
355
356static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
357{
358 return rtnl_dereference(*chain_info->pprev);
359}
360
361static void tcf_chain_tp_insert(struct tcf_chain *chain,
362 struct tcf_chain_info *chain_info,
363 struct tcf_proto *tp)
364{
365 if (chain->p_filter_chain &&
366 *chain_info->pprev == chain->filter_chain)
31efcc25 367 rcu_assign_pointer(*chain->p_filter_chain, tp);
2190d1d0
JP
368 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
369 rcu_assign_pointer(*chain_info->pprev, tp);
370}
371
372static void tcf_chain_tp_remove(struct tcf_chain *chain,
373 struct tcf_chain_info *chain_info,
374 struct tcf_proto *tp)
375{
376 struct tcf_proto *next = rtnl_dereference(chain_info->next);
377
378 if (chain->p_filter_chain && tp == chain->filter_chain)
31efcc25 379 RCU_INIT_POINTER(*chain->p_filter_chain, next);
2190d1d0
JP
380 RCU_INIT_POINTER(*chain_info->pprev, next);
381}
382
383static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
384 struct tcf_chain_info *chain_info,
385 u32 protocol, u32 prio,
386 bool prio_allocate)
387{
388 struct tcf_proto **pprev;
389 struct tcf_proto *tp;
390
391 /* Check the chain for existence of proto-tcf with this priority */
392 for (pprev = &chain->filter_chain;
393 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
394 if (tp->prio >= prio) {
395 if (tp->prio == prio) {
396 if (prio_allocate ||
397 (tp->protocol != protocol && protocol))
398 return ERR_PTR(-EINVAL);
399 } else {
400 tp = NULL;
401 }
402 break;
403 }
404 }
405 chain_info->pprev = pprev;
406 chain_info->next = tp ? tp->next : NULL;
407 return tp;
408}
409
1da177e4
LT
410/* Add/change/delete/get a filter node */
411
c21ef3e3
DA
412static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
413 struct netlink_ext_ack *extack)
1da177e4 414{
3b1e0a65 415 struct net *net = sock_net(skb->sk);
add93b61 416 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
417 struct tcmsg *t;
418 u32 protocol;
419 u32 prio;
9d36d9e5 420 bool prio_allocate;
1da177e4 421 u32 parent;
5bc17018 422 u32 chain_index;
1da177e4
LT
423 struct net_device *dev;
424 struct Qdisc *q;
2190d1d0 425 struct tcf_chain_info chain_info;
5bc17018 426 struct tcf_chain *chain = NULL;
6529eaba 427 struct tcf_block *block;
1da177e4 428 struct tcf_proto *tp;
20fea08b 429 const struct Qdisc_class_ops *cops;
1da177e4
LT
430 unsigned long cl;
431 unsigned long fh;
432 int err;
628185cf 433 int tp_created;
1da177e4 434
4e8bbb81 435 if ((n->nlmsg_type != RTM_GETTFILTER) &&
5f013c9b 436 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
dfc47ef8 437 return -EPERM;
de179c8c 438
1da177e4 439replay:
628185cf
DB
440 tp_created = 0;
441
c21ef3e3 442 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
de179c8c
H
443 if (err < 0)
444 return err;
445
942b8165 446 t = nlmsg_data(n);
1da177e4
LT
447 protocol = TC_H_MIN(t->tcm_info);
448 prio = TC_H_MAJ(t->tcm_info);
9d36d9e5 449 prio_allocate = false;
1da177e4
LT
450 parent = t->tcm_parent;
451 cl = 0;
452
453 if (prio == 0) {
ea7f8277
DB
454 switch (n->nlmsg_type) {
455 case RTM_DELTFILTER:
9f6ed032 456 if (protocol || t->tcm_handle || tca[TCA_KIND])
ea7f8277
DB
457 return -ENOENT;
458 break;
459 case RTM_NEWTFILTER:
460 /* If no priority is provided by the user,
461 * we allocate one.
462 */
463 if (n->nlmsg_flags & NLM_F_CREATE) {
464 prio = TC_H_MAKE(0x80000000U, 0U);
9d36d9e5 465 prio_allocate = true;
ea7f8277
DB
466 break;
467 }
468 /* fall-through */
469 default:
1da177e4 470 return -ENOENT;
ea7f8277 471 }
1da177e4
LT
472 }
473
474 /* Find head of filter chain. */
475
476 /* Find link */
7316ae88 477 dev = __dev_get_by_index(net, t->tcm_ifindex);
aa767bfe 478 if (dev == NULL)
1da177e4
LT
479 return -ENODEV;
480
481 /* Find qdisc */
482 if (!parent) {
af356afa 483 q = dev->qdisc;
1da177e4 484 parent = q->handle;
aa767bfe
SH
485 } else {
486 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
487 if (q == NULL)
488 return -EINVAL;
489 }
1da177e4
LT
490
491 /* Is it classful? */
cc7ec456
ED
492 cops = q->ops->cl_ops;
493 if (!cops)
1da177e4
LT
494 return -EINVAL;
495
6529eaba 496 if (!cops->tcf_block)
71ebe5e9
PM
497 return -EOPNOTSUPP;
498
1da177e4
LT
499 /* Do we search for filter, attached to class? */
500 if (TC_H_MIN(parent)) {
501 cl = cops->get(q, parent);
502 if (cl == 0)
503 return -ENOENT;
504 }
505
506 /* And the last stroke */
6529eaba
JP
507 block = cops->tcf_block(q, cl);
508 if (!block) {
6bb16e7a 509 err = -EINVAL;
1da177e4 510 goto errout;
6bb16e7a 511 }
5bc17018
JP
512
513 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
514 if (chain_index > TC_ACT_EXT_VAL_MASK) {
515 err = -EINVAL;
516 goto errout;
517 }
367a8ce8
WC
518 chain = tcf_chain_get(block, chain_index,
519 n->nlmsg_type == RTM_NEWTFILTER);
5bc17018 520 if (!chain) {
367a8ce8 521 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
5bc17018
JP
522 goto errout;
523 }
6529eaba 524
ea7f8277
DB
525 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
526 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
f93e1cdc 527 tcf_chain_flush(chain);
ea7f8277
DB
528 err = 0;
529 goto errout;
530 }
1da177e4 531
2190d1d0
JP
532 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
533 prio, prio_allocate);
534 if (IS_ERR(tp)) {
535 err = PTR_ERR(tp);
536 goto errout;
1da177e4
LT
537 }
538
539 if (tp == NULL) {
540 /* Proto-tcf does not exist, create new one */
541
6bb16e7a
JP
542 if (tca[TCA_KIND] == NULL || !protocol) {
543 err = -EINVAL;
1da177e4 544 goto errout;
6bb16e7a 545 }
1da177e4 546
cc7ec456 547 if (n->nlmsg_type != RTM_NEWTFILTER ||
6bb16e7a
JP
548 !(n->nlmsg_flags & NLM_F_CREATE)) {
549 err = -ENOENT;
1da177e4 550 goto errout;
6bb16e7a 551 }
1da177e4 552
9d36d9e5 553 if (prio_allocate)
2190d1d0 554 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1da177e4 555
33a48927 556 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
5bc17018 557 protocol, prio, parent, q, chain);
33a48927
JP
558 if (IS_ERR(tp)) {
559 err = PTR_ERR(tp);
1da177e4
LT
560 goto errout;
561 }
12186be7 562 tp_created = 1;
6bb16e7a
JP
563 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
564 err = -EINVAL;
1da177e4 565 goto errout;
6bb16e7a 566 }
1da177e4
LT
567
568 fh = tp->ops->get(tp, t->tcm_handle);
569
570 if (fh == 0) {
571 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
2190d1d0 572 tcf_chain_tp_remove(chain, &chain_info, tp);
fa59b27c
ED
573 tfilter_notify(net, skb, n, tp, fh,
574 RTM_DELTFILTER, false);
763dbf63 575 tcf_proto_destroy(tp);
1da177e4
LT
576 err = 0;
577 goto errout;
578 }
579
aa767bfe 580 if (n->nlmsg_type != RTM_NEWTFILTER ||
6bb16e7a
JP
581 !(n->nlmsg_flags & NLM_F_CREATE)) {
582 err = -ENOENT;
1da177e4 583 goto errout;
6bb16e7a 584 }
1da177e4 585 } else {
763dbf63
WC
586 bool last;
587
1da177e4 588 switch (n->nlmsg_type) {
10297b99 589 case RTM_NEWTFILTER:
12186be7
MU
590 if (n->nlmsg_flags & NLM_F_EXCL) {
591 if (tp_created)
763dbf63 592 tcf_proto_destroy(tp);
6bb16e7a 593 err = -EEXIST;
1da177e4 594 goto errout;
12186be7 595 }
1da177e4
LT
596 break;
597 case RTM_DELTFILTER:
763dbf63 598 err = tp->ops->delete(tp, fh, &last);
40c81b25
JP
599 if (err)
600 goto errout;
40c81b25
JP
601 tfilter_notify(net, skb, n, tp, t->tcm_handle,
602 RTM_DELTFILTER, false);
763dbf63 603 if (last) {
2190d1d0 604 tcf_chain_tp_remove(chain, &chain_info, tp);
763dbf63
WC
605 tcf_proto_destroy(tp);
606 }
d7cf52c2 607 goto errout;
1da177e4 608 case RTM_GETTFILTER:
5a7a5555 609 err = tfilter_notify(net, skb, n, tp, fh,
fa59b27c 610 RTM_NEWTFILTER, true);
1da177e4
LT
611 goto errout;
612 default:
613 err = -EINVAL;
614 goto errout;
615 }
616 }
617
2f7ef2f8
CW
618 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
619 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
12186be7 620 if (err == 0) {
2190d1d0
JP
621 if (tp_created)
622 tcf_chain_tp_insert(chain, &chain_info, tp);
fa59b27c 623 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
12186be7
MU
624 } else {
625 if (tp_created)
763dbf63 626 tcf_proto_destroy(tp);
12186be7 627 }
1da177e4
LT
628
629errout:
5bc17018
JP
630 if (chain)
631 tcf_chain_put(chain);
1da177e4
LT
632 if (cl)
633 cops->put(q, cl);
634 if (err == -EAGAIN)
635 /* Replay the request. */
636 goto replay;
637 return err;
638}
639
0b0f43fe
JHS
640static int tcf_fill_node(struct net *net, struct sk_buff *skb,
641 struct tcf_proto *tp, unsigned long fh, u32 portid,
642 u32 seq, u16 flags, int event)
1da177e4
LT
643{
644 struct tcmsg *tcm;
645 struct nlmsghdr *nlh;
27a884dc 646 unsigned char *b = skb_tail_pointer(skb);
1da177e4 647
15e47304 648 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
942b8165
DM
649 if (!nlh)
650 goto out_nlmsg_trim;
651 tcm = nlmsg_data(nlh);
1da177e4 652 tcm->tcm_family = AF_UNSPEC;
9ef1d4c7 653 tcm->tcm__pad1 = 0;
ad61df91 654 tcm->tcm__pad2 = 0;
5ce2d488 655 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
1da177e4
LT
656 tcm->tcm_parent = tp->classid;
657 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1b34ec43
DM
658 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
659 goto nla_put_failure;
5bc17018
JP
660 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
661 goto nla_put_failure;
1da177e4
LT
662 tcm->tcm_handle = fh;
663 if (RTM_DELTFILTER != event) {
664 tcm->tcm_handle = 0;
832d1d5b 665 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
add93b61 666 goto nla_put_failure;
1da177e4 667 }
27a884dc 668 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
669 return skb->len;
670
942b8165 671out_nlmsg_trim:
add93b61 672nla_put_failure:
dc5fc579 673 nlmsg_trim(skb, b);
1da177e4
LT
674 return -1;
675}
676
7316ae88
TG
677static int tfilter_notify(struct net *net, struct sk_buff *oskb,
678 struct nlmsghdr *n, struct tcf_proto *tp,
fa59b27c 679 unsigned long fh, int event, bool unicast)
1da177e4
LT
680{
681 struct sk_buff *skb;
15e47304 682 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1da177e4
LT
683
684 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
685 if (!skb)
686 return -ENOBUFS;
687
30a391a1
RM
688 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
689 n->nlmsg_flags, event) <= 0) {
1da177e4
LT
690 kfree_skb(skb);
691 return -EINVAL;
692 }
693
fa59b27c
ED
694 if (unicast)
695 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
696
15e47304 697 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
aa767bfe 698 n->nlmsg_flags & NLM_F_ECHO);
1da177e4
LT
699}
700
aa767bfe 701struct tcf_dump_args {
1da177e4
LT
702 struct tcf_walker w;
703 struct sk_buff *skb;
704 struct netlink_callback *cb;
705};
706
aa767bfe
SH
707static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
708 struct tcf_walker *arg)
1da177e4 709{
aa767bfe 710 struct tcf_dump_args *a = (void *)arg;
832d1d5b 711 struct net *net = sock_net(a->skb->sk);
1da177e4 712
832d1d5b 713 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
5a7a5555
JHS
714 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
715 RTM_NEWTFILTER);
1da177e4
LT
716}
717
5bc17018 718static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
acb31fae
JP
719 struct netlink_callback *cb,
720 long index_start, long *p_index)
721{
722 struct net *net = sock_net(skb->sk);
723 struct tcmsg *tcm = nlmsg_data(cb->nlh);
724 struct tcf_dump_args arg;
725 struct tcf_proto *tp;
726
727 for (tp = rtnl_dereference(chain->filter_chain);
728 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
729 if (*p_index < index_start)
730 continue;
731 if (TC_H_MAJ(tcm->tcm_info) &&
732 TC_H_MAJ(tcm->tcm_info) != tp->prio)
733 continue;
734 if (TC_H_MIN(tcm->tcm_info) &&
735 TC_H_MIN(tcm->tcm_info) != tp->protocol)
736 continue;
737 if (*p_index > index_start)
738 memset(&cb->args[1], 0,
739 sizeof(cb->args) - sizeof(cb->args[0]));
740 if (cb->args[1] == 0) {
741 if (tcf_fill_node(net, skb, tp, 0,
742 NETLINK_CB(cb->skb).portid,
743 cb->nlh->nlmsg_seq, NLM_F_MULTI,
744 RTM_NEWTFILTER) <= 0)
5bc17018 745 return false;
acb31fae
JP
746
747 cb->args[1] = 1;
748 }
749 if (!tp->ops->walk)
750 continue;
751 arg.w.fn = tcf_node_dump;
752 arg.skb = skb;
753 arg.cb = cb;
754 arg.w.stop = 0;
755 arg.w.skip = cb->args[1] - 1;
756 arg.w.count = 0;
757 tp->ops->walk(tp, &arg.w);
758 cb->args[1] = arg.w.count + 1;
759 if (arg.w.stop)
5bc17018 760 return false;
acb31fae 761 }
5bc17018 762 return true;
acb31fae
JP
763}
764
bd27a875 765/* called with RTNL */
1da177e4
LT
766static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
767{
3b1e0a65 768 struct net *net = sock_net(skb->sk);
5bc17018 769 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
770 struct net_device *dev;
771 struct Qdisc *q;
6529eaba 772 struct tcf_block *block;
2190d1d0 773 struct tcf_chain *chain;
942b8165 774 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1da177e4 775 unsigned long cl = 0;
20fea08b 776 const struct Qdisc_class_ops *cops;
acb31fae
JP
777 long index_start;
778 long index;
5bc17018 779 int err;
1da177e4 780
573ce260 781 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1da177e4 782 return skb->len;
5bc17018
JP
783
784 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
785 if (err)
786 return err;
787
cc7ec456
ED
788 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
789 if (!dev)
1da177e4
LT
790 return skb->len;
791
1da177e4 792 if (!tcm->tcm_parent)
af356afa 793 q = dev->qdisc;
1da177e4
LT
794 else
795 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
796 if (!q)
797 goto out;
cc7ec456
ED
798 cops = q->ops->cl_ops;
799 if (!cops)
1da177e4 800 goto errout;
6529eaba 801 if (!cops->tcf_block)
71ebe5e9 802 goto errout;
1da177e4
LT
803 if (TC_H_MIN(tcm->tcm_parent)) {
804 cl = cops->get(q, tcm->tcm_parent);
805 if (cl == 0)
806 goto errout;
807 }
6529eaba
JP
808 block = cops->tcf_block(q, cl);
809 if (!block)
1da177e4
LT
810 goto errout;
811
acb31fae
JP
812 index_start = cb->args[0];
813 index = 0;
5bc17018
JP
814
815 list_for_each_entry(chain, &block->chain_list, list) {
816 if (tca[TCA_CHAIN] &&
817 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
818 continue;
819 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
820 break;
821 }
822
acb31fae 823 cb->args[0] = index;
1da177e4
LT
824
825errout:
826 if (cl)
827 cops->put(q, cl);
828out:
1da177e4
LT
829 return skb->len;
830}
831
18d0264f 832void tcf_exts_destroy(struct tcf_exts *exts)
1da177e4
LT
833{
834#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
835 LIST_HEAD(actions);
836
837 tcf_exts_to_list(exts, &actions);
838 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
839 kfree(exts->actions);
840 exts->nr_actions = 0;
1da177e4
LT
841#endif
842}
aa767bfe 843EXPORT_SYMBOL(tcf_exts_destroy);
1da177e4 844
c1b52739 845int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
5a7a5555 846 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1da177e4 847{
1da177e4
LT
848#ifdef CONFIG_NET_CLS_ACT
849 {
1da177e4
LT
850 struct tc_action *act;
851
5da57f42 852 if (exts->police && tb[exts->police]) {
9fb9f251
JP
853 act = tcf_action_init_1(net, tp, tb[exts->police],
854 rate_tlv, "police", ovr,
855 TCA_ACT_BIND);
ab27cfb8
PM
856 if (IS_ERR(act))
857 return PTR_ERR(act);
1da177e4 858
33be6271 859 act->type = exts->type = TCA_OLD_COMPAT;
22dc13c8
WC
860 exts->actions[0] = act;
861 exts->nr_actions = 1;
5da57f42 862 } else if (exts->action && tb[exts->action]) {
22dc13c8
WC
863 LIST_HEAD(actions);
864 int err, i = 0;
865
9fb9f251
JP
866 err = tcf_action_init(net, tp, tb[exts->action],
867 rate_tlv, NULL, ovr, TCA_ACT_BIND,
5a7a5555 868 &actions);
33be6271
WC
869 if (err)
870 return err;
22dc13c8
WC
871 list_for_each_entry(act, &actions, list)
872 exts->actions[i++] = act;
873 exts->nr_actions = i;
1da177e4
LT
874 }
875 }
1da177e4 876#else
5da57f42
WC
877 if ((exts->action && tb[exts->action]) ||
878 (exts->police && tb[exts->police]))
1da177e4
LT
879 return -EOPNOTSUPP;
880#endif
881
882 return 0;
883}
aa767bfe 884EXPORT_SYMBOL(tcf_exts_validate);
1da177e4 885
aa767bfe
SH
886void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
887 struct tcf_exts *src)
1da177e4
LT
888{
889#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
890 struct tcf_exts old = *dst;
891
a49eb42a 892 tcf_tree_lock(tp);
22dc13c8
WC
893 dst->nr_actions = src->nr_actions;
894 dst->actions = src->actions;
5301e3e1 895 dst->type = src->type;
a49eb42a 896 tcf_tree_unlock(tp);
22dc13c8
WC
897
898 tcf_exts_destroy(&old);
1da177e4
LT
899#endif
900}
aa767bfe 901EXPORT_SYMBOL(tcf_exts_change);
1da177e4 902
22dc13c8
WC
903#ifdef CONFIG_NET_CLS_ACT
904static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
905{
906 if (exts->nr_actions == 0)
907 return NULL;
908 else
909 return exts->actions[0];
910}
911#endif
33be6271 912
5da57f42 913int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
914{
915#ifdef CONFIG_NET_CLS_ACT
9cc63db5
CW
916 struct nlattr *nest;
917
22dc13c8 918 if (exts->action && exts->nr_actions) {
1da177e4
LT
919 /*
920 * again for backward compatible mode - we want
921 * to work with both old and new modes of entering
922 * tc data even if iproute2 was newer - jhs
923 */
33be6271 924 if (exts->type != TCA_OLD_COMPAT) {
22dc13c8
WC
925 LIST_HEAD(actions);
926
5da57f42 927 nest = nla_nest_start(skb, exts->action);
4b3550ef
PM
928 if (nest == NULL)
929 goto nla_put_failure;
22dc13c8
WC
930
931 tcf_exts_to_list(exts, &actions);
932 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
add93b61 933 goto nla_put_failure;
4b3550ef 934 nla_nest_end(skb, nest);
5da57f42 935 } else if (exts->police) {
33be6271 936 struct tc_action *act = tcf_exts_first_act(exts);
5da57f42 937 nest = nla_nest_start(skb, exts->police);
63acd680 938 if (nest == NULL || !act)
4b3550ef 939 goto nla_put_failure;
33be6271 940 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
add93b61 941 goto nla_put_failure;
4b3550ef 942 nla_nest_end(skb, nest);
1da177e4
LT
943 }
944 }
1da177e4 945 return 0;
9cc63db5
CW
946
947nla_put_failure:
948 nla_nest_cancel(skb, nest);
1da177e4 949 return -1;
9cc63db5
CW
950#else
951 return 0;
952#endif
1da177e4 953}
aa767bfe 954EXPORT_SYMBOL(tcf_exts_dump);
1da177e4 955
aa767bfe 956
5da57f42 957int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
958{
959#ifdef CONFIG_NET_CLS_ACT
33be6271 960 struct tc_action *a = tcf_exts_first_act(exts);
b057df24 961 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
33be6271 962 return -1;
1da177e4
LT
963#endif
964 return 0;
1da177e4 965}
aa767bfe 966EXPORT_SYMBOL(tcf_exts_dump_stats);
1da177e4 967
7091d8c7
HHZ
968int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
969 struct net_device **hw_dev)
970{
971#ifdef CONFIG_NET_CLS_ACT
972 const struct tc_action *a;
973 LIST_HEAD(actions);
974
975 if (tc_no_actions(exts))
976 return -EINVAL;
977
978 tcf_exts_to_list(exts, &actions);
979 list_for_each_entry(a, &actions, list) {
980 if (a->ops->get_dev) {
981 a->ops->get_dev(a, dev_net(dev), hw_dev);
982 break;
983 }
984 }
985 if (*hw_dev)
986 return 0;
987#endif
988 return -EOPNOTSUPP;
989}
990EXPORT_SYMBOL(tcf_exts_get_dev);
991
1da177e4
LT
992static int __init tc_filter_init(void)
993{
c7ac8679
GR
994 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
995 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
82623c0d 996 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
c7ac8679 997 tc_dump_tfilter, NULL);
1da177e4 998
1da177e4
LT
999 return 0;
1000}
1001
1002subsys_initcall(tc_filter_init);