blob: d06e925032dab42562b03e7deee4d6cc6a8f3ad8 [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,
38 const void *matchinfo,
39 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080040 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 int *hotdrop)
42{
Harald Welte2e4e6a12006-01-12 13:30:04 -080043 const struct xt_connmark_info *info = matchinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044 u_int32_t ctinfo;
45 const u_int32_t *ctmark = nf_ct_get_mark(skb, &ctinfo);
46 if (!ctmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return 0;
48
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049 return (((*ctmark) & info->mask) == info->mark) ^ info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static int
53checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 const void *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void *matchinfo,
56 unsigned int matchsize,
57 unsigned int hook_mask)
58{
Harald Welte2e4e6a12006-01-12 13:30:04 -080059 struct xt_connmark_info *cm =
60 (struct xt_connmark_info *)matchinfo;
61 if (matchsize != XT_ALIGN(sizeof(struct xt_connmark_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63
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");
66 return 0;
67 }
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 1;
70}
71
Harald Welte2e4e6a12006-01-12 13:30:04 -080072static struct xt_match connmark_match = {
73 .name = "connmark",
74 .match = &match,
75 .checkentry = &checkentry,
76 .me = THIS_MODULE
77};
78static struct xt_match connmark6_match = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .name = "connmark",
80 .match = &match,
81 .checkentry = &checkentry,
82 .me = THIS_MODULE
83};
84
Harald Welte2e4e6a12006-01-12 13:30:04 -080085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int __init init(void)
87{
Harald Welte2e4e6a12006-01-12 13:30:04 -080088 int ret;
89
90 need_conntrack();
91
92 ret = xt_register_match(AF_INET, &connmark_match);
93 if (ret)
94 return ret;
95
96 ret = xt_register_match(AF_INET6, &connmark6_match);
97 if (ret)
98 xt_unregister_match(AF_INET, &connmark_match);
99 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void __exit fini(void)
103{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104 xt_unregister_match(AF_INET6, &connmark6_match);
105 xt_unregister_match(AF_INET, &connmark_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108module_init(init);
109module_exit(fini);