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>
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
36 #include <net/pkt_sched.h>
40 struct fw_filter
*ht
[256];
45 struct fw_filter
*next
;
47 struct tcf_result res
;
48 #ifdef CONFIG_NET_CLS_POLICE
49 struct tcf_police
*police
;
53 static __inline__
int fw_hash(u32 handle
)
58 static int fw_classify(struct sk_buff
*skb
, struct tcf_proto
*tp
,
59 struct tcf_result
*res
)
61 struct fw_head
*head
= (struct fw_head
*)tp
->root
;
63 #ifdef CONFIG_IP_FIREWALL
72 for (f
=head
->ht
[fw_hash(id
)]; f
; f
=f
->next
) {
75 #ifdef CONFIG_NET_CLS_POLICE
77 return tcf_police(skb
, f
->police
);
85 if (id
&& (TC_H_MAJ(id
) == 0 ||
86 !(TC_H_MAJ(id
^tp
->q
->handle
)))) {
94 static unsigned long fw_get(struct tcf_proto
*tp
, u32 handle
)
96 struct fw_head
*head
= (struct fw_head
*)tp
->root
;
102 for (f
=head
->ht
[fw_hash(handle
)]; f
; f
=f
->next
) {
104 return (unsigned long)f
;
109 static void fw_put(struct tcf_proto
*tp
, unsigned long f
)
113 static int fw_init(struct tcf_proto
*tp
)
119 static void fw_destroy(struct tcf_proto
*tp
)
121 struct fw_head
*head
= (struct fw_head
*)xchg(&tp
->root
, NULL
);
130 for (h
=0; h
<256; h
++) {
131 while ((f
=head
->ht
[h
]) != NULL
) {
133 head
->ht
[h
] = f
->next
;
135 if ((cl
= cls_set_class(&f
->res
.class, 0)) != 0)
136 tp
->q
->ops
->cl_ops
->unbind_tcf(tp
->q
, cl
);
137 #ifdef CONFIG_NET_CLS_POLICE
138 tcf_police_release(f
->police
);
147 static int fw_delete(struct tcf_proto
*tp
, unsigned long arg
)
149 struct fw_head
*head
= (struct fw_head
*)xchg(&tp
->root
, NULL
);
150 struct fw_filter
*f
= (struct fw_filter
*)arg
;
151 struct fw_filter
**fp
;
153 if (head
== NULL
|| f
== NULL
)
156 for (fp
=&head
->ht
[fw_hash(f
->id
)]; *fp
; fp
= &(*fp
)->next
) {
163 if ((cl
= cls_set_class(&f
->res
.class, 0)) != 0)
164 tp
->q
->ops
->cl_ops
->unbind_tcf(tp
->q
, cl
);
165 #ifdef CONFIG_NET_CLS_POLICE
166 tcf_police_release(f
->police
);
175 static int fw_change(struct tcf_proto
*tp
, unsigned long base
,
180 struct fw_head
*head
= (struct fw_head
*)tp
->root
;
182 struct rtattr
*opt
= tca
[TCA_OPTIONS
-1];
183 struct rtattr
*tb
[TCA_FW_MAX
];
187 return handle
? -EINVAL
: 0;
189 if (rtattr_parse(tb
, TCA_FW_MAX
, RTA_DATA(opt
), RTA_PAYLOAD(opt
)) < 0)
192 if ((f
= (struct fw_filter
*)*arg
) != NULL
) {
193 /* Node exists: adjust only classid */
195 if (f
->id
!= handle
&& handle
)
197 if (tb
[TCA_FW_CLASSID
-1]) {
200 f
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_FW_CLASSID
-1]);
201 cl
= tp
->q
->ops
->cl_ops
->bind_tcf(tp
->q
, base
, f
->res
.classid
);
202 cl
= cls_set_class(&f
->res
.class, cl
);
204 tp
->q
->ops
->cl_ops
->unbind_tcf(tp
->q
, cl
);
206 #ifdef CONFIG_NET_CLS_POLICE
207 if (tb
[TCA_FW_POLICE
-1]) {
208 struct tcf_police
*police
= tcf_police_locate(tb
[TCA_FW_POLICE
-1], tca
[TCA_RATE
-1]);
210 police
= xchg(&f
->police
, police
);
213 tcf_police_release(police
);
223 head
= kmalloc(sizeof(struct fw_head
), GFP_KERNEL
);
226 memset(head
, 0, sizeof(*head
));
232 f
= kmalloc(sizeof(struct fw_filter
), GFP_KERNEL
);
235 memset(f
, 0, sizeof(*f
));
239 if (tb
[TCA_FW_CLASSID
-1]) {
241 if (RTA_PAYLOAD(tb
[TCA_FW_CLASSID
-1]) != 4)
243 f
->res
.classid
= *(u32
*)RTA_DATA(tb
[TCA_FW_CLASSID
-1]);
244 cls_set_class(&f
->res
.class, tp
->q
->ops
->cl_ops
->bind_tcf(tp
->q
, base
, f
->res
.classid
));
247 #ifdef CONFIG_NET_CLS_POLICE
248 if (tb
[TCA_FW_POLICE
-1])
249 f
->police
= tcf_police_locate(tb
[TCA_FW_POLICE
-1], tca
[TCA_RATE
-1]);
252 f
->next
= head
->ht
[fw_hash(handle
)];
254 head
->ht
[fw_hash(handle
)] = f
;
256 *arg
= (unsigned long)f
;
265 static void fw_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
267 struct fw_head
*head
= (struct fw_head
*)tp
->root
;
276 for (h
= 0; h
<= 256; h
++) {
279 for (f
= head
->ht
[h
]; f
; f
= f
->next
) {
280 if (arg
->count
< arg
->skip
) {
284 if (arg
->fn(tp
, (unsigned long)f
, arg
) < 0) {
293 #ifdef CONFIG_RTNETLINK
294 static int fw_dump(struct tcf_proto
*tp
, unsigned long fh
,
295 struct sk_buff
*skb
, struct tcmsg
*t
)
297 struct fw_filter
*f
= (struct fw_filter
*)fh
;
298 unsigned char *b
= skb
->tail
;
304 t
->tcm_handle
= f
->id
;
307 #ifdef CONFIG_NET_CLS_POLICE
313 rta
= (struct rtattr
*)b
;
314 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
317 RTA_PUT(skb
, TCA_FW_CLASSID
, 4, &f
->res
.classid
);
318 #ifdef CONFIG_NET_CLS_POLICE
320 struct rtattr
* p_rta
= (struct rtattr
*)skb
->tail
;
322 RTA_PUT(skb
, TCA_FW_POLICE
, 0, NULL
);
324 if (tcf_police_dump(skb
, f
->police
) < 0)
327 p_rta
->rta_len
= skb
->tail
- (u8
*)p_rta
;
331 rta
->rta_len
= skb
->tail
- b
;
332 #ifdef CONFIG_NET_CLS_POLICE
334 RTA_PUT(skb
, TCA_STATS
, sizeof(struct tc_stats
), &f
->police
->stats
);
340 skb_trim(skb
, b
- skb
->data
);
346 struct tcf_proto_ops cls_fw_ops
= {
358 #ifdef CONFIG_RTNETLINK
366 int init_module(void)
368 return register_tcf_proto_ops(&cls_fw_ops
);
371 void cleanup_module(void)
373 unregister_tcf_proto_ops(&cls_fw_ops
);