blob: 56324c8aff0a76c9507fc4133dd5ad194d360bfb [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>
24
25MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
26MODULE_DESCRIPTION("IP tables connmark match module");
27MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080028MODULE_ALIAS("ipt_connmark");
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Harald Welte2e4e6a12006-01-12 13:30:04 -080030#include <linux/netfilter/x_tables.h>
31#include <linux/netfilter/xt_connmark.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032#include <net/netfilter/nf_conntrack_compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static int
35match(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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int *hotdrop)
43{
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 const struct xt_connmark_info *info = matchinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045 u_int32_t ctinfo;
46 const u_int32_t *ctmark = nf_ct_get_mark(skb, &ctinfo);
47 if (!ctmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return 0;
49
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050 return (((*ctmark) & info->mask) == info->mark) ^ info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static int
54checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080055 const void *ip,
Patrick McHardyc4986732006-03-20 18:02:56 -080056 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 void *matchinfo,
58 unsigned int matchsize,
59 unsigned int hook_mask)
60{
Patrick McHardy3e72b2f2006-05-29 18:19:19 -070061 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");
65 return 0;
66 }
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080067#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
68 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
69 printk(KERN_WARNING "can't load nf_conntrack support for "
70 "proto=%d\n", match->family);
71 return 0;
72 }
73#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 1;
75}
76
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080077static void
78destroy(const struct xt_match *match, void *matchinfo, unsigned int matchsize)
79{
80#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
81 nf_ct_l3proto_module_put(match->family);
82#endif
83}
84
Harald Welte2e4e6a12006-01-12 13:30:04 -080085static struct xt_match connmark_match = {
Patrick McHardy5d04bff2006-03-20 18:01:58 -080086 .name = "connmark",
87 .match = match,
88 .matchsize = sizeof(struct xt_connmark_info),
89 .checkentry = checkentry,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080090 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080091 .family = AF_INET,
Patrick McHardy5d04bff2006-03-20 18:01:58 -080092 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
Patrick McHardy5d04bff2006-03-20 18:01:58 -080095static struct xt_match connmark6_match = {
96 .name = "connmark",
97 .match = match,
98 .matchsize = sizeof(struct xt_connmark_info),
99 .checkentry = checkentry,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800100 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800101 .family = AF_INET6,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800102 .me = THIS_MODULE
103};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800105static int __init xt_connmark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800107 int ret;
108
109 need_conntrack();
110
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800111 ret = xt_register_match(&connmark_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800112 if (ret)
113 return ret;
114
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800115 ret = xt_register_match(&connmark6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800116 if (ret)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800117 xt_unregister_match(&connmark_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800121static void __exit xt_connmark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800123 xt_unregister_match(&connmark6_match);
124 xt_unregister_match(&connmark_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800127module_init(xt_connmark_init);
128module_exit(xt_connmark_fini);