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