[NETFILTER]: Convert x_tables matches/targets to centralized error checking
[safe/jmp/linux-2.6] / net / netfilter / xt_MARK.c
1 /* This is a module which is used for setting the NFMARK field of an skb. */
2
3 /* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ip.h>
13 #include <net/checksum.h>
14
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter/xt_MARK.h>
17
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
20 MODULE_DESCRIPTION("ip[6]tables MARK modification module");
21 MODULE_ALIAS("ipt_MARK");
22 MODULE_ALIAS("ip6t_MARK");
23
24 static unsigned int
25 target_v0(struct sk_buff **pskb,
26           const struct net_device *in,
27           const struct net_device *out,
28           unsigned int hooknum,
29           const void *targinfo,
30           void *userinfo)
31 {
32         const struct xt_mark_target_info *markinfo = targinfo;
33
34         if((*pskb)->nfmark != markinfo->mark)
35                 (*pskb)->nfmark = markinfo->mark;
36
37         return XT_CONTINUE;
38 }
39
40 static unsigned int
41 target_v1(struct sk_buff **pskb,
42           const struct net_device *in,
43           const struct net_device *out,
44           unsigned int hooknum,
45           const void *targinfo,
46           void *userinfo)
47 {
48         const struct xt_mark_target_info_v1 *markinfo = targinfo;
49         int mark = 0;
50
51         switch (markinfo->mode) {
52         case XT_MARK_SET:
53                 mark = markinfo->mark;
54                 break;
55                 
56         case XT_MARK_AND:
57                 mark = (*pskb)->nfmark & markinfo->mark;
58                 break;
59                 
60         case XT_MARK_OR:
61                 mark = (*pskb)->nfmark | markinfo->mark;
62                 break;
63         }
64
65         if((*pskb)->nfmark != mark)
66                 (*pskb)->nfmark = mark;
67
68         return XT_CONTINUE;
69 }
70
71
72 static int
73 checkentry_v0(const char *tablename,
74               const void *entry,
75               void *targinfo,
76               unsigned int targinfosize,
77               unsigned int hook_mask)
78 {
79         struct xt_mark_target_info *markinfo = targinfo;
80
81         if (markinfo->mark > 0xffffffff) {
82                 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
83                 return 0;
84         }
85         return 1;
86 }
87
88 static int
89 checkentry_v1(const char *tablename,
90               const void *entry,
91               void *targinfo,
92               unsigned int targinfosize,
93               unsigned int hook_mask)
94 {
95         struct xt_mark_target_info_v1 *markinfo = targinfo;
96
97         if (markinfo->mode != XT_MARK_SET
98             && markinfo->mode != XT_MARK_AND
99             && markinfo->mode != XT_MARK_OR) {
100                 printk(KERN_WARNING "MARK: unknown mode %u\n",
101                        markinfo->mode);
102                 return 0;
103         }
104         if (markinfo->mark > 0xffffffff) {
105                 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
106                 return 0;
107         }
108         return 1;
109 }
110
111 static struct xt_target ipt_mark_reg_v0 = {
112         .name           = "MARK",
113         .target         = target_v0,
114         .targetsize     = sizeof(struct xt_mark_target_info),
115         .table          = "mangle",
116         .checkentry     = checkentry_v0,
117         .me             = THIS_MODULE,
118         .revision       = 0,
119 };
120
121 static struct xt_target ipt_mark_reg_v1 = {
122         .name           = "MARK",
123         .target         = target_v1,
124         .targetsize     = sizeof(struct xt_mark_target_info_v1),
125         .table          = "mangle",
126         .checkentry     = checkentry_v1,
127         .me             = THIS_MODULE,
128         .revision       = 1,
129 };
130
131 static struct xt_target ip6t_mark_reg_v0 = {
132         .name           = "MARK",
133         .target         = target_v0,
134         .targetsize     = sizeof(struct xt_mark_target_info),
135         .table          = "mangle",
136         .checkentry     = checkentry_v0,
137         .me             = THIS_MODULE,
138         .revision       = 0,
139 };
140
141 static int __init init(void)
142 {
143         int err;
144
145         err = xt_register_target(AF_INET, &ipt_mark_reg_v0);
146         if (err)
147                 return err;
148
149         err = xt_register_target(AF_INET, &ipt_mark_reg_v1);
150         if (err)
151                 xt_unregister_target(AF_INET, &ipt_mark_reg_v0);
152
153         err = xt_register_target(AF_INET6, &ip6t_mark_reg_v0);
154         if (err) {
155                 xt_unregister_target(AF_INET, &ipt_mark_reg_v0);
156                 xt_unregister_target(AF_INET, &ipt_mark_reg_v1);
157         }
158
159         return err;
160 }
161
162 static void __exit fini(void)
163 {
164         xt_unregister_target(AF_INET, &ipt_mark_reg_v0);
165         xt_unregister_target(AF_INET, &ipt_mark_reg_v1);
166         xt_unregister_target(AF_INET6, &ip6t_mark_reg_v0);
167 }
168
169 module_init(init);
170 module_exit(fini);