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