]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_recent.c
netfilter: rename ipt_recent to xt_recent
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_recent.c
CommitLineData
404bdbfd
PM
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>
6709dbbb 15#include <linux/ip.h>
404bdbfd 16#include <linux/moduleparam.h>
1da177e4 17#include <linux/proc_fs.h>
404bdbfd
PM
18#include <linux/seq_file.h>
19#include <linux/string.h>
1da177e4 20#include <linux/ctype.h>
404bdbfd
PM
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>
457c4cbc 27#include <net/net_namespace.h>
1da177e4 28
6709dbbb 29#include <linux/netfilter/x_tables.h>
e948b20a 30#include <linux/netfilter/xt_recent.h>
1da177e4 31
404bdbfd 32MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
2ae15b64 33MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
404bdbfd 34MODULE_LICENSE("GPL");
e948b20a 35MODULE_ALIAS("ipt_recent");
1da177e4 36
e7be6994
PM
37static unsigned int ip_list_tot = 100;
38static unsigned int ip_pkt_list_tot = 20;
39static unsigned int ip_list_hash_size = 0;
40static unsigned int ip_list_perms = 0644;
b93ff783
DDG
41static unsigned int ip_list_uid = 0;
42static unsigned int ip_list_gid = 0;
e7be6994
PM
43module_param(ip_list_tot, uint, 0400);
44module_param(ip_pkt_list_tot, uint, 0400);
45module_param(ip_list_hash_size, uint, 0400);
46module_param(ip_list_perms, uint, 0400);
b93ff783
DDG
47module_param(ip_list_uid, uint, 0400);
48module_param(ip_list_gid, uint, 0400);
404bdbfd
PM
49MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
50MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
51MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
52MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
b93ff783
DDG
53MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/ipt_recent/* files");
54MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/ipt_recent/* files");
404bdbfd 55
404bdbfd
PM
56struct recent_entry {
57 struct list_head list;
58 struct list_head lru_list;
6a19d614 59 __be32 addr;
404bdbfd
PM
60 u_int8_t ttl;
61 u_int8_t index;
62 u_int16_t nstamps;
63 unsigned long stamps[0];
1da177e4
LT
64};
65
404bdbfd
PM
66struct recent_table {
67 struct list_head list;
e948b20a 68 char name[XT_RECENT_NAME_LEN];
1da177e4 69#ifdef CONFIG_PROC_FS
404bdbfd
PM
70 struct proc_dir_entry *proc;
71#endif
72 unsigned int refcnt;
73 unsigned int entries;
74 struct list_head lru_list;
75 struct list_head iphash[0];
1da177e4
LT
76};
77
404bdbfd 78static LIST_HEAD(tables);
1da177e4 79static DEFINE_SPINLOCK(recent_lock);
a0e889bb 80static DEFINE_MUTEX(recent_mutex);
1da177e4
LT
81
82#ifdef CONFIG_PROC_FS
404bdbfd 83static struct proc_dir_entry *proc_dir;
9a32144e 84static const struct file_operations recent_fops;
1da177e4
LT
85#endif
86
404bdbfd
PM
87static u_int32_t hash_rnd;
88static int hash_rnd_initted;
1da177e4 89
6a19d614 90static unsigned int recent_entry_hash(__be32 addr)
1da177e4 91{
404bdbfd
PM
92 if (!hash_rnd_initted) {
93 get_random_bytes(&hash_rnd, 4);
94 hash_rnd_initted = 1;
1da177e4 95 }
6a19d614 96 return jhash_1word((__force u32)addr, hash_rnd) & (ip_list_hash_size - 1);
1da177e4
LT
97}
98
404bdbfd 99static struct recent_entry *
6a19d614 100recent_entry_lookup(const struct recent_table *table, __be32 addr, u_int8_t ttl)
1da177e4 101{
404bdbfd
PM
102 struct recent_entry *e;
103 unsigned int h;
104
105 h = recent_entry_hash(addr);
106 list_for_each_entry(e, &table->iphash[h], list)
107 if (e->addr == addr && (ttl == e->ttl || !ttl || !e->ttl))
108 return e;
109 return NULL;
110}
1da177e4 111
404bdbfd
PM
112static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
113{
114 list_del(&e->list);
115 list_del(&e->lru_list);
116 kfree(e);
117 t->entries--;
118}
1da177e4 119
404bdbfd 120static struct recent_entry *
6a19d614 121recent_entry_init(struct recent_table *t, __be32 addr, u_int8_t ttl)
404bdbfd
PM
122{
123 struct recent_entry *e;
1da177e4 124
404bdbfd
PM
125 if (t->entries >= ip_list_tot) {
126 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
127 recent_entry_remove(t, e);
1da177e4 128 }
404bdbfd
PM
129 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
130 GFP_ATOMIC);
131 if (e == NULL)
132 return NULL;
133 e->addr = addr;
134 e->ttl = ttl;
135 e->stamps[0] = jiffies;
136 e->nstamps = 1;
137 e->index = 1;
138 list_add_tail(&e->list, &t->iphash[recent_entry_hash(addr)]);
139 list_add_tail(&e->lru_list, &t->lru_list);
140 t->entries++;
141 return e;
142}
1da177e4 143
404bdbfd
PM
144static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
145{
146 e->stamps[e->index++] = jiffies;
147 if (e->index > e->nstamps)
148 e->nstamps = e->index;
149 e->index %= ip_pkt_list_tot;
150 list_move_tail(&e->lru_list, &t->lru_list);
151}
1da177e4 152
404bdbfd
PM
153static struct recent_table *recent_table_lookup(const char *name)
154{
155 struct recent_table *t;
1da177e4 156
404bdbfd
PM
157 list_for_each_entry(t, &tables, list)
158 if (!strcmp(t->name, name))
159 return t;
160 return NULL;
161}
1da177e4 162
404bdbfd
PM
163static void recent_table_flush(struct recent_table *t)
164{
165 struct recent_entry *e, *next;
166 unsigned int i;
1da177e4 167
7c4e36bc 168 for (i = 0; i < ip_list_hash_size; i++)
404bdbfd
PM
169 list_for_each_entry_safe(e, next, &t->iphash[i], list)
170 recent_entry_remove(t, e);
1da177e4
LT
171}
172
1d93a9cb 173static bool
d3c5ee6d
JE
174recent_mt(const struct sk_buff *skb, const struct net_device *in,
175 const struct net_device *out, const struct xt_match *match,
176 const void *matchinfo, int offset, unsigned int protoff,
177 bool *hotdrop)
1da177e4 178{
e948b20a 179 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd
PM
180 struct recent_table *t;
181 struct recent_entry *e;
6a19d614 182 __be32 addr;
404bdbfd 183 u_int8_t ttl;
1d93a9cb 184 bool ret = info->invert;
1da177e4 185
e948b20a 186 if (info->side == XT_RECENT_DEST)
eddc9ec5 187 addr = ip_hdr(skb)->daddr;
404bdbfd 188 else
eddc9ec5 189 addr = ip_hdr(skb)->saddr;
1da177e4 190
eddc9ec5 191 ttl = ip_hdr(skb)->ttl;
404bdbfd
PM
192 /* use TTL as seen before forwarding */
193 if (out && !skb->sk)
194 ttl++;
1da177e4 195
1da177e4 196 spin_lock_bh(&recent_lock);
404bdbfd
PM
197 t = recent_table_lookup(info->name);
198 e = recent_entry_lookup(t, addr,
e948b20a 199 info->check_set & XT_RECENT_TTL ? ttl : 0);
404bdbfd 200 if (e == NULL) {
e948b20a 201 if (!(info->check_set & XT_RECENT_SET))
404bdbfd
PM
202 goto out;
203 e = recent_entry_init(t, addr, ttl);
204 if (e == NULL)
cff533ac 205 *hotdrop = true;
1d93a9cb 206 ret = !ret;
404bdbfd 207 goto out;
1da177e4
LT
208 }
209
e948b20a 210 if (info->check_set & XT_RECENT_SET)
1d93a9cb 211 ret = !ret;
e948b20a 212 else if (info->check_set & XT_RECENT_REMOVE) {
404bdbfd 213 recent_entry_remove(t, e);
1d93a9cb 214 ret = !ret;
e948b20a 215 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
855304af 216 unsigned long time = jiffies - info->seconds * HZ;
404bdbfd
PM
217 unsigned int i, hits = 0;
218
219 for (i = 0; i < e->nstamps; i++) {
855304af 220 if (info->seconds && time_after(time, e->stamps[i]))
404bdbfd
PM
221 continue;
222 if (++hits >= info->hit_count) {
1d93a9cb 223 ret = !ret;
404bdbfd 224 break;
1da177e4 225 }
1da177e4 226 }
1da177e4
LT
227 }
228
e948b20a
JE
229 if (info->check_set & XT_RECENT_SET ||
230 (info->check_set & XT_RECENT_UPDATE && ret)) {
404bdbfd
PM
231 recent_entry_update(t, e);
232 e->ttl = ttl;
233 }
234out:
235 spin_unlock_bh(&recent_lock);
236 return ret;
1da177e4
LT
237}
238
ccb79bdc 239static bool
d3c5ee6d
JE
240recent_mt_check(const char *tablename, const void *ip,
241 const struct xt_match *match, void *matchinfo,
242 unsigned int hook_mask)
1da177e4 243{
e948b20a 244 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd
PM
245 struct recent_table *t;
246 unsigned i;
ccb79bdc 247 bool ret = false;
1da177e4 248
404bdbfd 249 if (hweight8(info->check_set &
e948b20a
JE
250 (XT_RECENT_SET | XT_RECENT_REMOVE |
251 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
ccb79bdc 252 return false;
e948b20a 253 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
404bdbfd 254 (info->seconds || info->hit_count))
ccb79bdc 255 return false;
d0ebf133
DHZ
256 if (info->hit_count > ip_pkt_list_tot)
257 return false;
404bdbfd 258 if (info->name[0] == '\0' ||
e948b20a 259 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
ccb79bdc 260 return false;
1da177e4 261
a0e889bb 262 mutex_lock(&recent_mutex);
404bdbfd
PM
263 t = recent_table_lookup(info->name);
264 if (t != NULL) {
265 t->refcnt++;
ccb79bdc 266 ret = true;
404bdbfd 267 goto out;
1da177e4
LT
268 }
269
404bdbfd 270 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
a0e889bb 271 GFP_KERNEL);
404bdbfd
PM
272 if (t == NULL)
273 goto out;
2b2283d0 274 t->refcnt = 1;
404bdbfd
PM
275 strcpy(t->name, info->name);
276 INIT_LIST_HEAD(&t->lru_list);
277 for (i = 0; i < ip_list_hash_size; i++)
278 INIT_LIST_HEAD(&t->iphash[i]);
279#ifdef CONFIG_PROC_FS
8eeee8b1 280 t->proc = proc_create(t->name, ip_list_perms, proc_dir, &recent_fops);
404bdbfd
PM
281 if (t->proc == NULL) {
282 kfree(t);
283 goto out;
1da177e4 284 }
b93ff783
DDG
285 t->proc->uid = ip_list_uid;
286 t->proc->gid = ip_list_gid;
404bdbfd 287 t->proc->data = t;
1da177e4 288#endif
a0e889bb 289 spin_lock_bh(&recent_lock);
404bdbfd 290 list_add_tail(&t->list, &tables);
a0e889bb 291 spin_unlock_bh(&recent_lock);
ccb79bdc 292 ret = true;
404bdbfd 293out:
a0e889bb 294 mutex_unlock(&recent_mutex);
404bdbfd
PM
295 return ret;
296}
1da177e4 297
d3c5ee6d 298static void recent_mt_destroy(const struct xt_match *match, void *matchinfo)
404bdbfd 299{
e948b20a 300 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd 301 struct recent_table *t;
1da177e4 302
a0e889bb 303 mutex_lock(&recent_mutex);
404bdbfd
PM
304 t = recent_table_lookup(info->name);
305 if (--t->refcnt == 0) {
a0e889bb 306 spin_lock_bh(&recent_lock);
404bdbfd 307 list_del(&t->list);
a0e889bb 308 spin_unlock_bh(&recent_lock);
404bdbfd
PM
309#ifdef CONFIG_PROC_FS
310 remove_proc_entry(t->name, proc_dir);
1da177e4 311#endif
a8ddc916 312 recent_table_flush(t);
404bdbfd 313 kfree(t);
1da177e4 314 }
a0e889bb 315 mutex_unlock(&recent_mutex);
404bdbfd 316}
1da177e4 317
404bdbfd
PM
318#ifdef CONFIG_PROC_FS
319struct recent_iter_state {
320 struct recent_table *table;
321 unsigned int bucket;
322};
1da177e4 323
404bdbfd 324static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
855304af 325 __acquires(recent_lock)
404bdbfd
PM
326{
327 struct recent_iter_state *st = seq->private;
a47362a2 328 const struct recent_table *t = st->table;
404bdbfd
PM
329 struct recent_entry *e;
330 loff_t p = *pos;
1da177e4 331
1da177e4 332 spin_lock_bh(&recent_lock);
1da177e4 333
7c4e36bc
JE
334 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
335 list_for_each_entry(e, &t->iphash[st->bucket], list)
404bdbfd
PM
336 if (p-- == 0)
337 return e;
404bdbfd
PM
338 return NULL;
339}
1da177e4 340
404bdbfd
PM
341static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
342{
343 struct recent_iter_state *st = seq->private;
3cf93c96 344 const struct recent_table *t = st->table;
404bdbfd
PM
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);
1da177e4
LT
355}
356
404bdbfd 357static void recent_seq_stop(struct seq_file *s, void *v)
855304af 358 __releases(recent_lock)
1da177e4 359{
404bdbfd
PM
360 spin_unlock_bh(&recent_lock);
361}
1da177e4 362
404bdbfd
PM
363static int recent_seq_show(struct seq_file *seq, void *v)
364{
3cf93c96 365 const struct recent_entry *e = v;
404bdbfd
PM
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}
1da177e4 376
56b3d975 377static const struct seq_operations recent_seq_ops = {
404bdbfd
PM
378 .start = recent_seq_start,
379 .next = recent_seq_next,
380 .stop = recent_seq_stop,
381 .show = recent_seq_show,
382};
1da177e4 383
404bdbfd
PM
384static int recent_seq_open(struct inode *inode, struct file *file)
385{
386 struct proc_dir_entry *pde = PDE(inode);
404bdbfd 387 struct recent_iter_state *st;
404bdbfd 388
e2da5913 389 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
404bdbfd
PM
390 if (st == NULL)
391 return -ENOMEM;
3af8e31c 392
404bdbfd 393 st->table = pde->data;
e2da5913 394 return 0;
404bdbfd 395}
1da177e4 396
404bdbfd
PM
397static ssize_t recent_proc_write(struct file *file, const char __user *input,
398 size_t size, loff_t *loff)
399{
3cf93c96 400 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
404bdbfd
PM
401 struct recent_table *t = pde->data;
402 struct recent_entry *e;
403 char buf[sizeof("+255.255.255.255")], *c = buf;
6a19d614 404 __be32 addr;
404bdbfd
PM
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);
1da177e4 420 spin_unlock_bh(&recent_lock);
404bdbfd 421 return c - buf;
1da177e4 422 }
1da177e4 423
404bdbfd
PM
424 switch (*c) {
425 case '-':
426 add = 0;
427 c++;
428 break;
429 case '+':
430 c++;
431 default:
432 add = 1;
433 break;
1da177e4 434 }
404bdbfd 435 addr = in_aton(c);
1da177e4 436
404bdbfd
PM
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);
1da177e4 447 }
1da177e4 448 spin_unlock_bh(&recent_lock);
404bdbfd
PM
449 return size;
450}
1da177e4 451
9a32144e 452static const struct file_operations recent_fops = {
404bdbfd
PM
453 .open = recent_seq_open,
454 .read = seq_read,
455 .write = recent_proc_write,
456 .release = seq_release_private,
457 .owner = THIS_MODULE,
458};
1da177e4 459#endif /* CONFIG_PROC_FS */
1da177e4 460
d3c5ee6d 461static struct xt_match recent_mt_reg __read_mostly = {
1d5cd909 462 .name = "recent",
6709dbbb 463 .family = AF_INET,
d3c5ee6d 464 .match = recent_mt,
e948b20a 465 .matchsize = sizeof(struct xt_recent_mtinfo),
d3c5ee6d
JE
466 .checkentry = recent_mt_check,
467 .destroy = recent_mt_destroy,
404bdbfd 468 .me = THIS_MODULE,
1da177e4
LT
469};
470
d3c5ee6d 471static int __init recent_mt_init(void)
1da177e4 472{
404bdbfd 473 int err;
1da177e4 474
404bdbfd
PM
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);
1da177e4 478
d3c5ee6d 479 err = xt_register_match(&recent_mt_reg);
404bdbfd 480#ifdef CONFIG_PROC_FS
1da177e4 481 if (err)
404bdbfd 482 return err;
457c4cbc 483 proc_dir = proc_mkdir("ipt_recent", init_net.proc_net);
404bdbfd 484 if (proc_dir == NULL) {
d3c5ee6d 485 xt_unregister_match(&recent_mt_reg);
404bdbfd
PM
486 err = -ENOMEM;
487 }
488#endif
1da177e4
LT
489 return err;
490}
491
d3c5ee6d 492static void __exit recent_mt_exit(void)
1da177e4 493{
404bdbfd 494 BUG_ON(!list_empty(&tables));
d3c5ee6d 495 xt_unregister_match(&recent_mt_reg);
404bdbfd 496#ifdef CONFIG_PROC_FS
457c4cbc 497 remove_proc_entry("ipt_recent", init_net.proc_net);
404bdbfd 498#endif
1da177e4
LT
499}
500
d3c5ee6d
JE
501module_init(recent_mt_init);
502module_exit(recent_mt_exit);