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>
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>
35 target(struct sk_buff **pskb,
36 const struct net_device *in,
37 const struct net_device *out,
42 const struct ipt_connmark_target_info *markinfo = targinfo;
47 enum ip_conntrack_info ctinfo;
48 struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
50 switch(markinfo->mode) {
51 case IPT_CONNMARK_SET:
52 newmark = (ct->mark & ~markinfo->mask) | markinfo->mark;
53 if (newmark != ct->mark)
56 case IPT_CONNMARK_SAVE:
57 newmark = (ct->mark & ~markinfo->mask) | ((*pskb)->nfmark & markinfo->mask);
58 if (ct->mark != newmark)
61 case IPT_CONNMARK_RESTORE:
62 nfmark = (*pskb)->nfmark;
63 diff = (ct->mark ^ nfmark) & markinfo->mask;
65 (*pskb)->nfmark = nfmark ^ diff;
74 checkentry(const char *tablename,
75 const struct ipt_entry *e,
77 unsigned int targinfosize,
78 unsigned int hook_mask)
80 struct ipt_connmark_target_info *matchinfo = targinfo;
81 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_connmark_target_info))) {
82 printk(KERN_WARNING "CONNMARK: targinfosize %u != %Zu\n",
84 IPT_ALIGN(sizeof(struct ipt_connmark_target_info)));
88 if (matchinfo->mode == IPT_CONNMARK_RESTORE) {
89 if (strcmp(tablename, "mangle") != 0) {
90 printk(KERN_WARNING "CONNMARK: restore can only be called from \"mangle\" table, not \"%s\"\n", tablename);
95 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
96 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
103 static struct ipt_target ipt_connmark_reg = {
106 .checkentry = &checkentry,
110 static int __init init(void)
113 return ipt_register_target(&ipt_connmark_reg);
116 static void __exit fini(void)
118 ipt_unregister_target(&ipt_connmark_reg);