Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * xt_hashlimit - Netfilter module to limit the number of packets per time |
| 3 | * seperately for each hashbucket (sourceip/sourceport/dstip/dstport) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 4 | * |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 5 | * (C) 2003-2004 by Harald Welte <laforge@netfilter.org> |
| 6 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 7 | * |
| 8 | * Development of this code was funded by Astaro AG, http://www.astaro.com/ |
| 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/random.h> |
| 13 | #include <linux/jhash.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/proc_fs.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/skbuff.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 20 | #include <linux/mm.h> |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 21 | #include <linux/in.h> |
| 22 | #include <linux/ip.h> |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 23 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 24 | #include <linux/ipv6.h> |
Patrick McHardy | 193b23c | 2007-12-04 23:51:48 -0800 | [diff] [blame] | 25 | #include <net/ipv6.h> |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 26 | #endif |
| 27 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 29 | |
| 30 | #include <linux/netfilter/x_tables.h> |
| 31 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 32 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 33 | #include <linux/netfilter/xt_hashlimit.h> |
| 34 | #include <linux/mutex.h> |
| 35 | |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 38 | MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match"); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 40 | MODULE_ALIAS("ipt_hashlimit"); |
| 41 | MODULE_ALIAS("ip6t_hashlimit"); |
| 42 | |
| 43 | /* need to declare this at the top */ |
| 44 | static struct proc_dir_entry *hashlimit_procdir4; |
| 45 | static struct proc_dir_entry *hashlimit_procdir6; |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 46 | static const struct file_operations dl_file_ops; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 47 | |
| 48 | /* hash table crap */ |
| 49 | struct dsthash_dst { |
| 50 | union { |
| 51 | struct { |
| 52 | __be32 src; |
| 53 | __be32 dst; |
| 54 | } ip; |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 55 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 56 | struct { |
| 57 | __be32 src[4]; |
| 58 | __be32 dst[4]; |
| 59 | } ip6; |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 60 | #endif |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 61 | }; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 62 | __be16 src_port; |
| 63 | __be16 dst_port; |
| 64 | }; |
| 65 | |
| 66 | struct dsthash_ent { |
| 67 | /* static / read-only parts in the beginning */ |
| 68 | struct hlist_node node; |
| 69 | struct dsthash_dst dst; |
| 70 | |
| 71 | /* modified structure members in the end */ |
| 72 | unsigned long expires; /* precalculated expiry time */ |
| 73 | struct { |
| 74 | unsigned long prev; /* last modification */ |
| 75 | u_int32_t credit; |
| 76 | u_int32_t credit_cap, cost; |
| 77 | } rateinfo; |
| 78 | }; |
| 79 | |
| 80 | struct xt_hashlimit_htable { |
| 81 | struct hlist_node node; /* global list of all htables */ |
| 82 | atomic_t use; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 83 | u_int8_t family; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 84 | |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 85 | struct hashlimit_cfg1 cfg; /* config */ |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 86 | |
| 87 | /* used internally */ |
| 88 | spinlock_t lock; /* lock for list_head */ |
| 89 | u_int32_t rnd; /* random seed for hash */ |
| 90 | int rnd_initialized; |
| 91 | unsigned int count; /* number entries in table */ |
| 92 | struct timer_list timer; /* timer for gc */ |
| 93 | |
| 94 | /* seq_file stuff */ |
| 95 | struct proc_dir_entry *pde; |
| 96 | |
| 97 | struct hlist_head hash[0]; /* hashtable itself */ |
| 98 | }; |
| 99 | |
| 100 | static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */ |
| 101 | static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */ |
| 102 | static HLIST_HEAD(hashlimit_htables); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 103 | static struct kmem_cache *hashlimit_cachep __read_mostly; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 104 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 105 | static inline bool dst_cmp(const struct dsthash_ent *ent, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 106 | const struct dsthash_dst *b) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 107 | { |
| 108 | return !memcmp(&ent->dst, b, sizeof(ent->dst)); |
| 109 | } |
| 110 | |
| 111 | static u_int32_t |
| 112 | hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst) |
| 113 | { |
Eric Dumazet | e2f82ac | 2007-12-17 22:45:13 -0800 | [diff] [blame] | 114 | u_int32_t hash = jhash2((const u32 *)dst, |
| 115 | sizeof(*dst)/sizeof(u32), |
| 116 | ht->rnd); |
| 117 | /* |
| 118 | * Instead of returning hash % ht->cfg.size (implying a divide) |
| 119 | * we return the high 32 bits of the (hash * ht->cfg.size) that will |
| 120 | * give results between [0 and cfg.size-1] and same hash distribution, |
| 121 | * but using a multiply, less expensive than a divide |
| 122 | */ |
| 123 | return ((u64)hash * ht->cfg.size) >> 32; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static struct dsthash_ent * |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 127 | dsthash_find(const struct xt_hashlimit_htable *ht, |
| 128 | const struct dsthash_dst *dst) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 129 | { |
| 130 | struct dsthash_ent *ent; |
| 131 | struct hlist_node *pos; |
| 132 | u_int32_t hash = hash_dst(ht, dst); |
| 133 | |
| 134 | if (!hlist_empty(&ht->hash[hash])) { |
| 135 | hlist_for_each_entry(ent, pos, &ht->hash[hash], node) |
| 136 | if (dst_cmp(ent, dst)) |
| 137 | return ent; |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | /* allocate dsthash_ent, initialize dst, put in htable and lock it */ |
| 143 | static struct dsthash_ent * |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 144 | dsthash_alloc_init(struct xt_hashlimit_htable *ht, |
| 145 | const struct dsthash_dst *dst) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 146 | { |
| 147 | struct dsthash_ent *ent; |
| 148 | |
| 149 | /* initialize hash with random val at the time we allocate |
| 150 | * the first hashtable entry */ |
| 151 | if (!ht->rnd_initialized) { |
| 152 | get_random_bytes(&ht->rnd, 4); |
| 153 | ht->rnd_initialized = 1; |
| 154 | } |
| 155 | |
| 156 | if (ht->cfg.max && ht->count >= ht->cfg.max) { |
| 157 | /* FIXME: do something. question is what.. */ |
| 158 | if (net_ratelimit()) |
| 159 | printk(KERN_WARNING |
| 160 | "xt_hashlimit: max count of %u reached\n", |
| 161 | ht->cfg.max); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC); |
| 166 | if (!ent) { |
| 167 | if (net_ratelimit()) |
| 168 | printk(KERN_ERR |
| 169 | "xt_hashlimit: can't allocate dsthash_ent\n"); |
| 170 | return NULL; |
| 171 | } |
| 172 | memcpy(&ent->dst, dst, sizeof(ent->dst)); |
| 173 | |
| 174 | hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]); |
| 175 | ht->count++; |
| 176 | return ent; |
| 177 | } |
| 178 | |
| 179 | static inline void |
| 180 | dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent) |
| 181 | { |
| 182 | hlist_del(&ent->node); |
| 183 | kmem_cache_free(hashlimit_cachep, ent); |
| 184 | ht->count--; |
| 185 | } |
| 186 | static void htable_gc(unsigned long htlong); |
| 187 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 188 | static int htable_create_v0(struct xt_hashlimit_info *minfo, u_int8_t family) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 189 | { |
| 190 | struct xt_hashlimit_htable *hinfo; |
| 191 | unsigned int size; |
| 192 | unsigned int i; |
| 193 | |
| 194 | if (minfo->cfg.size) |
| 195 | size = minfo->cfg.size; |
| 196 | else { |
| 197 | size = ((num_physpages << PAGE_SHIFT) / 16384) / |
| 198 | sizeof(struct list_head); |
| 199 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
| 200 | size = 8192; |
| 201 | if (size < 16) |
| 202 | size = 16; |
| 203 | } |
| 204 | /* FIXME: don't use vmalloc() here or anywhere else -HW */ |
| 205 | hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + |
| 206 | sizeof(struct list_head) * size); |
| 207 | if (!hinfo) { |
| 208 | printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); |
| 209 | return -1; |
| 210 | } |
| 211 | minfo->hinfo = hinfo; |
| 212 | |
| 213 | /* copy match config into hashtable config */ |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 214 | hinfo->cfg.mode = minfo->cfg.mode; |
| 215 | hinfo->cfg.avg = minfo->cfg.avg; |
| 216 | hinfo->cfg.burst = minfo->cfg.burst; |
| 217 | hinfo->cfg.max = minfo->cfg.max; |
| 218 | hinfo->cfg.gc_interval = minfo->cfg.gc_interval; |
| 219 | hinfo->cfg.expire = minfo->cfg.expire; |
| 220 | |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 221 | if (family == NFPROTO_IPV4) |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 222 | hinfo->cfg.srcmask = hinfo->cfg.dstmask = 32; |
| 223 | else |
| 224 | hinfo->cfg.srcmask = hinfo->cfg.dstmask = 128; |
| 225 | |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 226 | hinfo->cfg.size = size; |
| 227 | if (!hinfo->cfg.max) |
| 228 | hinfo->cfg.max = 8 * hinfo->cfg.size; |
| 229 | else if (hinfo->cfg.max < hinfo->cfg.size) |
| 230 | hinfo->cfg.max = hinfo->cfg.size; |
| 231 | |
| 232 | for (i = 0; i < hinfo->cfg.size; i++) |
| 233 | INIT_HLIST_HEAD(&hinfo->hash[i]); |
| 234 | |
| 235 | atomic_set(&hinfo->use, 1); |
| 236 | hinfo->count = 0; |
| 237 | hinfo->family = family; |
| 238 | hinfo->rnd_initialized = 0; |
| 239 | spin_lock_init(&hinfo->lock); |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 240 | hinfo->pde = proc_create_data(minfo->name, 0, |
| 241 | (family == NFPROTO_IPV4) ? |
| 242 | hashlimit_procdir4 : hashlimit_procdir6, |
| 243 | &dl_file_ops, hinfo); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 244 | if (!hinfo->pde) { |
| 245 | vfree(hinfo); |
| 246 | return -1; |
| 247 | } |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 248 | |
Patrick McHardy | e6f689d | 2007-03-23 11:16:30 -0700 | [diff] [blame] | 249 | setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 250 | hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 251 | add_timer(&hinfo->timer); |
| 252 | |
| 253 | spin_lock_bh(&hashlimit_lock); |
| 254 | hlist_add_head(&hinfo->node, &hashlimit_htables); |
| 255 | spin_unlock_bh(&hashlimit_lock); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 260 | static int htable_create(struct xt_hashlimit_mtinfo1 *minfo, u_int8_t family) |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 261 | { |
| 262 | struct xt_hashlimit_htable *hinfo; |
| 263 | unsigned int size; |
| 264 | unsigned int i; |
| 265 | |
| 266 | if (minfo->cfg.size) { |
| 267 | size = minfo->cfg.size; |
| 268 | } else { |
| 269 | size = (num_physpages << PAGE_SHIFT) / 16384 / |
| 270 | sizeof(struct list_head); |
| 271 | if (num_physpages > 1024 * 1024 * 1024 / PAGE_SIZE) |
| 272 | size = 8192; |
| 273 | if (size < 16) |
| 274 | size = 16; |
| 275 | } |
| 276 | /* FIXME: don't use vmalloc() here or anywhere else -HW */ |
| 277 | hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + |
| 278 | sizeof(struct list_head) * size); |
| 279 | if (hinfo == NULL) { |
| 280 | printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | minfo->hinfo = hinfo; |
| 284 | |
| 285 | /* copy match config into hashtable config */ |
| 286 | memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg)); |
| 287 | hinfo->cfg.size = size; |
| 288 | if (hinfo->cfg.max == 0) |
| 289 | hinfo->cfg.max = 8 * hinfo->cfg.size; |
| 290 | else if (hinfo->cfg.max < hinfo->cfg.size) |
| 291 | hinfo->cfg.max = hinfo->cfg.size; |
| 292 | |
| 293 | for (i = 0; i < hinfo->cfg.size; i++) |
| 294 | INIT_HLIST_HEAD(&hinfo->hash[i]); |
| 295 | |
| 296 | atomic_set(&hinfo->use, 1); |
| 297 | hinfo->count = 0; |
| 298 | hinfo->family = family; |
| 299 | hinfo->rnd_initialized = 0; |
| 300 | spin_lock_init(&hinfo->lock); |
| 301 | |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 302 | hinfo->pde = proc_create_data(minfo->name, 0, |
| 303 | (family == NFPROTO_IPV4) ? |
| 304 | hashlimit_procdir4 : hashlimit_procdir6, |
| 305 | &dl_file_ops, hinfo); |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 306 | if (hinfo->pde == NULL) { |
| 307 | vfree(hinfo); |
| 308 | return -1; |
| 309 | } |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 310 | |
| 311 | setup_timer(&hinfo->timer, htable_gc, (unsigned long)hinfo); |
| 312 | hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); |
| 313 | add_timer(&hinfo->timer); |
| 314 | |
| 315 | spin_lock_bh(&hashlimit_lock); |
| 316 | hlist_add_head(&hinfo->node, &hashlimit_htables); |
| 317 | spin_unlock_bh(&hashlimit_lock); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 322 | static bool select_all(const struct xt_hashlimit_htable *ht, |
| 323 | const struct dsthash_ent *he) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 324 | { |
| 325 | return 1; |
| 326 | } |
| 327 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 328 | static bool select_gc(const struct xt_hashlimit_htable *ht, |
| 329 | const struct dsthash_ent *he) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 330 | { |
Eric Dumazet | cbebc51 | 2007-12-12 11:11:28 -0800 | [diff] [blame] | 331 | return time_after_eq(jiffies, he->expires); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void htable_selective_cleanup(struct xt_hashlimit_htable *ht, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 335 | bool (*select)(const struct xt_hashlimit_htable *ht, |
| 336 | const struct dsthash_ent *he)) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 337 | { |
| 338 | unsigned int i; |
| 339 | |
| 340 | /* lock hash table and iterate over it */ |
| 341 | spin_lock_bh(&ht->lock); |
| 342 | for (i = 0; i < ht->cfg.size; i++) { |
| 343 | struct dsthash_ent *dh; |
| 344 | struct hlist_node *pos, *n; |
| 345 | hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) { |
| 346 | if ((*select)(ht, dh)) |
| 347 | dsthash_free(ht, dh); |
| 348 | } |
| 349 | } |
| 350 | spin_unlock_bh(&ht->lock); |
| 351 | } |
| 352 | |
| 353 | /* hash table garbage collector, run by timer */ |
| 354 | static void htable_gc(unsigned long htlong) |
| 355 | { |
| 356 | struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong; |
| 357 | |
| 358 | htable_selective_cleanup(ht, select_gc); |
| 359 | |
| 360 | /* re-add the timer accordingly */ |
| 361 | ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval); |
| 362 | add_timer(&ht->timer); |
| 363 | } |
| 364 | |
| 365 | static void htable_destroy(struct xt_hashlimit_htable *hinfo) |
| 366 | { |
Pavel Emelyanov | 967ab99 | 2008-07-31 00:38:52 -0700 | [diff] [blame] | 367 | del_timer_sync(&hinfo->timer); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 368 | |
| 369 | /* remove proc entry */ |
| 370 | remove_proc_entry(hinfo->pde->name, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 371 | hinfo->family == NFPROTO_IPV4 ? hashlimit_procdir4 : |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 372 | hashlimit_procdir6); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 373 | htable_selective_cleanup(hinfo, select_all); |
| 374 | vfree(hinfo); |
| 375 | } |
| 376 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 377 | static struct xt_hashlimit_htable *htable_find_get(const char *name, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 378 | u_int8_t family) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 379 | { |
| 380 | struct xt_hashlimit_htable *hinfo; |
| 381 | struct hlist_node *pos; |
| 382 | |
| 383 | spin_lock_bh(&hashlimit_lock); |
| 384 | hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) { |
| 385 | if (!strcmp(name, hinfo->pde->name) && |
| 386 | hinfo->family == family) { |
| 387 | atomic_inc(&hinfo->use); |
| 388 | spin_unlock_bh(&hashlimit_lock); |
| 389 | return hinfo; |
| 390 | } |
| 391 | } |
| 392 | spin_unlock_bh(&hashlimit_lock); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | static void htable_put(struct xt_hashlimit_htable *hinfo) |
| 397 | { |
| 398 | if (atomic_dec_and_test(&hinfo->use)) { |
| 399 | spin_lock_bh(&hashlimit_lock); |
| 400 | hlist_del(&hinfo->node); |
| 401 | spin_unlock_bh(&hashlimit_lock); |
| 402 | htable_destroy(hinfo); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /* The algorithm used is the Simple Token Bucket Filter (TBF) |
| 407 | * see net/sched/sch_tbf.c in the linux source tree |
| 408 | */ |
| 409 | |
| 410 | /* Rusty: This is my (non-mathematically-inclined) understanding of |
| 411 | this algorithm. The `average rate' in jiffies becomes your initial |
| 412 | amount of credit `credit' and the most credit you can ever have |
| 413 | `credit_cap'. The `peak rate' becomes the cost of passing the |
| 414 | test, `cost'. |
| 415 | |
| 416 | `prev' tracks the last packet hit: you gain one credit per jiffy. |
| 417 | If you get credit balance more than this, the extra credit is |
| 418 | discarded. Every time the match passes, you lose `cost' credits; |
| 419 | if you don't have that many, the test fails. |
| 420 | |
| 421 | See Alexey's formal explanation in net/sched/sch_tbf.c. |
| 422 | |
| 423 | To get the maximum range, we multiply by this factor (ie. you get N |
| 424 | credits per jiffy). We want to allow a rate as low as 1 per day |
| 425 | (slowest userspace tool allows), which means |
| 426 | CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie. |
| 427 | */ |
| 428 | #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) |
| 429 | |
| 430 | /* Repeated shift and or gives us all 1s, final shift and add 1 gives |
| 431 | * us the power of 2 below the theoretical max, so GCC simply does a |
| 432 | * shift. */ |
| 433 | #define _POW2_BELOW2(x) ((x)|((x)>>1)) |
| 434 | #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) |
| 435 | #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) |
| 436 | #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) |
| 437 | #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) |
| 438 | #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) |
| 439 | |
| 440 | #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) |
| 441 | |
| 442 | /* Precision saver. */ |
| 443 | static inline u_int32_t |
| 444 | user2credits(u_int32_t user) |
| 445 | { |
| 446 | /* If multiplying would overflow... */ |
| 447 | if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) |
| 448 | /* Divide first. */ |
| 449 | return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; |
| 450 | |
| 451 | return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE; |
| 452 | } |
| 453 | |
| 454 | static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) |
| 455 | { |
| 456 | dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY; |
| 457 | if (dh->rateinfo.credit > dh->rateinfo.credit_cap) |
| 458 | dh->rateinfo.credit = dh->rateinfo.credit_cap; |
| 459 | dh->rateinfo.prev = now; |
| 460 | } |
| 461 | |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 462 | static inline __be32 maskl(__be32 a, unsigned int l) |
| 463 | { |
Patrick McHardy | 1b9b70e | 2008-04-09 15:14:18 -0700 | [diff] [blame] | 464 | return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0; |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Pavel Emelyanov | 3ed5df4 | 2008-01-31 18:42:26 -0800 | [diff] [blame] | 467 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 468 | static void hashlimit_ipv6_mask(__be32 *i, unsigned int p) |
| 469 | { |
| 470 | switch (p) { |
Patrick McHardy | 1b9b70e | 2008-04-09 15:14:18 -0700 | [diff] [blame] | 471 | case 0 ... 31: |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 472 | i[0] = maskl(i[0], p); |
| 473 | i[1] = i[2] = i[3] = 0; |
| 474 | break; |
Patrick McHardy | 1b9b70e | 2008-04-09 15:14:18 -0700 | [diff] [blame] | 475 | case 32 ... 63: |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 476 | i[1] = maskl(i[1], p - 32); |
| 477 | i[2] = i[3] = 0; |
| 478 | break; |
Patrick McHardy | 1b9b70e | 2008-04-09 15:14:18 -0700 | [diff] [blame] | 479 | case 64 ... 95: |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 480 | i[2] = maskl(i[2], p - 64); |
| 481 | i[3] = 0; |
Patrick McHardy | 1b9b70e | 2008-04-09 15:14:18 -0700 | [diff] [blame] | 482 | case 96 ... 127: |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 483 | i[3] = maskl(i[3], p - 96); |
| 484 | break; |
| 485 | case 128: |
| 486 | break; |
| 487 | } |
| 488 | } |
Pavel Emelyanov | 3ed5df4 | 2008-01-31 18:42:26 -0800 | [diff] [blame] | 489 | #endif |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 490 | |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 491 | static int |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 492 | hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo, |
| 493 | struct dsthash_dst *dst, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 494 | const struct sk_buff *skb, unsigned int protoff) |
| 495 | { |
| 496 | __be16 _ports[2], *ports; |
Patrick McHardy | 193b23c | 2007-12-04 23:51:48 -0800 | [diff] [blame] | 497 | u8 nexthdr; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 498 | |
| 499 | memset(dst, 0, sizeof(*dst)); |
| 500 | |
| 501 | switch (hinfo->family) { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 502 | case NFPROTO_IPV4: |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 503 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 504 | dst->ip.dst = maskl(ip_hdr(skb)->daddr, |
| 505 | hinfo->cfg.dstmask); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 506 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 507 | dst->ip.src = maskl(ip_hdr(skb)->saddr, |
| 508 | hinfo->cfg.srcmask); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 509 | |
| 510 | if (!(hinfo->cfg.mode & |
| 511 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) |
| 512 | return 0; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 513 | nexthdr = ip_hdr(skb)->protocol; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 514 | break; |
Yasuyuki Kozakai | 02dba02 | 2006-12-02 22:19:01 -0800 | [diff] [blame] | 515 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 516 | case NFPROTO_IPV6: |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 517 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) { |
| 518 | memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr, |
| 519 | sizeof(dst->ip6.dst)); |
| 520 | hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask); |
| 521 | } |
| 522 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) { |
| 523 | memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr, |
| 524 | sizeof(dst->ip6.src)); |
| 525 | hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask); |
| 526 | } |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 527 | |
| 528 | if (!(hinfo->cfg.mode & |
| 529 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) |
| 530 | return 0; |
Patrick McHardy | 193b23c | 2007-12-04 23:51:48 -0800 | [diff] [blame] | 531 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 532 | protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr); |
| 533 | if ((int)protoff < 0) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 534 | return -1; |
| 535 | break; |
| 536 | #endif |
| 537 | default: |
| 538 | BUG(); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | switch (nexthdr) { |
| 543 | case IPPROTO_TCP: |
| 544 | case IPPROTO_UDP: |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 545 | case IPPROTO_UDPLITE: |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 546 | case IPPROTO_SCTP: |
| 547 | case IPPROTO_DCCP: |
| 548 | ports = skb_header_pointer(skb, protoff, sizeof(_ports), |
| 549 | &_ports); |
| 550 | break; |
| 551 | default: |
| 552 | _ports[0] = _ports[1] = 0; |
| 553 | ports = _ports; |
| 554 | break; |
| 555 | } |
| 556 | if (!ports) |
| 557 | return -1; |
| 558 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT) |
| 559 | dst->src_port = ports[0]; |
| 560 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT) |
| 561 | dst->dst_port = ports[1]; |
| 562 | return 0; |
| 563 | } |
| 564 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 565 | static bool |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 566 | hashlimit_mt_v0(const struct sk_buff *skb, const struct net_device *in, |
| 567 | const struct net_device *out, const struct xt_match *match, |
| 568 | const void *matchinfo, int offset, unsigned int protoff, |
| 569 | bool *hotdrop) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 570 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 571 | const struct xt_hashlimit_info *r = |
| 572 | ((const struct xt_hashlimit_info *)matchinfo)->u.master; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 573 | struct xt_hashlimit_htable *hinfo = r->hinfo; |
| 574 | unsigned long now = jiffies; |
| 575 | struct dsthash_ent *dh; |
| 576 | struct dsthash_dst dst; |
| 577 | |
| 578 | if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) |
| 579 | goto hotdrop; |
| 580 | |
| 581 | spin_lock_bh(&hinfo->lock); |
| 582 | dh = dsthash_find(hinfo, &dst); |
| 583 | if (!dh) { |
| 584 | dh = dsthash_alloc_init(hinfo, &dst); |
| 585 | if (!dh) { |
| 586 | spin_unlock_bh(&hinfo->lock); |
| 587 | goto hotdrop; |
| 588 | } |
| 589 | |
| 590 | dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); |
| 591 | dh->rateinfo.prev = jiffies; |
| 592 | dh->rateinfo.credit = user2credits(hinfo->cfg.avg * |
| 593 | hinfo->cfg.burst); |
| 594 | dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * |
| 595 | hinfo->cfg.burst); |
| 596 | dh->rateinfo.cost = user2credits(hinfo->cfg.avg); |
| 597 | } else { |
| 598 | /* update expiration timeout */ |
| 599 | dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); |
| 600 | rateinfo_recalc(dh, now); |
| 601 | } |
| 602 | |
| 603 | if (dh->rateinfo.credit >= dh->rateinfo.cost) { |
| 604 | /* We're underlimit. */ |
| 605 | dh->rateinfo.credit -= dh->rateinfo.cost; |
| 606 | spin_unlock_bh(&hinfo->lock); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 607 | return true; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 608 | } |
| 609 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 610 | spin_unlock_bh(&hinfo->lock); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 611 | |
| 612 | /* default case: we're overlimit, thus don't match */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 613 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 614 | |
| 615 | hotdrop: |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 616 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 617 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 620 | static bool |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 621 | hashlimit_mt(const struct sk_buff *skb, const struct net_device *in, |
| 622 | const struct net_device *out, const struct xt_match *match, |
| 623 | const void *matchinfo, int offset, unsigned int protoff, |
| 624 | bool *hotdrop) |
| 625 | { |
| 626 | const struct xt_hashlimit_mtinfo1 *info = matchinfo; |
| 627 | struct xt_hashlimit_htable *hinfo = info->hinfo; |
| 628 | unsigned long now = jiffies; |
| 629 | struct dsthash_ent *dh; |
| 630 | struct dsthash_dst dst; |
| 631 | |
| 632 | if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) |
| 633 | goto hotdrop; |
| 634 | |
| 635 | spin_lock_bh(&hinfo->lock); |
| 636 | dh = dsthash_find(hinfo, &dst); |
| 637 | if (dh == NULL) { |
| 638 | dh = dsthash_alloc_init(hinfo, &dst); |
| 639 | if (dh == NULL) { |
| 640 | spin_unlock_bh(&hinfo->lock); |
| 641 | goto hotdrop; |
| 642 | } |
| 643 | |
| 644 | dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); |
| 645 | dh->rateinfo.prev = jiffies; |
| 646 | dh->rateinfo.credit = user2credits(hinfo->cfg.avg * |
| 647 | hinfo->cfg.burst); |
| 648 | dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * |
| 649 | hinfo->cfg.burst); |
| 650 | dh->rateinfo.cost = user2credits(hinfo->cfg.avg); |
| 651 | } else { |
| 652 | /* update expiration timeout */ |
| 653 | dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); |
| 654 | rateinfo_recalc(dh, now); |
| 655 | } |
| 656 | |
| 657 | if (dh->rateinfo.credit >= dh->rateinfo.cost) { |
| 658 | /* below the limit */ |
| 659 | dh->rateinfo.credit -= dh->rateinfo.cost; |
| 660 | spin_unlock_bh(&hinfo->lock); |
| 661 | return !(info->cfg.mode & XT_HASHLIMIT_INVERT); |
| 662 | } |
| 663 | |
| 664 | spin_unlock_bh(&hinfo->lock); |
| 665 | /* default match is underlimit - so over the limit, we need to invert */ |
| 666 | return info->cfg.mode & XT_HASHLIMIT_INVERT; |
| 667 | |
| 668 | hotdrop: |
| 669 | *hotdrop = true; |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | static bool |
| 674 | hashlimit_mt_check_v0(const char *tablename, const void *inf, |
| 675 | const struct xt_match *match, void *matchinfo, |
| 676 | unsigned int hook_mask) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 677 | { |
| 678 | struct xt_hashlimit_info *r = matchinfo; |
| 679 | |
| 680 | /* Check for overflow. */ |
| 681 | if (r->cfg.burst == 0 || |
| 682 | user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) { |
| 683 | printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", |
| 684 | r->cfg.avg, r->cfg.burst); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 685 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 686 | } |
| 687 | if (r->cfg.mode == 0 || |
| 688 | r->cfg.mode > (XT_HASHLIMIT_HASH_DPT | |
| 689 | XT_HASHLIMIT_HASH_DIP | |
| 690 | XT_HASHLIMIT_HASH_SIP | |
| 691 | XT_HASHLIMIT_HASH_SPT)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 692 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 693 | if (!r->cfg.gc_interval) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 694 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 695 | if (!r->cfg.expire) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 696 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 697 | if (r->name[sizeof(r->name) - 1] != '\0') |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 698 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 699 | |
| 700 | /* This is the best we've got: We cannot release and re-grab lock, |
| 701 | * since checkentry() is called before x_tables.c grabs xt_mutex. |
| 702 | * We also cannot grab the hashtable spinlock, since htable_create will |
| 703 | * call vmalloc, and that can sleep. And we cannot just re-search |
| 704 | * the list of htable's in htable_create(), since then we would |
| 705 | * create duplicate proc files. -HW */ |
| 706 | mutex_lock(&hlimit_mutex); |
| 707 | r->hinfo = htable_find_get(r->name, match->family); |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 708 | if (!r->hinfo && htable_create_v0(r, match->family) != 0) { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 709 | mutex_unlock(&hlimit_mutex); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 710 | return false; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 711 | } |
| 712 | mutex_unlock(&hlimit_mutex); |
| 713 | |
| 714 | /* Ugly hack: For SMP, we only want to use one set */ |
| 715 | r->u.master = r; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 716 | return true; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 719 | static bool |
| 720 | hashlimit_mt_check(const char *tablename, const void *inf, |
| 721 | const struct xt_match *match, void *matchinfo, |
| 722 | unsigned int hook_mask) |
| 723 | { |
| 724 | struct xt_hashlimit_mtinfo1 *info = matchinfo; |
| 725 | |
| 726 | /* Check for overflow. */ |
| 727 | if (info->cfg.burst == 0 || |
| 728 | user2credits(info->cfg.avg * info->cfg.burst) < |
| 729 | user2credits(info->cfg.avg)) { |
| 730 | printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", |
| 731 | info->cfg.avg, info->cfg.burst); |
| 732 | return false; |
| 733 | } |
| 734 | if (info->cfg.gc_interval == 0 || info->cfg.expire == 0) |
| 735 | return false; |
| 736 | if (info->name[sizeof(info->name)-1] != '\0') |
| 737 | return false; |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 738 | if (match->family == NFPROTO_IPV4) { |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 739 | if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32) |
| 740 | return false; |
| 741 | } else { |
| 742 | if (info->cfg.srcmask > 128 || info->cfg.dstmask > 128) |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | /* This is the best we've got: We cannot release and re-grab lock, |
| 747 | * since checkentry() is called before x_tables.c grabs xt_mutex. |
| 748 | * We also cannot grab the hashtable spinlock, since htable_create will |
| 749 | * call vmalloc, and that can sleep. And we cannot just re-search |
| 750 | * the list of htable's in htable_create(), since then we would |
| 751 | * create duplicate proc files. -HW */ |
| 752 | mutex_lock(&hlimit_mutex); |
| 753 | info->hinfo = htable_find_get(info->name, match->family); |
| 754 | if (!info->hinfo && htable_create(info, match->family) != 0) { |
| 755 | mutex_unlock(&hlimit_mutex); |
| 756 | return false; |
| 757 | } |
| 758 | mutex_unlock(&hlimit_mutex); |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 762 | static void |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 763 | hashlimit_mt_destroy_v0(const struct xt_match *match, void *matchinfo) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 764 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 765 | const struct xt_hashlimit_info *r = matchinfo; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 766 | |
| 767 | htable_put(r->hinfo); |
| 768 | } |
| 769 | |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 770 | static void |
| 771 | hashlimit_mt_destroy(const struct xt_match *match, void *matchinfo) |
| 772 | { |
| 773 | const struct xt_hashlimit_mtinfo1 *info = matchinfo; |
| 774 | |
| 775 | htable_put(info->hinfo); |
| 776 | } |
| 777 | |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 778 | #ifdef CONFIG_COMPAT |
| 779 | struct compat_xt_hashlimit_info { |
| 780 | char name[IFNAMSIZ]; |
| 781 | struct hashlimit_cfg cfg; |
| 782 | compat_uptr_t hinfo; |
| 783 | compat_uptr_t master; |
| 784 | }; |
| 785 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 786 | static void hashlimit_mt_compat_from_user(void *dst, void *src) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 787 | { |
| 788 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
| 789 | |
| 790 | memcpy(dst, src, off); |
| 791 | memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off); |
| 792 | } |
| 793 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 794 | static int hashlimit_mt_compat_to_user(void __user *dst, void *src) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 795 | { |
| 796 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
| 797 | |
| 798 | return copy_to_user(dst, src, off) ? -EFAULT : 0; |
| 799 | } |
| 800 | #endif |
| 801 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 802 | static struct xt_match hashlimit_mt_reg[] __read_mostly = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 803 | { |
| 804 | .name = "hashlimit", |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 805 | .revision = 0, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 806 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 807 | .match = hashlimit_mt_v0, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 808 | .matchsize = sizeof(struct xt_hashlimit_info), |
| 809 | #ifdef CONFIG_COMPAT |
| 810 | .compatsize = sizeof(struct compat_xt_hashlimit_info), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 811 | .compat_from_user = hashlimit_mt_compat_from_user, |
| 812 | .compat_to_user = hashlimit_mt_compat_to_user, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 813 | #endif |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 814 | .checkentry = hashlimit_mt_check_v0, |
| 815 | .destroy = hashlimit_mt_destroy_v0, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 816 | .me = THIS_MODULE |
| 817 | }, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 818 | { |
| 819 | .name = "hashlimit", |
| 820 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 821 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 822 | .match = hashlimit_mt, |
| 823 | .matchsize = sizeof(struct xt_hashlimit_mtinfo1), |
| 824 | .checkentry = hashlimit_mt_check, |
| 825 | .destroy = hashlimit_mt_destroy, |
| 826 | .me = THIS_MODULE, |
| 827 | }, |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 828 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 829 | { |
| 830 | .name = "hashlimit", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 831 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 832 | .match = hashlimit_mt_v0, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 833 | .matchsize = sizeof(struct xt_hashlimit_info), |
| 834 | #ifdef CONFIG_COMPAT |
| 835 | .compatsize = sizeof(struct compat_xt_hashlimit_info), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 836 | .compat_from_user = hashlimit_mt_compat_from_user, |
| 837 | .compat_to_user = hashlimit_mt_compat_to_user, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 838 | #endif |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 839 | .checkentry = hashlimit_mt_check_v0, |
| 840 | .destroy = hashlimit_mt_destroy_v0, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 841 | .me = THIS_MODULE |
| 842 | }, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 843 | { |
| 844 | .name = "hashlimit", |
| 845 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 846 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 847 | .match = hashlimit_mt, |
| 848 | .matchsize = sizeof(struct xt_hashlimit_mtinfo1), |
| 849 | .checkentry = hashlimit_mt_check, |
| 850 | .destroy = hashlimit_mt_destroy, |
| 851 | .me = THIS_MODULE, |
| 852 | }, |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 853 | #endif |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | /* PROC stuff */ |
| 857 | static void *dl_seq_start(struct seq_file *s, loff_t *pos) |
Stephen Hemminger | f4f6fb7 | 2008-01-31 04:08:39 -0800 | [diff] [blame] | 858 | __acquires(htable->lock) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 859 | { |
| 860 | struct proc_dir_entry *pde = s->private; |
| 861 | struct xt_hashlimit_htable *htable = pde->data; |
| 862 | unsigned int *bucket; |
| 863 | |
| 864 | spin_lock_bh(&htable->lock); |
| 865 | if (*pos >= htable->cfg.size) |
| 866 | return NULL; |
| 867 | |
| 868 | bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC); |
| 869 | if (!bucket) |
| 870 | return ERR_PTR(-ENOMEM); |
| 871 | |
| 872 | *bucket = *pos; |
| 873 | return bucket; |
| 874 | } |
| 875 | |
| 876 | static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 877 | { |
| 878 | struct proc_dir_entry *pde = s->private; |
| 879 | struct xt_hashlimit_htable *htable = pde->data; |
| 880 | unsigned int *bucket = (unsigned int *)v; |
| 881 | |
| 882 | *pos = ++(*bucket); |
| 883 | if (*pos >= htable->cfg.size) { |
| 884 | kfree(v); |
| 885 | return NULL; |
| 886 | } |
| 887 | return bucket; |
| 888 | } |
| 889 | |
| 890 | static void dl_seq_stop(struct seq_file *s, void *v) |
Stephen Hemminger | f4f6fb7 | 2008-01-31 04:08:39 -0800 | [diff] [blame] | 891 | __releases(htable->lock) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 892 | { |
| 893 | struct proc_dir_entry *pde = s->private; |
| 894 | struct xt_hashlimit_htable *htable = pde->data; |
| 895 | unsigned int *bucket = (unsigned int *)v; |
| 896 | |
| 897 | kfree(bucket); |
| 898 | spin_unlock_bh(&htable->lock); |
| 899 | } |
| 900 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 901 | static int dl_seq_real_show(struct dsthash_ent *ent, u_int8_t family, |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 902 | struct seq_file *s) |
| 903 | { |
| 904 | /* recalculate to show accurate numbers */ |
| 905 | rateinfo_recalc(ent, jiffies); |
| 906 | |
| 907 | switch (family) { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 908 | case NFPROTO_IPV4: |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 909 | return seq_printf(s, "%ld %u.%u.%u.%u:%u->" |
| 910 | "%u.%u.%u.%u:%u %u %u %u\n", |
| 911 | (long)(ent->expires - jiffies)/HZ, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 912 | NIPQUAD(ent->dst.ip.src), |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 913 | ntohs(ent->dst.src_port), |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 914 | NIPQUAD(ent->dst.ip.dst), |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 915 | ntohs(ent->dst.dst_port), |
| 916 | ent->rateinfo.credit, ent->rateinfo.credit_cap, |
| 917 | ent->rateinfo.cost); |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 918 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 919 | case NFPROTO_IPV6: |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 920 | return seq_printf(s, "%ld " NIP6_FMT ":%u->" |
| 921 | NIP6_FMT ":%u %u %u %u\n", |
| 922 | (long)(ent->expires - jiffies)/HZ, |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 923 | NIP6(*(struct in6_addr *)&ent->dst.ip6.src), |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 924 | ntohs(ent->dst.src_port), |
Jan Engelhardt | 09e410d | 2008-01-31 04:48:13 -0800 | [diff] [blame] | 925 | NIP6(*(struct in6_addr *)&ent->dst.ip6.dst), |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 926 | ntohs(ent->dst.dst_port), |
| 927 | ent->rateinfo.credit, ent->rateinfo.credit_cap, |
| 928 | ent->rateinfo.cost); |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 929 | #endif |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 930 | default: |
| 931 | BUG(); |
| 932 | return 0; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | static int dl_seq_show(struct seq_file *s, void *v) |
| 937 | { |
| 938 | struct proc_dir_entry *pde = s->private; |
| 939 | struct xt_hashlimit_htable *htable = pde->data; |
| 940 | unsigned int *bucket = (unsigned int *)v; |
| 941 | struct dsthash_ent *ent; |
| 942 | struct hlist_node *pos; |
| 943 | |
| 944 | if (!hlist_empty(&htable->hash[*bucket])) { |
| 945 | hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node) |
| 946 | if (dl_seq_real_show(ent, htable->family, s)) |
| 947 | return 1; |
| 948 | } |
| 949 | return 0; |
| 950 | } |
| 951 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 952 | static const struct seq_operations dl_seq_ops = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 953 | .start = dl_seq_start, |
| 954 | .next = dl_seq_next, |
| 955 | .stop = dl_seq_stop, |
| 956 | .show = dl_seq_show |
| 957 | }; |
| 958 | |
| 959 | static int dl_proc_open(struct inode *inode, struct file *file) |
| 960 | { |
| 961 | int ret = seq_open(file, &dl_seq_ops); |
| 962 | |
| 963 | if (!ret) { |
| 964 | struct seq_file *sf = file->private_data; |
| 965 | sf->private = PDE(inode); |
| 966 | } |
| 967 | return ret; |
| 968 | } |
| 969 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 970 | static const struct file_operations dl_file_ops = { |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 971 | .owner = THIS_MODULE, |
| 972 | .open = dl_proc_open, |
| 973 | .read = seq_read, |
| 974 | .llseek = seq_lseek, |
| 975 | .release = seq_release |
| 976 | }; |
| 977 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 978 | static int __init hashlimit_mt_init(void) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 979 | { |
| 980 | int err; |
| 981 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 982 | err = xt_register_matches(hashlimit_mt_reg, |
| 983 | ARRAY_SIZE(hashlimit_mt_reg)); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 984 | if (err < 0) |
| 985 | goto err1; |
| 986 | |
| 987 | err = -ENOMEM; |
| 988 | hashlimit_cachep = kmem_cache_create("xt_hashlimit", |
| 989 | sizeof(struct dsthash_ent), 0, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 990 | NULL); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 991 | if (!hashlimit_cachep) { |
| 992 | printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n"); |
| 993 | goto err2; |
| 994 | } |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 995 | hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", init_net.proc_net); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 996 | if (!hashlimit_procdir4) { |
| 997 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " |
| 998 | "entry\n"); |
| 999 | goto err3; |
| 1000 | } |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 1001 | err = 0; |
| 1002 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1003 | hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", init_net.proc_net); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1004 | if (!hashlimit_procdir6) { |
Alexey Dobriyan | 9c2440b | 2007-01-02 00:42:00 -0800 | [diff] [blame] | 1005 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1006 | "entry\n"); |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 1007 | err = -ENOMEM; |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1008 | } |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 1009 | #endif |
| 1010 | if (!err) |
| 1011 | return 0; |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1012 | remove_proc_entry("ipt_hashlimit", init_net.proc_net); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1013 | err3: |
| 1014 | kmem_cache_destroy(hashlimit_cachep); |
| 1015 | err2: |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 1016 | xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1017 | err1: |
| 1018 | return err; |
| 1019 | |
| 1020 | } |
| 1021 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 1022 | static void __exit hashlimit_mt_exit(void) |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1023 | { |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1024 | remove_proc_entry("ipt_hashlimit", init_net.proc_net); |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 1025 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1026 | remove_proc_entry("ip6t_hashlimit", init_net.proc_net); |
Eric Dumazet | 7b21e09 | 2007-12-17 22:45:28 -0800 | [diff] [blame] | 1027 | #endif |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1028 | kmem_cache_destroy(hashlimit_cachep); |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 1029 | xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); |
Patrick McHardy | 39b46fc | 2006-11-29 02:35:36 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 1032 | module_init(hashlimit_mt_init); |
| 1033 | module_exit(hashlimit_mt_exit); |