]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_connmark.c
[NETFILTER]: replace list_for_each with list_for_each_entry
[net-next-2.6.git] / net / netfilter / xt_connmark.c
CommitLineData
1da177e4
LT
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>
587aa641
PM
24#include <net/netfilter/nf_conntrack.h>
25#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_connmark.h>
1da177e4 27
3a4fa0a2 28MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
1da177e4
LT
29MODULE_DESCRIPTION("IP tables connmark match module");
30MODULE_LICENSE("GPL");
2e4e6a17 31MODULE_ALIAS("ipt_connmark");
73aaf935 32MODULE_ALIAS("ip6t_connmark");
1da177e4 33
1d93a9cb 34static bool
1da177e4
LT
35match(const struct sk_buff *skb,
36 const struct net_device *in,
37 const struct net_device *out,
c4986734 38 const struct xt_match *match,
1da177e4
LT
39 const void *matchinfo,
40 int offset,
2e4e6a17 41 unsigned int protoff,
cff533ac 42 bool *hotdrop)
1da177e4 43{
2e4e6a17 44 const struct xt_connmark_info *info = matchinfo;
a47362a2 45 const struct nf_conn *ct;
587aa641
PM
46 enum ip_conntrack_info ctinfo;
47
48 ct = nf_ct_get(skb, &ctinfo);
49 if (!ct)
1d93a9cb 50 return false;
1da177e4 51
7c4e36bc 52 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
1da177e4
LT
53}
54
ccb79bdc 55static bool
1da177e4 56checkentry(const char *tablename,
2e4e6a17 57 const void *ip,
c4986734 58 const struct xt_match *match,
1da177e4 59 void *matchinfo,
1da177e4
LT
60 unsigned int hook_mask)
61{
a47362a2 62 const struct xt_connmark_info *cm = matchinfo;
1da177e4 63
bf3a46aa
HW
64 if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
65 printk(KERN_WARNING "connmark: only support 32bit mark\n");
ccb79bdc 66 return false;
bf3a46aa 67 }
b9f78f9f 68 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
fe0b9294 69 printk(KERN_WARNING "can't load conntrack support for "
b9f78f9f 70 "proto=%d\n", match->family);
ccb79bdc 71 return false;
b9f78f9f 72 }
ccb79bdc 73 return true;
1da177e4
LT
74}
75
b9f78f9f 76static void
efa74165 77destroy(const struct xt_match *match, void *matchinfo)
b9f78f9f 78{
b9f78f9f 79 nf_ct_l3proto_module_put(match->family);
b9f78f9f
PNA
80}
81
f1eda053
PM
82#ifdef CONFIG_COMPAT
83struct compat_xt_connmark_info {
84 compat_ulong_t mark, mask;
85 u_int8_t invert;
86 u_int8_t __pad1;
87 u_int16_t __pad2;
88};
89
90static void compat_from_user(void *dst, void *src)
91{
a47362a2 92 const struct compat_xt_connmark_info *cm = src;
f1eda053
PM
93 struct xt_connmark_info m = {
94 .mark = cm->mark,
95 .mask = cm->mask,
96 .invert = cm->invert,
97 };
98 memcpy(dst, &m, sizeof(m));
99}
100
101static int compat_to_user(void __user *dst, void *src)
102{
a47362a2 103 const struct xt_connmark_info *m = src;
f1eda053
PM
104 struct compat_xt_connmark_info cm = {
105 .mark = m->mark,
106 .mask = m->mask,
107 .invert = m->invert,
108 };
109 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
110}
111#endif /* CONFIG_COMPAT */
112
9f15c530 113static struct xt_match xt_connmark_match[] __read_mostly = {
4470bbc7
PM
114 {
115 .name = "connmark",
116 .family = AF_INET,
117 .checkentry = checkentry,
118 .match = match,
119 .destroy = destroy,
120 .matchsize = sizeof(struct xt_connmark_info),
f1eda053
PM
121#ifdef CONFIG_COMPAT
122 .compatsize = sizeof(struct compat_xt_connmark_info),
123 .compat_from_user = compat_from_user,
124 .compat_to_user = compat_to_user,
125#endif
4470bbc7
PM
126 .me = THIS_MODULE
127 },
128 {
129 .name = "connmark",
130 .family = AF_INET6,
131 .checkentry = checkentry,
132 .match = match,
133 .destroy = destroy,
134 .matchsize = sizeof(struct xt_connmark_info),
135 .me = THIS_MODULE
136 },
1da177e4
LT
137};
138
65b4b4e8 139static int __init xt_connmark_init(void)
1da177e4 140{
4470bbc7
PM
141 return xt_register_matches(xt_connmark_match,
142 ARRAY_SIZE(xt_connmark_match));
1da177e4
LT
143}
144
65b4b4e8 145static void __exit xt_connmark_fini(void)
1da177e4 146{
f64ad5bb 147 xt_unregister_matches(xt_connmark_match, ARRAY_SIZE(xt_connmark_match));
1da177e4
LT
148}
149
65b4b4e8
AM
150module_init(xt_connmark_init);
151module_exit(xt_connmark_fini);