Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Patrick McHardy <kaber@trash.net> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This is a replacement of the old ipt_recent module, which carried the |
| 9 | * following copyright notice: |
| 10 | * |
| 11 | * Author: Stephen Frost <sfrost@snowman.net> |
| 12 | * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org |
| 13 | */ |
| 14 | #include <linux/init.h> |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 15 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/moduleparam.h> |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 17 | #include <linux/proc_fs.h> |
| 18 | #include <linux/seq_file.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/random.h> |
| 23 | #include <linux/jhash.h> |
| 24 | #include <linux/bitops.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/inet.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 28 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/netfilter_ipv4/ipt_recent.h> |
| 30 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 31 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 32 | MODULE_DESCRIPTION("IP tables recently seen matching module"); |
| 33 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 35 | static unsigned int ip_list_tot = 100; |
| 36 | static unsigned int ip_pkt_list_tot = 20; |
| 37 | static unsigned int ip_list_hash_size = 0; |
| 38 | static unsigned int ip_list_perms = 0644; |
Daniel De Graaf | b93ff78 | 2006-08-22 00:30:55 -0700 | [diff] [blame] | 39 | static unsigned int ip_list_uid = 0; |
| 40 | static unsigned int ip_list_gid = 0; |
Patrick McHardy | e7be699 | 2006-01-05 12:19:46 -0800 | [diff] [blame] | 41 | module_param(ip_list_tot, uint, 0400); |
| 42 | module_param(ip_pkt_list_tot, uint, 0400); |
| 43 | module_param(ip_list_hash_size, uint, 0400); |
| 44 | module_param(ip_list_perms, uint, 0400); |
Daniel De Graaf | b93ff78 | 2006-08-22 00:30:55 -0700 | [diff] [blame] | 45 | module_param(ip_list_uid, uint, 0400); |
| 46 | module_param(ip_list_gid, uint, 0400); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 47 | MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list"); |
| 48 | MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)"); |
| 49 | MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs"); |
| 50 | MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files"); |
Daniel De Graaf | b93ff78 | 2006-08-22 00:30:55 -0700 | [diff] [blame] | 51 | MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/ipt_recent/* files"); |
| 52 | MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/ipt_recent/* files"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 54 | struct recent_entry { |
| 55 | struct list_head list; |
| 56 | struct list_head lru_list; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 57 | __be32 addr; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 58 | u_int8_t ttl; |
| 59 | u_int8_t index; |
| 60 | u_int16_t nstamps; |
| 61 | unsigned long stamps[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 64 | struct recent_table { |
| 65 | struct list_head list; |
| 66 | char name[IPT_RECENT_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #ifdef CONFIG_PROC_FS |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 68 | struct proc_dir_entry *proc; |
| 69 | #endif |
| 70 | unsigned int refcnt; |
| 71 | unsigned int entries; |
| 72 | struct list_head lru_list; |
| 73 | struct list_head iphash[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 76 | static LIST_HEAD(tables); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static DEFINE_SPINLOCK(recent_lock); |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 78 | static DEFINE_MUTEX(recent_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | #ifdef CONFIG_PROC_FS |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 81 | static struct proc_dir_entry *proc_dir; |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 82 | static const struct file_operations recent_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #endif |
| 84 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 85 | static u_int32_t hash_rnd; |
| 86 | static int hash_rnd_initted; |
| 87 | |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 88 | static unsigned int recent_entry_hash(__be32 addr) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 89 | { |
| 90 | if (!hash_rnd_initted) { |
| 91 | get_random_bytes(&hash_rnd, 4); |
| 92 | hash_rnd_initted = 1; |
| 93 | } |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 94 | return jhash_1word((__force u32)addr, hash_rnd) & (ip_list_hash_size - 1); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static struct recent_entry * |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 98 | recent_entry_lookup(const struct recent_table *table, __be32 addr, u_int8_t ttl) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 99 | { |
| 100 | struct recent_entry *e; |
| 101 | unsigned int h; |
| 102 | |
| 103 | h = recent_entry_hash(addr); |
| 104 | list_for_each_entry(e, &table->iphash[h], list) |
| 105 | if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl)) |
| 106 | return e; |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) |
| 111 | { |
| 112 | list_del(&e->list); |
| 113 | list_del(&e->lru_list); |
| 114 | kfree(e); |
| 115 | t->entries--; |
| 116 | } |
| 117 | |
| 118 | static struct recent_entry * |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 119 | recent_entry_init(struct recent_table *t, __be32 addr, u_int8_t ttl) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 120 | { |
| 121 | struct recent_entry *e; |
| 122 | |
| 123 | if (t->entries >= ip_list_tot) { |
| 124 | e = list_entry(t->lru_list.next, struct recent_entry, lru_list); |
| 125 | recent_entry_remove(t, e); |
| 126 | } |
| 127 | e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot, |
| 128 | GFP_ATOMIC); |
| 129 | if (e == NULL) |
| 130 | return NULL; |
| 131 | e->addr = addr; |
| 132 | e->ttl = ttl; |
| 133 | e->stamps[0] = jiffies; |
| 134 | e->nstamps = 1; |
| 135 | e->index = 1; |
| 136 | list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]); |
| 137 | list_add_tail(&e->lru_list, &t->lru_list); |
| 138 | t->entries++; |
| 139 | return e; |
| 140 | } |
| 141 | |
| 142 | static void recent_entry_update(struct recent_table *t, struct recent_entry *e) |
| 143 | { |
| 144 | e->stamps[e->index++] = jiffies; |
| 145 | if (e->index > e->nstamps) |
| 146 | e->nstamps = e->index; |
| 147 | e->index %= ip_pkt_list_tot; |
| 148 | list_move_tail(&e->lru_list, &t->lru_list); |
| 149 | } |
| 150 | |
| 151 | static struct recent_table *recent_table_lookup(const char *name) |
| 152 | { |
| 153 | struct recent_table *t; |
| 154 | |
| 155 | list_for_each_entry(t, &tables, list) |
| 156 | if (!strcmp(t->name, name)) |
| 157 | return t; |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static void recent_table_flush(struct recent_table *t) |
| 162 | { |
| 163 | struct recent_entry *e, *next; |
| 164 | unsigned int i; |
| 165 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 166 | for (i = 0; i < ip_list_hash_size; i++) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 167 | list_for_each_entry_safe(e, next, &t->iphash[i], list) |
| 168 | recent_entry_remove(t, e); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 171 | static bool |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 172 | ipt_recent_match(const struct sk_buff *skb, |
| 173 | const struct net_device *in, const struct net_device *out, |
| 174 | const struct xt_match *match, const void *matchinfo, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 175 | int offset, unsigned int protoff, bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | const struct ipt_recent_info *info = matchinfo; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 178 | struct recent_table *t; |
| 179 | struct recent_entry *e; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 180 | __be32 addr; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 181 | u_int8_t ttl; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 182 | bool ret = info->invert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 184 | if (info->side == IPT_RECENT_DEST) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 185 | addr = ip_hdr(skb)->daddr; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 186 | else |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 187 | addr = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 189 | ttl = ip_hdr(skb)->ttl; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 190 | /* use TTL as seen before forwarding */ |
| 191 | if (out && !skb->sk) |
| 192 | ttl++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | spin_lock_bh(&recent_lock); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 195 | t = recent_table_lookup(info->name); |
| 196 | e = recent_entry_lookup(t, addr, |
| 197 | info->check_set & IPT_RECENT_TTL ? ttl : 0); |
| 198 | if (e == NULL) { |
| 199 | if (!(info->check_set & IPT_RECENT_SET)) |
| 200 | goto out; |
| 201 | e = recent_entry_init(t, addr, ttl); |
| 202 | if (e == NULL) |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 203 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 204 | ret = !ret; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 205 | goto out; |
| 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 208 | if (info->check_set & IPT_RECENT_SET) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 209 | ret = !ret; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 210 | else if (info->check_set & IPT_RECENT_REMOVE) { |
| 211 | recent_entry_remove(t, e); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 212 | ret = !ret; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 213 | } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) { |
| 214 | unsigned long t = jiffies - info->seconds * HZ; |
| 215 | unsigned int i, hits = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 217 | for (i = 0; i < e->nstamps; i++) { |
| 218 | if (info->seconds && time_after(t, e->stamps[i])) |
| 219 | continue; |
| 220 | if (++hits >= info->hit_count) { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 221 | ret = !ret; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (info->check_set & IPT_RECENT_SET || |
| 228 | (info->check_set & IPT_RECENT_UPDATE && ret)) { |
| 229 | recent_entry_update(t, e); |
| 230 | e->ttl = ttl; |
| 231 | } |
| 232 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | spin_unlock_bh(&recent_lock); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 234 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 237 | static bool |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 238 | ipt_recent_checkentry(const char *tablename, const void *ip, |
| 239 | const struct xt_match *match, void *matchinfo, |
Patrick McHardy | efa7416 | 2006-08-22 00:36:37 -0700 | [diff] [blame] | 240 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | const struct ipt_recent_info *info = matchinfo; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 243 | struct recent_table *t; |
| 244 | unsigned i; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 245 | bool ret = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 247 | if (hweight8(info->check_set & |
| 248 | (IPT_RECENT_SET | IPT_RECENT_REMOVE | |
| 249 | IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 250 | return false; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 251 | if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) && |
| 252 | (info->seconds || info->hit_count)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 253 | return false; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 254 | if (info->name[0] == '\0' || |
| 255 | strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 256 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 258 | mutex_lock(&recent_mutex); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 259 | t = recent_table_lookup(info->name); |
| 260 | if (t != NULL) { |
| 261 | t->refcnt++; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 262 | ret = true; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 263 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 266 | t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size, |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 267 | GFP_KERNEL); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 268 | if (t == NULL) |
| 269 | goto out; |
Patrick McHardy | 2b2283d | 2006-06-09 12:18:17 -0700 | [diff] [blame] | 270 | t->refcnt = 1; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 271 | strcpy(t->name, info->name); |
| 272 | INIT_LIST_HEAD(&t->lru_list); |
| 273 | for (i = 0; i < ip_list_hash_size; i++) |
| 274 | INIT_LIST_HEAD(&t->iphash[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | #ifdef CONFIG_PROC_FS |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 276 | t->proc = create_proc_entry(t->name, ip_list_perms, proc_dir); |
| 277 | if (t->proc == NULL) { |
| 278 | kfree(t); |
| 279 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 281 | t->proc->proc_fops = &recent_fops; |
Daniel De Graaf | b93ff78 | 2006-08-22 00:30:55 -0700 | [diff] [blame] | 282 | t->proc->uid = ip_list_uid; |
| 283 | t->proc->gid = ip_list_gid; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 284 | t->proc->data = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | #endif |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 286 | spin_lock_bh(&recent_lock); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 287 | list_add_tail(&t->list, &tables); |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 288 | spin_unlock_bh(&recent_lock); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 289 | ret = true; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 290 | out: |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 291 | mutex_unlock(&recent_mutex); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 292 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static void |
Patrick McHardy | efa7416 | 2006-08-22 00:36:37 -0700 | [diff] [blame] | 296 | ipt_recent_destroy(const struct xt_match *match, void *matchinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
| 298 | const struct ipt_recent_info *info = matchinfo; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 299 | struct recent_table *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 301 | mutex_lock(&recent_mutex); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 302 | t = recent_table_lookup(info->name); |
| 303 | if (--t->refcnt == 0) { |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 304 | spin_lock_bh(&recent_lock); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 305 | list_del(&t->list); |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 306 | spin_unlock_bh(&recent_lock); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 307 | recent_table_flush(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | #ifdef CONFIG_PROC_FS |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 309 | remove_proc_entry(t->name, proc_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | #endif |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 311 | kfree(t); |
| 312 | } |
Patrick McHardy | a0e889b | 2006-06-09 12:17:41 -0700 | [diff] [blame] | 313 | mutex_unlock(&recent_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 316 | #ifdef CONFIG_PROC_FS |
| 317 | struct recent_iter_state { |
| 318 | struct recent_table *table; |
| 319 | unsigned int bucket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 322 | static void *recent_seq_start(struct seq_file *seq, loff_t *pos) |
| 323 | { |
| 324 | struct recent_iter_state *st = seq->private; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 325 | const struct recent_table *t = st->table; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 326 | struct recent_entry *e; |
| 327 | loff_t p = *pos; |
| 328 | |
| 329 | spin_lock_bh(&recent_lock); |
| 330 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 331 | for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) |
| 332 | list_for_each_entry(e, &t->iphash[st->bucket], list) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 333 | if (p-- == 0) |
| 334 | return e; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 339 | { |
| 340 | struct recent_iter_state *st = seq->private; |
| 341 | struct recent_table *t = st->table; |
| 342 | struct recent_entry *e = v; |
| 343 | struct list_head *head = e->list.next; |
| 344 | |
| 345 | while (head == &t->iphash[st->bucket]) { |
| 346 | if (++st->bucket >= ip_list_hash_size) |
| 347 | return NULL; |
| 348 | head = t->iphash[st->bucket].next; |
| 349 | } |
| 350 | (*pos)++; |
| 351 | return list_entry(head, struct recent_entry, list); |
| 352 | } |
| 353 | |
| 354 | static void recent_seq_stop(struct seq_file *s, void *v) |
| 355 | { |
| 356 | spin_unlock_bh(&recent_lock); |
| 357 | } |
| 358 | |
| 359 | static int recent_seq_show(struct seq_file *seq, void *v) |
| 360 | { |
| 361 | struct recent_entry *e = v; |
| 362 | unsigned int i; |
| 363 | |
| 364 | i = (e->index - 1) % ip_pkt_list_tot; |
| 365 | seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u", |
| 366 | NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index); |
| 367 | for (i = 0; i < e->nstamps; i++) |
| 368 | seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]); |
| 369 | seq_printf(seq, "\n"); |
| 370 | return 0; |
| 371 | } |
| 372 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame^] | 373 | static const struct seq_operations recent_seq_ops = { |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 374 | .start = recent_seq_start, |
| 375 | .next = recent_seq_next, |
| 376 | .stop = recent_seq_stop, |
| 377 | .show = recent_seq_show, |
| 378 | }; |
| 379 | |
| 380 | static int recent_seq_open(struct inode *inode, struct file *file) |
| 381 | { |
| 382 | struct proc_dir_entry *pde = PDE(inode); |
| 383 | struct seq_file *seq; |
| 384 | struct recent_iter_state *st; |
| 385 | int ret; |
| 386 | |
| 387 | st = kzalloc(sizeof(*st), GFP_KERNEL); |
| 388 | if (st == NULL) |
| 389 | return -ENOMEM; |
| 390 | ret = seq_open(file, &recent_seq_ops); |
| 391 | if (ret) |
| 392 | kfree(st); |
| 393 | st->table = pde->data; |
| 394 | seq = file->private_data; |
| 395 | seq->private = st; |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static ssize_t recent_proc_write(struct file *file, const char __user *input, |
| 400 | size_t size, loff_t *loff) |
| 401 | { |
Josef Sipek | 6df81ab | 2006-12-08 02:37:24 -0800 | [diff] [blame] | 402 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 403 | struct recent_table *t = pde->data; |
| 404 | struct recent_entry *e; |
| 405 | char buf[sizeof("+255.255.255.255")], *c = buf; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 406 | __be32 addr; |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 407 | int add; |
| 408 | |
| 409 | if (size > sizeof(buf)) |
| 410 | size = sizeof(buf); |
| 411 | if (copy_from_user(buf, input, size)) |
| 412 | return -EFAULT; |
| 413 | while (isspace(*c)) |
| 414 | c++; |
| 415 | |
| 416 | if (size - (c - buf) < 5) |
| 417 | return c - buf; |
| 418 | if (!strncmp(c, "clear", 5)) { |
| 419 | c += 5; |
| 420 | spin_lock_bh(&recent_lock); |
| 421 | recent_table_flush(t); |
| 422 | spin_unlock_bh(&recent_lock); |
| 423 | return c - buf; |
| 424 | } |
| 425 | |
| 426 | switch (*c) { |
| 427 | case '-': |
| 428 | add = 0; |
| 429 | c++; |
| 430 | break; |
| 431 | case '+': |
| 432 | c++; |
| 433 | default: |
| 434 | add = 1; |
| 435 | break; |
| 436 | } |
| 437 | addr = in_aton(c); |
| 438 | |
| 439 | spin_lock_bh(&recent_lock); |
| 440 | e = recent_entry_lookup(t, addr, 0); |
| 441 | if (e == NULL) { |
| 442 | if (add) |
| 443 | recent_entry_init(t, addr, 0); |
| 444 | } else { |
| 445 | if (add) |
| 446 | recent_entry_update(t, e); |
| 447 | else |
| 448 | recent_entry_remove(t, e); |
| 449 | } |
| 450 | spin_unlock_bh(&recent_lock); |
| 451 | return size; |
| 452 | } |
| 453 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 454 | static const struct file_operations recent_fops = { |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 455 | .open = recent_seq_open, |
| 456 | .read = seq_read, |
| 457 | .write = recent_proc_write, |
| 458 | .release = seq_release_private, |
| 459 | .owner = THIS_MODULE, |
| 460 | }; |
| 461 | #endif /* CONFIG_PROC_FS */ |
| 462 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 463 | static struct xt_match recent_match __read_mostly = { |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 464 | .name = "recent", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 465 | .family = AF_INET, |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 466 | .match = ipt_recent_match, |
| 467 | .matchsize = sizeof(struct ipt_recent_info), |
| 468 | .checkentry = ipt_recent_checkentry, |
| 469 | .destroy = ipt_recent_destroy, |
| 470 | .me = THIS_MODULE, |
| 471 | }; |
| 472 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 473 | static int __init ipt_recent_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 475 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 477 | if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255) |
| 478 | return -EINVAL; |
| 479 | ip_list_hash_size = 1 << fls(ip_list_tot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 481 | err = xt_register_match(&recent_match); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 482 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (err) |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 484 | return err; |
| 485 | proc_dir = proc_mkdir("ipt_recent", proc_net); |
| 486 | if (proc_dir == NULL) { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 487 | xt_unregister_match(&recent_match); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 488 | err = -ENOMEM; |
| 489 | } |
| 490 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | return err; |
| 492 | } |
| 493 | |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 494 | static void __exit ipt_recent_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 496 | BUG_ON(!list_empty(&tables)); |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 497 | xt_unregister_match(&recent_match); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 498 | #ifdef CONFIG_PROC_FS |
| 499 | remove_proc_entry("ipt_recent", proc_net); |
| 500 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 503 | module_init(ipt_recent_init); |
Patrick McHardy | 404bdbf | 2006-05-29 18:21:34 -0700 | [diff] [blame] | 504 | module_exit(ipt_recent_exit); |