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