Merge branch 'master' of /home/sam/kernel/linux-2.6/
[linux-2.6] / net / sched / cls_fw.c
1 /*
2  * net/sched/cls_fw.c   Classifier mapping ipchains' fwmark to traffic class.
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  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
13  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
14  * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
15  *
16  * JHS: We should remove the CONFIG_NET_CLS_IND from here
17  * eventually when the meta match extension is made available
18  *
19  */
20
21 #include <linux/module.h>
22 #include <asm/uaccess.h>
23 #include <asm/system.h>
24 #include <linux/bitops.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/string.h>
29 #include <linux/mm.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/in.h>
33 #include <linux/errno.h>
34 #include <linux/interrupt.h>
35 #include <linux/if_ether.h>
36 #include <linux/inet.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/notifier.h>
40 #include <linux/netfilter.h>
41 #include <net/ip.h>
42 #include <net/route.h>
43 #include <linux/skbuff.h>
44 #include <net/sock.h>
45 #include <net/act_api.h>
46 #include <net/pkt_cls.h>
47
48 #define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
49
50 struct fw_head
51 {
52         struct fw_filter *ht[HTSIZE];
53 };
54
55 struct fw_filter
56 {
57         struct fw_filter        *next;
58         u32                     id;
59         struct tcf_result       res;
60 #ifdef CONFIG_NET_CLS_IND
61         char                    indev[IFNAMSIZ];
62 #endif /* CONFIG_NET_CLS_IND */
63         struct tcf_exts         exts;
64 };
65
66 static struct tcf_ext_map fw_ext_map = {
67         .action = TCA_FW_ACT,
68         .police = TCA_FW_POLICE
69 };
70
71 static __inline__ int fw_hash(u32 handle)
72 {
73         if (HTSIZE == 4096)
74                 return ((handle >> 24) & 0xFFF) ^
75                        ((handle >> 12) & 0xFFF) ^
76                        (handle & 0xFFF);
77         else if (HTSIZE == 2048)
78                 return ((handle >> 22) & 0x7FF) ^
79                        ((handle >> 11) & 0x7FF) ^
80                        (handle & 0x7FF);
81         else if (HTSIZE == 1024)
82                 return ((handle >> 20) & 0x3FF) ^
83                        ((handle >> 10) & 0x3FF) ^
84                        (handle & 0x3FF);
85         else if (HTSIZE == 512)
86                 return (handle >> 27) ^
87                        ((handle >> 18) & 0x1FF) ^
88                        ((handle >> 9) & 0x1FF) ^
89                        (handle & 0x1FF);
90         else if (HTSIZE == 256) {
91                 u8 *t = (u8 *) &handle;
92                 return t[0] ^ t[1] ^ t[2] ^ t[3];
93         } else 
94                 return handle & (HTSIZE - 1);
95 }
96
97 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
98                           struct tcf_result *res)
99 {
100         struct fw_head *head = (struct fw_head*)tp->root;
101         struct fw_filter *f;
102         int r;
103 #ifdef CONFIG_NETFILTER
104         u32 id = skb->nfmark;
105 #else
106         u32 id = 0;
107 #endif
108
109         if (head != NULL) {
110                 for (f=head->ht[fw_hash(id)]; f; f=f->next) {
111                         if (f->id == id) {
112                                 *res = f->res;
113 #ifdef CONFIG_NET_CLS_IND
114                                 if (!tcf_match_indev(skb, f->indev))
115                                         continue;
116 #endif /* CONFIG_NET_CLS_IND */
117                                 r = tcf_exts_exec(skb, &f->exts, res);
118                                 if (r < 0)
119                                         continue;
120
121                                 return r;
122                         }
123                 }
124         } else {
125                 /* old method */
126                 if (id && (TC_H_MAJ(id) == 0 || !(TC_H_MAJ(id^tp->q->handle)))) {
127                         res->classid = id;
128                         res->class = 0;
129                         return 0;
130                 }
131         }
132
133         return -1;
134 }
135
136 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
137 {
138         struct fw_head *head = (struct fw_head*)tp->root;
139         struct fw_filter *f;
140
141         if (head == NULL)
142                 return 0;
143
144         for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
145                 if (f->id == handle)
146                         return (unsigned long)f;
147         }
148         return 0;
149 }
150
151 static void fw_put(struct tcf_proto *tp, unsigned long f)
152 {
153 }
154
155 static int fw_init(struct tcf_proto *tp)
156 {
157         return 0;
158 }
159
160 static inline void
161 fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
162 {
163         tcf_unbind_filter(tp, &f->res);
164         tcf_exts_destroy(tp, &f->exts);
165         kfree(f);
166 }
167
168 static void fw_destroy(struct tcf_proto *tp)
169 {
170         struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
171         struct fw_filter *f;
172         int h;
173
174         if (head == NULL)
175                 return;
176
177         for (h=0; h<HTSIZE; h++) {
178                 while ((f=head->ht[h]) != NULL) {
179                         head->ht[h] = f->next;
180                         fw_delete_filter(tp, f);
181                 }
182         }
183         kfree(head);
184 }
185
186 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
187 {
188         struct fw_head *head = (struct fw_head*)tp->root;
189         struct fw_filter *f = (struct fw_filter*)arg;
190         struct fw_filter **fp;
191
192         if (head == NULL || f == NULL)
193                 goto out;
194
195         for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
196                 if (*fp == f) {
197                         tcf_tree_lock(tp);
198                         *fp = f->next;
199                         tcf_tree_unlock(tp);
200                         fw_delete_filter(tp, f);
201                         return 0;
202                 }
203         }
204 out:
205         return -EINVAL;
206 }
207
208 static int
209 fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
210         struct rtattr **tb, struct rtattr **tca, unsigned long base)
211 {
212         struct tcf_exts e;
213         int err;
214
215         err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
216         if (err < 0)
217                 return err;
218
219         err = -EINVAL;
220         if (tb[TCA_FW_CLASSID-1]) {
221                 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
222                         goto errout;
223                 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
224                 tcf_bind_filter(tp, &f->res, base);
225         }
226
227 #ifdef CONFIG_NET_CLS_IND
228         if (tb[TCA_FW_INDEV-1]) {
229                 err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
230                 if (err < 0)
231                         goto errout;
232         }
233 #endif /* CONFIG_NET_CLS_IND */
234
235         tcf_exts_change(tp, &f->exts, &e);
236
237         return 0;
238 errout:
239         tcf_exts_destroy(tp, &e);
240         return err;
241 }
242
243 static int fw_change(struct tcf_proto *tp, unsigned long base,
244                      u32 handle,
245                      struct rtattr **tca,
246                      unsigned long *arg)
247 {
248         struct fw_head *head = (struct fw_head*)tp->root;
249         struct fw_filter *f = (struct fw_filter *) *arg;
250         struct rtattr *opt = tca[TCA_OPTIONS-1];
251         struct rtattr *tb[TCA_FW_MAX];
252         int err;
253
254         if (!opt)
255                 return handle ? -EINVAL : 0;
256
257         if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
258                 return -EINVAL;
259
260         if (f != NULL) {
261                 if (f->id != handle && handle)
262                         return -EINVAL;
263                 return fw_change_attrs(tp, f, tb, tca, base);
264         }
265
266         if (!handle)
267                 return -EINVAL;
268
269         if (head == NULL) {
270                 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
271                 if (head == NULL)
272                         return -ENOBUFS;
273                 memset(head, 0, sizeof(*head));
274
275                 tcf_tree_lock(tp);
276                 tp->root = head;
277                 tcf_tree_unlock(tp);
278         }
279
280         f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
281         if (f == NULL)
282                 return -ENOBUFS;
283         memset(f, 0, sizeof(*f));
284
285         f->id = handle;
286
287         err = fw_change_attrs(tp, f, tb, tca, base);
288         if (err < 0)
289                 goto errout;
290
291         f->next = head->ht[fw_hash(handle)];
292         tcf_tree_lock(tp);
293         head->ht[fw_hash(handle)] = f;
294         tcf_tree_unlock(tp);
295
296         *arg = (unsigned long)f;
297         return 0;
298
299 errout:
300         kfree(f);
301         return err;
302 }
303
304 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
305 {
306         struct fw_head *head = (struct fw_head*)tp->root;
307         int h;
308
309         if (head == NULL)
310                 arg->stop = 1;
311
312         if (arg->stop)
313                 return;
314
315         for (h = 0; h < HTSIZE; h++) {
316                 struct fw_filter *f;
317
318                 for (f = head->ht[h]; f; f = f->next) {
319                         if (arg->count < arg->skip) {
320                                 arg->count++;
321                                 continue;
322                         }
323                         if (arg->fn(tp, (unsigned long)f, arg) < 0) {
324                                 arg->stop = 1;
325                                 return;
326                         }
327                         arg->count++;
328                 }
329         }
330 }
331
332 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
333                    struct sk_buff *skb, struct tcmsg *t)
334 {
335         struct fw_filter *f = (struct fw_filter*)fh;
336         unsigned char    *b = skb->tail;
337         struct rtattr *rta;
338
339         if (f == NULL)
340                 return skb->len;
341
342         t->tcm_handle = f->id;
343
344         if (!f->res.classid && !tcf_exts_is_available(&f->exts))
345                 return skb->len;
346
347         rta = (struct rtattr*)b;
348         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
349
350         if (f->res.classid)
351                 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
352 #ifdef CONFIG_NET_CLS_IND
353         if (strlen(f->indev))
354                 RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
355 #endif /* CONFIG_NET_CLS_IND */
356
357         if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
358                 goto rtattr_failure;
359
360         rta->rta_len = skb->tail - b;
361
362         if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
363                 goto rtattr_failure;
364
365         return skb->len;
366
367 rtattr_failure:
368         skb_trim(skb, b - skb->data);
369         return -1;
370 }
371
372 static struct tcf_proto_ops cls_fw_ops = {
373         .next           =       NULL,
374         .kind           =       "fw",
375         .classify       =       fw_classify,
376         .init           =       fw_init,
377         .destroy        =       fw_destroy,
378         .get            =       fw_get,
379         .put            =       fw_put,
380         .change         =       fw_change,
381         .delete         =       fw_delete,
382         .walk           =       fw_walk,
383         .dump           =       fw_dump,
384         .owner          =       THIS_MODULE,
385 };
386
387 static int __init init_fw(void)
388 {
389         return register_tcf_proto_ops(&cls_fw_ops);
390 }
391
392 static void __exit exit_fw(void) 
393 {
394         unregister_tcf_proto_ops(&cls_fw_ops);
395 }
396
397 module_init(init_fw)
398 module_exit(exit_fw)
399 MODULE_LICENSE("GPL");