[NETFILTER]: xt_limit: don't reset state on unrelated rule updates
[linux-block.git] / net / netfilter / xt_CONNMARK.c
CommitLineData
1da177e4
LT
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
26MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28MODULE_LICENSE("GPL");
2e4e6a17 29MODULE_ALIAS("ipt_CONNMARK");
1da177e4 30
2e4e6a17
HW
31#include <linux/netfilter/x_tables.h>
32#include <linux/netfilter/xt_CONNMARK.h>
9fb9cbb1 33#include <net/netfilter/nf_conntrack_compat.h>
1da177e4
LT
34
35static unsigned int
36target(struct sk_buff **pskb,
37 const struct net_device *in,
38 const struct net_device *out,
39 unsigned int hooknum,
c4986734 40 const struct xt_target *target,
fe1cb108 41 const void *targinfo)
1da177e4 42{
2e4e6a17 43 const struct xt_connmark_target_info *markinfo = targinfo;
bf3a46aa
HW
44 u_int32_t diff;
45 u_int32_t nfmark;
46 u_int32_t newmark;
9fb9cbb1
YK
47 u_int32_t ctinfo;
48 u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo);
1da177e4 49
9fb9cbb1 50 if (ctmark) {
90528e6f
PM
51 switch(markinfo->mode) {
52 case XT_CONNMARK_SET:
53 newmark = (*ctmark & ~markinfo->mask) | markinfo->mark;
54 if (newmark != *ctmark) {
55 *ctmark = newmark;
0719bdf1 56#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
90528e6f 57 ip_conntrack_event_cache(IPCT_MARK, *pskb);
2521c12c 58#else
90528e6f 59 nf_conntrack_event_cache(IPCT_MARK, *pskb);
2521c12c
PNA
60#endif
61 }
90528e6f
PM
62 break;
63 case XT_CONNMARK_SAVE:
64 newmark = (*ctmark & ~markinfo->mask) |
65 ((*pskb)->nfmark & markinfo->mask);
66 if (*ctmark != newmark) {
67 *ctmark = newmark;
0719bdf1 68#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
90528e6f 69 ip_conntrack_event_cache(IPCT_MARK, *pskb);
2521c12c 70#else
90528e6f 71 nf_conntrack_event_cache(IPCT_MARK, *pskb);
2521c12c 72#endif
90528e6f
PM
73 }
74 break;
75 case XT_CONNMARK_RESTORE:
76 nfmark = (*pskb)->nfmark;
77 diff = (*ctmark ^ nfmark) & markinfo->mask;
78 if (diff != 0)
79 (*pskb)->nfmark = nfmark ^ diff;
80 break;
2521c12c 81 }
1da177e4
LT
82 }
83
2e4e6a17 84 return XT_CONTINUE;
1da177e4
LT
85}
86
87static int
88checkentry(const char *tablename,
2e4e6a17 89 const void *entry,
c4986734 90 const struct xt_target *target,
1da177e4 91 void *targinfo,
1da177e4
LT
92 unsigned int hook_mask)
93{
2e4e6a17 94 struct xt_connmark_target_info *matchinfo = targinfo;
1da177e4 95
2e4e6a17 96 if (matchinfo->mode == XT_CONNMARK_RESTORE) {
90528e6f
PM
97 if (strcmp(tablename, "mangle") != 0) {
98 printk(KERN_WARNING "CONNMARK: restore can only be "
99 "called from \"mangle\" table, not \"%s\"\n",
100 tablename);
101 return 0;
102 }
1da177e4 103 }
bf3a46aa
HW
104 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
105 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
106 return 0;
107 }
1da177e4
LT
108 return 1;
109}
110
4470bbc7
PM
111static struct xt_target xt_connmark_target[] = {
112 {
113 .name = "CONNMARK",
114 .family = AF_INET,
115 .checkentry = checkentry,
116 .target = target,
117 .targetsize = sizeof(struct xt_connmark_target_info),
118 .me = THIS_MODULE
119 },
120 {
121 .name = "CONNMARK",
122 .family = AF_INET6,
123 .checkentry = checkentry,
124 .target = target,
125 .targetsize = sizeof(struct xt_connmark_target_info),
126 .me = THIS_MODULE
127 },
1da177e4
LT
128};
129
65b4b4e8 130static int __init xt_connmark_init(void)
1da177e4 131{
2e4e6a17 132 need_conntrack();
4470bbc7
PM
133 return xt_register_targets(xt_connmark_target,
134 ARRAY_SIZE(xt_connmark_target));
1da177e4
LT
135}
136
65b4b4e8 137static void __exit xt_connmark_fini(void)
1da177e4 138{
4470bbc7
PM
139 xt_unregister_targets(xt_connmark_target,
140 ARRAY_SIZE(xt_connmark_target));
1da177e4
LT
141}
142
65b4b4e8
AM
143module_init(xt_connmark_init);
144module_exit(xt_connmark_fini);