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