blob: 1071fc54d6d3c8811f4404773636ec15083d9cc3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Patrick McHardy587aa642007-03-14 16:37:25 -070024#include <net/netfilter/nf_conntrack.h>
25#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_connmark.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
29MODULE_DESCRIPTION("IP tables connmark match module");
30MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080031MODULE_ALIAS("ipt_connmark");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070032MODULE_ALIAS("ip6t_connmark");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070034static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070035match(const struct sk_buff *skb,
36 const struct net_device *in,
37 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080038 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 const void *matchinfo,
40 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070042 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 const struct xt_connmark_info *info = matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070045 const struct nf_conn *ct;
Patrick McHardy587aa642007-03-14 16:37:25 -070046 enum ip_conntrack_info ctinfo;
47
48 ct = nf_ct_get(skb, &ctinfo);
49 if (!ct)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070052 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070055static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070056checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080057 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -080058 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned int hook_mask)
61{
Jan Engelhardta47362a2007-07-07 22:16:55 -070062 const struct xt_connmark_info *cm = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Harald Weltebf3a46a2005-08-09 19:22:01 -070064 if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
65 printk(KERN_WARNING "connmark: only support 32bit mark\n");
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070066 return false;
Harald Weltebf3a46a2005-08-09 19:22:01 -070067 }
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080068 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080069 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080070 "proto=%d\n", match->family);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070071 return false;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080072 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070073 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080076static void
Patrick McHardyefa74162006-08-22 00:36:37 -070077destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080078{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080079 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080080}
81
Patrick McHardyf1eda052006-09-20 12:06:25 -070082#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{
Jan Engelhardta47362a2007-07-07 22:16:55 -070092 const struct compat_xt_connmark_info *cm = src;
Patrick McHardyf1eda052006-09-20 12:06:25 -070093 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{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700103 const struct xt_connmark_info *m = src;
Patrick McHardyf1eda052006-09-20 12:06:25 -0700104 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
Patrick McHardy9f15c532007-07-07 22:22:02 -0700113static struct xt_match xt_connmark_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700114 {
115 .name = "connmark",
116 .family = AF_INET,
117 .checkentry = checkentry,
118 .match = match,
119 .destroy = destroy,
120 .matchsize = sizeof(struct xt_connmark_info),
Patrick McHardyf1eda052006-09-20 12:06:25 -0700121#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
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700126 .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 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800137};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800139static int __init xt_connmark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700141 return xt_register_matches(xt_connmark_match,
142 ARRAY_SIZE(xt_connmark_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800145static void __exit xt_connmark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Patrick McHardyf64ad5b2006-10-12 14:07:52 -0700147 xt_unregister_matches(xt_connmark_match, ARRAY_SIZE(xt_connmark_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800150module_init(xt_connmark_init);
151module_exit(xt_connmark_fini);