blob: 71f3c1a5d5e555bb80fb8b4d24d5bf60c1185c73 [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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070033static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070034match(const struct sk_buff *skb,
35 const struct net_device *in,
36 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080037 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 const void *matchinfo,
39 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080040 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070041 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Harald Welte2e4e6a12006-01-12 13:30:04 -080043 const struct xt_connmark_info *info = matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070044 const struct nf_conn *ct;
Patrick McHardy587aa642007-03-14 16:37:25 -070045 enum ip_conntrack_info ctinfo;
46
47 ct = nf_ct_get(skb, &ctinfo);
48 if (!ct)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Patrick McHardy587aa642007-03-14 16:37:25 -070051 return (((ct->mark) & info->mask) == info->mark) ^ info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070054static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070055checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080056 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -080057 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int hook_mask)
60{
Jan Engelhardta47362a2007-07-07 22:16:55 -070061 const struct xt_connmark_info *cm = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Harald Weltebf3a46a2005-08-09 19:22:01 -070063 if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
64 printk(KERN_WARNING "connmark: only support 32bit mark\n");
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070065 return false;
Harald Weltebf3a46a2005-08-09 19:22:01 -070066 }
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080067 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080068 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080069 "proto=%d\n", match->family);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070070 return false;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080071 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070072 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080075static void
Patrick McHardyefa74162006-08-22 00:36:37 -070076destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080077{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080078 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080079}
80
Patrick McHardyf1eda052006-09-20 12:06:25 -070081#ifdef CONFIG_COMPAT
82struct compat_xt_connmark_info {
83 compat_ulong_t mark, mask;
84 u_int8_t invert;
85 u_int8_t __pad1;
86 u_int16_t __pad2;
87};
88
89static void compat_from_user(void *dst, void *src)
90{
Jan Engelhardta47362a2007-07-07 22:16:55 -070091 const struct compat_xt_connmark_info *cm = src;
Patrick McHardyf1eda052006-09-20 12:06:25 -070092 struct xt_connmark_info m = {
93 .mark = cm->mark,
94 .mask = cm->mask,
95 .invert = cm->invert,
96 };
97 memcpy(dst, &m, sizeof(m));
98}
99
100static int compat_to_user(void __user *dst, void *src)
101{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700102 const struct xt_connmark_info *m = src;
Patrick McHardyf1eda052006-09-20 12:06:25 -0700103 struct compat_xt_connmark_info cm = {
104 .mark = m->mark,
105 .mask = m->mask,
106 .invert = m->invert,
107 };
108 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
109}
110#endif /* CONFIG_COMPAT */
111
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700112static struct xt_match xt_connmark_match[] = {
113 {
114 .name = "connmark",
115 .family = AF_INET,
116 .checkentry = checkentry,
117 .match = match,
118 .destroy = destroy,
119 .matchsize = sizeof(struct xt_connmark_info),
Patrick McHardyf1eda052006-09-20 12:06:25 -0700120#ifdef CONFIG_COMPAT
121 .compatsize = sizeof(struct compat_xt_connmark_info),
122 .compat_from_user = compat_from_user,
123 .compat_to_user = compat_to_user,
124#endif
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700125 .me = THIS_MODULE
126 },
127 {
128 .name = "connmark",
129 .family = AF_INET6,
130 .checkentry = checkentry,
131 .match = match,
132 .destroy = destroy,
133 .matchsize = sizeof(struct xt_connmark_info),
134 .me = THIS_MODULE
135 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800136};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800138static int __init xt_connmark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700140 return xt_register_matches(xt_connmark_match,
141 ARRAY_SIZE(xt_connmark_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800144static void __exit xt_connmark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Patrick McHardyf64ad5b2006-10-12 14:07:52 -0700146 xt_unregister_matches(xt_connmark_match, ARRAY_SIZE(xt_connmark_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800149module_init(xt_connmark_init);
150module_exit(xt_connmark_fini);