Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 | |
| 26 | MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>"); |
| 27 | MODULE_DESCRIPTION("IP tables CONNMARK matching module"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 31 | #include <linux/netfilter_ipv4/ipt_CONNMARK.h> |
| 32 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 33 | |
| 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) |
| 41 | { |
| 42 | const struct ipt_connmark_target_info *markinfo = targinfo; |
| 43 | unsigned long diff; |
| 44 | unsigned long nfmark; |
| 45 | unsigned long newmark; |
| 46 | |
| 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; |
| 67 | } |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return IPT_CONTINUE; |
| 73 | } |
| 74 | |
| 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) |
| 81 | { |
| 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; |
| 88 | } |
| 89 | |
| 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; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | static struct ipt_target ipt_connmark_reg = { |
| 101 | .name = "CONNMARK", |
| 102 | .target = &target, |
| 103 | .checkentry = &checkentry, |
| 104 | .me = THIS_MODULE |
| 105 | }; |
| 106 | |
| 107 | static int __init init(void) |
| 108 | { |
| 109 | return ipt_register_target(&ipt_connmark_reg); |
| 110 | } |
| 111 | |
| 112 | static void __exit fini(void) |
| 113 | { |
| 114 | ipt_unregister_target(&ipt_connmark_reg); |
| 115 | } |
| 116 | |
| 117 | module_init(init); |
| 118 | module_exit(fini); |