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