VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.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 <asm/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
40 #if 0 /* control */
41 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
42 #else
43 #define DPRINTK(format,args...)
44 #endif
45
46 /* The list of all installed classifier types */
47
48 static struct tcf_proto_ops *tcf_proto_base;
49
50 /* Protects list of registered TC modules. It is pure SMP lock. */
51 static rwlock_t cls_mod_lock = RW_LOCK_UNLOCKED;
52
53 /* Find classifier type by string name */
54
55 struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
56 {
57         struct tcf_proto_ops *t = NULL;
58
59         if (kind) {
60                 read_lock(&cls_mod_lock);
61                 for (t = tcf_proto_base; t; t = t->next) {
62                         if (rtattr_strcmp(kind, t->kind) == 0)
63                                 break;
64                 }
65                 read_unlock(&cls_mod_lock);
66         }
67         return t;
68 }
69
70 /* Register(unregister) new classifier type */
71
72 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
73 {
74         struct tcf_proto_ops *t, **tp;
75         int rc = -EEXIST;
76
77         write_lock(&cls_mod_lock);
78         for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
79                 if (!strcmp(ops->kind, t->kind))
80                         goto out;
81
82         ops->next = NULL;
83         *tp = ops;
84         rc = 0;
85 out:
86         write_unlock(&cls_mod_lock);
87         return rc;
88 }
89
90 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
91 {
92         struct tcf_proto_ops *t, **tp;
93         int rc = -ENOENT;
94
95         write_lock(&cls_mod_lock);
96         for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
97                 if (t == ops)
98                         break;
99
100         if (!t)
101                 goto out;
102         *tp = t->next;
103         rc = 0;
104 out:
105         write_unlock(&cls_mod_lock);
106         return rc;
107 }
108
109 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
110                           struct tcf_proto *tp, unsigned long fh, int event);
111
112
113 /* Select new prio value from the range, managed by kernel. */
114
115 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
116 {
117         u32 first = TC_H_MAKE(0xC0000000U,0U);
118
119         if (tp)
120                 first = tp->prio-1;
121
122         return first;
123 }
124
125 /* Add/change/delete/get a filter node */
126
127 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
128 {
129         struct rtattr **tca = arg;
130         struct tcmsg *t = NLMSG_DATA(n);
131         u32 protocol = TC_H_MIN(t->tcm_info);
132         u32 prio = TC_H_MAJ(t->tcm_info);
133         u32 nprio = prio;
134         u32 parent = t->tcm_parent;
135         struct net_device *dev;
136         struct Qdisc  *q;
137         struct tcf_proto **back, **chain;
138         struct tcf_proto *tp = NULL;
139         struct tcf_proto_ops *tp_ops;
140         struct Qdisc_class_ops *cops;
141         unsigned long cl = 0;
142         unsigned long fh, fh_s;
143         int err;
144
145         if (prio == 0) {
146                 /* If no priority is given, user wants we allocated it. */
147                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
148                         return -ENOENT;
149                 prio = TC_H_MAKE(0x80000000U,0U);
150         }
151
152         /* Find head of filter chain. */
153
154         /* Find link */
155         if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
156                 return -ENODEV;
157
158         /* Find qdisc */
159         if (!parent) {
160                 q = dev->qdisc_sleeping;
161                 parent = q->handle;
162         } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
163                 return -EINVAL;
164
165         /* Is it classful? */
166         if ((cops = q->ops->cl_ops) == NULL)
167                 return -EINVAL;
168
169         /* Do we search for filter, attached to class? */
170         if (TC_H_MIN(parent)) {
171                 cl = cops->get(q, parent);
172                 if (cl == 0)
173                         return -ENOENT;
174         }
175
176         /* And the last stroke */
177         chain = cops->tcf_chain(q, cl);
178         err = -EINVAL;
179         if (chain == NULL)
180                 goto errout;
181
182         /* Check the chain for existence of proto-tcf with this priority */
183         for (back = chain; (tp=*back) != NULL; back = &tp->next) {
184                 if (tp->prio >= prio) {
185                         if (tp->prio == prio) {
186                                 if (!nprio || (tp->protocol != protocol && protocol))
187                                         goto errout;
188                         } else
189                                 tp = NULL;
190                         break;
191                 }
192         }
193
194         if (tp == NULL) {
195                 /* Proto-tcf does not exist, create new one */
196
197                 if (tca[TCA_KIND-1] == NULL || !protocol)
198                         goto errout;
199
200                 err = -ENOENT;
201                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
202                         goto errout;
203
204
205                 /* Create new proto tcf */
206
207                 err = -ENOBUFS;
208                 if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
209                         goto errout;
210                 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
211 #ifdef CONFIG_KMOD
212                 if (tp_ops==NULL && tca[TCA_KIND-1] != NULL) {
213                         struct rtattr *kind = tca[TCA_KIND-1];
214
215                         if (RTA_PAYLOAD(kind) <= IFNAMSIZ) {
216                                 request_module("cls_%s", (char*)RTA_DATA(kind));
217                                 tp_ops = tcf_proto_lookup_ops(kind);
218                         }
219                 }
220 #endif
221                 if (tp_ops == NULL) {
222                         err = -EINVAL;
223                         kfree(tp);
224                         goto errout;
225                 }
226                 memset(tp, 0, sizeof(*tp));
227                 tp->ops = tp_ops;
228                 tp->protocol = protocol;
229                 tp->prio = nprio ? : tcf_auto_prio(*back);
230                 tp->q = q;
231                 tp->classify = tp_ops->classify;
232                 tp->classid = parent;
233                 err = -EBUSY;
234                 if (!try_module_get(tp_ops->owner) ||
235                     (err = tp_ops->init(tp)) != 0) {
236                         kfree(tp);
237                         goto errout;
238                 }
239
240                 qdisc_lock_tree(dev);
241                 tp->next = *back;
242                 *back = tp;
243                 qdisc_unlock_tree(dev);
244
245         } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
246                 goto errout;
247
248         fh_s = fh = tp->ops->get(tp, t->tcm_handle);
249
250         if (fh == 0) {
251                 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
252                         qdisc_lock_tree(dev);
253                         *back = tp->next;
254                         qdisc_unlock_tree(dev);
255
256                         tfilter_notify(skb, n, tp, fh_s, RTM_DELTFILTER);
257                         tcf_destroy(tp);
258                         err = 0;
259                         goto errout;
260                 }
261
262                 err = -ENOENT;
263                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
264                         goto errout;
265         } else {
266                 switch (n->nlmsg_type) {
267                 case RTM_NEWTFILTER:    
268                         err = -EEXIST;
269                         if (n->nlmsg_flags&NLM_F_EXCL)
270                                 goto errout;
271                         break;
272                 case RTM_DELTFILTER:
273                         err = tp->ops->delete(tp, fh);
274                         if (err == 0)
275                                 tfilter_notify(skb, n, tp, fh_s, RTM_DELTFILTER);
276                         goto errout;
277                 case RTM_GETTFILTER:
278                         err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
279                         goto errout;
280                 default:
281                         err = -EINVAL;
282                         goto errout;
283                 }
284         }
285
286         err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
287         if (err == 0)
288                 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
289
290 errout:
291         if (cl)
292                 cops->put(q, cl);
293         return err;
294 }
295
296 unsigned long tcf_set_class(struct tcf_proto *tp, unsigned long *clp, 
297                             unsigned long cl)
298 {
299         unsigned long old_cl;
300
301         tcf_tree_lock(tp);
302         old_cl = __cls_set_class(clp, cl);
303         tcf_tree_unlock(tp);
304
305         return old_cl;
306 }
307
308
309 static int
310 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
311               u32 pid, u32 seq, unsigned flags, int event)
312 {
313         struct tcmsg *tcm;
314         struct nlmsghdr  *nlh;
315         unsigned char    *b = skb->tail;
316
317         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
318         nlh->nlmsg_flags = flags;
319         tcm = NLMSG_DATA(nlh);
320         tcm->tcm_family = AF_UNSPEC;
321         tcm->tcm_ifindex = tp->q->dev->ifindex;
322         tcm->tcm_parent = tp->classid;
323         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
324         RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
325         tcm->tcm_handle = fh;
326         if (RTM_DELTFILTER != event) {
327                 tcm->tcm_handle = 0;
328                 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
329                         goto rtattr_failure;
330         }
331         nlh->nlmsg_len = skb->tail - b;
332         return skb->len;
333
334 nlmsg_failure:
335 rtattr_failure:
336         skb_trim(skb, b - skb->data);
337         return -1;
338 }
339
340 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
341                           struct tcf_proto *tp, unsigned long fh, int event)
342 {
343         struct sk_buff *skb;
344         u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
345
346         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
347         if (!skb)
348                 return -ENOBUFS;
349
350         if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
351                 kfree_skb(skb);
352                 return -EINVAL;
353         }
354
355         return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
356 }
357
358 struct tcf_dump_args
359 {
360         struct tcf_walker w;
361         struct sk_buff *skb;
362         struct netlink_callback *cb;
363 };
364
365 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
366 {
367         struct tcf_dump_args *a = (void*)arg;
368
369         return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
370                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
371 }
372
373 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
374 {
375         int t;
376         int s_t;
377         struct net_device *dev;
378         struct Qdisc *q;
379         struct tcf_proto *tp, **chain;
380         struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
381         unsigned long cl = 0;
382         struct Qdisc_class_ops *cops;
383         struct tcf_dump_args arg;
384
385         if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
386                 return skb->len;
387         if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
388                 return skb->len;
389
390         read_lock_bh(&qdisc_tree_lock);
391         if (!tcm->tcm_parent)
392                 q = dev->qdisc_sleeping;
393         else
394                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
395         if (!q)
396                 goto out;
397         if ((cops = q->ops->cl_ops) == NULL)
398                 goto errout;
399         if (TC_H_MIN(tcm->tcm_parent)) {
400                 cl = cops->get(q, tcm->tcm_parent);
401                 if (cl == 0)
402                         goto errout;
403         }
404         chain = cops->tcf_chain(q, cl);
405         if (chain == NULL)
406                 goto errout;
407
408         s_t = cb->args[0];
409
410         for (tp=*chain, t=0; tp; tp = tp->next, t++) {
411                 if (t < s_t) continue;
412                 if (TC_H_MAJ(tcm->tcm_info) &&
413                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
414                         continue;
415                 if (TC_H_MIN(tcm->tcm_info) &&
416                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
417                         continue;
418                 if (t > s_t)
419                         memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
420                 if (cb->args[1] == 0) {
421                         if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
422                                           cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
423                                 break;
424                         }
425                         cb->args[1] = 1;
426                 }
427                 if (tp->ops->walk == NULL)
428                         continue;
429                 arg.w.fn = tcf_node_dump;
430                 arg.skb = skb;
431                 arg.cb = cb;
432                 arg.w.stop = 0;
433                 arg.w.skip = cb->args[1]-1;
434                 arg.w.count = 0;
435                 tp->ops->walk(tp, &arg.w);
436                 cb->args[1] = arg.w.count+1;
437                 if (arg.w.stop)
438                         break;
439         }
440
441         cb->args[0] = t;
442
443 errout:
444         if (cl)
445                 cops->put(q, cl);
446 out:
447         read_unlock_bh(&qdisc_tree_lock);
448         dev_put(dev);
449         return skb->len;
450 }
451
452
453 static int __init tc_filter_init(void)
454 {
455         struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
456
457         /* Setup rtnetlink links. It is made here to avoid
458            exporting large number of public symbols.
459          */
460
461         if (link_p) {
462                 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
463                 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
464                 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
465                 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
466         }
467         return 0;
468 }
469
470 subsys_initcall(tc_filter_init);
471
472 EXPORT_SYMBOL(register_tcf_proto_ops);
473 EXPORT_SYMBOL(unregister_tcf_proto_ops);
474 EXPORT_SYMBOL(tcf_set_class);