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.
13 * Karlis Peisenieks <
[email protected]> : 990415 : fw_delete killed all the filter (and kernel).
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/config.h>
22 #include <linux/module.h>
23 #include <asm/uaccess.h>
24 #include <asm/system.h>
25 #include <linux/bitops.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/string.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
34 #include <linux/errno.h>
35 #include <linux/interrupt.h>
36 #include <linux/if_ether.h>
37 #include <linux/inet.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/notifier.h>
41 #include <linux/netfilter.h>
43 #include <net/route.h>
44 #include <linux/skbuff.h>
46 #include <net/act_api.h>
47 #include <net/pkt_cls.h>
51 struct fw_filter *ht[256];
56 struct fw_filter *next;
58 struct tcf_result res;
59 #ifdef CONFIG_NET_CLS_IND
61 #endif /* CONFIG_NET_CLS_IND */
65 static struct tcf_ext_map fw_ext_map = {
67 .police = TCA_FW_POLICE
70 static __inline__ int fw_hash(u32 handle)
75 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
76 struct tcf_result *res)
78 struct fw_head *head = (struct fw_head*)tp->root;
81 #ifdef CONFIG_NETFILTER
88 for (f=head->ht[fw_hash(id)]; f; f=f->next) {
91 #ifdef CONFIG_NET_CLS_IND
92 if (!tcf_match_indev(skb, f->indev))
94 #endif /* CONFIG_NET_CLS_IND */
95 r = tcf_exts_exec(skb, &f->exts, res);
104 if (id && (TC_H_MAJ(id) == 0 || !(TC_H_MAJ(id^tp->q->handle)))) {
114 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
116 struct fw_head *head = (struct fw_head*)tp->root;
122 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
124 return (unsigned long)f;
129 static void fw_put(struct tcf_proto *tp, unsigned long f)
133 static int fw_init(struct tcf_proto *tp)
139 fw_delete_filter(struct tcf_proto *tp, struct fw_filter *f)
141 tcf_unbind_filter(tp, &f->res);
142 tcf_exts_destroy(tp, &f->exts);
146 static void fw_destroy(struct tcf_proto *tp)
148 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
155 for (h=0; h<256; h++) {
156 while ((f=head->ht[h]) != NULL) {
157 head->ht[h] = f->next;
158 fw_delete_filter(tp, f);
164 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
166 struct fw_head *head = (struct fw_head*)tp->root;
167 struct fw_filter *f = (struct fw_filter*)arg;
168 struct fw_filter **fp;
170 if (head == NULL || f == NULL)
173 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
178 fw_delete_filter(tp, f);
187 fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
188 struct rtattr **tb, struct rtattr **tca, unsigned long base)
193 err = tcf_exts_validate(tp, tb, tca[TCA_RATE-1], &e, &fw_ext_map);
198 if (tb[TCA_FW_CLASSID-1]) {
199 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
201 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
202 tcf_bind_filter(tp, &f->res, base);
205 #ifdef CONFIG_NET_CLS_IND
206 if (tb[TCA_FW_INDEV-1]) {
207 err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
211 #endif /* CONFIG_NET_CLS_IND */
213 tcf_exts_change(tp, &f->exts, &e);
217 tcf_exts_destroy(tp, &e);
221 static int fw_change(struct tcf_proto *tp, unsigned long base,
226 struct fw_head *head = (struct fw_head*)tp->root;
227 struct fw_filter *f = (struct fw_filter *) *arg;
228 struct rtattr *opt = tca[TCA_OPTIONS-1];
229 struct rtattr *tb[TCA_FW_MAX];
233 return handle ? -EINVAL : 0;
235 if (rtattr_parse_nested(tb, TCA_FW_MAX, opt) < 0)
239 if (f->id != handle && handle)
241 return fw_change_attrs(tp, f, tb, tca, base);
248 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
251 memset(head, 0, sizeof(*head));
258 f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
261 memset(f, 0, sizeof(*f));
265 err = fw_change_attrs(tp, f, tb, tca, base);
269 f->next = head->ht[fw_hash(handle)];
271 head->ht[fw_hash(handle)] = f;
274 *arg = (unsigned long)f;
283 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
285 struct fw_head *head = (struct fw_head*)tp->root;
294 for (h = 0; h < 256; h++) {
297 for (f = head->ht[h]; f; f = f->next) {
298 if (arg->count < arg->skip) {
302 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
311 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
312 struct sk_buff *skb, struct tcmsg *t)
314 struct fw_filter *f = (struct fw_filter*)fh;
315 unsigned char *b = skb->tail;
321 t->tcm_handle = f->id;
323 if (!f->res.classid && !tcf_exts_is_available(&f->exts))
326 rta = (struct rtattr*)b;
327 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
330 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
331 #ifdef CONFIG_NET_CLS_IND
332 if (strlen(f->indev))
333 RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
334 #endif /* CONFIG_NET_CLS_IND */
336 if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
339 rta->rta_len = skb->tail - b;
341 if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
347 skb_trim(skb, b - skb->data);
351 static struct tcf_proto_ops cls_fw_ops = {
354 .classify = fw_classify,
356 .destroy = fw_destroy,
363 .owner = THIS_MODULE,
366 static int __init init_fw(void)
368 return register_tcf_proto_ops(&cls_fw_ops);
371 static void __exit exit_fw(void)
373 unregister_tcf_proto_ops(&cls_fw_ops);
378 MODULE_LICENSE("GPL");