vserver 1.9.3
[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;
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                         kfree(tp);
236                         goto errout;
237                 }
238                 if ((err = tp_ops->init(tp)) != 0) {
239                         module_put(tp_ops->owner);
240                         kfree(tp);
241                         goto errout;
242                 }
243
244                 qdisc_lock_tree(dev);
245                 tp->next = *back;
246                 *back = tp;
247                 qdisc_unlock_tree(dev);
248
249         } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
250                 goto errout;
251
252         fh = tp->ops->get(tp, t->tcm_handle);
253
254         if (fh == 0) {
255                 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
256                         qdisc_lock_tree(dev);
257                         *back = tp->next;
258                         qdisc_unlock_tree(dev);
259
260                         tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
261                         tcf_destroy(tp);
262                         err = 0;
263                         goto errout;
264                 }
265
266                 err = -ENOENT;
267                 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
268                         goto errout;
269         } else {
270                 switch (n->nlmsg_type) {
271                 case RTM_NEWTFILTER:    
272                         err = -EEXIST;
273                         if (n->nlmsg_flags&NLM_F_EXCL)
274                                 goto errout;
275                         break;
276                 case RTM_DELTFILTER:
277                         err = tp->ops->delete(tp, fh);
278                         if (err == 0)
279                                 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
280                         goto errout;
281                 case RTM_GETTFILTER:
282                         err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
283                         goto errout;
284                 default:
285                         err = -EINVAL;
286                         goto errout;
287                 }
288         }
289
290         err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
291         if (err == 0)
292                 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
293
294 errout:
295         if (cl)
296                 cops->put(q, cl);
297         return err;
298 }
299
300 unsigned long tcf_set_class(struct tcf_proto *tp, unsigned long *clp, 
301                             unsigned long cl)
302 {
303         unsigned long old_cl;
304
305         tcf_tree_lock(tp);
306         old_cl = __cls_set_class(clp, cl);
307         tcf_tree_unlock(tp);
308
309         return old_cl;
310 }
311
312
313 static int
314 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
315               u32 pid, u32 seq, unsigned flags, int event)
316 {
317         struct tcmsg *tcm;
318         struct nlmsghdr  *nlh;
319         unsigned char    *b = skb->tail;
320
321         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
322         nlh->nlmsg_flags = flags;
323         tcm = NLMSG_DATA(nlh);
324         tcm->tcm_family = AF_UNSPEC;
325         tcm->tcm_ifindex = tp->q->dev->ifindex;
326         tcm->tcm_parent = tp->classid;
327         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
328         RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
329         tcm->tcm_handle = fh;
330         if (RTM_DELTFILTER != event) {
331                 tcm->tcm_handle = 0;
332                 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
333                         goto rtattr_failure;
334         }
335         nlh->nlmsg_len = skb->tail - b;
336         return skb->len;
337
338 nlmsg_failure:
339 rtattr_failure:
340         skb_trim(skb, b - skb->data);
341         return -1;
342 }
343
344 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
345                           struct tcf_proto *tp, unsigned long fh, int event)
346 {
347         struct sk_buff *skb;
348         u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
349
350         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
351         if (!skb)
352                 return -ENOBUFS;
353
354         if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
355                 kfree_skb(skb);
356                 return -EINVAL;
357         }
358
359         return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
360 }
361
362 struct tcf_dump_args
363 {
364         struct tcf_walker w;
365         struct sk_buff *skb;
366         struct netlink_callback *cb;
367 };
368
369 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
370 {
371         struct tcf_dump_args *a = (void*)arg;
372
373         return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
374                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
375 }
376
377 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
378 {
379         int t;
380         int s_t;
381         struct net_device *dev;
382         struct Qdisc *q;
383         struct tcf_proto *tp, **chain;
384         struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
385         unsigned long cl = 0;
386         struct Qdisc_class_ops *cops;
387         struct tcf_dump_args arg;
388
389         if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
390                 return skb->len;
391         if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
392                 return skb->len;
393
394         read_lock_bh(&qdisc_tree_lock);
395         if (!tcm->tcm_parent)
396                 q = dev->qdisc_sleeping;
397         else
398                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
399         if (!q)
400                 goto out;
401         if ((cops = q->ops->cl_ops) == NULL)
402                 goto errout;
403         if (TC_H_MIN(tcm->tcm_parent)) {
404                 cl = cops->get(q, tcm->tcm_parent);
405                 if (cl == 0)
406                         goto errout;
407         }
408         chain = cops->tcf_chain(q, cl);
409         if (chain == NULL)
410                 goto errout;
411
412         s_t = cb->args[0];
413
414         for (tp=*chain, t=0; tp; tp = tp->next, t++) {
415                 if (t < s_t) continue;
416                 if (TC_H_MAJ(tcm->tcm_info) &&
417                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
418                         continue;
419                 if (TC_H_MIN(tcm->tcm_info) &&
420                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
421                         continue;
422                 if (t > s_t)
423                         memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
424                 if (cb->args[1] == 0) {
425                         if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
426                                           cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
427                                 break;
428                         }
429                         cb->args[1] = 1;
430                 }
431                 if (tp->ops->walk == NULL)
432                         continue;
433                 arg.w.fn = tcf_node_dump;
434                 arg.skb = skb;
435                 arg.cb = cb;
436                 arg.w.stop = 0;
437                 arg.w.skip = cb->args[1]-1;
438                 arg.w.count = 0;
439                 tp->ops->walk(tp, &arg.w);
440                 cb->args[1] = arg.w.count+1;
441                 if (arg.w.stop)
442                         break;
443         }
444
445         cb->args[0] = t;
446
447 errout:
448         if (cl)
449                 cops->put(q, cl);
450 out:
451         read_unlock_bh(&qdisc_tree_lock);
452         dev_put(dev);
453         return skb->len;
454 }
455
456
457 static int __init tc_filter_init(void)
458 {
459         struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
460
461         /* Setup rtnetlink links. It is made here to avoid
462            exporting large number of public symbols.
463          */
464
465         if (link_p) {
466                 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
467                 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
468                 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
469                 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
470         }
471         return 0;
472 }
473
474 subsys_initcall(tc_filter_init);
475
476 EXPORT_SYMBOL(register_tcf_proto_ops);
477 EXPORT_SYMBOL(unregister_tcf_proto_ops);
478 EXPORT_SYMBOL(tcf_set_class);