blob: 5dbb9d451f7300a0620019656f695dbe0506ab78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
46
Thomas Grafc5c13fa2005-04-24 20:19:54 -070047#define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct fw_head
50{
Thomas Grafc5c13fa2005-04-24 20:19:54 -070051 struct fw_filter *ht[HTSIZE];
Patrick McHardyb4e9b522006-08-25 16:11:42 -070052 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55struct 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
66static struct tcf_ext_map fw_ext_map = {
67 .action = TCA_FW_ACT,
68 .police = TCA_FW_POLICE
69};
70
71static __inline__ int fw_hash(u32 handle)
72{
Thomas Grafc5c13fa2005-04-24 20:19:54 -070073 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];
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090093 } else
Thomas Grafc5c13fa2005-04-24 20:19:54 -070094 return handle & (HTSIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static 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;
Patrick McHardy5c804bf2006-12-05 13:46:13 -0800103 u32 id = skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (head != NULL) {
Patrick McHardy5c804bf2006-12-05 13:46:13 -0800106 id &= head->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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;
117
118 return r;
119 }
120 }
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;
127 }
128 }
129
130 return -1;
131}
132
133static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
134{
135 struct fw_head *head = (struct fw_head*)tp->root;
136 struct fw_filter *f;
137
138 if (head == NULL)
139 return 0;
140
141 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
142 if (f->id == handle)
143 return (unsigned long)f;
144 }
145 return 0;
146}
147
148static void fw_put(struct tcf_proto *tp, unsigned long f)
149{
150}
151
152static int fw_init(struct tcf_proto *tp)
153{
154 return 0;
155}
156
157static inline void
158fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
159{
160 tcf_unbind_filter(tp, &f->res);
161 tcf_exts_destroy(tp, &f->exts);
162 kfree(f);
163}
164
165static void fw_destroy(struct tcf_proto *tp)
166{
167 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
168 struct fw_filter *f;
169 int h;
170
171 if (head == NULL)
172 return;
173
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700174 for (h=0; h<HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 while ((f=head->ht[h]) != NULL) {
176 head->ht[h] = f->next;
177 fw_delete_filter(tp, f);
178 }
179 }
180 kfree(head);
181}
182
183static int fw_delete(struct tcf_proto *tp, unsigned long arg)
184{
185 struct fw_head *head = (struct fw_head*)tp->root;
186 struct fw_filter *f = (struct fw_filter*)arg;
187 struct fw_filter **fp;
188
189 if (head == NULL || f == NULL)
190 goto out;
191
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;
199 }
200 }
201out:
202 return -EINVAL;
203}
204
205static int
206fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
207 struct rtattr **tb, struct rtattr **tca, unsigned long base)
208{
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700209 struct fw_head *head = (struct fw_head *)tp->root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct tcf_exts e;
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700211 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int err;
213
214 err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
215 if (err < 0)
216 return err;
217
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);
224 }
225
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;
231 }
232#endif /* CONFIG_NET_CLS_IND */
233
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700234 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;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 tcf_exts_change(tp, &f->exts, &e);
244
245 return 0;
246errout:
247 tcf_exts_destroy(tp, &e);
248 return err;
249}
250
251static int fw_change(struct tcf_proto *tp, unsigned long base,
252 u32 handle,
253 struct rtattr **tca,
254 unsigned long *arg)
255{
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;
261
262 if (!opt)
263 return handle ? -EINVAL : 0;
264
265 if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
266 return -EINVAL;
267
268 if (f != NULL) {
269 if (f->id != handle && handle)
270 return -EINVAL;
271 return fw_change_attrs(tp, f, tb, tca, base);
272 }
273
274 if (!handle)
275 return -EINVAL;
276
277 if (head == NULL) {
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700278 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]);
283 }
284
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700285 head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (head == NULL)
287 return -ENOBUFS;
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700288 head->mask = mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 tcf_tree_lock(tp);
291 tp->root = head;
292 tcf_tree_unlock(tp);
293 }
294
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700295 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (f == NULL)
297 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 f->id = handle;
300
301 err = fw_change_attrs(tp, f, tb, tca, base);
302 if (err < 0)
303 goto errout;
304
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);
309
310 *arg = (unsigned long)f;
311 return 0;
312
313errout:
Jesper Juhla51482b2005-11-08 09:41:34 -0800314 kfree(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return err;
316}
317
318static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
319{
320 struct fw_head *head = (struct fw_head*)tp->root;
321 int h;
322
323 if (head == NULL)
324 arg->stop = 1;
325
326 if (arg->stop)
327 return;
328
Thomas Grafc5c13fa2005-04-24 20:19:54 -0700329 for (h = 0; h < HTSIZE; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct fw_filter *f;
331
332 for (f = head->ht[h]; f; f = f->next) {
333 if (arg->count < arg->skip) {
334 arg->count++;
335 continue;
336 }
337 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
338 arg->stop = 1;
339 return;
340 }
341 arg->count++;
342 }
343 }
344}
345
346static int fw_dump(struct tcf_proto *tp, unsigned long fh,
347 struct sk_buff *skb, struct tcmsg *t)
348{
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700349 struct fw_head *head = (struct fw_head *)tp->root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct fw_filter *f = (struct fw_filter*)fh;
351 unsigned char *b = skb->tail;
352 struct rtattr *rta;
353
354 if (f == NULL)
355 return skb->len;
356
357 t->tcm_handle = f->id;
358
359 if (!f->res.classid && !tcf_exts_is_available(&f->exts))
360 return skb->len;
361
362 rta = (struct rtattr*)b;
363 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
364
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 */
Patrick McHardyb4e9b522006-08-25 16:11:42 -0700371 if (head->mask != 0xFFFFFFFF)
372 RTA_PUT(skb, TCA_FW_MASK, 4, &head->mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
375 goto rtattr_failure;
376
377 rta->rta_len = skb->tail - b;
378
379 if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
380 goto rtattr_failure;
381
382 return skb->len;
383
384rtattr_failure:
385 skb_trim(skb, b - skb->data);
386 return -1;
387}
388
389static 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,
402};
403
404static int __init init_fw(void)
405{
406 return register_tcf_proto_ops(&cls_fw_ops);
407}
408
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900409static void __exit exit_fw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 unregister_tcf_proto_ops(&cls_fw_ops);
412}
413
414module_init(init_fw)
415module_exit(exit_fw)
416MODULE_LICENSE("GPL");