Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6/mini2440.git] / net / sched / cls_fw.c
blob5dbb9d451f7300a0620019656f695dbe0506ab78
1 /*
2 * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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
16 * JHS: We should remove the CONFIG_NET_CLS_IND from here
17 * eventually when the meta match extension is made available
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/string.h>
28 #include <linux/mm.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/in.h>
32 #include <linux/errno.h>
33 #include <linux/interrupt.h>
34 #include <linux/if_ether.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/notifier.h>
39 #include <linux/netfilter.h>
40 #include <net/ip.h>
41 #include <net/route.h>
42 #include <linux/skbuff.h>
43 #include <net/sock.h>
44 #include <net/act_api.h>
45 #include <net/pkt_cls.h>
47 #define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
49 struct fw_head
51 struct fw_filter *ht[HTSIZE];
52 u32 mask;
55 struct fw_filter
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;
66 static struct tcf_ext_map fw_ext_map = {
67 .action = TCA_FW_ACT,
68 .police = TCA_FW_POLICE
71 static __inline__ int fw_hash(u32 handle)
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);
97 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
98 struct tcf_result *res)
100 struct fw_head *head = (struct fw_head*)tp->root;
101 struct fw_filter *f;
102 int r;
103 u32 id = skb->mark;
105 if (head != NULL) {
106 id &= head->mask;
107 for (f=head->ht[fw_hash(id)]; f; f=f->next) {
108 if (f->id == id) {
109 *res = f->res;
110 #ifdef CONFIG_NET_CLS_IND
111 if (!tcf_match_indev(skb, f->indev))
112 continue;
113 #endif /* CONFIG_NET_CLS_IND */
114 r = tcf_exts_exec(skb, &f->exts, res);
115 if (r < 0)
116 continue;
118 return r;
121 } else {
122 /* old method */
123 if (id && (TC_H_MAJ(id) == 0 || !(TC_H_MAJ(id^tp->q->handle)))) {
124 res->classid = id;
125 res->class = 0;
126 return 0;
130 return -1;
133 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
135 struct fw_head *head = (struct fw_head*)tp->root;
136 struct fw_filter *f;
138 if (head == NULL)
139 return 0;
141 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
142 if (f->id == handle)
143 return (unsigned long)f;
145 return 0;
148 static void fw_put(struct tcf_proto *tp, unsigned long f)
152 static int fw_init(struct tcf_proto *tp)
154 return 0;
157 static inline void
158 fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
160 tcf_unbind_filter(tp, &f->res);
161 tcf_exts_destroy(tp, &f->exts);
162 kfree(f);
165 static void fw_destroy(struct tcf_proto *tp)
167 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
168 struct fw_filter *f;
169 int h;
171 if (head == NULL)
172 return;
174 for (h=0; h<HTSIZE; h++) {
175 while ((f=head->ht[h]) != NULL) {
176 head->ht[h] = f->next;
177 fw_delete_filter(tp, f);
180 kfree(head);
183 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
185 struct fw_head *head = (struct fw_head*)tp->root;
186 struct fw_filter *f = (struct fw_filter*)arg;
187 struct fw_filter **fp;
189 if (head == NULL || f == NULL)
190 goto out;
192 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
193 if (*fp == f) {
194 tcf_tree_lock(tp);
195 *fp = f->next;
196 tcf_tree_unlock(tp);
197 fw_delete_filter(tp, f);
198 return 0;
201 out:
202 return -EINVAL;
205 static int
206 fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
207 struct rtattr **tb, struct rtattr **tca, unsigned long base)
209 struct fw_head *head = (struct fw_head *)tp->root;
210 struct tcf_exts e;
211 u32 mask;
212 int err;
214 err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
215 if (err < 0)
216 return err;
218 err = -EINVAL;
219 if (tb[TCA_FW_CLASSID-1]) {
220 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
221 goto errout;
222 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
223 tcf_bind_filter(tp, &f->res, base);
226 #ifdef CONFIG_NET_CLS_IND
227 if (tb[TCA_FW_INDEV-1]) {
228 err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
229 if (err < 0)
230 goto errout;
232 #endif /* CONFIG_NET_CLS_IND */
234 if (tb[TCA_FW_MASK-1]) {
235 if (RTA_PAYLOAD(tb[TCA_FW_MASK-1]) != sizeof(u32))
236 goto errout;
237 mask = *(u32*)RTA_DATA(tb[TCA_FW_MASK-1]);
238 if (mask != head->mask)
239 goto errout;
240 } else if (head->mask != 0xFFFFFFFF)
241 goto errout;
243 tcf_exts_change(tp, &f->exts, &e);
245 return 0;
246 errout:
247 tcf_exts_destroy(tp, &e);
248 return err;
251 static int fw_change(struct tcf_proto *tp, unsigned long base,
252 u32 handle,
253 struct rtattr **tca,
254 unsigned long *arg)
256 struct fw_head *head = (struct fw_head*)tp->root;
257 struct fw_filter *f = (struct fw_filter *) *arg;
258 struct rtattr *opt = tca[TCA_OPTIONS-1];
259 struct rtattr *tb[TCA_FW_MAX];
260 int err;
262 if (!opt)
263 return handle ? -EINVAL : 0;
265 if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
266 return -EINVAL;
268 if (f != NULL) {
269 if (f->id != handle && handle)
270 return -EINVAL;
271 return fw_change_attrs(tp, f, tb, tca, base);
274 if (!handle)
275 return -EINVAL;
277 if (head == NULL) {
278 u32 mask = 0xFFFFFFFF;
279 if (tb[TCA_FW_MASK-1]) {
280 if (RTA_PAYLOAD(tb[TCA_FW_MASK-1]) != sizeof(u32))
281 return -EINVAL;
282 mask = *(u32*)RTA_DATA(tb[TCA_FW_MASK-1]);
285 head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
286 if (head == NULL)
287 return -ENOBUFS;
288 head->mask = mask;
290 tcf_tree_lock(tp);
291 tp->root = head;
292 tcf_tree_unlock(tp);
295 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
296 if (f == NULL)
297 return -ENOBUFS;
299 f->id = handle;
301 err = fw_change_attrs(tp, f, tb, tca, base);
302 if (err < 0)
303 goto errout;
305 f->next = head->ht[fw_hash(handle)];
306 tcf_tree_lock(tp);
307 head->ht[fw_hash(handle)] = f;
308 tcf_tree_unlock(tp);
310 *arg = (unsigned long)f;
311 return 0;
313 errout:
314 kfree(f);
315 return err;
318 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
320 struct fw_head *head = (struct fw_head*)tp->root;
321 int h;
323 if (head == NULL)
324 arg->stop = 1;
326 if (arg->stop)
327 return;
329 for (h = 0; h < HTSIZE; h++) {
330 struct fw_filter *f;
332 for (f = head->ht[h]; f; f = f->next) {
333 if (arg->count < arg->skip) {
334 arg->count++;
335 continue;
337 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
338 arg->stop = 1;
339 return;
341 arg->count++;
346 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
347 struct sk_buff *skb, struct tcmsg *t)
349 struct fw_head *head = (struct fw_head *)tp->root;
350 struct fw_filter *f = (struct fw_filter*)fh;
351 unsigned char *b = skb->tail;
352 struct rtattr *rta;
354 if (f == NULL)
355 return skb->len;
357 t->tcm_handle = f->id;
359 if (!f->res.classid && !tcf_exts_is_available(&f->exts))
360 return skb->len;
362 rta = (struct rtattr*)b;
363 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
365 if (f->res.classid)
366 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
367 #ifdef CONFIG_NET_CLS_IND
368 if (strlen(f->indev))
369 RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
370 #endif /* CONFIG_NET_CLS_IND */
371 if (head->mask != 0xFFFFFFFF)
372 RTA_PUT(skb, TCA_FW_MASK, 4, &head->mask);
374 if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
375 goto rtattr_failure;
377 rta->rta_len = skb->tail - b;
379 if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
380 goto rtattr_failure;
382 return skb->len;
384 rtattr_failure:
385 skb_trim(skb, b - skb->data);
386 return -1;
389 static struct tcf_proto_ops cls_fw_ops = {
390 .next = NULL,
391 .kind = "fw",
392 .classify = fw_classify,
393 .init = fw_init,
394 .destroy = fw_destroy,
395 .get = fw_get,
396 .put = fw_put,
397 .change = fw_change,
398 .delete = fw_delete,
399 .walk = fw_walk,
400 .dump = fw_dump,
401 .owner = THIS_MODULE,
404 static int __init init_fw(void)
406 return register_tcf_proto_ops(&cls_fw_ops);
409 static void __exit exit_fw(void)
411 unregister_tcf_proto_ops(&cls_fw_ops);
414 module_init(init_fw)
415 module_exit(exit_fw)
416 MODULE_LICENSE("GPL");