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