blob: 791e030ea9031358d8d19f6da099554fb090b304 [file] [log] [blame]
Patrick McHardy404bdbf2006-05-29 18:21:34 -07001/*
2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
Jan Engelhardt079aa882008-10-08 11:35:00 +02003 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
Patrick McHardy404bdbf2006-05-29 18:21:34 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This is a replacement of the old ipt_recent module, which carried the
10 * following copyright notice:
11 *
12 * Author: Stephen Frost <sfrost@snowman.net>
13 * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
14 */
15#include <linux/init.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080016#include <linux/ip.h>
Jan Engelhardt079aa882008-10-08 11:35:00 +020017#include <linux/ipv6.h>
18#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/moduleparam.h>
Patrick McHardy404bdbf2006-05-29 18:21:34 -070020#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/list.h>
25#include <linux/random.h>
26#include <linux/jhash.h>
27#include <linux/bitops.h>
28#include <linux/skbuff.h>
29#include <linux/inet.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080032#include <linux/netfilter/x_tables.h>
Jan Engelhardte948b202008-10-08 11:35:00 +020033#include <linux/netfilter/xt_recent.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Patrick McHardy404bdbf2006-05-29 18:21:34 -070035MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Jan Engelhardt079aa882008-10-08 11:35:00 +020036MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080037MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
Patrick McHardy404bdbf2006-05-29 18:21:34 -070038MODULE_LICENSE("GPL");
Jan Engelhardte948b202008-10-08 11:35:00 +020039MODULE_ALIAS("ipt_recent");
Jan Engelhardt079aa882008-10-08 11:35:00 +020040MODULE_ALIAS("ip6t_recent");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Patrick McHardye7be6992006-01-05 12:19:46 -080042static unsigned int ip_list_tot = 100;
43static unsigned int ip_pkt_list_tot = 20;
44static unsigned int ip_list_hash_size = 0;
45static unsigned int ip_list_perms = 0644;
Daniel De Graafb93ff782006-08-22 00:30:55 -070046static unsigned int ip_list_uid = 0;
47static unsigned int ip_list_gid = 0;
Patrick McHardye7be6992006-01-05 12:19:46 -080048module_param(ip_list_tot, uint, 0400);
49module_param(ip_pkt_list_tot, uint, 0400);
50module_param(ip_list_hash_size, uint, 0400);
51module_param(ip_list_perms, uint, 0400);
Daniel De Graafb93ff782006-08-22 00:30:55 -070052module_param(ip_list_uid, uint, 0400);
53module_param(ip_list_gid, uint, 0400);
Patrick McHardy404bdbf2006-05-29 18:21:34 -070054MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
55MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
56MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
Jan Engelhardt079aa882008-10-08 11:35:00 +020057MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
58MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
59MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Patrick McHardy404bdbf2006-05-29 18:21:34 -070061struct recent_entry {
62 struct list_head list;
63 struct list_head lru_list;
Jan Engelhardt079aa882008-10-08 11:35:00 +020064 union nf_inet_addr addr;
65 u_int16_t family;
Patrick McHardy404bdbf2006-05-29 18:21:34 -070066 u_int8_t ttl;
67 u_int8_t index;
68 u_int16_t nstamps;
69 unsigned long stamps[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Patrick McHardy404bdbf2006-05-29 18:21:34 -070072struct recent_table {
73 struct list_head list;
Jan Engelhardte948b202008-10-08 11:35:00 +020074 char name[XT_RECENT_NAME_LEN];
Patrick McHardy404bdbf2006-05-29 18:21:34 -070075 unsigned int refcnt;
76 unsigned int entries;
77 struct list_head lru_list;
78 struct list_head iphash[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Patrick McHardy404bdbf2006-05-29 18:21:34 -070081static LIST_HEAD(tables);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static DEFINE_SPINLOCK(recent_lock);
Patrick McHardya0e889b2006-06-09 12:17:41 -070083static DEFINE_MUTEX(recent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#ifdef CONFIG_PROC_FS
Jan Engelhardt079aa882008-10-08 11:35:00 +020086#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
87static struct proc_dir_entry *proc_old_dir;
88#endif
89static struct proc_dir_entry *recent_proc_dir;
90static const struct file_operations recent_old_fops, recent_mt_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
92
Patrick McHardy404bdbf2006-05-29 18:21:34 -070093static u_int32_t hash_rnd;
Jan Engelhardt079aa882008-10-08 11:35:00 +020094static bool hash_rnd_initted;
Patrick McHardy404bdbf2006-05-29 18:21:34 -070095
Jan Engelhardt079aa882008-10-08 11:35:00 +020096static unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
Patrick McHardy404bdbf2006-05-29 18:21:34 -070097{
98 if (!hash_rnd_initted) {
Jan Engelhardt079aa882008-10-08 11:35:00 +020099 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
100 hash_rnd_initted = true;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700101 }
Jan Engelhardt079aa882008-10-08 11:35:00 +0200102 return jhash_1word((__force u32)addr->ip, hash_rnd) &
103 (ip_list_hash_size - 1);
104}
105
106static unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
107{
108 if (!hash_rnd_initted) {
109 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
110 hash_rnd_initted = true;
111 }
112 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
113 (ip_list_hash_size - 1);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700114}
115
116static struct recent_entry *
Jan Engelhardt079aa882008-10-08 11:35:00 +0200117recent_entry_lookup(const struct recent_table *table,
118 const union nf_inet_addr *addrp, u_int16_t family,
119 u_int8_t ttl)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700120{
121 struct recent_entry *e;
122 unsigned int h;
123
Jan Engelhardtee999d82008-10-08 11:35:01 +0200124 if (family == NFPROTO_IPV4)
Jan Engelhardt079aa882008-10-08 11:35:00 +0200125 h = recent_entry_hash4(addrp);
126 else
127 h = recent_entry_hash6(addrp);
128
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700129 list_for_each_entry(e, &table->iphash[h], list)
Jan Engelhardt079aa882008-10-08 11:35:00 +0200130 if (e->family == family &&
131 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
132 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700133 return e;
134 return NULL;
135}
136
137static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
138{
139 list_del(&e->list);
140 list_del(&e->lru_list);
141 kfree(e);
142 t->entries--;
143}
144
145static struct recent_entry *
Jan Engelhardt079aa882008-10-08 11:35:00 +0200146recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
147 u_int16_t family, u_int8_t ttl)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700148{
149 struct recent_entry *e;
150
151 if (t->entries >= ip_list_tot) {
152 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
153 recent_entry_remove(t, e);
154 }
155 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
156 GFP_ATOMIC);
157 if (e == NULL)
158 return NULL;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200159 memcpy(&e->addr, addr, sizeof(e->addr));
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700160 e->ttl = ttl;
161 e->stamps[0] = jiffies;
162 e->nstamps = 1;
163 e->index = 1;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200164 e->family = family;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200165 if (family == NFPROTO_IPV4)
Jan Engelhardt079aa882008-10-08 11:35:00 +0200166 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
167 else
168 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700169 list_add_tail(&e->lru_list, &t->lru_list);
170 t->entries++;
171 return e;
172}
173
174static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
175{
176 e->stamps[e->index++] = jiffies;
177 if (e->index > e->nstamps)
178 e->nstamps = e->index;
179 e->index %= ip_pkt_list_tot;
180 list_move_tail(&e->lru_list, &t->lru_list);
181}
182
183static struct recent_table *recent_table_lookup(const char *name)
184{
185 struct recent_table *t;
186
187 list_for_each_entry(t, &tables, list)
188 if (!strcmp(t->name, name))
189 return t;
190 return NULL;
191}
192
193static void recent_table_flush(struct recent_table *t)
194{
195 struct recent_entry *e, *next;
196 unsigned int i;
197
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700198 for (i = 0; i < ip_list_hash_size; i++)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700199 list_for_each_entry_safe(e, next, &t->iphash[i], list)
200 recent_entry_remove(t, e);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700201}
202
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700203static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200204recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200206 const struct xt_recent_mtinfo *info = par->matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700207 struct recent_table *t;
208 struct recent_entry *e;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200209 union nf_inet_addr addr = {};
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700210 u_int8_t ttl;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700211 bool ret = info->invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200213 if (par->match->family == NFPROTO_IPV4) {
Jan Engelhardt079aa882008-10-08 11:35:00 +0200214 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jan Engelhardt079aa882008-10-08 11:35:00 +0200216 if (info->side == XT_RECENT_DEST)
217 addr.ip = iph->daddr;
218 else
219 addr.ip = iph->saddr;
220
221 ttl = iph->ttl;
222 } else {
223 const struct ipv6hdr *iph = ipv6_hdr(skb);
224
225 if (info->side == XT_RECENT_DEST)
226 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
227 else
228 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
229
230 ttl = iph->hop_limit;
231 }
232
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700233 /* use TTL as seen before forwarding */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200234 if (par->out != NULL && skb->sk == NULL)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700235 ttl++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700238 t = recent_table_lookup(info->name);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200239 e = recent_entry_lookup(t, &addr, par->match->family,
Jan Engelhardt079aa882008-10-08 11:35:00 +0200240 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700241 if (e == NULL) {
Jan Engelhardte948b202008-10-08 11:35:00 +0200242 if (!(info->check_set & XT_RECENT_SET))
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700243 goto out;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200244 e = recent_entry_init(t, &addr, par->match->family, ttl);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700245 if (e == NULL)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200246 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700247 ret = !ret;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700248 goto out;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jan Engelhardte948b202008-10-08 11:35:00 +0200251 if (info->check_set & XT_RECENT_SET)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700252 ret = !ret;
Jan Engelhardte948b202008-10-08 11:35:00 +0200253 else if (info->check_set & XT_RECENT_REMOVE) {
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700254 recent_entry_remove(t, e);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700255 ret = !ret;
Jan Engelhardte948b202008-10-08 11:35:00 +0200256 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
Patrick McHardy855304a2008-01-31 04:09:46 -0800257 unsigned long time = jiffies - info->seconds * HZ;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700258 unsigned int i, hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700260 for (i = 0; i < e->nstamps; i++) {
Patrick McHardy855304a2008-01-31 04:09:46 -0800261 if (info->seconds && time_after(time, e->stamps[i]))
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700262 continue;
263 if (++hits >= info->hit_count) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700264 ret = !ret;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700265 break;
266 }
267 }
268 }
269
Jan Engelhardte948b202008-10-08 11:35:00 +0200270 if (info->check_set & XT_RECENT_SET ||
271 (info->check_set & XT_RECENT_UPDATE && ret)) {
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700272 recent_entry_update(t, e);
273 e->ttl = ttl;
274 }
275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 spin_unlock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200280static bool recent_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200282 const struct xt_recent_mtinfo *info = par->matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700283 struct recent_table *t;
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100284#ifdef CONFIG_PROC_FS
285 struct proc_dir_entry *pde;
286#endif
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700287 unsigned i;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700288 bool ret = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700290 if (hweight8(info->check_set &
Jan Engelhardte948b202008-10-08 11:35:00 +0200291 (XT_RECENT_SET | XT_RECENT_REMOVE |
292 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700293 return false;
Jan Engelhardte948b202008-10-08 11:35:00 +0200294 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700295 (info->seconds || info->hit_count))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700296 return false;
Daniel Hokka Zakrissond0ebf132008-03-20 15:07:10 -0700297 if (info->hit_count > ip_pkt_list_tot)
298 return false;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700299 if (info->name[0] == '\0' ||
Jan Engelhardte948b202008-10-08 11:35:00 +0200300 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700301 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Patrick McHardya0e889b2006-06-09 12:17:41 -0700303 mutex_lock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700304 t = recent_table_lookup(info->name);
305 if (t != NULL) {
306 t->refcnt++;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700307 ret = true;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700311 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
Patrick McHardya0e889b2006-06-09 12:17:41 -0700312 GFP_KERNEL);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700313 if (t == NULL)
314 goto out;
Patrick McHardy2b2283d2006-06-09 12:18:17 -0700315 t->refcnt = 1;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700316 strcpy(t->name, info->name);
317 INIT_LIST_HEAD(&t->lru_list);
318 for (i = 0; i < ip_list_hash_size; i++)
319 INIT_LIST_HEAD(&t->iphash[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#ifdef CONFIG_PROC_FS
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100321 pde = proc_create_data(t->name, ip_list_perms, recent_proc_dir,
Alexey Dobriyanb09eec12008-10-20 03:33:49 -0700322 &recent_mt_fops, t);
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100323 if (pde == NULL) {
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700324 kfree(t);
325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100327 pde->uid = ip_list_uid;
328 pde->gid = ip_list_gid;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200329#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100330 pde = proc_create_data(t->name, ip_list_perms, proc_old_dir,
Alexey Dobriyanb09eec12008-10-20 03:33:49 -0700331 &recent_old_fops, t);
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100332 if (pde == NULL) {
Jan Engelhardt079aa882008-10-08 11:35:00 +0200333 remove_proc_entry(t->name, proc_old_dir);
334 kfree(t);
335 goto out;
336 }
Alexey Dobriyanb0ceb562008-11-20 09:57:01 +0100337 pde->uid = ip_list_uid;
338 pde->gid = ip_list_gid;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
Patrick McHardya0e889b2006-06-09 12:17:41 -0700341 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700342 list_add_tail(&t->list, &tables);
Patrick McHardya0e889b2006-06-09 12:17:41 -0700343 spin_unlock_bh(&recent_lock);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700344 ret = true;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700345out:
Patrick McHardya0e889b2006-06-09 12:17:41 -0700346 mutex_unlock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700347 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200350static void recent_mt_destroy(const struct xt_mtdtor_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200352 const struct xt_recent_mtinfo *info = par->matchinfo;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700353 struct recent_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Patrick McHardya0e889b2006-06-09 12:17:41 -0700355 mutex_lock(&recent_mutex);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700356 t = recent_table_lookup(info->name);
357 if (--t->refcnt == 0) {
Patrick McHardya0e889b2006-06-09 12:17:41 -0700358 spin_lock_bh(&recent_lock);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700359 list_del(&t->list);
Patrick McHardya0e889b2006-06-09 12:17:41 -0700360 spin_unlock_bh(&recent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifdef CONFIG_PROC_FS
Jan Engelhardt079aa882008-10-08 11:35:00 +0200362#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
363 remove_proc_entry(t->name, proc_old_dir);
364#endif
365 remove_proc_entry(t->name, recent_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#endif
Pavel Emelyanova8ddc912008-07-31 00:38:31 -0700367 recent_table_flush(t);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700368 kfree(t);
369 }
Patrick McHardya0e889b2006-06-09 12:17:41 -0700370 mutex_unlock(&recent_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700373#ifdef CONFIG_PROC_FS
374struct recent_iter_state {
Jan Engelhardt079aa882008-10-08 11:35:00 +0200375 const struct recent_table *table;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700376 unsigned int bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700379static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy855304a2008-01-31 04:09:46 -0800380 __acquires(recent_lock)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700381{
382 struct recent_iter_state *st = seq->private;
Jan Engelhardta47362a2007-07-07 22:16:55 -0700383 const struct recent_table *t = st->table;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700384 struct recent_entry *e;
385 loff_t p = *pos;
386
387 spin_lock_bh(&recent_lock);
388
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700389 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
390 list_for_each_entry(e, &t->iphash[st->bucket], list)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700391 if (p-- == 0)
392 return e;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700393 return NULL;
394}
395
396static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
397{
398 struct recent_iter_state *st = seq->private;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200399 const struct recent_table *t = st->table;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200400 const struct recent_entry *e = v;
401 const struct list_head *head = e->list.next;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700402
403 while (head == &t->iphash[st->bucket]) {
404 if (++st->bucket >= ip_list_hash_size)
405 return NULL;
406 head = t->iphash[st->bucket].next;
407 }
408 (*pos)++;
409 return list_entry(head, struct recent_entry, list);
410}
411
412static void recent_seq_stop(struct seq_file *s, void *v)
Patrick McHardy855304a2008-01-31 04:09:46 -0800413 __releases(recent_lock)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700414{
415 spin_unlock_bh(&recent_lock);
416}
417
418static int recent_seq_show(struct seq_file *seq, void *v)
419{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200420 const struct recent_entry *e = v;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700421 unsigned int i;
422
423 i = (e->index - 1) % ip_pkt_list_tot;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200424 if (e->family == NFPROTO_IPV4)
Harvey Harrison14d5e832008-10-31 00:54:29 -0700425 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
426 &e->addr.ip, e->ttl, e->stamps[i], e->index);
Jan Engelhardt079aa882008-10-08 11:35:00 +0200427 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700428 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700429 &e->addr.in6, e->ttl, e->stamps[i], e->index);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700430 for (i = 0; i < e->nstamps; i++)
431 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
432 seq_printf(seq, "\n");
433 return 0;
434}
435
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700436static const struct seq_operations recent_seq_ops = {
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700437 .start = recent_seq_start,
438 .next = recent_seq_next,
439 .stop = recent_seq_stop,
440 .show = recent_seq_show,
441};
442
443static int recent_seq_open(struct inode *inode, struct file *file)
444{
445 struct proc_dir_entry *pde = PDE(inode);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700446 struct recent_iter_state *st;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700447
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700448 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700449 if (st == NULL)
450 return -ENOMEM;
Jesper Juhl3af8e312007-08-07 18:10:54 -0700451
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700452 st->table = pde->data;
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700453 return 0;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700454}
455
Jan Engelhardt079aa882008-10-08 11:35:00 +0200456#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
457static int recent_old_seq_open(struct inode *inode, struct file *filp)
458{
459 static bool warned_of_old;
460
461 if (unlikely(!warned_of_old)) {
462 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
463 " is deprecated; use /proc/net/xt_recent.\n");
464 warned_of_old = true;
465 }
466 return recent_seq_open(inode, filp);
467}
468
469static ssize_t recent_old_proc_write(struct file *file,
470 const char __user *input,
471 size_t size, loff_t *loff)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700472{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200473 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700474 struct recent_table *t = pde->data;
475 struct recent_entry *e;
476 char buf[sizeof("+255.255.255.255")], *c = buf;
Al Viro6a19d612006-09-28 14:22:24 -0700477 __be32 addr;
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700478 int add;
479
480 if (size > sizeof(buf))
481 size = sizeof(buf);
482 if (copy_from_user(buf, input, size))
483 return -EFAULT;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200484
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700485 while (isspace(*c))
486 c++;
487
488 if (size - (c - buf) < 5)
489 return c - buf;
490 if (!strncmp(c, "clear", 5)) {
491 c += 5;
492 spin_lock_bh(&recent_lock);
493 recent_table_flush(t);
494 spin_unlock_bh(&recent_lock);
495 return c - buf;
496 }
497
498 switch (*c) {
499 case '-':
500 add = 0;
501 c++;
502 break;
503 case '+':
504 c++;
505 default:
506 add = 1;
507 break;
508 }
509 addr = in_aton(c);
510
511 spin_lock_bh(&recent_lock);
Jan Engelhardtee999d82008-10-08 11:35:01 +0200512 e = recent_entry_lookup(t, (const void *)&addr, NFPROTO_IPV4, 0);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700513 if (e == NULL) {
514 if (add)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200515 recent_entry_init(t, (const void *)&addr,
516 NFPROTO_IPV4, 0);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700517 } else {
518 if (add)
519 recent_entry_update(t, e);
520 else
521 recent_entry_remove(t, e);
522 }
523 spin_unlock_bh(&recent_lock);
524 return size;
525}
526
Jan Engelhardt079aa882008-10-08 11:35:00 +0200527static const struct file_operations recent_old_fops = {
528 .open = recent_old_seq_open,
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700529 .read = seq_read,
Jan Engelhardt079aa882008-10-08 11:35:00 +0200530 .write = recent_old_proc_write,
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700531 .release = seq_release_private,
532 .owner = THIS_MODULE,
533};
Jan Engelhardt079aa882008-10-08 11:35:00 +0200534#endif
535
536static ssize_t
537recent_mt_proc_write(struct file *file, const char __user *input,
538 size_t size, loff_t *loff)
539{
540 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
541 struct recent_table *t = pde->data;
542 struct recent_entry *e;
543 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
544 const char *c = buf;
Josef Drexler325fb5b2009-02-24 14:53:12 +0100545 union nf_inet_addr addr = {};
Jan Engelhardt079aa882008-10-08 11:35:00 +0200546 u_int16_t family;
547 bool add, succ;
548
549 if (size == 0)
550 return 0;
551 if (size > sizeof(buf))
552 size = sizeof(buf);
553 if (copy_from_user(buf, input, size) != 0)
554 return -EFAULT;
555
556 /* Strict protocol! */
557 if (*loff != 0)
558 return -ESPIPE;
559 switch (*c) {
560 case '/': /* flush table */
561 spin_lock_bh(&recent_lock);
562 recent_table_flush(t);
563 spin_unlock_bh(&recent_lock);
564 return size;
565 case '-': /* remove address */
566 add = false;
567 break;
568 case '+': /* add address */
569 add = true;
570 break;
571 default:
572 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
573 return -EINVAL;
574 }
575
576 ++c;
577 --size;
578 if (strnchr(c, size, ':') != NULL) {
Jan Engelhardtee999d82008-10-08 11:35:01 +0200579 family = NFPROTO_IPV6;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200580 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
581 } else {
Jan Engelhardtee999d82008-10-08 11:35:01 +0200582 family = NFPROTO_IPV4;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200583 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
584 }
585
586 if (!succ) {
587 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
588 "to procfs\n");
589 return -EINVAL;
590 }
591
592 spin_lock_bh(&recent_lock);
593 e = recent_entry_lookup(t, &addr, family, 0);
594 if (e == NULL) {
595 if (add)
596 recent_entry_init(t, &addr, family, 0);
597 } else {
598 if (add)
599 recent_entry_update(t, e);
600 else
601 recent_entry_remove(t, e);
602 }
603 spin_unlock_bh(&recent_lock);
604 /* Note we removed one above */
605 *loff += size + 1;
606 return size + 1;
607}
608
609static const struct file_operations recent_mt_fops = {
610 .open = recent_seq_open,
611 .read = seq_read,
612 .write = recent_mt_proc_write,
613 .release = seq_release_private,
614 .owner = THIS_MODULE,
615};
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700616#endif /* CONFIG_PROC_FS */
617
Jan Engelhardt079aa882008-10-08 11:35:00 +0200618static struct xt_match recent_mt_reg[] __read_mostly = {
619 {
620 .name = "recent",
621 .revision = 0,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200622 .family = NFPROTO_IPV4,
Jan Engelhardt079aa882008-10-08 11:35:00 +0200623 .match = recent_mt,
624 .matchsize = sizeof(struct xt_recent_mtinfo),
625 .checkentry = recent_mt_check,
626 .destroy = recent_mt_destroy,
627 .me = THIS_MODULE,
628 },
629 {
630 .name = "recent",
631 .revision = 0,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200632 .family = NFPROTO_IPV6,
Jan Engelhardt079aa882008-10-08 11:35:00 +0200633 .match = recent_mt,
634 .matchsize = sizeof(struct xt_recent_mtinfo),
635 .checkentry = recent_mt_check,
636 .destroy = recent_mt_destroy,
637 .me = THIS_MODULE,
638 },
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700639};
640
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800641static int __init recent_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700643 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700645 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
646 return -EINVAL;
647 ip_list_hash_size = 1 << fls(ip_list_tot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jan Engelhardt079aa882008-10-08 11:35:00 +0200649 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700650#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (err)
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700652 return err;
Jan Engelhardt079aa882008-10-08 11:35:00 +0200653 recent_proc_dir = proc_mkdir("xt_recent", init_net.proc_net);
654 if (recent_proc_dir == NULL) {
655 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700656 err = -ENOMEM;
657 }
Jan Engelhardt079aa882008-10-08 11:35:00 +0200658#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
659 if (err < 0)
660 return err;
661 proc_old_dir = proc_mkdir("ipt_recent", init_net.proc_net);
662 if (proc_old_dir == NULL) {
663 remove_proc_entry("xt_recent", init_net.proc_net);
664 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
665 err = -ENOMEM;
666 }
667#endif
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700668#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return err;
670}
671
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800672static void __exit recent_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700674 BUG_ON(!list_empty(&tables));
Jan Engelhardt079aa882008-10-08 11:35:00 +0200675 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700676#ifdef CONFIG_PROC_FS
Jan Engelhardt079aa882008-10-08 11:35:00 +0200677#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200678 remove_proc_entry("ipt_recent", init_net.proc_net);
Patrick McHardy404bdbf2006-05-29 18:21:34 -0700679#endif
Jan Engelhardt079aa882008-10-08 11:35:00 +0200680 remove_proc_entry("xt_recent", init_net.proc_net);
681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800684module_init(recent_mt_init);
685module_exit(recent_mt_exit);