blob: b03ce009d0bf0f267af5e154086c3418cd5989d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* This kernel module is used to modify the connection mark values, or
2 * to optionally restore the skb nfmark from the connection mark
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#include <linux/module.h>
22#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <net/checksum.h>
25
26MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080029MODULE_ALIAS("ipt_CONNMARK");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Harald Welte2e4e6a12006-01-12 13:30:04 -080031#include <linux/netfilter/x_tables.h>
32#include <linux/netfilter/xt_CONNMARK.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010033#include <net/netfilter/nf_conntrack_ecache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static unsigned int
36target(struct sk_buff **pskb,
37 const struct net_device *in,
38 const struct net_device *out,
39 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080040 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070041 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Harald Welte2e4e6a12006-01-12 13:30:04 -080043 const struct xt_connmark_target_info *markinfo = targinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -070044 struct nf_conn *ct;
45 enum ip_conntrack_info ctinfo;
Harald Weltebf3a46a2005-08-09 19:22:01 -070046 u_int32_t diff;
Thomas Graf82e91ff2006-11-09 15:19:14 -080047 u_int32_t mark;
Harald Weltebf3a46a2005-08-09 19:22:01 -070048 u_int32_t newmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Patrick McHardy587aa642007-03-14 16:37:25 -070050 ct = nf_ct_get(*pskb, &ctinfo);
51 if (ct) {
Patrick McHardy90528e62006-08-22 00:33:26 -070052 switch(markinfo->mode) {
53 case XT_CONNMARK_SET:
Patrick McHardy587aa642007-03-14 16:37:25 -070054 newmark = (ct->mark & ~markinfo->mask) | markinfo->mark;
55 if (newmark != ct->mark) {
56 ct->mark = newmark;
Patrick McHardy90528e62006-08-22 00:33:26 -070057 nf_conntrack_event_cache(IPCT_MARK, *pskb);
Jan Engelhardt2822b0d2007-02-07 15:06:43 -080058 }
Patrick McHardy90528e62006-08-22 00:33:26 -070059 break;
60 case XT_CONNMARK_SAVE:
Patrick McHardy587aa642007-03-14 16:37:25 -070061 newmark = (ct->mark & ~markinfo->mask) |
Thomas Graf82e91ff2006-11-09 15:19:14 -080062 ((*pskb)->mark & markinfo->mask);
Patrick McHardy587aa642007-03-14 16:37:25 -070063 if (ct->mark != newmark) {
64 ct->mark = newmark;
Patrick McHardy90528e62006-08-22 00:33:26 -070065 nf_conntrack_event_cache(IPCT_MARK, *pskb);
Patrick McHardy90528e62006-08-22 00:33:26 -070066 }
67 break;
68 case XT_CONNMARK_RESTORE:
Thomas Graf82e91ff2006-11-09 15:19:14 -080069 mark = (*pskb)->mark;
Patrick McHardy587aa642007-03-14 16:37:25 -070070 diff = (ct->mark ^ mark) & markinfo->mask;
Jan Engelhardt2822b0d2007-02-07 15:06:43 -080071 (*pskb)->mark = mark ^ diff;
Patrick McHardy90528e62006-08-22 00:33:26 -070072 break;
Pablo Neira Ayuso2521c122006-08-22 00:31:24 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
Harald Welte2e4e6a12006-01-12 13:30:04 -080076 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static int
80checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080081 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080082 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned int hook_mask)
85{
Harald Welte2e4e6a12006-01-12 13:30:04 -080086 struct xt_connmark_target_info *matchinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -080088 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
89 printk(KERN_WARNING "can't load conntrack support for "
90 "proto=%d\n", target->family);
91 return 0;
92 }
Harald Welte2e4e6a12006-01-12 13:30:04 -080093 if (matchinfo->mode == XT_CONNMARK_RESTORE) {
Patrick McHardy90528e62006-08-22 00:33:26 -070094 if (strcmp(tablename, "mangle") != 0) {
95 printk(KERN_WARNING "CONNMARK: restore can only be "
96 "called from \"mangle\" table, not \"%s\"\n",
97 tablename);
98 return 0;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Harald Weltebf3a46a2005-08-09 19:22:01 -0700101 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
102 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
103 return 0;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 1;
106}
107
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800108static void
109destroy(const struct xt_target *target, void *targinfo)
110{
111 nf_ct_l3proto_module_put(target->family);
112}
113
Patrick McHardy7ce975b2006-09-20 12:06:40 -0700114#ifdef CONFIG_COMPAT
115struct compat_xt_connmark_target_info {
116 compat_ulong_t mark, mask;
117 u_int8_t mode;
118 u_int8_t __pad1;
119 u_int16_t __pad2;
120};
121
122static void compat_from_user(void *dst, void *src)
123{
124 struct compat_xt_connmark_target_info *cm = src;
125 struct xt_connmark_target_info m = {
126 .mark = cm->mark,
127 .mask = cm->mask,
128 .mode = cm->mode,
129 };
130 memcpy(dst, &m, sizeof(m));
131}
132
133static int compat_to_user(void __user *dst, void *src)
134{
135 struct xt_connmark_target_info *m = src;
136 struct compat_xt_connmark_target_info cm = {
137 .mark = m->mark,
138 .mask = m->mask,
139 .mode = m->mode,
140 };
141 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
142}
143#endif /* CONFIG_COMPAT */
144
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700145static struct xt_target xt_connmark_target[] = {
146 {
147 .name = "CONNMARK",
148 .family = AF_INET,
149 .checkentry = checkentry,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800150 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700151 .target = target,
152 .targetsize = sizeof(struct xt_connmark_target_info),
Patrick McHardy7ce975b2006-09-20 12:06:40 -0700153#ifdef CONFIG_COMPAT
154 .compatsize = sizeof(struct compat_xt_connmark_target_info),
155 .compat_from_user = compat_from_user,
156 .compat_to_user = compat_to_user,
157#endif
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700158 .me = THIS_MODULE
159 },
160 {
161 .name = "CONNMARK",
162 .family = AF_INET6,
163 .checkentry = checkentry,
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800164 .destroy = destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700165 .target = target,
166 .targetsize = sizeof(struct xt_connmark_target_info),
167 .me = THIS_MODULE
168 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800171static int __init xt_connmark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700173 return xt_register_targets(xt_connmark_target,
174 ARRAY_SIZE(xt_connmark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800177static void __exit xt_connmark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700179 xt_unregister_targets(xt_connmark_target,
180 ARRAY_SIZE(xt_connmark_target));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800183module_init(xt_connmark_init);
184module_exit(xt_connmark_fini);