[NETFILTER]: Convert x_tables matches/targets to centralized error checking
[safe/jmp/linux-2.6] / net / netfilter / xt_connmark.c
1 /* This kernel module matches connection mark values set by the
2  * CONNMARK target
3  *
4  * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5  * by Henrik Nordstrom <hno@marasystems.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24
25 MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
26 MODULE_DESCRIPTION("IP tables connmark match module");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("ipt_connmark");
29
30 #include <linux/netfilter/x_tables.h>
31 #include <linux/netfilter/xt_connmark.h>
32 #include <net/netfilter/nf_conntrack_compat.h>
33
34 static int
35 match(const struct sk_buff *skb,
36       const struct net_device *in,
37       const struct net_device *out,
38       const void *matchinfo,
39       int offset,
40       unsigned int protoff,
41       int *hotdrop)
42 {
43         const struct xt_connmark_info *info = matchinfo;
44         u_int32_t ctinfo;
45         const u_int32_t *ctmark = nf_ct_get_mark(skb, &ctinfo);
46         if (!ctmark)
47                 return 0;
48
49         return (((*ctmark) & info->mask) == info->mark) ^ info->invert;
50 }
51
52 static int
53 checkentry(const char *tablename,
54            const void *ip,
55            void *matchinfo,
56            unsigned int matchsize,
57            unsigned int hook_mask)
58 {
59         struct xt_connmark_info *cm = (struct xt_connmark_info *)matchinfo;
60
61         if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
62                 printk(KERN_WARNING "connmark: only support 32bit mark\n");
63                 return 0;
64         }
65         return 1;
66 }
67
68 static struct xt_match connmark_match = {
69         .name           = "connmark",
70         .match          = match,
71         .matchsize      = sizeof(struct xt_connmark_info),
72         .checkentry     = checkentry,
73         .me             = THIS_MODULE
74 };
75
76 static struct xt_match connmark6_match = {
77         .name           = "connmark",
78         .match          = match,
79         .matchsize      = sizeof(struct xt_connmark_info),
80         .checkentry     = checkentry,
81         .me             = THIS_MODULE
82 };
83
84 static int __init init(void)
85 {
86         int ret;
87
88         need_conntrack();
89
90         ret = xt_register_match(AF_INET, &connmark_match);
91         if (ret)
92                 return ret;
93
94         ret = xt_register_match(AF_INET6, &connmark6_match);
95         if (ret)
96                 xt_unregister_match(AF_INET, &connmark_match);
97         return ret;
98 }
99
100 static void __exit fini(void)
101 {
102         xt_unregister_match(AF_INET6, &connmark6_match);
103         xt_unregister_match(AF_INET, &connmark_match);
104 }
105
106 module_init(init);
107 module_exit(fini);