]> err.no Git - linux-2.6/blob - net/netfilter/xt_CONNMARK.c
[NETFILTER]: x_tables: make use of mass registation helpers
[linux-2.6] / net / netfilter / xt_CONNMARK.c
1 /* This kernel module is used to modify the connection mark values, or
2  * to optionally restore the skb nfmark from the connection mark
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 #include <linux/module.h>
22 #include <linux/skbuff.h>
23 #include <linux/ip.h>
24 #include <net/checksum.h>
25
26 MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27 MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS("ipt_CONNMARK");
30
31 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter/xt_CONNMARK.h>
33 #include <net/netfilter/nf_conntrack_compat.h>
34
35 static unsigned int
36 target(struct sk_buff **pskb,
37        const struct net_device *in,
38        const struct net_device *out,
39        unsigned int hooknum,
40        const struct xt_target *target,
41        const void *targinfo,
42        void *userinfo)
43 {
44         const struct xt_connmark_target_info *markinfo = targinfo;
45         u_int32_t diff;
46         u_int32_t nfmark;
47         u_int32_t newmark;
48         u_int32_t ctinfo;
49         u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo);
50
51         if (ctmark) {
52                 switch(markinfo->mode) {
53                 case XT_CONNMARK_SET:
54                         newmark = (*ctmark & ~markinfo->mask) | markinfo->mark;
55                         if (newmark != *ctmark) {
56                                 *ctmark = newmark;
57 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
58                                 ip_conntrack_event_cache(IPCT_MARK, *pskb);
59 #else
60                                 nf_conntrack_event_cache(IPCT_MARK, *pskb);
61 #endif
62                 }
63                         break;
64                 case XT_CONNMARK_SAVE:
65                         newmark = (*ctmark & ~markinfo->mask) |
66                                   ((*pskb)->nfmark & markinfo->mask);
67                         if (*ctmark != newmark) {
68                                 *ctmark = newmark;
69 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
70                                 ip_conntrack_event_cache(IPCT_MARK, *pskb);
71 #else
72                                 nf_conntrack_event_cache(IPCT_MARK, *pskb);
73 #endif
74                         }
75                         break;
76                 case XT_CONNMARK_RESTORE:
77                         nfmark = (*pskb)->nfmark;
78                         diff = (*ctmark ^ nfmark) & markinfo->mask;
79                         if (diff != 0)
80                                 (*pskb)->nfmark = nfmark ^ diff;
81                         break;
82                 }
83         }
84
85         return XT_CONTINUE;
86 }
87
88 static int
89 checkentry(const char *tablename,
90            const void *entry,
91            const struct xt_target *target,
92            void *targinfo,
93            unsigned int targinfosize,
94            unsigned int hook_mask)
95 {
96         struct xt_connmark_target_info *matchinfo = targinfo;
97
98         if (matchinfo->mode == XT_CONNMARK_RESTORE) {
99                 if (strcmp(tablename, "mangle") != 0) {
100                         printk(KERN_WARNING "CONNMARK: restore can only be "
101                                "called from \"mangle\" table, not \"%s\"\n",
102                                tablename);
103                         return 0;
104                 }
105         }
106         if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
107                 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
108                 return 0;
109         }
110         return 1;
111 }
112
113 static struct xt_target xt_connmark_target[] = {
114         {
115                 .name           = "CONNMARK",
116                 .family         = AF_INET,
117                 .checkentry     = checkentry,
118                 .target         = target,
119                 .targetsize     = sizeof(struct xt_connmark_target_info),
120                 .me             = THIS_MODULE
121         },
122         {
123                 .name           = "CONNMARK",
124                 .family         = AF_INET6,
125                 .checkentry     = checkentry,
126                 .target         = target,
127                 .targetsize     = sizeof(struct xt_connmark_target_info),
128                 .me             = THIS_MODULE
129         },
130 };
131
132 static int __init xt_connmark_init(void)
133 {
134         need_conntrack();
135         return xt_register_targets(xt_connmark_target,
136                                    ARRAY_SIZE(xt_connmark_target));
137 }
138
139 static void __exit xt_connmark_fini(void)
140 {
141         xt_unregister_targets(xt_connmark_target,
142                               ARRAY_SIZE(xt_connmark_target));
143 }
144
145 module_init(xt_connmark_init);
146 module_exit(xt_connmark_fini);