Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/skbuff.h>
24#include <linux/init.h>
25#include <linux/kmod.h>
26#include <linux/slab.h>
27#include <net/net_namespace.h>
28#include <net/sock.h>
29#include <net/netlink.h>
30#include <net/pkt_sched.h>
31#include <net/pkt_cls.h>
32
33/* The list of all installed classifier types */
34static LIST_HEAD(tcf_proto_base);
35
36/* Protects list of registered TC modules. It is pure SMP lock. */
37static DEFINE_RWLOCK(cls_mod_lock);
38
39/* Find classifier type by string name */
40
41static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
42{
43 const struct tcf_proto_ops *t, *res = NULL;
44
45 if (kind) {
46 read_lock(&cls_mod_lock);
47 list_for_each_entry(t, &tcf_proto_base, head) {
48 if (strcmp(kind, t->kind) == 0) {
49 if (try_module_get(t->owner))
50 res = t;
51 break;
52 }
53 }
54 read_unlock(&cls_mod_lock);
55 }
56 return res;
57}
58
59/* Register(unregister) new classifier type */
60
61int register_tcf_proto_ops(struct tcf_proto_ops *ops)
62{
63 struct tcf_proto_ops *t;
64 int rc = -EEXIST;
65
66 write_lock(&cls_mod_lock);
67 list_for_each_entry(t, &tcf_proto_base, head)
68 if (!strcmp(ops->kind, t->kind))
69 goto out;
70
71 list_add_tail(&ops->head, &tcf_proto_base);
72 rc = 0;
73out:
74 write_unlock(&cls_mod_lock);
75 return rc;
76}
77EXPORT_SYMBOL(register_tcf_proto_ops);
78
79static struct workqueue_struct *tc_filter_wq;
80
81int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
82{
83 struct tcf_proto_ops *t;
84 int rc = -ENOENT;
85
86 /* Wait for outstanding call_rcu()s, if any, from a
87 * tcf_proto_ops's destroy() handler.
88 */
89 rcu_barrier();
90 flush_workqueue(tc_filter_wq);
91
92 write_lock(&cls_mod_lock);
93 list_for_each_entry(t, &tcf_proto_base, head) {
94 if (t == ops) {
95 list_del(&t->head);
96 rc = 0;
97 break;
98 }
99 }
100 write_unlock(&cls_mod_lock);
101 return rc;
102}
103EXPORT_SYMBOL(unregister_tcf_proto_ops);
104
105bool tcf_queue_work(struct work_struct *work)
106{
107 return queue_work(tc_filter_wq, work);
108}
109EXPORT_SYMBOL(tcf_queue_work);
110
111/* Select new prio value from the range, managed by kernel. */
112
113static inline u32 tcf_auto_prio(struct tcf_proto *tp)
114{
115 u32 first = TC_H_MAKE(0xC0000000U, 0U);
116
117 if (tp)
118 first = tp->prio - 1;
119
120 return TC_H_MAJ(first);
121}
122
123static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
124 u32 prio, u32 parent, struct Qdisc *q,
125 struct tcf_chain *chain)
126{
127 struct tcf_proto *tp;
128 int err;
129
130 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
131 if (!tp)
132 return ERR_PTR(-ENOBUFS);
133
134 err = -ENOENT;
135 tp->ops = tcf_proto_lookup_ops(kind);
136 if (!tp->ops) {
137#ifdef CONFIG_MODULES
138 rtnl_unlock();
139 request_module("cls_%s", kind);
140 rtnl_lock();
141 tp->ops = tcf_proto_lookup_ops(kind);
142 /* We dropped the RTNL semaphore in order to perform
143 * the module load. So, even if we succeeded in loading
144 * the module we have to replay the request. We indicate
145 * this using -EAGAIN.
146 */
147 if (tp->ops) {
148 module_put(tp->ops->owner);
149 err = -EAGAIN;
150 } else {
151 err = -ENOENT;
152 }
153 goto errout;
154#endif
155 }
156 tp->classify = tp->ops->classify;
157 tp->protocol = protocol;
158 tp->prio = prio;
159 tp->classid = parent;
160 tp->q = q;
161 tp->chain = chain;
162
163 err = tp->ops->init(tp);
164 if (err) {
165 module_put(tp->ops->owner);
166 goto errout;
167 }
168 return tp;
169
170errout:
171 kfree(tp);
172 return ERR_PTR(err);
173}
174
175static void tcf_proto_destroy(struct tcf_proto *tp)
176{
177 tp->ops->destroy(tp);
178 module_put(tp->ops->owner);
179 kfree_rcu(tp, rcu);
180}
181
182static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
183 u32 chain_index)
184{
185 struct tcf_chain *chain;
186
187 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
188 if (!chain)
189 return NULL;
190 list_add_tail(&chain->list, &block->chain_list);
191 chain->block = block;
192 chain->index = chain_index;
193 chain->refcnt = 1;
194 return chain;
195}
196
197static void tcf_chain_head_change(struct tcf_chain *chain,
198 struct tcf_proto *tp_head)
199{
200 if (chain->chain_head_change)
201 chain->chain_head_change(tp_head,
202 chain->chain_head_change_priv);
203}
204
205static void tcf_chain_flush(struct tcf_chain *chain)
206{
207 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
208
209 tcf_chain_head_change(chain, NULL);
210 while (tp) {
211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
212 tcf_proto_destroy(tp);
213 tp = rtnl_dereference(chain->filter_chain);
214 tcf_chain_put(chain);
215 }
216}
217
218static void tcf_chain_destroy(struct tcf_chain *chain)
219{
220 list_del(&chain->list);
221 kfree(chain);
222}
223
224static void tcf_chain_hold(struct tcf_chain *chain)
225{
226 ++chain->refcnt;
227}
228
229struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
230 bool create)
231{
232 struct tcf_chain *chain;
233
234 list_for_each_entry(chain, &block->chain_list, list) {
235 if (chain->index == chain_index) {
236 tcf_chain_hold(chain);
237 return chain;
238 }
239 }
240
241 return create ? tcf_chain_create(block, chain_index) : NULL;
242}
243EXPORT_SYMBOL(tcf_chain_get);
244
245void tcf_chain_put(struct tcf_chain *chain)
246{
247 if (--chain->refcnt == 0)
248 tcf_chain_destroy(chain);
249}
250EXPORT_SYMBOL(tcf_chain_put);
251
252static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
253 struct tcf_block_ext_info *ei,
254 enum tc_block_command command)
255{
256 struct net_device *dev = q->dev_queue->dev;
257 struct tc_block_offload bo = {};
258
259 if (!dev->netdev_ops->ndo_setup_tc)
260 return;
261 bo.command = command;
262 bo.binder_type = ei->binder_type;
263 bo.block = block;
264 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
265}
266
267static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
268 struct tcf_block_ext_info *ei)
269{
270 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
271}
272
273static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
274 struct tcf_block_ext_info *ei)
275{
276 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
277}
278
279int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
280 struct tcf_block_ext_info *ei)
281{
282 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
283 struct tcf_chain *chain;
284 int err;
285
286 if (!block)
287 return -ENOMEM;
288 INIT_LIST_HEAD(&block->chain_list);
289 INIT_LIST_HEAD(&block->cb_list);
290
291 /* Create chain 0 by default, it has to be always present. */
292 chain = tcf_chain_create(block, 0);
293 if (!chain) {
294 err = -ENOMEM;
295 goto err_chain_create;
296 }
297 WARN_ON(!ei->chain_head_change);
298 chain->chain_head_change = ei->chain_head_change;
299 chain->chain_head_change_priv = ei->chain_head_change_priv;
300 block->net = qdisc_net(q);
301 block->q = q;
302 tcf_block_offload_bind(block, q, ei);
303 *p_block = block;
304 return 0;
305
306err_chain_create:
307 kfree(block);
308 return err;
309}
310EXPORT_SYMBOL(tcf_block_get_ext);
311
312static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
313{
314 struct tcf_proto __rcu **p_filter_chain = priv;
315
316 rcu_assign_pointer(*p_filter_chain, tp_head);
317}
318
319int tcf_block_get(struct tcf_block **p_block,
320 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
321{
322 struct tcf_block_ext_info ei = {
323 .chain_head_change = tcf_chain_head_change_dflt,
324 .chain_head_change_priv = p_filter_chain,
325 };
326
327 WARN_ON(!p_filter_chain);
328 return tcf_block_get_ext(p_block, q, &ei);
329}
330EXPORT_SYMBOL(tcf_block_get);
331
332static void tcf_block_put_final(struct work_struct *work)
333{
334 struct tcf_block *block = container_of(work, struct tcf_block, work);
335 struct tcf_chain *chain, *tmp;
336
337 rtnl_lock();
338
339 /* At this point, all the chains should have refcnt == 1. */
340 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
341 tcf_chain_put(chain);
342 rtnl_unlock();
343 kfree(block);
344}
345
346/* XXX: Standalone actions are not allowed to jump to any chain, and bound
347 * actions should be all removed after flushing.
348 */
349void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
350 struct tcf_block_ext_info *ei)
351{
352 struct tcf_chain *chain;
353
354 if (!block)
355 return;
356 /* Hold a refcnt for all chains, except 0, so that they don't disappear
357 * while we are iterating.
358 */
359 list_for_each_entry(chain, &block->chain_list, list)
360 if (chain->index)
361 tcf_chain_hold(chain);
362
363 list_for_each_entry(chain, &block->chain_list, list)
364 tcf_chain_flush(chain);
365
366 tcf_block_offload_unbind(block, q, ei);
367
368 INIT_WORK(&block->work, tcf_block_put_final);
369 /* Wait for existing RCU callbacks to cool down, make sure their works
370 * have been queued before this. We can not flush pending works here
371 * because we are holding the RTNL lock.
372 */
373 rcu_barrier();
374 tcf_queue_work(&block->work);
375}
376EXPORT_SYMBOL(tcf_block_put_ext);
377
378void tcf_block_put(struct tcf_block *block)
379{
380 struct tcf_block_ext_info ei = {0, };
381
382 if (!block)
383 return;
384 tcf_block_put_ext(block, block->q, &ei);
385}
386
387EXPORT_SYMBOL(tcf_block_put);
388
389struct tcf_block_cb {
390 struct list_head list;
391 tc_setup_cb_t *cb;
392 void *cb_ident;
393 void *cb_priv;
394 unsigned int refcnt;
395};
396
397void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
398{
399 return block_cb->cb_priv;
400}
401EXPORT_SYMBOL(tcf_block_cb_priv);
402
403struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
404 tc_setup_cb_t *cb, void *cb_ident)
405{ struct tcf_block_cb *block_cb;
406
407 list_for_each_entry(block_cb, &block->cb_list, list)
408 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
409 return block_cb;
410 return NULL;
411}
412EXPORT_SYMBOL(tcf_block_cb_lookup);
413
414void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
415{
416 block_cb->refcnt++;
417}
418EXPORT_SYMBOL(tcf_block_cb_incref);
419
420unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
421{
422 return --block_cb->refcnt;
423}
424EXPORT_SYMBOL(tcf_block_cb_decref);
425
426struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
427 tc_setup_cb_t *cb, void *cb_ident,
428 void *cb_priv)
429{
430 struct tcf_block_cb *block_cb;
431
432 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
433 if (!block_cb)
434 return NULL;
435 block_cb->cb = cb;
436 block_cb->cb_ident = cb_ident;
437 block_cb->cb_priv = cb_priv;
438 list_add(&block_cb->list, &block->cb_list);
439 return block_cb;
440}
441EXPORT_SYMBOL(__tcf_block_cb_register);
442
443int tcf_block_cb_register(struct tcf_block *block,
444 tc_setup_cb_t *cb, void *cb_ident,
445 void *cb_priv)
446{
447 struct tcf_block_cb *block_cb;
448
449 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
450 return block_cb ? 0 : -ENOMEM;
451}
452EXPORT_SYMBOL(tcf_block_cb_register);
453
454void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
455{
456 list_del(&block_cb->list);
457 kfree(block_cb);
458}
459EXPORT_SYMBOL(__tcf_block_cb_unregister);
460
461void tcf_block_cb_unregister(struct tcf_block *block,
462 tc_setup_cb_t *cb, void *cb_ident)
463{
464 struct tcf_block_cb *block_cb;
465
466 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
467 if (!block_cb)
468 return;
469 __tcf_block_cb_unregister(block_cb);
470}
471EXPORT_SYMBOL(tcf_block_cb_unregister);
472
473static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
474 void *type_data, bool err_stop)
475{
476 struct tcf_block_cb *block_cb;
477 int ok_count = 0;
478 int err;
479
480 list_for_each_entry(block_cb, &block->cb_list, list) {
481 err = block_cb->cb(type, type_data, block_cb->cb_priv);
482 if (err) {
483 if (err_stop)
484 return err;
485 } else {
486 ok_count++;
487 }
488 }
489 return ok_count;
490}
491
492/* Main classifier routine: scans classifier chain attached
493 * to this qdisc, (optionally) tests for protocol and asks
494 * specific classifiers.
495 */
496int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
497 struct tcf_result *res, bool compat_mode)
498{
499 __be16 protocol = tc_skb_protocol(skb);
500#ifdef CONFIG_NET_CLS_ACT
501 const int max_reclassify_loop = 4;
502 const struct tcf_proto *orig_tp = tp;
503 const struct tcf_proto *first_tp;
504 int limit = 0;
505
506reclassify:
507#endif
508 for (; tp; tp = rcu_dereference_bh(tp->next)) {
509 int err;
510
511 if (tp->protocol != protocol &&
512 tp->protocol != htons(ETH_P_ALL))
513 continue;
514
515 err = tp->classify(skb, tp, res);
516#ifdef CONFIG_NET_CLS_ACT
517 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
518 first_tp = orig_tp;
519 goto reset;
520 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
521 first_tp = res->goto_tp;
522 goto reset;
523 }
524#endif
525 if (err >= 0)
526 return err;
527 }
528
529 return TC_ACT_UNSPEC; /* signal: continue lookup */
530#ifdef CONFIG_NET_CLS_ACT
531reset:
532 if (unlikely(limit++ >= max_reclassify_loop)) {
533 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
534 tp->q->ops->id, tp->prio & 0xffff,
535 ntohs(tp->protocol));
536 return TC_ACT_SHOT;
537 }
538
539 tp = first_tp;
540 protocol = tc_skb_protocol(skb);
541 goto reclassify;
542#endif
543}
544EXPORT_SYMBOL(tcf_classify);
545
546struct tcf_chain_info {
547 struct tcf_proto __rcu **pprev;
548 struct tcf_proto __rcu *next;
549};
550
551static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
552{
553 return rtnl_dereference(*chain_info->pprev);
554}
555
556static void tcf_chain_tp_insert(struct tcf_chain *chain,
557 struct tcf_chain_info *chain_info,
558 struct tcf_proto *tp)
559{
560 if (*chain_info->pprev == chain->filter_chain)
561 tcf_chain_head_change(chain, tp);
562 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
563 rcu_assign_pointer(*chain_info->pprev, tp);
564 tcf_chain_hold(chain);
565}
566
567static void tcf_chain_tp_remove(struct tcf_chain *chain,
568 struct tcf_chain_info *chain_info,
569 struct tcf_proto *tp)
570{
571 struct tcf_proto *next = rtnl_dereference(chain_info->next);
572
573 if (tp == chain->filter_chain)
574 tcf_chain_head_change(chain, next);
575 RCU_INIT_POINTER(*chain_info->pprev, next);
576 tcf_chain_put(chain);
577}
578
579static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
580 struct tcf_chain_info *chain_info,
581 u32 protocol, u32 prio,
582 bool prio_allocate)
583{
584 struct tcf_proto **pprev;
585 struct tcf_proto *tp;
586
587 /* Check the chain for existence of proto-tcf with this priority */
588 for (pprev = &chain->filter_chain;
589 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
590 if (tp->prio >= prio) {
591 if (tp->prio == prio) {
592 if (prio_allocate ||
593 (tp->protocol != protocol && protocol))
594 return ERR_PTR(-EINVAL);
595 } else {
596 tp = NULL;
597 }
598 break;
599 }
600 }
601 chain_info->pprev = pprev;
602 chain_info->next = tp ? tp->next : NULL;
603 return tp;
604}
605
606static int tcf_fill_node(struct net *net, struct sk_buff *skb,
607 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
608 void *fh, u32 portid, u32 seq, u16 flags, int event)
609{
610 struct tcmsg *tcm;
611 struct nlmsghdr *nlh;
612 unsigned char *b = skb_tail_pointer(skb);
613
614 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
615 if (!nlh)
616 goto out_nlmsg_trim;
617 tcm = nlmsg_data(nlh);
618 tcm->tcm_family = AF_UNSPEC;
619 tcm->tcm__pad1 = 0;
620 tcm->tcm__pad2 = 0;
621 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
622 tcm->tcm_parent = parent;
623 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
624 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
625 goto nla_put_failure;
626 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
627 goto nla_put_failure;
628 if (!fh) {
629 tcm->tcm_handle = 0;
630 } else {
631 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
632 goto nla_put_failure;
633 }
634 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
635 return skb->len;
636
637out_nlmsg_trim:
638nla_put_failure:
639 nlmsg_trim(skb, b);
640 return -1;
641}
642
643static int tfilter_notify(struct net *net, struct sk_buff *oskb,
644 struct nlmsghdr *n, struct tcf_proto *tp,
645 struct Qdisc *q, u32 parent,
646 void *fh, int event, bool unicast)
647{
648 struct sk_buff *skb;
649 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
650
651 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
652 if (!skb)
653 return -ENOBUFS;
654
655 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
656 n->nlmsg_flags, event) <= 0) {
657 kfree_skb(skb);
658 return -EINVAL;
659 }
660
661 if (unicast)
662 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
663
664 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
665 n->nlmsg_flags & NLM_F_ECHO);
666}
667
668static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
669 struct nlmsghdr *n, struct tcf_proto *tp,
670 struct Qdisc *q, u32 parent,
671 void *fh, bool unicast, bool *last)
672{
673 struct sk_buff *skb;
674 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
675 int err;
676
677 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
678 if (!skb)
679 return -ENOBUFS;
680
681 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
682 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
683 kfree_skb(skb);
684 return -EINVAL;
685 }
686
687 err = tp->ops->delete(tp, fh, last);
688 if (err) {
689 kfree_skb(skb);
690 return err;
691 }
692
693 if (unicast)
694 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
695
696 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
697 n->nlmsg_flags & NLM_F_ECHO);
698}
699
700static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
701 struct Qdisc *q, u32 parent,
702 struct nlmsghdr *n,
703 struct tcf_chain *chain, int event)
704{
705 struct tcf_proto *tp;
706
707 for (tp = rtnl_dereference(chain->filter_chain);
708 tp; tp = rtnl_dereference(tp->next))
709 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
710}
711
712/* Add/change/delete/get a filter node */
713
714static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
715 struct netlink_ext_ack *extack)
716{
717 struct net *net = sock_net(skb->sk);
718 struct nlattr *tca[TCA_MAX + 1];
719 struct tcmsg *t;
720 u32 protocol;
721 u32 prio;
722 bool prio_allocate;
723 u32 parent;
724 u32 chain_index;
725 struct net_device *dev;
726 struct Qdisc *q;
727 struct tcf_chain_info chain_info;
728 struct tcf_chain *chain = NULL;
729 struct tcf_block *block;
730 struct tcf_proto *tp;
731 const struct Qdisc_class_ops *cops;
732 unsigned long cl;
733 void *fh;
734 int err;
735 int tp_created;
736
737 if ((n->nlmsg_type != RTM_GETTFILTER) &&
738 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
739 return -EPERM;
740
741replay:
742 tp_created = 0;
743
744 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
745 if (err < 0)
746 return err;
747
748 t = nlmsg_data(n);
749 protocol = TC_H_MIN(t->tcm_info);
750 prio = TC_H_MAJ(t->tcm_info);
751 prio_allocate = false;
752 parent = t->tcm_parent;
753 cl = 0;
754
755 if (prio == 0) {
756 switch (n->nlmsg_type) {
757 case RTM_DELTFILTER:
758 if (protocol || t->tcm_handle || tca[TCA_KIND])
759 return -ENOENT;
760 break;
761 case RTM_NEWTFILTER:
762 /* If no priority is provided by the user,
763 * we allocate one.
764 */
765 if (n->nlmsg_flags & NLM_F_CREATE) {
766 prio = TC_H_MAKE(0x80000000U, 0U);
767 prio_allocate = true;
768 break;
769 }
770 /* fall-through */
771 default:
772 return -ENOENT;
773 }
774 }
775
776 /* Find head of filter chain. */
777
778 /* Find link */
779 dev = __dev_get_by_index(net, t->tcm_ifindex);
780 if (dev == NULL)
781 return -ENODEV;
782
783 /* Find qdisc */
784 if (!parent) {
785 q = dev->qdisc;
786 parent = q->handle;
787 } else {
788 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
789 if (q == NULL)
790 return -EINVAL;
791 }
792
793 /* Is it classful? */
794 cops = q->ops->cl_ops;
795 if (!cops)
796 return -EINVAL;
797
798 if (!cops->tcf_block)
799 return -EOPNOTSUPP;
800
801 /* Do we search for filter, attached to class? */
802 if (TC_H_MIN(parent)) {
803 cl = cops->find(q, parent);
804 if (cl == 0)
805 return -ENOENT;
806 }
807
808 /* And the last stroke */
809 block = cops->tcf_block(q, cl);
810 if (!block) {
811 err = -EINVAL;
812 goto errout;
813 }
814
815 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
816 if (chain_index > TC_ACT_EXT_VAL_MASK) {
817 err = -EINVAL;
818 goto errout;
819 }
820 chain = tcf_chain_get(block, chain_index,
821 n->nlmsg_type == RTM_NEWTFILTER);
822 if (!chain) {
823 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
824 goto errout;
825 }
826
827 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
828 tfilter_notify_chain(net, skb, q, parent, n,
829 chain, RTM_DELTFILTER);
830 tcf_chain_flush(chain);
831 err = 0;
832 goto errout;
833 }
834
835 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
836 prio, prio_allocate);
837 if (IS_ERR(tp)) {
838 err = PTR_ERR(tp);
839 goto errout;
840 }
841
842 if (tp == NULL) {
843 /* Proto-tcf does not exist, create new one */
844
845 if (tca[TCA_KIND] == NULL || !protocol) {
846 err = -EINVAL;
847 goto errout;
848 }
849
850 if (n->nlmsg_type != RTM_NEWTFILTER ||
851 !(n->nlmsg_flags & NLM_F_CREATE)) {
852 err = -ENOENT;
853 goto errout;
854 }
855
856 if (prio_allocate)
857 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
858
859 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
860 protocol, prio, parent, q, chain);
861 if (IS_ERR(tp)) {
862 err = PTR_ERR(tp);
863 goto errout;
864 }
865 tp_created = 1;
866 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
867 err = -EINVAL;
868 goto errout;
869 }
870
871 fh = tp->ops->get(tp, t->tcm_handle);
872
873 if (!fh) {
874 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
875 tcf_chain_tp_remove(chain, &chain_info, tp);
876 tfilter_notify(net, skb, n, tp, q, parent, fh,
877 RTM_DELTFILTER, false);
878 tcf_proto_destroy(tp);
879 err = 0;
880 goto errout;
881 }
882
883 if (n->nlmsg_type != RTM_NEWTFILTER ||
884 !(n->nlmsg_flags & NLM_F_CREATE)) {
885 err = -ENOENT;
886 goto errout;
887 }
888 } else {
889 bool last;
890
891 switch (n->nlmsg_type) {
892 case RTM_NEWTFILTER:
893 if (n->nlmsg_flags & NLM_F_EXCL) {
894 if (tp_created)
895 tcf_proto_destroy(tp);
896 err = -EEXIST;
897 goto errout;
898 }
899 break;
900 case RTM_DELTFILTER:
901 err = tfilter_del_notify(net, skb, n, tp, q, parent,
902 fh, false, &last);
903 if (err)
904 goto errout;
905 if (last) {
906 tcf_chain_tp_remove(chain, &chain_info, tp);
907 tcf_proto_destroy(tp);
908 }
909 goto errout;
910 case RTM_GETTFILTER:
911 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
912 RTM_NEWTFILTER, true);
913 goto errout;
914 default:
915 err = -EINVAL;
916 goto errout;
917 }
918 }
919
920 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
921 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
922 if (err == 0) {
923 if (tp_created)
924 tcf_chain_tp_insert(chain, &chain_info, tp);
925 tfilter_notify(net, skb, n, tp, q, parent, fh,
926 RTM_NEWTFILTER, false);
927 } else {
928 if (tp_created)
929 tcf_proto_destroy(tp);
930 }
931
932errout:
933 if (chain)
934 tcf_chain_put(chain);
935 if (err == -EAGAIN)
936 /* Replay the request. */
937 goto replay;
938 return err;
939}
940
941struct tcf_dump_args {
942 struct tcf_walker w;
943 struct sk_buff *skb;
944 struct netlink_callback *cb;
945 struct Qdisc *q;
946 u32 parent;
947};
948
949static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
950{
951 struct tcf_dump_args *a = (void *)arg;
952 struct net *net = sock_net(a->skb->sk);
953
954 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
955 n, NETLINK_CB(a->cb->skb).portid,
956 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
957 RTM_NEWTFILTER);
958}
959
960static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
961 struct sk_buff *skb, struct netlink_callback *cb,
962 long index_start, long *p_index)
963{
964 struct net *net = sock_net(skb->sk);
965 struct tcmsg *tcm = nlmsg_data(cb->nlh);
966 struct tcf_dump_args arg;
967 struct tcf_proto *tp;
968
969 for (tp = rtnl_dereference(chain->filter_chain);
970 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
971 if (*p_index < index_start)
972 continue;
973 if (TC_H_MAJ(tcm->tcm_info) &&
974 TC_H_MAJ(tcm->tcm_info) != tp->prio)
975 continue;
976 if (TC_H_MIN(tcm->tcm_info) &&
977 TC_H_MIN(tcm->tcm_info) != tp->protocol)
978 continue;
979 if (*p_index > index_start)
980 memset(&cb->args[1], 0,
981 sizeof(cb->args) - sizeof(cb->args[0]));
982 if (cb->args[1] == 0) {
983 if (tcf_fill_node(net, skb, tp, q, parent, 0,
984 NETLINK_CB(cb->skb).portid,
985 cb->nlh->nlmsg_seq, NLM_F_MULTI,
986 RTM_NEWTFILTER) <= 0)
987 return false;
988
989 cb->args[1] = 1;
990 }
991 if (!tp->ops->walk)
992 continue;
993 arg.w.fn = tcf_node_dump;
994 arg.skb = skb;
995 arg.cb = cb;
996 arg.q = q;
997 arg.parent = parent;
998 arg.w.stop = 0;
999 arg.w.skip = cb->args[1] - 1;
1000 arg.w.count = 0;
1001 tp->ops->walk(tp, &arg.w);
1002 cb->args[1] = arg.w.count + 1;
1003 if (arg.w.stop)
1004 return false;
1005 }
1006 return true;
1007}
1008
1009/* called with RTNL */
1010static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1011{
1012 struct net *net = sock_net(skb->sk);
1013 struct nlattr *tca[TCA_MAX + 1];
1014 struct net_device *dev;
1015 struct Qdisc *q;
1016 struct tcf_block *block;
1017 struct tcf_chain *chain;
1018 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1019 unsigned long cl = 0;
1020 const struct Qdisc_class_ops *cops;
1021 long index_start;
1022 long index;
1023 u32 parent;
1024 int err;
1025
1026 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1027 return skb->len;
1028
1029 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1030 if (err)
1031 return err;
1032
1033 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1034 if (!dev)
1035 return skb->len;
1036
1037 parent = tcm->tcm_parent;
1038 if (!parent) {
1039 q = dev->qdisc;
1040 parent = q->handle;
1041 } else {
1042 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1043 }
1044 if (!q)
1045 goto out;
1046 cops = q->ops->cl_ops;
1047 if (!cops)
1048 goto out;
1049 if (!cops->tcf_block)
1050 goto out;
1051 if (TC_H_MIN(tcm->tcm_parent)) {
1052 cl = cops->find(q, tcm->tcm_parent);
1053 if (cl == 0)
1054 goto out;
1055 }
1056 block = cops->tcf_block(q, cl);
1057 if (!block)
1058 goto out;
1059
1060 index_start = cb->args[0];
1061 index = 0;
1062
1063 list_for_each_entry(chain, &block->chain_list, list) {
1064 if (tca[TCA_CHAIN] &&
1065 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1066 continue;
1067 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1068 index_start, &index))
1069 break;
1070 }
1071
1072 cb->args[0] = index;
1073
1074out:
1075 return skb->len;
1076}
1077
1078void tcf_exts_destroy(struct tcf_exts *exts)
1079{
1080#ifdef CONFIG_NET_CLS_ACT
1081 LIST_HEAD(actions);
1082
1083 ASSERT_RTNL();
1084 tcf_exts_to_list(exts, &actions);
1085 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1086 kfree(exts->actions);
1087 exts->nr_actions = 0;
1088#endif
1089}
1090EXPORT_SYMBOL(tcf_exts_destroy);
1091
1092int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1093 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1094{
1095#ifdef CONFIG_NET_CLS_ACT
1096 {
1097 struct tc_action *act;
1098
1099 if (exts->police && tb[exts->police]) {
1100 act = tcf_action_init_1(net, tp, tb[exts->police],
1101 rate_tlv, "police", ovr,
1102 TCA_ACT_BIND);
1103 if (IS_ERR(act))
1104 return PTR_ERR(act);
1105
1106 act->type = exts->type = TCA_OLD_COMPAT;
1107 exts->actions[0] = act;
1108 exts->nr_actions = 1;
1109 } else if (exts->action && tb[exts->action]) {
1110 LIST_HEAD(actions);
1111 int err, i = 0;
1112
1113 err = tcf_action_init(net, tp, tb[exts->action],
1114 rate_tlv, NULL, ovr, TCA_ACT_BIND,
1115 &actions);
1116 if (err)
1117 return err;
1118 list_for_each_entry(act, &actions, list)
1119 exts->actions[i++] = act;
1120 exts->nr_actions = i;
1121 }
1122 exts->net = net;
1123 }
1124#else
1125 if ((exts->action && tb[exts->action]) ||
1126 (exts->police && tb[exts->police]))
1127 return -EOPNOTSUPP;
1128#endif
1129
1130 return 0;
1131}
1132EXPORT_SYMBOL(tcf_exts_validate);
1133
1134void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1135{
1136#ifdef CONFIG_NET_CLS_ACT
1137 struct tcf_exts old = *dst;
1138
1139 *dst = *src;
1140 tcf_exts_destroy(&old);
1141#endif
1142}
1143EXPORT_SYMBOL(tcf_exts_change);
1144
1145#ifdef CONFIG_NET_CLS_ACT
1146static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1147{
1148 if (exts->nr_actions == 0)
1149 return NULL;
1150 else
1151 return exts->actions[0];
1152}
1153#endif
1154
1155int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1156{
1157#ifdef CONFIG_NET_CLS_ACT
1158 struct nlattr *nest;
1159
1160 if (exts->action && tcf_exts_has_actions(exts)) {
1161 /*
1162 * again for backward compatible mode - we want
1163 * to work with both old and new modes of entering
1164 * tc data even if iproute2 was newer - jhs
1165 */
1166 if (exts->type != TCA_OLD_COMPAT) {
1167 LIST_HEAD(actions);
1168
1169 nest = nla_nest_start(skb, exts->action);
1170 if (nest == NULL)
1171 goto nla_put_failure;
1172
1173 tcf_exts_to_list(exts, &actions);
1174 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1175 goto nla_put_failure;
1176 nla_nest_end(skb, nest);
1177 } else if (exts->police) {
1178 struct tc_action *act = tcf_exts_first_act(exts);
1179 nest = nla_nest_start(skb, exts->police);
1180 if (nest == NULL || !act)
1181 goto nla_put_failure;
1182 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1183 goto nla_put_failure;
1184 nla_nest_end(skb, nest);
1185 }
1186 }
1187 return 0;
1188
1189nla_put_failure:
1190 nla_nest_cancel(skb, nest);
1191 return -1;
1192#else
1193 return 0;
1194#endif
1195}
1196EXPORT_SYMBOL(tcf_exts_dump);
1197
1198
1199int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1200{
1201#ifdef CONFIG_NET_CLS_ACT
1202 struct tc_action *a = tcf_exts_first_act(exts);
1203 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1204 return -1;
1205#endif
1206 return 0;
1207}
1208EXPORT_SYMBOL(tcf_exts_dump_stats);
1209
1210static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1211 enum tc_setup_type type,
1212 void *type_data, bool err_stop)
1213{
1214 int ok_count = 0;
1215#ifdef CONFIG_NET_CLS_ACT
1216 const struct tc_action *a;
1217 struct net_device *dev;
1218 int i, ret;
1219
1220 if (!tcf_exts_has_actions(exts))
1221 return 0;
1222
1223 for (i = 0; i < exts->nr_actions; i++) {
1224 a = exts->actions[i];
1225 if (!a->ops->get_dev)
1226 continue;
1227 dev = a->ops->get_dev(a);
1228 if (!dev)
1229 continue;
1230 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1231 if (ret < 0)
1232 return ret;
1233 ok_count += ret;
1234 }
1235#endif
1236 return ok_count;
1237}
1238
1239int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1240 enum tc_setup_type type, void *type_data, bool err_stop)
1241{
1242 int ok_count;
1243 int ret;
1244
1245 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1246 if (ret < 0)
1247 return ret;
1248 ok_count = ret;
1249
1250 if (!exts)
1251 return ok_count;
1252 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1253 if (ret < 0)
1254 return ret;
1255 ok_count += ret;
1256
1257 return ok_count;
1258}
1259EXPORT_SYMBOL(tc_setup_cb_call);
1260
1261static int __init tc_filter_init(void)
1262{
1263 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1264 if (!tc_filter_wq)
1265 return -ENOMEM;
1266
1267 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1268 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1269 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1270 tc_dump_tfilter, 0);
1271
1272 return 0;
1273}
1274
1275subsys_initcall(tc_filter_init);