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"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 29 | MODULE_ALIAS("ipt_CONNMARK"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | #include <linux/netfilter/x_tables.h> |
| 32 | #include <linux/netfilter/xt_CONNMARK.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 33 | #include <net/netfilter/nf_conntrack_compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | static unsigned int |
| 36 | target(struct sk_buff **pskb, |
| 37 | const struct net_device *in, |
| 38 | const struct net_device *out, |
| 39 | unsigned int hooknum, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 40 | const struct xt_target *target, |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame^] | 41 | const void *targinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | const struct xt_connmark_target_info *markinfo = targinfo; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 44 | u_int32_t diff; |
| 45 | u_int32_t nfmark; |
| 46 | u_int32_t newmark; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 47 | u_int32_t ctinfo; |
| 48 | u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 50 | if (ctmark) { |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 51 | switch(markinfo->mode) { |
| 52 | case XT_CONNMARK_SET: |
| 53 | newmark = (*ctmark & ~markinfo->mask) | markinfo->mark; |
| 54 | if (newmark != *ctmark) { |
| 55 | *ctmark = newmark; |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 56 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 57 | ip_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 58 | #else |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 59 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 60 | #endif |
| 61 | } |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 62 | break; |
| 63 | case XT_CONNMARK_SAVE: |
| 64 | newmark = (*ctmark & ~markinfo->mask) | |
| 65 | ((*pskb)->nfmark & markinfo->mask); |
| 66 | if (*ctmark != newmark) { |
| 67 | *ctmark = newmark; |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 68 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 69 | ip_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 70 | #else |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 71 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 72 | #endif |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 73 | } |
| 74 | break; |
| 75 | case XT_CONNMARK_RESTORE: |
| 76 | nfmark = (*pskb)->nfmark; |
| 77 | diff = (*ctmark ^ nfmark) & markinfo->mask; |
| 78 | if (diff != 0) |
| 79 | (*pskb)->nfmark = nfmark ^ diff; |
| 80 | break; |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 84 | return XT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int |
| 88 | checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 89 | const void *entry, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 90 | const struct xt_target *target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | void *targinfo, |
| 92 | unsigned int targinfosize, |
| 93 | unsigned int hook_mask) |
| 94 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 95 | struct xt_connmark_target_info *matchinfo = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | if (matchinfo->mode == XT_CONNMARK_RESTORE) { |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 98 | if (strcmp(tablename, "mangle") != 0) { |
| 99 | printk(KERN_WARNING "CONNMARK: restore can only be " |
| 100 | "called from \"mangle\" table, not \"%s\"\n", |
| 101 | tablename); |
| 102 | return 0; |
| 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 105 | if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) { |
| 106 | printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n"); |
| 107 | return 0; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 112 | static struct xt_target xt_connmark_target[] = { |
| 113 | { |
| 114 | .name = "CONNMARK", |
| 115 | .family = AF_INET, |
| 116 | .checkentry = checkentry, |
| 117 | .target = target, |
| 118 | .targetsize = sizeof(struct xt_connmark_target_info), |
| 119 | .me = THIS_MODULE |
| 120 | }, |
| 121 | { |
| 122 | .name = "CONNMARK", |
| 123 | .family = AF_INET6, |
| 124 | .checkentry = checkentry, |
| 125 | .target = target, |
| 126 | .targetsize = sizeof(struct xt_connmark_target_info), |
| 127 | .me = THIS_MODULE |
| 128 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 131 | static int __init xt_connmark_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 133 | need_conntrack(); |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 134 | return xt_register_targets(xt_connmark_target, |
| 135 | ARRAY_SIZE(xt_connmark_target)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 138 | static void __exit xt_connmark_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 140 | xt_unregister_targets(xt_connmark_target, |
| 141 | ARRAY_SIZE(xt_connmark_target)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 144 | module_init(xt_connmark_init); |
| 145 | module_exit(xt_connmark_fini); |