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