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 * 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).
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <asm/uaccess.h>
19 #include <asm/system.h>
20 #include <asm/bitops.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
29 #include <linux/errno.h>
30 #include <linux/interrupt.h>
31 #include <linux/if_ether.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/notifier.h>
36 #include <linux/netfilter.h>
38 #include <net/route.h>
39 #include <linux/skbuff.h>
41 #include <net/pkt_sched.h>
45 struct fw_filter *ht[256];
50 struct fw_filter *next;
52 struct tcf_result res;
53 #ifdef CONFIG_NET_CLS_POLICE
54 struct tcf_police *police;
58 static __inline__ int fw_hash(u32 handle)
63 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
64 struct tcf_result *res)
66 struct fw_head *head = (struct fw_head*)tp->root;
68 #ifdef CONFIG_NETFILTER
77 for (f=head->ht[fw_hash(id)]; f; f=f->next) {
80 #ifdef CONFIG_NET_CLS_POLICE
82 return tcf_police(skb, f->police);
90 if (id && (TC_H_MAJ(id) == 0 ||
91 !(TC_H_MAJ(id^tp->q->handle)))) {
99 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
101 struct fw_head *head = (struct fw_head*)tp->root;
107 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
109 return (unsigned long)f;
114 static void fw_put(struct tcf_proto *tp, unsigned long f)
118 static int fw_init(struct tcf_proto *tp)
123 static void fw_destroy(struct tcf_proto *tp)
125 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
132 for (h=0; h<256; h++) {
133 while ((f=head->ht[h]) != NULL) {
135 head->ht[h] = f->next;
137 if ((cl = __cls_set_class(&f->res.class, 0)) != 0)
138 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
139 #ifdef CONFIG_NET_CLS_POLICE
140 tcf_police_release(f->police);
148 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
150 struct fw_head *head = (struct fw_head*)tp->root;
151 struct fw_filter *f = (struct fw_filter*)arg;
152 struct fw_filter **fp;
154 if (head == NULL || f == NULL)
157 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
165 if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0)
166 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
167 #ifdef CONFIG_NET_CLS_POLICE
168 tcf_police_release(f->police);
178 static int fw_change(struct tcf_proto *tp, unsigned long base,
183 struct fw_head *head = (struct fw_head*)tp->root;
185 struct rtattr *opt = tca[TCA_OPTIONS-1];
186 struct rtattr *tb[TCA_FW_MAX];
190 return handle ? -EINVAL : 0;
192 if (rtattr_parse(tb, TCA_FW_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
195 if ((f = (struct fw_filter*)*arg) != NULL) {
196 /* Node exists: adjust only classid */
198 if (f->id != handle && handle)
200 if (tb[TCA_FW_CLASSID-1]) {
203 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
204 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid);
205 cl = cls_set_class(tp, &f->res.class, cl);
207 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
209 #ifdef CONFIG_NET_CLS_POLICE
210 if (tb[TCA_FW_POLICE-1]) {
211 struct tcf_police *police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
214 police = xchg(&f->police, police);
217 tcf_police_release(police);
227 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
230 memset(head, 0, sizeof(*head));
237 f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
240 memset(f, 0, sizeof(*f));
244 if (tb[TCA_FW_CLASSID-1]) {
246 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != 4)
248 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
249 cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
252 #ifdef CONFIG_NET_CLS_POLICE
253 if (tb[TCA_FW_POLICE-1])
254 f->police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
257 f->next = head->ht[fw_hash(handle)];
259 head->ht[fw_hash(handle)] = f;
262 *arg = (unsigned long)f;
271 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
273 struct fw_head *head = (struct fw_head*)tp->root;
282 for (h = 0; h < 256; h++) {
285 for (f = head->ht[h]; f; f = f->next) {
286 if (arg->count < arg->skip) {
290 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
299 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
300 struct sk_buff *skb, struct tcmsg *t)
302 struct fw_filter *f = (struct fw_filter*)fh;
303 unsigned char *b = skb->tail;
309 t->tcm_handle = f->id;
312 #ifdef CONFIG_NET_CLS_POLICE
318 rta = (struct rtattr*)b;
319 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
322 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
323 #ifdef CONFIG_NET_CLS_POLICE
325 struct rtattr * p_rta = (struct rtattr*)skb->tail;
327 RTA_PUT(skb, TCA_FW_POLICE, 0, NULL);
329 if (tcf_police_dump(skb, f->police) < 0)
332 p_rta->rta_len = skb->tail - (u8*)p_rta;
336 rta->rta_len = skb->tail - b;
337 #ifdef CONFIG_NET_CLS_POLICE
339 if (qdisc_copy_stats(skb, &f->police->stats))
346 skb_trim(skb, b - skb->data);
350 static struct tcf_proto_ops cls_fw_ops = {
353 .classify = fw_classify,
355 .destroy = fw_destroy,
362 .owner = THIS_MODULE,
365 static int __init init_fw(void)
367 return register_tcf_proto_ops(&cls_fw_ops);
370 static void __exit exit_fw(void)
372 unregister_tcf_proto_ops(&cls_fw_ops);
377 MODULE_LICENSE("GPL");