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 <asm/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/pkt_sched.h>
50 struct fw_filter *ht[256];
55 struct fw_filter *next;
57 struct tcf_result res;
58 #ifdef CONFIG_NET_CLS_ACT
59 struct tc_action *action;
60 #ifdef CONFIG_NET_CLS_IND
64 #ifdef CONFIG_NET_CLS_POLICE
65 struct tcf_police *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;
80 #ifdef CONFIG_NETFILTER
89 for (f=head->ht[fw_hash(id)]; f; f=f->next) {
92 #ifdef CONFIG_NET_CLS_ACT
93 #ifdef CONFIG_NET_CLS_IND
94 if (0 != f->indev[0]) {
95 if (NULL == skb->input_dev) {
98 if (0 != strcmp(f->indev, skb->input_dev->name)) {
105 int pol_res = tcf_action_exec(skb, f->action);
110 #ifdef CONFIG_NET_CLS_POLICE
112 return tcf_police(skb, f->police);
121 if (id && (TC_H_MAJ(id) == 0 ||
122 !(TC_H_MAJ(id^tp->q->handle)))) {
130 static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
132 struct fw_head *head = (struct fw_head*)tp->root;
138 for (f=head->ht[fw_hash(handle)]; f; f=f->next) {
140 return (unsigned long)f;
145 static void fw_put(struct tcf_proto *tp, unsigned long f)
149 static int fw_init(struct tcf_proto *tp)
154 static void fw_destroy(struct tcf_proto *tp)
156 struct fw_head *head = (struct fw_head*)xchg(&tp->root, NULL);
163 for (h=0; h<256; h++) {
164 while ((f=head->ht[h]) != NULL) {
166 head->ht[h] = f->next;
168 if ((cl = __cls_set_class(&f->res.class, 0)) != 0)
169 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
170 #ifdef CONFIG_NET_CLS_ACT
172 tcf_action_destroy(f->action,TCA_ACT_UNBIND);
175 #ifdef CONFIG_NET_CLS_POLICE
176 tcf_police_release(f->police,TCA_ACT_UNBIND);
186 static int fw_delete(struct tcf_proto *tp, unsigned long arg)
188 struct fw_head *head = (struct fw_head*)tp->root;
189 struct fw_filter *f = (struct fw_filter*)arg;
190 struct fw_filter **fp;
192 if (head == NULL || f == NULL)
195 for (fp=&head->ht[fw_hash(f->id)]; *fp; fp = &(*fp)->next) {
203 if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0)
204 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
205 #ifdef CONFIG_NET_CLS_ACT
207 tcf_action_destroy(f->action,TCA_ACT_UNBIND);
210 #ifdef CONFIG_NET_CLS_POLICE
211 tcf_police_release(f->police,TCA_ACT_UNBIND);
222 static int fw_change(struct tcf_proto *tp, unsigned long base,
227 struct fw_head *head = (struct fw_head*)tp->root;
229 struct rtattr *opt = tca[TCA_OPTIONS-1];
230 struct rtattr *tb[TCA_FW_MAX];
232 #ifdef CONFIG_NET_CLS_ACT
233 struct tc_action *act = NULL;
239 return handle ? -EINVAL : 0;
241 if (rtattr_parse(tb, TCA_FW_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
244 if ((f = (struct fw_filter*)*arg) != NULL) {
245 /* Node exists: adjust only classid */
247 if (f->id != handle && handle)
249 if (tb[TCA_FW_CLASSID-1]) {
252 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
253 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid);
254 cl = cls_set_class(tp, &f->res.class, cl);
256 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
258 #ifdef CONFIG_NET_CLS_ACT
259 if (tb[TCA_FW_POLICE-1]) {
260 act = kmalloc(sizeof(*act),GFP_KERNEL);
264 memset(act,0,sizeof(*act));
265 ret = tcf_action_init_1(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1] ,act,"police",TCA_ACT_NOREPLACE,TCA_ACT_BIND);
267 tcf_action_destroy(act,TCA_ACT_UNBIND);
270 act->type = TCA_OLD_COMPAT;
272 sch_tree_lock(tp->q);
273 act = xchg(&f->action, act);
274 sch_tree_unlock(tp->q);
276 tcf_action_destroy(act,TCA_ACT_UNBIND);
280 if(tb[TCA_FW_ACT-1]) {
281 act = kmalloc(sizeof(*act),GFP_KERNEL);
284 memset(act,0,sizeof(*act));
285 ret = tcf_action_init(tb[TCA_FW_ACT-1], tca[TCA_RATE-1],act,NULL, TCA_ACT_NOREPLACE,TCA_ACT_BIND);
287 tcf_action_destroy(act,TCA_ACT_UNBIND);
291 sch_tree_lock(tp->q);
292 act = xchg(&f->action, act);
293 sch_tree_unlock(tp->q);
295 tcf_action_destroy(act,TCA_ACT_UNBIND);
297 #ifdef CONFIG_NET_CLS_IND
298 if(tb[TCA_FW_INDEV-1]) {
299 struct rtattr *idev = tb[TCA_FW_INDEV-1];
300 if (RTA_PAYLOAD(idev) >= IFNAMSIZ) {
301 printk("cls_fw: bad indev name %s\n",(char*)RTA_DATA(idev));
305 memset(f->indev,0,IFNAMSIZ);
306 sprintf(f->indev, "%s", (char*)RTA_DATA(idev));
309 #else /* only POLICE defined */
310 #ifdef CONFIG_NET_CLS_POLICE
311 if (tb[TCA_FW_POLICE-1]) {
312 struct tcf_police *police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
315 police = xchg(&f->police, police);
318 tcf_police_release(police,TCA_ACT_UNBIND);
329 head = kmalloc(sizeof(struct fw_head), GFP_KERNEL);
332 memset(head, 0, sizeof(*head));
339 f = kmalloc(sizeof(struct fw_filter), GFP_KERNEL);
342 memset(f, 0, sizeof(*f));
346 if (tb[TCA_FW_CLASSID-1]) {
348 if (RTA_PAYLOAD(tb[TCA_FW_CLASSID-1]) != 4)
350 f->res.classid = *(u32*)RTA_DATA(tb[TCA_FW_CLASSID-1]);
351 cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
354 #ifdef CONFIG_NET_CLS_ACT
355 if(tb[TCA_FW_ACT-1]) {
356 act = kmalloc(sizeof(*act),GFP_KERNEL);
359 memset(act,0,sizeof(*act));
360 ret = tcf_action_init(tb[TCA_FW_ACT-1], tca[TCA_RATE-1],act,NULL,TCA_ACT_NOREPLACE,TCA_ACT_BIND);
362 tcf_action_destroy(act,TCA_ACT_UNBIND);
367 #ifdef CONFIG_NET_CLS_IND
368 if(tb[TCA_FW_INDEV-1]) {
369 struct rtattr *idev = tb[TCA_FW_INDEV-1];
370 if (RTA_PAYLOAD(idev) >= IFNAMSIZ) {
371 printk("cls_fw: bad indev name %s\n",(char*)RTA_DATA(idev));
375 memset(f->indev,0,IFNAMSIZ);
376 sprintf(f->indev, "%s", (char*)RTA_DATA(idev));
380 #ifdef CONFIG_NET_CLS_POLICE
381 if (tb[TCA_FW_POLICE-1])
382 f->police = tcf_police_locate(tb[TCA_FW_POLICE-1], tca[TCA_RATE-1]);
386 f->next = head->ht[fw_hash(handle)];
388 head->ht[fw_hash(handle)] = f;
391 *arg = (unsigned long)f;
400 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
402 struct fw_head *head = (struct fw_head*)tp->root;
411 for (h = 0; h < 256; h++) {
414 for (f = head->ht[h]; f; f = f->next) {
415 if (arg->count < arg->skip) {
419 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
428 static int fw_dump(struct tcf_proto *tp, unsigned long fh,
429 struct sk_buff *skb, struct tcmsg *t)
431 struct fw_filter *f = (struct fw_filter*)fh;
432 unsigned char *b = skb->tail;
438 t->tcm_handle = f->id;
441 #ifdef CONFIG_NET_CLS_ACT
444 #ifdef CONFIG_NET_CLS_POLICE
451 rta = (struct rtattr*)b;
452 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
455 RTA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
456 #ifdef CONFIG_NET_CLS_ACT
457 /* again for backward compatible mode - we want
458 * to work with both old and new modes of entering
459 * tc data even if iproute2 was newer - jhs
462 struct rtattr * p_rta = (struct rtattr*)skb->tail;
464 if (f->action->type != TCA_OLD_COMPAT) {
465 RTA_PUT(skb, TCA_FW_ACT, 0, NULL);
466 if (tcf_action_dump(skb,f->action,0,0) < 0) {
470 RTA_PUT(skb, TCA_FW_POLICE, 0, NULL);
471 if (tcf_action_dump_old(skb,f->action,0,0) < 0) {
476 p_rta->rta_len = skb->tail - (u8*)p_rta;
478 #ifdef CONFIG_NET_CLS_IND
479 if(strlen(f->indev)) {
480 struct rtattr * p_rta = (struct rtattr*)skb->tail;
481 RTA_PUT(skb, TCA_FW_INDEV, IFNAMSIZ, f->indev);
482 p_rta->rta_len = skb->tail - (u8*)p_rta;
486 #ifdef CONFIG_NET_CLS_POLICE
488 struct rtattr * p_rta = (struct rtattr*)skb->tail;
490 RTA_PUT(skb, TCA_FW_POLICE, 0, NULL);
492 if (tcf_police_dump(skb, f->police) < 0)
495 p_rta->rta_len = skb->tail - (u8*)p_rta;
500 rta->rta_len = skb->tail - b;
501 #ifdef CONFIG_NET_CLS_ACT
502 if (f->action && f->action->type == TCA_OLD_COMPAT) {
503 if (tcf_action_copy_stats(skb,f->action))
507 #ifdef CONFIG_NET_CLS_POLICE
509 if (qdisc_copy_stats(skb, &f->police->stats,
510 f->police->stats_lock))
518 skb_trim(skb, b - skb->data);
522 static struct tcf_proto_ops cls_fw_ops = {
525 .classify = fw_classify,
527 .destroy = fw_destroy,
534 .owner = THIS_MODULE,
537 static int __init init_fw(void)
539 return register_tcf_proto_ops(&cls_fw_ops);
542 static void __exit exit_fw(void)
544 unregister_tcf_proto_ops(&cls_fw_ops);
549 MODULE_LICENSE("GPL");