blob: 60c375d36f017c49b78fe41eb10509a7bdec1e7b [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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033#include <net/netfilter/nf_conntrack_compat.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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 const void *targinfo,
42 void *userinfo)
43{
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 const struct xt_connmark_target_info *markinfo = targinfo;
Harald Weltebf3a46a2005-08-09 19:22:01 -070045 u_int32_t diff;
46 u_int32_t nfmark;
47 u_int32_t newmark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048 u_int32_t ctinfo;
49 u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051 if (ctmark) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch(markinfo->mode) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 case XT_CONNMARK_SET:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054 newmark = (*ctmark & ~markinfo->mask) | markinfo->mark;
55 if (newmark != *ctmark)
56 *ctmark = newmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080058 case XT_CONNMARK_SAVE:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059 newmark = (*ctmark & ~markinfo->mask) | ((*pskb)->nfmark & markinfo->mask);
60 if (*ctmark != newmark)
61 *ctmark = newmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
Harald Welte2e4e6a12006-01-12 13:30:04 -080063 case XT_CONNMARK_RESTORE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 nfmark = (*pskb)->nfmark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080065 diff = (*ctmark ^ nfmark) & markinfo->mask;
Harald Welte6869c4d2005-08-09 19:24:19 -070066 if (diff != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 (*pskb)->nfmark = nfmark ^ diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 }
70 }
71
Harald Welte2e4e6a12006-01-12 13:30:04 -080072 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75static int
76checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080078 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 void *targinfo,
80 unsigned int targinfosize,
81 unsigned int hook_mask)
82{
Harald Welte2e4e6a12006-01-12 13:30:04 -080083 struct xt_connmark_target_info *matchinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Harald Welte2e4e6a12006-01-12 13:30:04 -080085 if (matchinfo->mode == XT_CONNMARK_RESTORE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (strcmp(tablename, "mangle") != 0) {
87 printk(KERN_WARNING "CONNMARK: restore can only be called from \"mangle\" table, not \"%s\"\n", tablename);
88 return 0;
89 }
90 }
91
Harald Weltebf3a46a2005-08-09 19:22:01 -070092 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
93 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
94 return 0;
95 }
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 1;
98}
99
Harald Welte2e4e6a12006-01-12 13:30:04 -0800100static struct xt_target connmark_reg = {
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800101 .name = "CONNMARK",
102 .target = target,
103 .targetsize = sizeof(struct xt_connmark_target_info),
104 .checkentry = checkentry,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800105 .family = AF_INET,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800106 .me = THIS_MODULE
Harald Welte2e4e6a12006-01-12 13:30:04 -0800107};
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800108
Harald Welte2e4e6a12006-01-12 13:30:04 -0800109static struct xt_target connmark6_reg = {
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800110 .name = "CONNMARK",
111 .target = target,
112 .targetsize = sizeof(struct xt_connmark_target_info),
113 .checkentry = checkentry,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800114 .family = AF_INET6,
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800115 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800118static int __init xt_connmark_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800120 int ret;
121
122 need_conntrack();
123
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800124 ret = xt_register_target(&connmark_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125 if (ret)
126 return ret;
127
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800128 ret = xt_register_target(&connmark6_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129 if (ret)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800130 xt_unregister_target(&connmark_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131
132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800135static void __exit xt_connmark_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800137 xt_unregister_target(&connmark_reg);
138 xt_unregister_target(&connmark6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800141module_init(xt_connmark_init);
142module_exit(xt_connmark_fini);