Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* This kernel module matches connection mark values set by the |
| 2 | * CONNMARK target |
| 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 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/skbuff.h> |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack.h> |
| 25 | #include <linux/netfilter/x_tables.h> |
| 26 | #include <linux/netfilter/xt_connmark.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>"); |
| 29 | MODULE_DESCRIPTION("IP tables connmark match module"); |
| 30 | MODULE_LICENSE("GPL"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | MODULE_ALIAS("ipt_connmark"); |
Jan Engelhardt | 73aaf93 | 2007-10-11 14:36:40 -0700 | [diff] [blame^] | 32 | MODULE_ALIAS("ip6t_connmark"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 34 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | match(const struct sk_buff *skb, |
| 36 | const struct net_device *in, |
| 37 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 38 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | const void *matchinfo, |
| 40 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 42 | bool *hotdrop) |
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_info *info = matchinfo; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 45 | const struct nf_conn *ct; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 46 | enum ip_conntrack_info ctinfo; |
| 47 | |
| 48 | ct = nf_ct_get(skb, &ctinfo); |
| 49 | if (!ct) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 50 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 52 | return ((ct->mark & info->mask) == info->mark) ^ info->invert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 55 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 57 | const void *ip, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 58 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | void *matchinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | unsigned int hook_mask) |
| 61 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 62 | const struct xt_connmark_info *cm = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 64 | if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) { |
| 65 | printk(KERN_WARNING "connmark: only support 32bit mark\n"); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 66 | return false; |
Harald Welte | bf3a46a | 2005-08-09 19:22:01 -0700 | [diff] [blame] | 67 | } |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 68 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
Yasuyuki Kozakai | fe0b929 | 2006-12-12 00:28:40 -0800 | [diff] [blame] | 69 | printk(KERN_WARNING "can't load conntrack support for " |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 70 | "proto=%d\n", match->family); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 71 | return false; |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 72 | } |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 73 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 76 | static void |
Patrick McHardy | efa7416 | 2006-08-22 00:36:37 -0700 | [diff] [blame] | 77 | destroy(const struct xt_match *match, void *matchinfo) |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 78 | { |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 79 | nf_ct_l3proto_module_put(match->family); |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Patrick McHardy | f1eda05 | 2006-09-20 12:06:25 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_COMPAT |
| 83 | struct compat_xt_connmark_info { |
| 84 | compat_ulong_t mark, mask; |
| 85 | u_int8_t invert; |
| 86 | u_int8_t __pad1; |
| 87 | u_int16_t __pad2; |
| 88 | }; |
| 89 | |
| 90 | static void compat_from_user(void *dst, void *src) |
| 91 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 92 | const struct compat_xt_connmark_info *cm = src; |
Patrick McHardy | f1eda05 | 2006-09-20 12:06:25 -0700 | [diff] [blame] | 93 | struct xt_connmark_info m = { |
| 94 | .mark = cm->mark, |
| 95 | .mask = cm->mask, |
| 96 | .invert = cm->invert, |
| 97 | }; |
| 98 | memcpy(dst, &m, sizeof(m)); |
| 99 | } |
| 100 | |
| 101 | static int compat_to_user(void __user *dst, void *src) |
| 102 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 103 | const struct xt_connmark_info *m = src; |
Patrick McHardy | f1eda05 | 2006-09-20 12:06:25 -0700 | [diff] [blame] | 104 | struct compat_xt_connmark_info cm = { |
| 105 | .mark = m->mark, |
| 106 | .mask = m->mask, |
| 107 | .invert = m->invert, |
| 108 | }; |
| 109 | return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; |
| 110 | } |
| 111 | #endif /* CONFIG_COMPAT */ |
| 112 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 113 | static struct xt_match xt_connmark_match[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 114 | { |
| 115 | .name = "connmark", |
| 116 | .family = AF_INET, |
| 117 | .checkentry = checkentry, |
| 118 | .match = match, |
| 119 | .destroy = destroy, |
| 120 | .matchsize = sizeof(struct xt_connmark_info), |
Patrick McHardy | f1eda05 | 2006-09-20 12:06:25 -0700 | [diff] [blame] | 121 | #ifdef CONFIG_COMPAT |
| 122 | .compatsize = sizeof(struct compat_xt_connmark_info), |
| 123 | .compat_from_user = compat_from_user, |
| 124 | .compat_to_user = compat_to_user, |
| 125 | #endif |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 126 | .me = THIS_MODULE |
| 127 | }, |
| 128 | { |
| 129 | .name = "connmark", |
| 130 | .family = AF_INET6, |
| 131 | .checkentry = checkentry, |
| 132 | .match = match, |
| 133 | .destroy = destroy, |
| 134 | .matchsize = sizeof(struct xt_connmark_info), |
| 135 | .me = THIS_MODULE |
| 136 | }, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 137 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 138 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 139 | static int __init xt_connmark_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 141 | return xt_register_matches(xt_connmark_match, |
| 142 | ARRAY_SIZE(xt_connmark_match)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 145 | static void __exit xt_connmark_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Patrick McHardy | f64ad5b | 2006-10-12 14:07:52 -0700 | [diff] [blame] | 147 | xt_unregister_matches(xt_connmark_match, ARRAY_SIZE(xt_connmark_match)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 150 | module_init(xt_connmark_init); |
| 151 | module_exit(xt_connmark_fini); |