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