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> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 34 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 35 | #include <net/netfilter/nf_conntrack_ecache.h> |
| 36 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static unsigned int |
| 39 | target(struct sk_buff **pskb, |
| 40 | const struct net_device *in, |
| 41 | const struct net_device *out, |
| 42 | unsigned int hooknum, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 43 | const struct xt_target *target, |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 44 | const void *targinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 46 | const struct xt_connmark_target_info *markinfo = targinfo; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 47 | u_int32_t diff; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 48 | u_int32_t mark; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 49 | u_int32_t newmark; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 50 | u_int32_t ctinfo; |
| 51 | u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 53 | if (ctmark) { |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 54 | switch(markinfo->mode) { |
| 55 | case XT_CONNMARK_SET: |
| 56 | newmark = (*ctmark & ~markinfo->mask) | markinfo->mark; |
| 57 | if (newmark != *ctmark) { |
| 58 | *ctmark = newmark; |
Benoit Boissinot | 0719bdf | 2006-08-28 17:50:37 -0700 | [diff] [blame] | 59 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 60 | ip_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 61 | #else |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 62 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 63 | #endif |
Jan Engelhardt | 2822b0d | 2007-02-07 15:06:43 -0800 | [diff] [blame] | 64 | } |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 65 | break; |
| 66 | case XT_CONNMARK_SAVE: |
| 67 | newmark = (*ctmark & ~markinfo->mask) | |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 68 | ((*pskb)->mark & markinfo->mask); |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 69 | if (*ctmark != newmark) { |
| 70 | *ctmark = newmark; |
Benoit Boissinot | 0719bdf | 2006-08-28 17:50:37 -0700 | [diff] [blame] | 71 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 72 | ip_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 73 | #else |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 74 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 75 | #endif |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 76 | } |
| 77 | break; |
| 78 | case XT_CONNMARK_RESTORE: |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 79 | mark = (*pskb)->mark; |
| 80 | diff = (*ctmark ^ mark) & markinfo->mask; |
Jan Engelhardt | 2822b0d | 2007-02-07 15:06:43 -0800 | [diff] [blame] | 81 | (*pskb)->mark = mark ^ diff; |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 82 | break; |
Pablo Neira Ayuso | 2521c12 | 2006-08-22 00:31:24 -0700 | [diff] [blame] | 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 86 | return XT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int |
| 90 | checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 91 | const void *entry, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 92 | const struct xt_target *target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | void *targinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | unsigned int hook_mask) |
| 95 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | struct xt_connmark_target_info *matchinfo = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 98 | if (nf_ct_l3proto_try_module_get(target->family) < 0) { |
| 99 | printk(KERN_WARNING "can't load conntrack support for " |
| 100 | "proto=%d\n", target->family); |
| 101 | return 0; |
| 102 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 103 | if (matchinfo->mode == XT_CONNMARK_RESTORE) { |
Patrick McHardy | 90528e6 | 2006-08-22 00:33:26 -0700 | [diff] [blame] | 104 | if (strcmp(tablename, "mangle") != 0) { |
| 105 | printk(KERN_WARNING "CONNMARK: restore can only be " |
| 106 | "called from \"mangle\" table, not \"%s\"\n", |
| 107 | tablename); |
| 108 | return 0; |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 111 | if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) { |
| 112 | printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n"); |
| 113 | return 0; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 118 | static void |
| 119 | destroy(const struct xt_target *target, void *targinfo) |
| 120 | { |
| 121 | nf_ct_l3proto_module_put(target->family); |
| 122 | } |
| 123 | |
Patrick McHardy | 7ce975b | 2006-09-20 12:06:40 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_COMPAT |
| 125 | struct compat_xt_connmark_target_info { |
| 126 | compat_ulong_t mark, mask; |
| 127 | u_int8_t mode; |
| 128 | u_int8_t __pad1; |
| 129 | u_int16_t __pad2; |
| 130 | }; |
| 131 | |
| 132 | static void compat_from_user(void *dst, void *src) |
| 133 | { |
| 134 | struct compat_xt_connmark_target_info *cm = src; |
| 135 | struct xt_connmark_target_info m = { |
| 136 | .mark = cm->mark, |
| 137 | .mask = cm->mask, |
| 138 | .mode = cm->mode, |
| 139 | }; |
| 140 | memcpy(dst, &m, sizeof(m)); |
| 141 | } |
| 142 | |
| 143 | static int compat_to_user(void __user *dst, void *src) |
| 144 | { |
| 145 | struct xt_connmark_target_info *m = src; |
| 146 | struct compat_xt_connmark_target_info cm = { |
| 147 | .mark = m->mark, |
| 148 | .mask = m->mask, |
| 149 | .mode = m->mode, |
| 150 | }; |
| 151 | return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; |
| 152 | } |
| 153 | #endif /* CONFIG_COMPAT */ |
| 154 | |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 155 | static struct xt_target xt_connmark_target[] = { |
| 156 | { |
| 157 | .name = "CONNMARK", |
| 158 | .family = AF_INET, |
| 159 | .checkentry = checkentry, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 160 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 161 | .target = target, |
| 162 | .targetsize = sizeof(struct xt_connmark_target_info), |
Patrick McHardy | 7ce975b | 2006-09-20 12:06:40 -0700 | [diff] [blame] | 163 | #ifdef CONFIG_COMPAT |
| 164 | .compatsize = sizeof(struct compat_xt_connmark_target_info), |
| 165 | .compat_from_user = compat_from_user, |
| 166 | .compat_to_user = compat_to_user, |
| 167 | #endif |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 168 | .me = THIS_MODULE |
| 169 | }, |
| 170 | { |
| 171 | .name = "CONNMARK", |
| 172 | .family = AF_INET6, |
| 173 | .checkentry = checkentry, |
Yasuyuki Kozakai | 11078c3 | 2006-12-12 00:29:02 -0800 | [diff] [blame] | 174 | .destroy = destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 175 | .target = target, |
| 176 | .targetsize = sizeof(struct xt_connmark_target_info), |
| 177 | .me = THIS_MODULE |
| 178 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 181 | static int __init xt_connmark_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 183 | return xt_register_targets(xt_connmark_target, |
| 184 | ARRAY_SIZE(xt_connmark_target)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 187 | static void __exit xt_connmark_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 189 | xt_unregister_targets(xt_connmark_target, |
| 190 | ARRAY_SIZE(xt_connmark_target)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 193 | module_init(xt_connmark_init); |
| 194 | module_exit(xt_connmark_fini); |