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).
14 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
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 */
62 #ifdef CONFIG_NET_CLS_ACT
63 struct tc_action *action;
64 #else /* CONFIG_NET_CLS_ACT */
65 #ifdef CONFIG_NET_CLS_POLICE
66 struct tcf_police *police;
67 #endif /* CONFIG_NET_CLS_POLICE */
68 #endif /* CONFIG_NET_CLS_ACT */
71 static __inline__ int fw_hash(u32 handle)
76 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
77 struct tcf_result *res)
79 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 #ifdef CONFIG_NET_CLS_ACT
97 int act_res = tcf_action_exec(skb, f->action, res);
102 #else /* CONFIG_NET_CLS_ACT */
103 #ifdef CONFIG_NET_CLS_POLICE
105 return tcf_police(skb, f->police);
106 #endif /* CONFIG_NET_CLS_POLICE */
107 #endif /* CONFIG_NET_CLS_ACT */
113 if (id && (TC_H_MAJ(id) == 0 || !(TC_H_MAJ(id^tp->q->handle)))) {
123 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
125 struct fw_head *head = (struct fw_head*)tp->root;
131 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
133 return (unsigned long)f;
138 static void fw_put(struct tcf_proto *tp, unsigned long f)
142 static int fw_init(struct tcf_proto *tp)
147 static void fw_destroy(struct tcf_proto *tp)
149 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
156 for (h=0; h<256; h++) {
157 while ((f=head->ht[h]) != NULL) {
158 head->ht[h] = f->next;
159 tcf_unbind_filter(tp, &f->res);
160 #ifdef CONFIG_NET_CLS_ACT
162 tcf_action_destroy(f->action, TCA_ACT_UNBIND);
163 #else /* CONFIG_NET_CLS_ACT */
164 #ifdef CONFIG_NET_CLS_POLICE
166 tcf_police_release(f->police, TCA_ACT_UNBIND);
167 #endif /* CONFIG_NET_CLS_POLICE */
168 #endif /* CONFIG_NET_CLS_ACT */
176 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
178 struct fw_head *head = (struct fw_head*)tp->root;
179 struct fw_filter *f = (struct fw_filter*)arg;
180 struct fw_filter **fp;
182 if (head == NULL || f == NULL)
185 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
190 tcf_unbind_filter(tp, &f->res);
191 #ifdef CONFIG_NET_CLS_ACT
193 tcf_action_destroy(f->action,TCA_ACT_UNBIND);
194 #else /* CONFIG_NET_CLS_ACT */
195 #ifdef CONFIG_NET_CLS_POLICE
196 tcf_police_release(f->police,TCA_ACT_UNBIND);
197 #endif /* CONFIG_NET_CLS_POLICE */
198 #endif /* CONFIG_NET_CLS_ACT */
208 fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
209 struct rtattr **tb, struct rtattr **tca, unsigned long base)
213 if (tb[TCA_FW_CLASSID-1]) {
214 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != sizeof(u32))
216 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
217 tcf_bind_filter(tp, &f->res, base);
220 #ifdef CONFIG_NET_CLS_IND
221 if (tb[TCA_FW_INDEV-1]) {
222 err = tcf_change_indev(tp, f->indev, tb[TCA_FW_INDEV-1]);
226 #endif /* CONFIG_NET_CLS_IND */
228 #ifdef CONFIG_NET_CLS_ACT
229 if (tb[TCA_FW_POLICE-1]) {
230 err = tcf_change_act_police(tp, &f->action, tb[TCA_FW_POLICE-1],
236 if (tb[TCA_FW_ACT-1]) {
237 err = tcf_change_act(tp, &f->action, tb[TCA_FW_ACT-1],
242 #else /* CONFIG_NET_CLS_ACT */
243 #ifdef CONFIG_NET_CLS_POLICE
244 if (tb[TCA_FW_POLICE-1]) {
245 err = tcf_change_police(tp, &f->police, tb[TCA_FW_POLICE-1],
250 #endif /* CONFIG_NET_CLS_POLICE */
251 #endif /* CONFIG_NET_CLS_ACT */
258 static int fw_change(struct tcf_proto *tp, unsigned long base,
263 struct fw_head *head = (struct fw_head*)tp->root;
264 struct fw_filter *f = (struct fw_filter *) *arg;
265 struct rtattr *opt = tca[TCA_OPTIONS-1];
266 struct rtattr *tb[TCA_FW_MAX];
270 return handle ? -EINVAL : 0;
272 if (rtattr_parse(tb, TCA_FW_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
276 if (f->id != handle && handle)
278 return fw_change_attrs(tp, f, tb, tca, base);
285 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
288 memset(head, 0, sizeof(*head));
295 f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
298 memset(f, 0, sizeof(*f));
302 err = fw_change_attrs(tp, f, tb, tca, base);
306 f->next = head->ht[fw_hash(handle)];
308 head->ht[fw_hash(handle)] = f;
311 *arg = (unsigned long)f;
320 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
322 struct fw_head *head = (struct fw_head*)tp->root;
331 for (h = 0; h < 256; h++) {
334 for (f = head->ht[h]; f; f = f->next) {
335 if (arg->count < arg->skip) {
339 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
348 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
349 struct sk_buff *skb, struct tcmsg *t)
351 struct fw_filter *f = (struct fw_filter*)fh;
352 unsigned char *b = skb->tail;
358 t->tcm_handle = f->id;
361 #ifdef CONFIG_NET_CLS_ACT
364 #ifdef CONFIG_NET_CLS_POLICE
371 rta = (struct rtattr*)b;
372 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
375 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
376 #ifdef CONFIG_NET_CLS_IND
377 if (strlen(f->indev))
378 RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
379 #endif /* CONFIG_NET_CLS_IND */
380 #ifdef CONFIG_NET_CLS_ACT
381 if (tcf_dump_act(skb, f->action, TCA_FW_ACT, TCA_FW_POLICE) < 0)
383 #else /* CONFIG_NET_CLS_ACT */
384 #ifdef CONFIG_NET_CLS_POLICE
385 if (tcf_dump_police(skb, f->police, TCA_FW_POLICE) < 0)
387 #endif /* CONFIG_NET_CLS_POLICE */
388 #endif /* CONFIG_NET_CLS_ACT */
390 rta->rta_len = skb->tail - b;
391 #ifdef CONFIG_NET_CLS_ACT
392 if (f->action && f->action->type == TCA_OLD_COMPAT) {
393 if (tcf_action_copy_stats(skb,f->action))
396 #else /* CONFIG_NET_CLS_ACT */
397 #ifdef CONFIG_NET_CLS_POLICE
399 if (tcf_police_dump_stats(skb, f->police) < 0)
401 #endif /* CONFIG_NET_CLS_POLICE */
402 #endif /* CONFIG_NET_CLS_ACT */
406 skb_trim(skb, b - skb->data);
410 static struct tcf_proto_ops cls_fw_ops = {
413 .classify = fw_classify,
415 .destroy = fw_destroy,
422 .owner = THIS_MODULE,
425 static int __init init_fw(void)
427 return register_tcf_proto_ops(&cls_fw_ops);
430 static void __exit exit_fw(void)
432 unregister_tcf_proto_ops(&cls_fw_ops);
437 MODULE_LICENSE("GPL");