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