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)
124 static void fw_destroy(struct tcf_proto *tp)
126 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
135 for (h=0; h<256; h++) {
136 while ((f=head->ht[h]) != NULL) {
138 head->ht[h] = f->next;
140 if ((cl = __cls_set_class(&f->res.class, 0)) != 0)
141 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
142 #ifdef CONFIG_NET_CLS_POLICE
143 tcf_police_release(f->police);
152 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
154 struct fw_head *head = (struct fw_head*)tp->root;
155 struct fw_filter *f = (struct fw_filter*)arg;
156 struct fw_filter **fp;
158 if (head == NULL || f == NULL)
161 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
169 if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0)
170 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
171 #ifdef CONFIG_NET_CLS_POLICE
172 tcf_police_release(f->police);
181 static int fw_change(struct tcf_proto *tp, unsigned long base,
186 struct fw_head *head = (struct fw_head*)tp->root;
188 struct rtattr *opt = tca[TCA_OPTIONS-1];
189 struct rtattr *tb[TCA_FW_MAX];
193 return handle ? -EINVAL : 0;
195 if (rtattr_parse(tb, TCA_FW_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
198 if ((f = (struct fw_filter*)*arg) != NULL) {
199 /* Node exists: adjust only classid */
201 if (f->id != handle && handle)
203 if (tb[TCA_FW_CLASSID-1]) {
206 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
207 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid);
208 cl = cls_set_class(tp, &f->res.class, cl);
210 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
212 #ifdef CONFIG_NET_CLS_POLICE
213 if (tb[TCA_FW_POLICE-1]) {
214 struct tcf_police *police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
217 police = xchg(&f->police, police);
220 tcf_police_release(police);
230 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
233 memset(head, 0, sizeof(*head));
240 f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
243 memset(f, 0, sizeof(*f));
247 if (tb[TCA_FW_CLASSID-1]) {
249 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != 4)
251 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
252 cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
255 #ifdef CONFIG_NET_CLS_POLICE
256 if (tb[TCA_FW_POLICE-1])
257 f->police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
260 f->next = head->ht[fw_hash(handle)];
262 head->ht[fw_hash(handle)] = f;
265 *arg = (unsigned long)f;
274 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
276 struct fw_head *head = (struct fw_head*)tp->root;
285 for (h = 0; h < 256; h++) {
288 for (f = head->ht[h]; f; f = f->next) {
289 if (arg->count < arg->skip) {
293 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
302 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
303 struct sk_buff *skb, struct tcmsg *t)
305 struct fw_filter *f = (struct fw_filter*)fh;
306 unsigned char *b = skb->tail;
312 t->tcm_handle = f->id;
315 #ifdef CONFIG_NET_CLS_POLICE
321 rta = (struct rtattr*)b;
322 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
325 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
326 #ifdef CONFIG_NET_CLS_POLICE
328 struct rtattr * p_rta = (struct rtattr*)skb->tail;
330 RTA_PUT(skb, TCA_FW_POLICE, 0, NULL);
332 if (tcf_police_dump(skb, f->police) < 0)
335 p_rta->rta_len = skb->tail - (u8*)p_rta;
339 rta->rta_len = skb->tail - b;
340 #ifdef CONFIG_NET_CLS_POLICE
342 if (qdisc_copy_stats(skb, &f->police->stats))
349 skb_trim(skb, b - skb->data);
353 struct tcf_proto_ops cls_fw_ops = {
369 int init_module(void)
371 return register_tcf_proto_ops(&cls_fw_ops);
374 void cleanup_module(void)
376 unregister_tcf_proto_ops(&cls_fw_ops);
379 MODULE_LICENSE("GPL");