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