]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/netfilter/ipt_recent.c
netfilter: Use unsigned types for hooknum and pf vars
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / netfilter / ipt_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>
1da177e4
LT
30#include <linux/netfilter_ipv4/ipt_recent.h>
31
404bdbfd 32MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
2ae15b64 33MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
404bdbfd 34MODULE_LICENSE("GPL");
1da177e4 35
e7be6994
PM
36static unsigned int ip_list_tot = 100;
37static unsigned int ip_pkt_list_tot = 20;
38static unsigned int ip_list_hash_size = 0;
39static unsigned int ip_list_perms = 0644;
b93ff783
DDG
40static unsigned int ip_list_uid = 0;
41static unsigned int ip_list_gid = 0;
e7be6994
PM
42module_param(ip_list_tot, uint, 0400);
43module_param(ip_pkt_list_tot, uint, 0400);
44module_param(ip_list_hash_size, uint, 0400);
45module_param(ip_list_perms, uint, 0400);
b93ff783
DDG
46module_param(ip_list_uid, uint, 0400);
47module_param(ip_list_gid, uint, 0400);
404bdbfd
PM
48MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
49MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)");
50MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
51MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/ipt_recent/* files");
b93ff783
DDG
52MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/ipt_recent/* files");
53MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/ipt_recent/* files");
404bdbfd 54
404bdbfd
PM
55struct recent_entry {
56 struct list_head list;
57 struct list_head lru_list;
6a19d614 58 __be32 addr;
404bdbfd
PM
59 u_int8_t ttl;
60 u_int8_t index;
61 u_int16_t nstamps;
62 unsigned long stamps[0];
1da177e4
LT
63};
64
404bdbfd
PM
65struct recent_table {
66 struct list_head list;
67 char name[IPT_RECENT_NAME_LEN];
1da177e4 68#ifdef CONFIG_PROC_FS
404bdbfd
PM
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];
1da177e4
LT
75};
76
404bdbfd 77static LIST_HEAD(tables);
1da177e4 78static DEFINE_SPINLOCK(recent_lock);
a0e889bb 79static DEFINE_MUTEX(recent_mutex);
1da177e4
LT
80
81#ifdef CONFIG_PROC_FS
404bdbfd 82static struct proc_dir_entry *proc_dir;
9a32144e 83static const struct file_operations recent_fops;
1da177e4
LT
84#endif
85
404bdbfd
PM
86static u_int32_t hash_rnd;
87static int hash_rnd_initted;
1da177e4 88
6a19d614 89static unsigned int recent_entry_hash(__be32 addr)
1da177e4 90{
404bdbfd
PM
91 if (!hash_rnd_initted) {
92 get_random_bytes(&hash_rnd, 4);
93 hash_rnd_initted = 1;
1da177e4 94 }
6a19d614 95 return jhash_1word((__force u32)addr, hash_rnd) & (ip_list_hash_size - 1);
1da177e4
LT
96}
97
404bdbfd 98static struct recent_entry *
6a19d614 99recent_entry_lookup(const struct recent_table *table, __be32 addr, u_int8_t ttl)
1da177e4 100{
404bdbfd
PM
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}
1da177e4 110
404bdbfd
PM
111static 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}
1da177e4 118
404bdbfd 119static struct recent_entry *
6a19d614 120recent_entry_init(struct recent_table *t, __be32 addr, u_int8_t ttl)
404bdbfd
PM
121{
122 struct recent_entry *e;
1da177e4 123
404bdbfd
PM
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);
1da177e4 127 }
404bdbfd
PM
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}
1da177e4 142
404bdbfd
PM
143static 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}
1da177e4 151
404bdbfd
PM
152static struct recent_table *recent_table_lookup(const char *name)
153{
154 struct recent_table *t;
1da177e4 155
404bdbfd
PM
156 list_for_each_entry(t, &tables, list)
157 if (!strcmp(t->name, name))
158 return t;
159 return NULL;
160}
1da177e4 161
404bdbfd
PM
162static void recent_table_flush(struct recent_table *t)
163{
164 struct recent_entry *e, *next;
165 unsigned int i;
1da177e4 166
7c4e36bc 167 for (i = 0; i < ip_list_hash_size; i++)
404bdbfd
PM
168 list_for_each_entry_safe(e, next, &t->iphash[i], list)
169 recent_entry_remove(t, e);
1da177e4
LT
170}
171
1d93a9cb 172static bool
d3c5ee6d
JE
173recent_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)
1da177e4 177{
1da177e4 178 const struct ipt_recent_info *info = matchinfo;
404bdbfd
PM
179 struct recent_table *t;
180 struct recent_entry *e;
6a19d614 181 __be32 addr;
404bdbfd 182 u_int8_t ttl;
1d93a9cb 183 bool ret = info->invert;
1da177e4 184
404bdbfd 185 if (info->side == IPT_RECENT_DEST)
eddc9ec5 186 addr = ip_hdr(skb)->daddr;
404bdbfd 187 else
eddc9ec5 188 addr = ip_hdr(skb)->saddr;
1da177e4 189
eddc9ec5 190 ttl = ip_hdr(skb)->ttl;
404bdbfd
PM
191 /* use TTL as seen before forwarding */
192 if (out && !skb->sk)
193 ttl++;
1da177e4 194
1da177e4 195 spin_lock_bh(&recent_lock);
404bdbfd
PM
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)
cff533ac 204 *hotdrop = true;
1d93a9cb 205 ret = !ret;
404bdbfd 206 goto out;
1da177e4
LT
207 }
208
404bdbfd 209 if (info->check_set & IPT_RECENT_SET)
1d93a9cb 210 ret = !ret;
404bdbfd
PM
211 else if (info->check_set & IPT_RECENT_REMOVE) {
212 recent_entry_remove(t, e);
1d93a9cb 213 ret = !ret;
404bdbfd 214 } else if (info->check_set & (IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) {
855304af 215 unsigned long time = jiffies - info->seconds * HZ;
404bdbfd
PM
216 unsigned int i, hits = 0;
217
218 for (i = 0; i < e->nstamps; i++) {
855304af 219 if (info->seconds && time_after(time, e->stamps[i]))
404bdbfd
PM
220 continue;
221 if (++hits >= info->hit_count) {
1d93a9cb 222 ret = !ret;
404bdbfd 223 break;
1da177e4 224 }
1da177e4 225 }
1da177e4
LT
226 }
227
404bdbfd
PM
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 }
233out:
234 spin_unlock_bh(&recent_lock);
235 return ret;
1da177e4
LT
236}
237
ccb79bdc 238static bool
d3c5ee6d
JE
239recent_mt_check(const char *tablename, const void *ip,
240 const struct xt_match *match, void *matchinfo,
241 unsigned int hook_mask)
1da177e4 242{
1da177e4 243 const struct ipt_recent_info *info = matchinfo;
404bdbfd
PM
244 struct recent_table *t;
245 unsigned i;
ccb79bdc 246 bool ret = false;
1da177e4 247
404bdbfd
PM
248 if (hweight8(info->check_set &
249 (IPT_RECENT_SET | IPT_RECENT_REMOVE |
250 IPT_RECENT_CHECK | IPT_RECENT_UPDATE)) != 1)
ccb79bdc 251 return false;
404bdbfd
PM
252 if ((info->check_set & (IPT_RECENT_SET | IPT_RECENT_REMOVE)) &&
253 (info->seconds || info->hit_count))
ccb79bdc 254 return false;
d0ebf133
DHZ
255 if (info->hit_count > ip_pkt_list_tot)
256 return false;
404bdbfd
PM
257 if (info->name[0] == '\0' ||
258 strnlen(info->name, IPT_RECENT_NAME_LEN) == IPT_RECENT_NAME_LEN)
ccb79bdc 259 return false;
1da177e4 260
a0e889bb 261 mutex_lock(&recent_mutex);
404bdbfd
PM
262 t = recent_table_lookup(info->name);
263 if (t != NULL) {
264 t->refcnt++;
ccb79bdc 265 ret = true;
404bdbfd 266 goto out;
1da177e4
LT
267 }
268
404bdbfd 269 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
a0e889bb 270 GFP_KERNEL);
404bdbfd
PM
271 if (t == NULL)
272 goto out;
2b2283d0 273 t->refcnt = 1;
404bdbfd
PM
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]);
278#ifdef CONFIG_PROC_FS
8eeee8b1 279 t->proc = proc_create(t->name, ip_list_perms, proc_dir, &recent_fops);
404bdbfd
PM
280 if (t->proc == NULL) {
281 kfree(t);
282 goto out;
1da177e4 283 }
b93ff783
DDG
284 t->proc->uid = ip_list_uid;
285 t->proc->gid = ip_list_gid;
404bdbfd 286 t->proc->data = t;
1da177e4 287#endif
a0e889bb 288 spin_lock_bh(&recent_lock);
404bdbfd 289 list_add_tail(&t->list, &tables);
a0e889bb 290 spin_unlock_bh(&recent_lock);
ccb79bdc 291 ret = true;
404bdbfd 292out:
a0e889bb 293 mutex_unlock(&recent_mutex);
404bdbfd
PM
294 return ret;
295}
1da177e4 296
d3c5ee6d 297static void recent_mt_destroy(const struct xt_match *match, void *matchinfo)
404bdbfd
PM
298{
299 const struct ipt_recent_info *info = matchinfo;
300 struct recent_table *t;
1da177e4 301
a0e889bb 302 mutex_lock(&recent_mutex);
404bdbfd
PM
303 t = recent_table_lookup(info->name);
304 if (--t->refcnt == 0) {
a0e889bb 305 spin_lock_bh(&recent_lock);
404bdbfd 306 list_del(&t->list);
a0e889bb 307 spin_unlock_bh(&recent_lock);
404bdbfd
PM
308#ifdef CONFIG_PROC_FS
309 remove_proc_entry(t->name, proc_dir);
1da177e4 310#endif
a8ddc916 311 recent_table_flush(t);
404bdbfd 312 kfree(t);
1da177e4 313 }
a0e889bb 314 mutex_unlock(&recent_mutex);
404bdbfd 315}
1da177e4 316
404bdbfd
PM
317#ifdef CONFIG_PROC_FS
318struct recent_iter_state {
319 struct recent_table *table;
320 unsigned int bucket;
321};
1da177e4 322
404bdbfd 323static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
855304af 324 __acquires(recent_lock)
404bdbfd
PM
325{
326 struct recent_iter_state *st = seq->private;
a47362a2 327 const struct recent_table *t = st->table;
404bdbfd
PM
328 struct recent_entry *e;
329 loff_t p = *pos;
1da177e4 330
1da177e4 331 spin_lock_bh(&recent_lock);
1da177e4 332
7c4e36bc
JE
333 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
334 list_for_each_entry(e, &t->iphash[st->bucket], list)
404bdbfd
PM
335 if (p-- == 0)
336 return e;
404bdbfd
PM
337 return NULL;
338}
1da177e4 339
404bdbfd
PM
340static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
341{
342 struct recent_iter_state *st = seq->private;
3cf93c96 343 const struct recent_table *t = st->table;
404bdbfd
PM
344 struct recent_entry *e = v;
345 struct list_head *head = e->list.next;
346
347 while (head == &t->iphash[st->bucket]) {
348 if (++st->bucket >= ip_list_hash_size)
349 return NULL;
350 head = t->iphash[st->bucket].next;
351 }
352 (*pos)++;
353 return list_entry(head, struct recent_entry, list);
1da177e4
LT
354}
355
404bdbfd 356static void recent_seq_stop(struct seq_file *s, void *v)
855304af 357 __releases(recent_lock)
1da177e4 358{
404bdbfd
PM
359 spin_unlock_bh(&recent_lock);
360}
1da177e4 361
404bdbfd
PM
362static int recent_seq_show(struct seq_file *seq, void *v)
363{
3cf93c96 364 const struct recent_entry *e = v;
404bdbfd
PM
365 unsigned int i;
366
367 i = (e->index - 1) % ip_pkt_list_tot;
368 seq_printf(seq, "src=%u.%u.%u.%u ttl: %u last_seen: %lu oldest_pkt: %u",
369 NIPQUAD(e->addr), e->ttl, e->stamps[i], e->index);
370 for (i = 0; i < e->nstamps; i++)
371 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
372 seq_printf(seq, "\n");
373 return 0;
374}
1da177e4 375
56b3d975 376static const struct seq_operations recent_seq_ops = {
404bdbfd
PM
377 .start = recent_seq_start,
378 .next = recent_seq_next,
379 .stop = recent_seq_stop,
380 .show = recent_seq_show,
381};
1da177e4 382
404bdbfd
PM
383static int recent_seq_open(struct inode *inode, struct file *file)
384{
385 struct proc_dir_entry *pde = PDE(inode);
404bdbfd 386 struct recent_iter_state *st;
404bdbfd 387
e2da5913 388 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
404bdbfd
PM
389 if (st == NULL)
390 return -ENOMEM;
3af8e31c 391
404bdbfd 392 st->table = pde->data;
e2da5913 393 return 0;
404bdbfd 394}
1da177e4 395
404bdbfd
PM
396static ssize_t recent_proc_write(struct file *file, const char __user *input,
397 size_t size, loff_t *loff)
398{
3cf93c96 399 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
404bdbfd
PM
400 struct recent_table *t = pde->data;
401 struct recent_entry *e;
402 char buf[sizeof("+255.255.255.255")], *c = buf;
6a19d614 403 __be32 addr;
404bdbfd
PM
404 int add;
405
406 if (size > sizeof(buf))
407 size = sizeof(buf);
408 if (copy_from_user(buf, input, size))
409 return -EFAULT;
410 while (isspace(*c))
411 c++;
412
413 if (size - (c - buf) < 5)
414 return c - buf;
415 if (!strncmp(c, "clear", 5)) {
416 c += 5;
417 spin_lock_bh(&recent_lock);
418 recent_table_flush(t);
1da177e4 419 spin_unlock_bh(&recent_lock);
404bdbfd 420 return c - buf;
1da177e4 421 }
1da177e4 422
404bdbfd
PM
423 switch (*c) {
424 case '-':
425 add = 0;
426 c++;
427 break;
428 case '+':
429 c++;
430 default:
431 add = 1;
432 break;
1da177e4 433 }
404bdbfd 434 addr = in_aton(c);
1da177e4 435
404bdbfd
PM
436 spin_lock_bh(&recent_lock);
437 e = recent_entry_lookup(t, addr, 0);
438 if (e == NULL) {
439 if (add)
440 recent_entry_init(t, addr, 0);
441 } else {
442 if (add)
443 recent_entry_update(t, e);
444 else
445 recent_entry_remove(t, e);
1da177e4 446 }
1da177e4 447 spin_unlock_bh(&recent_lock);
404bdbfd
PM
448 return size;
449}
1da177e4 450
9a32144e 451static const struct file_operations recent_fops = {
404bdbfd
PM
452 .open = recent_seq_open,
453 .read = seq_read,
454 .write = recent_proc_write,
455 .release = seq_release_private,
456 .owner = THIS_MODULE,
457};
1da177e4 458#endif /* CONFIG_PROC_FS */
1da177e4 459
d3c5ee6d 460static struct xt_match recent_mt_reg __read_mostly = {
1d5cd909 461 .name = "recent",
6709dbbb 462 .family = AF_INET,
d3c5ee6d 463 .match = recent_mt,
1d5cd909 464 .matchsize = sizeof(struct ipt_recent_info),
d3c5ee6d
JE
465 .checkentry = recent_mt_check,
466 .destroy = recent_mt_destroy,
404bdbfd 467 .me = THIS_MODULE,
1da177e4
LT
468};
469
d3c5ee6d 470static int __init recent_mt_init(void)
1da177e4 471{
404bdbfd 472 int err;
1da177e4 473
404bdbfd
PM
474 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
475 return -EINVAL;
476 ip_list_hash_size = 1 << fls(ip_list_tot);
1da177e4 477
d3c5ee6d 478 err = xt_register_match(&recent_mt_reg);
404bdbfd 479#ifdef CONFIG_PROC_FS
1da177e4 480 if (err)
404bdbfd 481 return err;
457c4cbc 482 proc_dir = proc_mkdir("ipt_recent", init_net.proc_net);
404bdbfd 483 if (proc_dir == NULL) {
d3c5ee6d 484 xt_unregister_match(&recent_mt_reg);
404bdbfd
PM
485 err = -ENOMEM;
486 }
487#endif
1da177e4
LT
488 return err;
489}
490
d3c5ee6d 491static void __exit recent_mt_exit(void)
1da177e4 492{
404bdbfd 493 BUG_ON(!list_empty(&tables));
d3c5ee6d 494 xt_unregister_match(&recent_mt_reg);
404bdbfd 495#ifdef CONFIG_PROC_FS
457c4cbc 496 remove_proc_entry("ipt_recent", init_net.proc_net);
404bdbfd 497#endif
1da177e4
LT
498}
499
d3c5ee6d
JE
500module_init(recent_mt_init);
501module_exit(recent_mt_exit);