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