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