Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / netfilter / ipt_CONNMARK.c
blob30ddd3e18eb747184b80eea693778058a85c14ec
1 /* This kernel module is used to modify the connection mark values, or
2 * to optionally restore the skb nfmark from the connection mark
4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
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.
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.
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
21 #include <linux/module.h>
22 #include <linux/skbuff.h>
23 #include <linux/ip.h>
24 #include <net/checksum.h>
26 MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27 MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28 MODULE_LICENSE("GPL");
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <linux/netfilter_ipv4/ipt_CONNMARK.h>
32 #include <linux/netfilter_ipv4/ip_conntrack.h>
34 static unsigned int
35 target(struct sk_buff **pskb,
36 const struct net_device *in,
37 const struct net_device *out,
38 unsigned int hooknum,
39 const void *targinfo,
40 void *userinfo)
42 const struct ipt_connmark_target_info *markinfo = targinfo;
43 unsigned long diff;
44 unsigned long nfmark;
45 unsigned long newmark;
47 enum ip_conntrack_info ctinfo;
48 struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
49 if (ct) {
50 switch(markinfo->mode) {
51 case IPT_CONNMARK_SET:
52 newmark = (ct->mark & ~markinfo->mask) | markinfo->mark;
53 if (newmark != ct->mark)
54 ct->mark = newmark;
55 break;
56 case IPT_CONNMARK_SAVE:
57 newmark = (ct->mark & ~markinfo->mask) | ((*pskb)->nfmark & markinfo->mask);
58 if (ct->mark != newmark)
59 ct->mark = newmark;
60 break;
61 case IPT_CONNMARK_RESTORE:
62 nfmark = (*pskb)->nfmark;
63 diff = (ct->mark ^ nfmark) & markinfo->mask;
64 if (diff != 0) {
65 (*pskb)->nfmark = nfmark ^ diff;
66 (*pskb)->nfcache |= NFC_ALTERED;
68 break;
72 return IPT_CONTINUE;
75 static int
76 checkentry(const char *tablename,
77 const struct ipt_entry *e,
78 void *targinfo,
79 unsigned int targinfosize,
80 unsigned int hook_mask)
82 struct ipt_connmark_target_info *matchinfo = targinfo;
83 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_connmark_target_info))) {
84 printk(KERN_WARNING "CONNMARK: targinfosize %u != %Zu\n",
85 targinfosize,
86 IPT_ALIGN(sizeof(struct ipt_connmark_target_info)));
87 return 0;
90 if (matchinfo->mode == IPT_CONNMARK_RESTORE) {
91 if (strcmp(tablename, "mangle") != 0) {
92 printk(KERN_WARNING "CONNMARK: restore can only be called from \"mangle\" table, not \"%s\"\n", tablename);
93 return 0;
97 return 1;
100 static struct ipt_target ipt_connmark_reg = {
101 .name = "CONNMARK",
102 .target = &target,
103 .checkentry = &checkentry,
104 .me = THIS_MODULE
107 static int __init init(void)
109 return ipt_register_target(&ipt_connmark_reg);
112 static void __exit fini(void)
114 ipt_unregister_target(&ipt_connmark_reg);
117 module_init(init);
118 module_exit(fini);