[NET_SCHED]: Convert classifiers from rtnetlink to new netlink API
[safe/jmp/linux-2.6] / 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/skbuff.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/netlink.h>
26 #include <net/net_namespace.h>
27 #include <net/sock.h>
28 #include <net/netlink.h>
29 #include <net/pkt_sched.h>
30 #include <net/pkt_cls.h>
31
32 /* The list of all installed classifier types */
33
34 static struct tcf_proto_ops *tcf_proto_base __read_mostly;
35
36 /* Protects list of registered TC modules. It is pure SMP lock. */
37 static DEFINE_RWLOCK(cls_mod_lock);
38
39 /* Find classifier type by string name */
40
41 static struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
42 {
43         struct tcf_proto_ops *t = NULL;
44
45         if (kind) {
46                 read_lock(&cls_mod_lock);
47                 for (t = tcf_proto_base; t; t = t->next) {
48                         if (nla_strcmp(kind, t->kind) == 0) {
49                                 if (!try_module_get(t->owner))
50                                         t = NULL;
51                                 break;
52                         }
53                 }
54                 read_unlock(&cls_mod_lock);
55         }
56         return t;
57 }
58
59 /* Register(unregister) new classifier type */
60
61 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
62 {
63         struct tcf_proto_ops *t, **tp;
64         int rc = -EEXIST;
65
66         write_lock(&cls_mod_lock);
67         for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
68                 if (!strcmp(ops->kind, t->kind))
69                         goto out;
70
71         ops->next = NULL;
72         *tp = ops;
73         rc = 0;
74 out:
75         write_unlock(&cls_mod_lock);
76         return rc;
77 }
78 EXPORT_SYMBOL(register_tcf_proto_ops);
79
80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81 {
82         struct tcf_proto_ops *t, **tp;
83         int rc = -ENOENT;
84
85         write_lock(&cls_mod_lock);
86         for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
87                 if (t == ops)
88                         break;
89
90         if (!t)
91                 goto out;
92         *tp = t->next;
93         rc = 0;
94 out:
95         write_unlock(&cls_mod_lock);
96         return rc;
97 }
98 EXPORT_SYMBOL(unregister_tcf_proto_ops);
99
100 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
101                           struct tcf_proto *tp, unsigned long fh, int event);
102
103
104 /* Select new prio value from the range, managed by kernel. */
105
106 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
107 {
108         u32 first = TC_H_MAKE(0xC0000000U, 0U);
109
110         if (tp)
111                 first = tp->prio-1;
112
113         return first;
114 }
115
116 /* Add/change/delete/get a filter node */
117
118 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
119 {
120         struct net *net = skb->sk->sk_net;
121         struct nlattr *tca[TCA_MAX + 1];
122         struct tcmsg *t;
123         u32 protocol;
124         u32 prio;
125         u32 nprio;
126         u32 parent;
127         struct net_device *dev;
128         struct Qdisc  *q;
129         struct tcf_proto **back, **chain;
130         struct tcf_proto *tp;
131         struct tcf_proto_ops *tp_ops;
132         const struct Qdisc_class_ops *cops;
133         unsigned long cl;
134         unsigned long fh;
135         int err;
136
137         if (net != &init_net)
138                 return -EINVAL;
139
140 replay:
141         t = NLMSG_DATA(n);
142         protocol = TC_H_MIN(t->tcm_info);
143         prio = TC_H_MAJ(t->tcm_info);
144         nprio = prio;
145         parent = t->tcm_parent;
146         cl = 0;
147
148         if (prio == 0) {
149                 /* If no priority is given, user wants we allocated it. */
150                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
151                         return -ENOENT;
152                 prio = TC_H_MAKE(0x80000000U, 0U);
153         }
154
155         /* Find head of filter chain. */
156
157         /* Find link */
158         dev = __dev_get_by_index(&init_net, t->tcm_ifindex);
159         if (dev == NULL)
160                 return -ENODEV;
161
162         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
163         if (err < 0)
164                 return err;
165
166         /* Find qdisc */
167         if (!parent) {
168                 q = dev->qdisc_sleeping;
169                 parent = q->handle;
170         } else {
171                 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
172                 if (q == NULL)
173                         return -EINVAL;
174         }
175
176         /* Is it classful? */
177         if ((cops = q->ops->cl_ops) == NULL)
178                 return -EINVAL;
179
180         /* Do we search for filter, attached to class? */
181         if (TC_H_MIN(parent)) {
182                 cl = cops->get(q, parent);
183                 if (cl == 0)
184                         return -ENOENT;
185         }
186
187         /* And the last stroke */
188         chain = cops->tcf_chain(q, cl);
189         err = -EINVAL;
190         if (chain == NULL)
191                 goto errout;
192
193         /* Check the chain for existence of proto-tcf with this priority */
194         for (back = chain; (tp=*back) != NULL; back = &tp->next) {
195                 if (tp->prio >= prio) {
196                         if (tp->prio == prio) {
197                                 if (!nprio || (tp->protocol != protocol && protocol))
198                                         goto errout;
199                         } else
200                                 tp = NULL;
201                         break;
202                 }
203         }
204
205         if (tp == NULL) {
206                 /* Proto-tcf does not exist, create new one */
207
208                 if (tca[TCA_KIND] == NULL || !protocol)
209                         goto errout;
210
211                 err = -ENOENT;
212                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
213                         goto errout;
214
215
216                 /* Create new proto tcf */
217
218                 err = -ENOBUFS;
219                 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
220                 if (tp == NULL)
221                         goto errout;
222                 err = -EINVAL;
223                 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
224                 if (tp_ops == NULL) {
225 #ifdef CONFIG_KMOD
226                         struct nlattr *kind = tca[TCA_KIND];
227                         char name[IFNAMSIZ];
228
229                         if (kind != NULL &&
230                             nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
231                                 rtnl_unlock();
232                                 request_module("cls_%s", name);
233                                 rtnl_lock();
234                                 tp_ops = tcf_proto_lookup_ops(kind);
235                                 /* We dropped the RTNL semaphore in order to
236                                  * perform the module load.  So, even if we
237                                  * succeeded in loading the module we have to
238                                  * replay the request.  We indicate this using
239                                  * -EAGAIN.
240                                  */
241                                 if (tp_ops != NULL) {
242                                         module_put(tp_ops->owner);
243                                         err = -EAGAIN;
244                                 }
245                         }
246 #endif
247                         kfree(tp);
248                         goto errout;
249                 }
250                 tp->ops = tp_ops;
251                 tp->protocol = protocol;
252                 tp->prio = nprio ? : tcf_auto_prio(*back);
253                 tp->q = q;
254                 tp->classify = tp_ops->classify;
255                 tp->classid = parent;
256
257                 err = tp_ops->init(tp);
258                 if (err != 0) {
259                         module_put(tp_ops->owner);
260                         kfree(tp);
261                         goto errout;
262                 }
263
264                 qdisc_lock_tree(dev);
265                 tp->next = *back;
266                 *back = tp;
267                 qdisc_unlock_tree(dev);
268
269         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
270                 goto errout;
271
272         fh = tp->ops->get(tp, t->tcm_handle);
273
274         if (fh == 0) {
275                 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
276                         qdisc_lock_tree(dev);
277                         *back = tp->next;
278                         qdisc_unlock_tree(dev);
279
280                         tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
281                         tcf_destroy(tp);
282                         err = 0;
283                         goto errout;
284                 }
285
286                 err = -ENOENT;
287                 if (n->nlmsg_type != RTM_NEWTFILTER ||
288                     !(n->nlmsg_flags & NLM_F_CREATE))
289                         goto errout;
290         } else {
291                 switch (n->nlmsg_type) {
292                 case RTM_NEWTFILTER:
293                         err = -EEXIST;
294                         if (n->nlmsg_flags & NLM_F_EXCL)
295                                 goto errout;
296                         break;
297                 case RTM_DELTFILTER:
298                         err = tp->ops->delete(tp, fh);
299                         if (err == 0)
300                                 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
301                         goto errout;
302                 case RTM_GETTFILTER:
303                         err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
304                         goto errout;
305                 default:
306                         err = -EINVAL;
307                         goto errout;
308                 }
309         }
310
311         err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
312         if (err == 0)
313                 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
314
315 errout:
316         if (cl)
317                 cops->put(q, cl);
318         if (err == -EAGAIN)
319                 /* Replay the request. */
320                 goto replay;
321         return err;
322 }
323
324 static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
325                          unsigned long fh, u32 pid, u32 seq, u16 flags, int event)
326 {
327         struct tcmsg *tcm;
328         struct nlmsghdr  *nlh;
329         unsigned char *b = skb_tail_pointer(skb);
330
331         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
332         tcm = NLMSG_DATA(nlh);
333         tcm->tcm_family = AF_UNSPEC;
334         tcm->tcm__pad1 = 0;
335         tcm->tcm__pad1 = 0;
336         tcm->tcm_ifindex = tp->q->dev->ifindex;
337         tcm->tcm_parent = tp->classid;
338         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
339         NLA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
340         tcm->tcm_handle = fh;
341         if (RTM_DELTFILTER != event) {
342                 tcm->tcm_handle = 0;
343                 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
344                         goto nla_put_failure;
345         }
346         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
347         return skb->len;
348
349 nlmsg_failure:
350 nla_put_failure:
351         nlmsg_trim(skb, b);
352         return -1;
353 }
354
355 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
356                           struct tcf_proto *tp, unsigned long fh, int event)
357 {
358         struct sk_buff *skb;
359         u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
360
361         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
362         if (!skb)
363                 return -ENOBUFS;
364
365         if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
366                 kfree_skb(skb);
367                 return -EINVAL;
368         }
369
370         return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
371                               n->nlmsg_flags & NLM_F_ECHO);
372 }
373
374 struct tcf_dump_args {
375         struct tcf_walker w;
376         struct sk_buff *skb;
377         struct netlink_callback *cb;
378 };
379
380 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
381                          struct tcf_walker *arg)
382 {
383         struct tcf_dump_args *a = (void *)arg;
384
385         return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
386                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
387 }
388
389 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
390 {
391         struct net *net = skb->sk->sk_net;
392         int t;
393         int s_t;
394         struct net_device *dev;
395         struct Qdisc *q;
396         struct tcf_proto *tp, **chain;
397         struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(cb->nlh);
398         unsigned long cl = 0;
399         const struct Qdisc_class_ops *cops;
400         struct tcf_dump_args arg;
401
402         if (net != &init_net)
403                 return 0;
404
405         if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
406                 return skb->len;
407         if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
408                 return skb->len;
409
410         if (!tcm->tcm_parent)
411                 q = dev->qdisc_sleeping;
412         else
413                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
414         if (!q)
415                 goto out;
416         if ((cops = q->ops->cl_ops) == NULL)
417                 goto errout;
418         if (TC_H_MIN(tcm->tcm_parent)) {
419                 cl = cops->get(q, tcm->tcm_parent);
420                 if (cl == 0)
421                         goto errout;
422         }
423         chain = cops->tcf_chain(q, cl);
424         if (chain == NULL)
425                 goto errout;
426
427         s_t = cb->args[0];
428
429         for (tp=*chain, t=0; tp; tp = tp->next, t++) {
430                 if (t < s_t) continue;
431                 if (TC_H_MAJ(tcm->tcm_info) &&
432                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
433                         continue;
434                 if (TC_H_MIN(tcm->tcm_info) &&
435                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
436                         continue;
437                 if (t > s_t)
438                         memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
439                 if (cb->args[1] == 0) {
440                         if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
441                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
442                                           RTM_NEWTFILTER) <= 0)
443                                 break;
444
445                         cb->args[1] = 1;
446                 }
447                 if (tp->ops->walk == NULL)
448                         continue;
449                 arg.w.fn = tcf_node_dump;
450                 arg.skb = skb;
451                 arg.cb = cb;
452                 arg.w.stop = 0;
453                 arg.w.skip = cb->args[1]-1;
454                 arg.w.count = 0;
455                 tp->ops->walk(tp, &arg.w);
456                 cb->args[1] = arg.w.count+1;
457                 if (arg.w.stop)
458                         break;
459         }
460
461         cb->args[0] = t;
462
463 errout:
464         if (cl)
465                 cops->put(q, cl);
466 out:
467         dev_put(dev);
468         return skb->len;
469 }
470
471 void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
472 {
473 #ifdef CONFIG_NET_CLS_ACT
474         if (exts->action) {
475                 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
476                 exts->action = NULL;
477         }
478 #endif
479 }
480 EXPORT_SYMBOL(tcf_exts_destroy);
481
482 int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb,
483                   struct nlattr *rate_tlv, struct tcf_exts *exts,
484                   struct tcf_ext_map *map)
485 {
486         memset(exts, 0, sizeof(*exts));
487
488 #ifdef CONFIG_NET_CLS_ACT
489         {
490                 int err;
491                 struct tc_action *act;
492
493                 if (map->police && tb[map->police]) {
494                         act = tcf_action_init_1((struct rtattr *)tb[map->police],
495                                                 (struct rtattr *)rate_tlv,
496                                                 "police", TCA_ACT_NOREPLACE,
497                                                 TCA_ACT_BIND, &err);
498                         if (act == NULL)
499                                 return err;
500
501                         act->type = TCA_OLD_COMPAT;
502                         exts->action = act;
503                 } else if (map->action && tb[map->action]) {
504                         act = tcf_action_init((struct rtattr *)tb[map->action],
505                                               (struct rtattr *)rate_tlv, NULL,
506                                 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
507                         if (act == NULL)
508                                 return err;
509
510                         exts->action = act;
511                 }
512         }
513 #else
514         if ((map->action && tb[map->action]) ||
515             (map->police && tb[map->police]))
516                 return -EOPNOTSUPP;
517 #endif
518
519         return 0;
520 }
521 EXPORT_SYMBOL(tcf_exts_validate);
522
523 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
524                      struct tcf_exts *src)
525 {
526 #ifdef CONFIG_NET_CLS_ACT
527         if (src->action) {
528                 struct tc_action *act;
529                 tcf_tree_lock(tp);
530                 act = xchg(&dst->action, src->action);
531                 tcf_tree_unlock(tp);
532                 if (act)
533                         tcf_action_destroy(act, TCA_ACT_UNBIND);
534         }
535 #endif
536 }
537 EXPORT_SYMBOL(tcf_exts_change);
538
539 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
540               struct tcf_ext_map *map)
541 {
542 #ifdef CONFIG_NET_CLS_ACT
543         if (map->action && exts->action) {
544                 /*
545                  * again for backward compatible mode - we want
546                  * to work with both old and new modes of entering
547                  * tc data even if iproute2  was newer - jhs
548                  */
549                 struct nlattr *p_rta = (struct nlattr *)skb_tail_pointer(skb);
550
551                 if (exts->action->type != TCA_OLD_COMPAT) {
552                         NLA_PUT(skb, map->action, 0, NULL);
553                         if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
554                                 goto nla_put_failure;
555                         p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
556                 } else if (map->police) {
557                         NLA_PUT(skb, map->police, 0, NULL);
558                         if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
559                                 goto nla_put_failure;
560                         p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
561                 }
562         }
563 #endif
564         return 0;
565 nla_put_failure: __attribute__ ((unused))
566         return -1;
567 }
568 EXPORT_SYMBOL(tcf_exts_dump);
569
570
571 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
572                         struct tcf_ext_map *map)
573 {
574 #ifdef CONFIG_NET_CLS_ACT
575         if (exts->action)
576                 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
577                         goto nla_put_failure;
578 #endif
579         return 0;
580 nla_put_failure: __attribute__ ((unused))
581         return -1;
582 }
583 EXPORT_SYMBOL(tcf_exts_dump_stats);
584
585 static int __init tc_filter_init(void)
586 {
587         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
588         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
589         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
590                                                  tc_dump_tfilter);
591
592         return 0;
593 }
594
595 subsys_initcall(tc_filter_init);