Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.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 <asm/uaccess.h>
18 #include <asm/system.h>
19 #include <linux/bitops.h>
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/in.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/init.h>
36 #include <linux/kmod.h>
37 #include <net/sock.h>
38 #include <net/pkt_sched.h>
39 #include <net/pkt_cls.h>
40
41 #if 0 /* control */
42 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
43 #else
44 #define DPRINTK(format,args...)
45 #endif
46
47 /* The list of all installed classifier types */
48
49 static struct tcf_proto_ops *tcf_proto_base;
50
51 /* Protects list of registered TC modules. It is pure SMP lock. */
52 static DEFINE_RWLOCK(cls_mod_lock);
53
54 /* Find classifier type by string name */
55
56 static struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
57 {
58         struct tcf_proto_ops *t = NULL;
59
60         if (kind) {
61                 read_lock(&cls_mod_lock);
62                 for (t = tcf_proto_base; t; t = t->next) {
63                         if (rtattr_strcmp(kind, t->kind) == 0) {
64                                 if (!try_module_get(t->owner))
65                                         t = NULL;
66                                 break;
67                         }
68                 }
69                 read_unlock(&cls_mod_lock);
70         }
71         return t;
72 }
73
74 /* Register(unregister) new classifier type */
75
76 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
77 {
78         struct tcf_proto_ops *t, **tp;
79         int rc = -EEXIST;
80
81         write_lock(&cls_mod_lock);
82         for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
83                 if (!strcmp(ops->kind, t->kind))
84                         goto out;
85
86         ops->next = NULL;
87         *tp = ops;
88         rc = 0;
89 out:
90         write_unlock(&cls_mod_lock);
91         return rc;
92 }
93
94 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
95 {
96         struct tcf_proto_ops *t, **tp;
97         int rc = -ENOENT;
98
99         write_lock(&cls_mod_lock);
100         for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
101                 if (t == ops)
102                         break;
103
104         if (!t)
105                 goto out;
106         *tp = t->next;
107         rc = 0;
108 out:
109         write_unlock(&cls_mod_lock);
110         return rc;
111 }
112
113 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
114                           struct tcf_proto *tp, unsigned long fh, int event);
115
116
117 /* Select new prio value from the range, managed by kernel. */
118
119 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
120 {
121         u32 first = TC_H_MAKE(0xC0000000U,0U);
122
123         if (tp)
124                 first = tp->prio-1;
125
126         return first;
127 }
128
129 /* Add/change/delete/get a filter node */
130
131 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
132 {
133         struct rtattr **tca;
134         struct tcmsg *t;
135         u32 protocol;
136         u32 prio;
137         u32 nprio;
138         u32 parent;
139         struct net_device *dev;
140         struct Qdisc  *q;
141         struct tcf_proto **back, **chain;
142         struct tcf_proto *tp;
143         struct tcf_proto_ops *tp_ops;
144         struct Qdisc_class_ops *cops;
145         unsigned long cl;
146         unsigned long fh;
147         int err;
148
149 replay:
150         tca = arg;
151         t = NLMSG_DATA(n);
152         protocol = TC_H_MIN(t->tcm_info);
153         prio = TC_H_MAJ(t->tcm_info);
154         nprio = prio;
155         parent = t->tcm_parent;
156         cl = 0;
157
158         if (prio == 0) {
159                 /* If no priority is given, user wants we allocated it. */
160                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
161                         return -ENOENT;
162                 prio = TC_H_MAKE(0x80000000U,0U);
163         }
164
165         /* Find head of filter chain. */
166
167         /* Find link */
168         if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
169                 return -ENODEV;
170
171         /* Find qdisc */
172         if (!parent) {
173                 q = dev->qdisc_sleeping;
174                 parent = q->handle;
175         } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
176                 return -EINVAL;
177
178         /* Is it classful? */
179         if ((cops = q->ops->cl_ops) == NULL)
180                 return -EINVAL;
181
182         /* Do we search for filter, attached to class? */
183         if (TC_H_MIN(parent)) {
184                 cl = cops->get(q, parent);
185                 if (cl == 0)
186                         return -ENOENT;
187         }
188
189         /* And the last stroke */
190         chain = cops->tcf_chain(q, cl);
191         err = -EINVAL;
192         if (chain == NULL)
193                 goto errout;
194
195         /* Check the chain for existence of proto-tcf with this priority */
196         for (back = chain; (tp=*back) != NULL; back = &tp->next) {
197                 if (tp->prio >= prio) {
198                         if (tp->prio == prio) {
199                                 if (!nprio || (tp->protocol != protocol && protocol))
200                                         goto errout;
201                         } else
202                                 tp = NULL;
203                         break;
204                 }
205         }
206
207         if (tp == NULL) {
208                 /* Proto-tcf does not exist, create new one */
209
210                 if (tca[TCA_KIND-1] == NULL || !protocol)
211                         goto errout;
212
213                 err = -ENOENT;
214                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
215                         goto errout;
216
217
218                 /* Create new proto tcf */
219
220                 err = -ENOBUFS;
221                 if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
222                         goto errout;
223                 err = -EINVAL;
224                 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
225                 if (tp_ops == NULL) {
226 #ifdef CONFIG_KMOD
227                         struct rtattr *kind = tca[TCA_KIND-1];
228                         char name[IFNAMSIZ];
229
230                         if (kind != NULL &&
231                             rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
232                                 rtnl_unlock();
233                                 request_module("cls_%s", name);
234                                 rtnl_lock();
235                                 tp_ops = tcf_proto_lookup_ops(kind);
236                                 /* We dropped the RTNL semaphore in order to
237                                  * perform the module load.  So, even if we
238                                  * succeeded in loading the module we have to
239                                  * replay the request.  We indicate this using
240                                  * -EAGAIN.
241                                  */
242                                 if (tp_ops != NULL) {
243                                         module_put(tp_ops->owner);
244                                         err = -EAGAIN;
245                                 }
246                         }
247 #endif
248                         kfree(tp);
249                         goto errout;
250                 }
251                 memset(tp, 0, sizeof(*tp));
252                 tp->ops = tp_ops;
253                 tp->protocol = protocol;
254                 tp->prio = nprio ? : tcf_auto_prio(*back);
255                 tp->q = q;
256                 tp->classify = tp_ops->classify;
257                 tp->classid = parent;
258                 if ((err = tp_ops->init(tp)) != 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-1] && rtattr_strcmp(tca[TCA_KIND-1], 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 || !(n->nlmsg_flags&NLM_F_CREATE))
288                         goto errout;
289         } else {
290                 switch (n->nlmsg_type) {
291                 case RTM_NEWTFILTER:    
292                         err = -EEXIST;
293                         if (n->nlmsg_flags&NLM_F_EXCL)
294                                 goto errout;
295                         break;
296                 case RTM_DELTFILTER:
297                         err = tp->ops->delete(tp, fh);
298                         if (err == 0)
299                                 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
300                         goto errout;
301                 case RTM_GETTFILTER:
302                         err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
303                         goto errout;
304                 default:
305                         err = -EINVAL;
306                         goto errout;
307                 }
308         }
309
310         err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
311         if (err == 0)
312                 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
313
314 errout:
315         if (cl)
316                 cops->put(q, cl);
317         if (err == -EAGAIN)
318                 /* Replay the request. */
319                 goto replay;
320         return err;
321 }
322
323 static int
324 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
325               u32 pid, u32 seq, u16 flags, int event)
326 {
327         struct tcmsg *tcm;
328         struct nlmsghdr  *nlh;
329         unsigned char    *b = skb->tail;
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         RTA_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 rtattr_failure;
345         }
346         nlh->nlmsg_len = skb->tail - b;
347         return skb->len;
348
349 nlmsg_failure:
350 rtattr_failure:
351         skb_trim(skb, b - skb->data);
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, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
371 }
372
373 struct tcf_dump_args
374 {
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, struct tcf_walker *arg)
381 {
382         struct tcf_dump_args *a = (void*)arg;
383
384         return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
385                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
386 }
387
388 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
389 {
390         int t;
391         int s_t;
392         struct net_device *dev;
393         struct Qdisc *q;
394         struct tcf_proto *tp, **chain;
395         struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
396         unsigned long cl = 0;
397         struct Qdisc_class_ops *cops;
398         struct tcf_dump_args arg;
399
400         if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
401                 return skb->len;
402         if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
403                 return skb->len;
404
405         read_lock_bh(&qdisc_tree_lock);
406         if (!tcm->tcm_parent)
407                 q = dev->qdisc_sleeping;
408         else
409                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
410         if (!q)
411                 goto out;
412         if ((cops = q->ops->cl_ops) == NULL)
413                 goto errout;
414         if (TC_H_MIN(tcm->tcm_parent)) {
415                 cl = cops->get(q, tcm->tcm_parent);
416                 if (cl == 0)
417                         goto errout;
418         }
419         chain = cops->tcf_chain(q, cl);
420         if (chain == NULL)
421                 goto errout;
422
423         s_t = cb->args[0];
424
425         for (tp=*chain, t=0; tp; tp = tp->next, t++) {
426                 if (t < s_t) continue;
427                 if (TC_H_MAJ(tcm->tcm_info) &&
428                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
429                         continue;
430                 if (TC_H_MIN(tcm->tcm_info) &&
431                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
432                         continue;
433                 if (t > s_t)
434                         memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
435                 if (cb->args[1] == 0) {
436                         if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
437                                           cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
438                                 break;
439                         }
440                         cb->args[1] = 1;
441                 }
442                 if (tp->ops->walk == NULL)
443                         continue;
444                 arg.w.fn = tcf_node_dump;
445                 arg.skb = skb;
446                 arg.cb = cb;
447                 arg.w.stop = 0;
448                 arg.w.skip = cb->args[1]-1;
449                 arg.w.count = 0;
450                 tp->ops->walk(tp, &arg.w);
451                 cb->args[1] = arg.w.count+1;
452                 if (arg.w.stop)
453                         break;
454         }
455
456         cb->args[0] = t;
457
458 errout:
459         if (cl)
460                 cops->put(q, cl);
461 out:
462         read_unlock_bh(&qdisc_tree_lock);
463         dev_put(dev);
464         return skb->len;
465 }
466
467 void
468 tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
469 {
470 #ifdef CONFIG_NET_CLS_ACT
471         if (exts->action) {
472                 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
473                 exts->action = NULL;
474         }
475 #elif defined CONFIG_NET_CLS_POLICE
476         if (exts->police) {
477                 tcf_police_release(exts->police, TCA_ACT_UNBIND);
478                 exts->police = NULL;
479         }
480 #endif
481 }
482
483
484 int
485 tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
486                   struct rtattr *rate_tlv, struct tcf_exts *exts,
487                   struct tcf_ext_map *map)
488 {
489         memset(exts, 0, sizeof(*exts));
490         
491 #ifdef CONFIG_NET_CLS_ACT
492         {
493                 int err;
494                 struct tc_action *act;
495
496                 if (map->police && tb[map->police-1]) {
497                         act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
498                                 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
499                         if (act == NULL)
500                                 return err;
501
502                         act->type = TCA_OLD_COMPAT;
503                         exts->action = act;
504                 } else if (map->action && tb[map->action-1]) {
505                         act = tcf_action_init(tb[map->action-1], 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 #elif defined CONFIG_NET_CLS_POLICE
514         if (map->police && tb[map->police-1]) {
515                 struct tcf_police *p;
516                 
517                 p = tcf_police_locate(tb[map->police-1], rate_tlv);
518                 if (p == NULL)
519                         return -EINVAL;
520
521                 exts->police = p;
522         } else if (map->action && tb[map->action-1])
523                 return -EOPNOTSUPP;
524 #else
525         if ((map->action && tb[map->action-1]) ||
526             (map->police && tb[map->police-1]))
527                 return -EOPNOTSUPP;
528 #endif
529
530         return 0;
531 }
532
533 void
534 tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
535                 struct tcf_exts *src)
536 {
537 #ifdef CONFIG_NET_CLS_ACT
538         if (src->action) {
539                 struct tc_action *act;
540                 tcf_tree_lock(tp);
541                 act = xchg(&dst->action, src->action);
542                 tcf_tree_unlock(tp);
543                 if (act)
544                         tcf_action_destroy(act, TCA_ACT_UNBIND);
545         }
546 #elif defined CONFIG_NET_CLS_POLICE
547         if (src->police) {
548                 struct tcf_police *p;
549                 tcf_tree_lock(tp);
550                 p = xchg(&dst->police, src->police);
551                 tcf_tree_unlock(tp);
552                 if (p)
553                         tcf_police_release(p, TCA_ACT_UNBIND);
554         }
555 #endif
556 }
557
558 int
559 tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
560               struct tcf_ext_map *map)
561 {
562 #ifdef CONFIG_NET_CLS_ACT
563         if (map->action && exts->action) {
564                 /*
565                  * again for backward compatible mode - we want
566                  * to work with both old and new modes of entering
567                  * tc data even if iproute2  was newer - jhs
568                  */
569                 struct rtattr * p_rta = (struct rtattr*) skb->tail;
570
571                 if (exts->action->type != TCA_OLD_COMPAT) {
572                         RTA_PUT(skb, map->action, 0, NULL);
573                         if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
574                                 goto rtattr_failure;
575                         p_rta->rta_len = skb->tail - (u8*)p_rta;
576                 } else if (map->police) {
577                         RTA_PUT(skb, map->police, 0, NULL);
578                         if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
579                                 goto rtattr_failure;
580                         p_rta->rta_len = skb->tail - (u8*)p_rta;
581                 }
582         }
583 #elif defined CONFIG_NET_CLS_POLICE
584         if (map->police && exts->police) {
585                 struct rtattr * p_rta = (struct rtattr*) skb->tail;
586
587                 RTA_PUT(skb, map->police, 0, NULL);
588
589                 if (tcf_police_dump(skb, exts->police) < 0)
590                         goto rtattr_failure;
591
592                 p_rta->rta_len = skb->tail - (u8*)p_rta;
593         }
594 #endif
595         return 0;
596 rtattr_failure: __attribute__ ((unused))
597         return -1;
598 }
599
600 int
601 tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
602                     struct tcf_ext_map *map)
603 {
604 #ifdef CONFIG_NET_CLS_ACT
605         if (exts->action)
606                 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
607                         goto rtattr_failure;
608 #elif defined CONFIG_NET_CLS_POLICE
609         if (exts->police)
610                 if (tcf_police_dump_stats(skb, exts->police) < 0)
611                         goto rtattr_failure;
612 #endif
613         return 0;
614 rtattr_failure: __attribute__ ((unused))
615         return -1;
616 }
617
618 static int __init tc_filter_init(void)
619 {
620         struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
621
622         /* Setup rtnetlink links. It is made here to avoid
623            exporting large number of public symbols.
624          */
625
626         if (link_p) {
627                 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
628                 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
629                 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
630                 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
631         }
632         return 0;
633 }
634
635 subsys_initcall(tc_filter_init);
636
637 EXPORT_SYMBOL(register_tcf_proto_ops);
638 EXPORT_SYMBOL(unregister_tcf_proto_ops);
639 EXPORT_SYMBOL(tcf_exts_validate);
640 EXPORT_SYMBOL(tcf_exts_destroy);
641 EXPORT_SYMBOL(tcf_exts_change);
642 EXPORT_SYMBOL(tcf_exts_dump);
643 EXPORT_SYMBOL(tcf_exts_dump_stats);