netfilter: Introduce NFPROTO_* constants
[deliverable/linux.git] / net / netfilter / xt_recent.c
CommitLineData
404bdbfd
PM
1/*
2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
079aa88f 3 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
404bdbfd
PM
4 *
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>
6709dbbb 16#include <linux/ip.h>
079aa88f
JE
17#include <linux/ipv6.h>
18#include <linux/module.h>
404bdbfd 19#include <linux/moduleparam.h>
1da177e4 20#include <linux/proc_fs.h>
404bdbfd
PM
21#include <linux/seq_file.h>
22#include <linux/string.h>
1da177e4 23#include <linux/ctype.h>
404bdbfd
PM
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>
457c4cbc 30#include <net/net_namespace.h>
1da177e4 31
6709dbbb 32#include <linux/netfilter/x_tables.h>
e948b20a 33#include <linux/netfilter/xt_recent.h>
1da177e4 34
404bdbfd 35MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
079aa88f 36MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
2ae15b64 37MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
404bdbfd 38MODULE_LICENSE("GPL");
e948b20a 39MODULE_ALIAS("ipt_recent");
079aa88f 40MODULE_ALIAS("ip6t_recent");
1da177e4 41
e7be6994
PM
42static 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;
b93ff783
DDG
46static unsigned int ip_list_uid = 0;
47static unsigned int ip_list_gid = 0;
e7be6994
PM
48module_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);
b93ff783
DDG
52module_param(ip_list_uid, uint, 0400);
53module_param(ip_list_gid, uint, 0400);
404bdbfd
PM
54MODULE_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");
079aa88f
JE
57MODULE_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");
404bdbfd 60
404bdbfd
PM
61struct recent_entry {
62 struct list_head list;
63 struct list_head lru_list;
079aa88f
JE
64 union nf_inet_addr addr;
65 u_int16_t family;
404bdbfd
PM
66 u_int8_t ttl;
67 u_int8_t index;
68 u_int16_t nstamps;
69 unsigned long stamps[0];
1da177e4
LT
70};
71
404bdbfd
PM
72struct recent_table {
73 struct list_head list;
e948b20a 74 char name[XT_RECENT_NAME_LEN];
1da177e4 75#ifdef CONFIG_PROC_FS
079aa88f 76 struct proc_dir_entry *proc_old, *proc;
404bdbfd
PM
77#endif
78 unsigned int refcnt;
79 unsigned int entries;
80 struct list_head lru_list;
81 struct list_head iphash[0];
1da177e4
LT
82};
83
404bdbfd 84static LIST_HEAD(tables);
1da177e4 85static DEFINE_SPINLOCK(recent_lock);
a0e889bb 86static DEFINE_MUTEX(recent_mutex);
1da177e4
LT
87
88#ifdef CONFIG_PROC_FS
079aa88f
JE
89#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
90static struct proc_dir_entry *proc_old_dir;
91#endif
92static struct proc_dir_entry *recent_proc_dir;
93static const struct file_operations recent_old_fops, recent_mt_fops;
1da177e4
LT
94#endif
95
404bdbfd 96static u_int32_t hash_rnd;
079aa88f
JE
97static bool hash_rnd_initted;
98
99static unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
100{
101 if (!hash_rnd_initted) {
102 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
103 hash_rnd_initted = true;
104 }
105 return jhash_1word((__force u32)addr->ip, hash_rnd) &
106 (ip_list_hash_size - 1);
107}
1da177e4 108
079aa88f 109static unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
1da177e4 110{
404bdbfd 111 if (!hash_rnd_initted) {
079aa88f
JE
112 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
113 hash_rnd_initted = true;
1da177e4 114 }
079aa88f
JE
115 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
116 (ip_list_hash_size - 1);
1da177e4
LT
117}
118
404bdbfd 119static struct recent_entry *
079aa88f
JE
120recent_entry_lookup(const struct recent_table *table,
121 const union nf_inet_addr *addrp, u_int16_t family,
122 u_int8_t ttl)
1da177e4 123{
404bdbfd
PM
124 struct recent_entry *e;
125 unsigned int h;
126
079aa88f
JE
127 if (family == AF_INET)
128 h = recent_entry_hash4(addrp);
129 else
130 h = recent_entry_hash6(addrp);
131
404bdbfd 132 list_for_each_entry(e, &table->iphash[h], list)
079aa88f
JE
133 if (e->family == family &&
134 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
135 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
404bdbfd
PM
136 return e;
137 return NULL;
138}
1da177e4 139
404bdbfd
PM
140static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
141{
142 list_del(&e->list);
143 list_del(&e->lru_list);
144 kfree(e);
145 t->entries--;
146}
1da177e4 147
404bdbfd 148static struct recent_entry *
079aa88f
JE
149recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
150 u_int16_t family, u_int8_t ttl)
404bdbfd
PM
151{
152 struct recent_entry *e;
1da177e4 153
404bdbfd
PM
154 if (t->entries >= ip_list_tot) {
155 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
156 recent_entry_remove(t, e);
1da177e4 157 }
404bdbfd
PM
158 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
159 GFP_ATOMIC);
160 if (e == NULL)
161 return NULL;
079aa88f 162 memcpy(&e->addr, addr, sizeof(e->addr));
404bdbfd
PM
163 e->ttl = ttl;
164 e->stamps[0] = jiffies;
165 e->nstamps = 1;
166 e->index = 1;
079aa88f
JE
167 e->family = family;
168 if (family == AF_INET)
169 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
170 else
171 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
404bdbfd
PM
172 list_add_tail(&e->lru_list, &t->lru_list);
173 t->entries++;
174 return e;
175}
1da177e4 176
404bdbfd
PM
177static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
178{
179 e->stamps[e->index++] = jiffies;
180 if (e->index > e->nstamps)
181 e->nstamps = e->index;
182 e->index %= ip_pkt_list_tot;
183 list_move_tail(&e->lru_list, &t->lru_list);
184}
1da177e4 185
404bdbfd
PM
186static struct recent_table *recent_table_lookup(const char *name)
187{
188 struct recent_table *t;
1da177e4 189
404bdbfd
PM
190 list_for_each_entry(t, &tables, list)
191 if (!strcmp(t->name, name))
192 return t;
193 return NULL;
194}
1da177e4 195
404bdbfd
PM
196static void recent_table_flush(struct recent_table *t)
197{
198 struct recent_entry *e, *next;
199 unsigned int i;
1da177e4 200
7c4e36bc 201 for (i = 0; i < ip_list_hash_size; i++)
404bdbfd
PM
202 list_for_each_entry_safe(e, next, &t->iphash[i], list)
203 recent_entry_remove(t, e);
1da177e4
LT
204}
205
1d93a9cb 206static bool
d3c5ee6d
JE
207recent_mt(const struct sk_buff *skb, const struct net_device *in,
208 const struct net_device *out, const struct xt_match *match,
209 const void *matchinfo, int offset, unsigned int protoff,
210 bool *hotdrop)
1da177e4 211{
e948b20a 212 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd
PM
213 struct recent_table *t;
214 struct recent_entry *e;
079aa88f 215 union nf_inet_addr addr = {};
404bdbfd 216 u_int8_t ttl;
1d93a9cb 217 bool ret = info->invert;
1da177e4 218
079aa88f
JE
219 if (match->family == AF_INET) {
220 const struct iphdr *iph = ip_hdr(skb);
221
222 if (info->side == XT_RECENT_DEST)
223 addr.ip = iph->daddr;
224 else
225 addr.ip = iph->saddr;
226
227 ttl = iph->ttl;
228 } else {
229 const struct ipv6hdr *iph = ipv6_hdr(skb);
230
231 if (info->side == XT_RECENT_DEST)
232 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
233 else
234 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
235
236 ttl = iph->hop_limit;
237 }
1da177e4 238
404bdbfd
PM
239 /* use TTL as seen before forwarding */
240 if (out && !skb->sk)
241 ttl++;
1da177e4 242
1da177e4 243 spin_lock_bh(&recent_lock);
404bdbfd 244 t = recent_table_lookup(info->name);
079aa88f
JE
245 e = recent_entry_lookup(t, &addr, match->family,
246 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
404bdbfd 247 if (e == NULL) {
e948b20a 248 if (!(info->check_set & XT_RECENT_SET))
404bdbfd 249 goto out;
079aa88f 250 e = recent_entry_init(t, &addr, match->family, ttl);
404bdbfd 251 if (e == NULL)
cff533ac 252 *hotdrop = true;
1d93a9cb 253 ret = !ret;
404bdbfd 254 goto out;
1da177e4
LT
255 }
256
e948b20a 257 if (info->check_set & XT_RECENT_SET)
1d93a9cb 258 ret = !ret;
e948b20a 259 else if (info->check_set & XT_RECENT_REMOVE) {
404bdbfd 260 recent_entry_remove(t, e);
1d93a9cb 261 ret = !ret;
e948b20a 262 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
855304af 263 unsigned long time = jiffies - info->seconds * HZ;
404bdbfd
PM
264 unsigned int i, hits = 0;
265
266 for (i = 0; i < e->nstamps; i++) {
855304af 267 if (info->seconds && time_after(time, e->stamps[i]))
404bdbfd
PM
268 continue;
269 if (++hits >= info->hit_count) {
1d93a9cb 270 ret = !ret;
404bdbfd 271 break;
1da177e4 272 }
1da177e4 273 }
1da177e4
LT
274 }
275
e948b20a
JE
276 if (info->check_set & XT_RECENT_SET ||
277 (info->check_set & XT_RECENT_UPDATE && ret)) {
404bdbfd
PM
278 recent_entry_update(t, e);
279 e->ttl = ttl;
280 }
281out:
282 spin_unlock_bh(&recent_lock);
283 return ret;
1da177e4
LT
284}
285
ccb79bdc 286static bool
d3c5ee6d
JE
287recent_mt_check(const char *tablename, const void *ip,
288 const struct xt_match *match, void *matchinfo,
289 unsigned int hook_mask)
1da177e4 290{
e948b20a 291 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd
PM
292 struct recent_table *t;
293 unsigned i;
ccb79bdc 294 bool ret = false;
1da177e4 295
404bdbfd 296 if (hweight8(info->check_set &
e948b20a
JE
297 (XT_RECENT_SET | XT_RECENT_REMOVE |
298 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
ccb79bdc 299 return false;
e948b20a 300 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
404bdbfd 301 (info->seconds || info->hit_count))
ccb79bdc 302 return false;
d0ebf133
DHZ
303 if (info->hit_count > ip_pkt_list_tot)
304 return false;
404bdbfd 305 if (info->name[0] == '\0' ||
e948b20a 306 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
ccb79bdc 307 return false;
1da177e4 308
a0e889bb 309 mutex_lock(&recent_mutex);
404bdbfd
PM
310 t = recent_table_lookup(info->name);
311 if (t != NULL) {
312 t->refcnt++;
ccb79bdc 313 ret = true;
404bdbfd 314 goto out;
1da177e4
LT
315 }
316
404bdbfd 317 t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
a0e889bb 318 GFP_KERNEL);
404bdbfd
PM
319 if (t == NULL)
320 goto out;
2b2283d0 321 t->refcnt = 1;
404bdbfd
PM
322 strcpy(t->name, info->name);
323 INIT_LIST_HEAD(&t->lru_list);
324 for (i = 0; i < ip_list_hash_size; i++)
325 INIT_LIST_HEAD(&t->iphash[i]);
326#ifdef CONFIG_PROC_FS
079aa88f
JE
327 t->proc = proc_create(t->name, ip_list_perms, recent_proc_dir,
328 &recent_mt_fops);
404bdbfd
PM
329 if (t->proc == NULL) {
330 kfree(t);
331 goto out;
1da177e4 332 }
079aa88f
JE
333#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
334 t->proc_old = proc_create(t->name, ip_list_perms, proc_old_dir,
335 &recent_old_fops);
336 if (t->proc_old == NULL) {
337 remove_proc_entry(t->name, proc_old_dir);
338 kfree(t);
339 goto out;
340 }
341 t->proc_old->uid = ip_list_uid;
342 t->proc_old->gid = ip_list_gid;
343 t->proc_old->data = t;
344#endif
b93ff783
DDG
345 t->proc->uid = ip_list_uid;
346 t->proc->gid = ip_list_gid;
404bdbfd 347 t->proc->data = t;
1da177e4 348#endif
a0e889bb 349 spin_lock_bh(&recent_lock);
404bdbfd 350 list_add_tail(&t->list, &tables);
a0e889bb 351 spin_unlock_bh(&recent_lock);
ccb79bdc 352 ret = true;
404bdbfd 353out:
a0e889bb 354 mutex_unlock(&recent_mutex);
404bdbfd
PM
355 return ret;
356}
1da177e4 357
d3c5ee6d 358static void recent_mt_destroy(const struct xt_match *match, void *matchinfo)
404bdbfd 359{
e948b20a 360 const struct xt_recent_mtinfo *info = matchinfo;
404bdbfd 361 struct recent_table *t;
1da177e4 362
a0e889bb 363 mutex_lock(&recent_mutex);
404bdbfd
PM
364 t = recent_table_lookup(info->name);
365 if (--t->refcnt == 0) {
a0e889bb 366 spin_lock_bh(&recent_lock);
404bdbfd 367 list_del(&t->list);
a0e889bb 368 spin_unlock_bh(&recent_lock);
404bdbfd 369#ifdef CONFIG_PROC_FS
079aa88f
JE
370#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
371 remove_proc_entry(t->name, proc_old_dir);
372#endif
373 remove_proc_entry(t->name, recent_proc_dir);
1da177e4 374#endif
a8ddc916 375 recent_table_flush(t);
404bdbfd 376 kfree(t);
1da177e4 377 }
a0e889bb 378 mutex_unlock(&recent_mutex);
404bdbfd 379}
1da177e4 380
404bdbfd
PM
381#ifdef CONFIG_PROC_FS
382struct recent_iter_state {
079aa88f 383 const struct recent_table *table;
404bdbfd
PM
384 unsigned int bucket;
385};
1da177e4 386
404bdbfd 387static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
855304af 388 __acquires(recent_lock)
404bdbfd
PM
389{
390 struct recent_iter_state *st = seq->private;
a47362a2 391 const struct recent_table *t = st->table;
404bdbfd
PM
392 struct recent_entry *e;
393 loff_t p = *pos;
1da177e4 394
1da177e4 395 spin_lock_bh(&recent_lock);
1da177e4 396
7c4e36bc
JE
397 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
398 list_for_each_entry(e, &t->iphash[st->bucket], list)
404bdbfd
PM
399 if (p-- == 0)
400 return e;
404bdbfd
PM
401 return NULL;
402}
1da177e4 403
404bdbfd
PM
404static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
405{
406 struct recent_iter_state *st = seq->private;
3cf93c96 407 const struct recent_table *t = st->table;
079aa88f
JE
408 const struct recent_entry *e = v;
409 const struct list_head *head = e->list.next;
404bdbfd
PM
410
411 while (head == &t->iphash[st->bucket]) {
412 if (++st->bucket >= ip_list_hash_size)
413 return NULL;
414 head = t->iphash[st->bucket].next;
415 }
416 (*pos)++;
417 return list_entry(head, struct recent_entry, list);
1da177e4
LT
418}
419
404bdbfd 420static void recent_seq_stop(struct seq_file *s, void *v)
855304af 421 __releases(recent_lock)
1da177e4 422{
404bdbfd
PM
423 spin_unlock_bh(&recent_lock);
424}
1da177e4 425
404bdbfd
PM
426static int recent_seq_show(struct seq_file *seq, void *v)
427{
3cf93c96 428 const struct recent_entry *e = v;
404bdbfd
PM
429 unsigned int i;
430
431 i = (e->index - 1) % ip_pkt_list_tot;
079aa88f
JE
432 if (e->family == AF_INET)
433 seq_printf(seq, "src=" NIPQUAD_FMT " ttl: %u last_seen: %lu "
434 "oldest_pkt: %u", NIPQUAD(e->addr.ip), e->ttl,
435 e->stamps[i], e->index);
436 else
437 seq_printf(seq, "src=" NIP6_FMT " ttl: %u last_seen: %lu "
438 "oldest_pkt: %u", NIP6(e->addr.in6), e->ttl,
439 e->stamps[i], e->index);
404bdbfd
PM
440 for (i = 0; i < e->nstamps; i++)
441 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
442 seq_printf(seq, "\n");
443 return 0;
444}
1da177e4 445
56b3d975 446static const struct seq_operations recent_seq_ops = {
404bdbfd
PM
447 .start = recent_seq_start,
448 .next = recent_seq_next,
449 .stop = recent_seq_stop,
450 .show = recent_seq_show,
451};
1da177e4 452
404bdbfd
PM
453static int recent_seq_open(struct inode *inode, struct file *file)
454{
455 struct proc_dir_entry *pde = PDE(inode);
404bdbfd 456 struct recent_iter_state *st;
404bdbfd 457
e2da5913 458 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
404bdbfd
PM
459 if (st == NULL)
460 return -ENOMEM;
3af8e31c 461
404bdbfd 462 st->table = pde->data;
e2da5913 463 return 0;
404bdbfd 464}
1da177e4 465
079aa88f
JE
466#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
467static int recent_old_seq_open(struct inode *inode, struct file *filp)
468{
469 static bool warned_of_old;
470
471 if (unlikely(!warned_of_old)) {
472 printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
473 " is deprecated; use /proc/net/xt_recent.\n");
474 warned_of_old = true;
475 }
476 return recent_seq_open(inode, filp);
477}
478
479static ssize_t recent_old_proc_write(struct file *file,
480 const char __user *input,
481 size_t size, loff_t *loff)
404bdbfd 482{
3cf93c96 483 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
404bdbfd
PM
484 struct recent_table *t = pde->data;
485 struct recent_entry *e;
486 char buf[sizeof("+255.255.255.255")], *c = buf;
6a19d614 487 __be32 addr;
404bdbfd
PM
488 int add;
489
490 if (size > sizeof(buf))
491 size = sizeof(buf);
492 if (copy_from_user(buf, input, size))
493 return -EFAULT;
079aa88f 494
404bdbfd
PM
495 while (isspace(*c))
496 c++;
497
498 if (size - (c - buf) < 5)
499 return c - buf;
500 if (!strncmp(c, "clear", 5)) {
501 c += 5;
502 spin_lock_bh(&recent_lock);
503 recent_table_flush(t);
1da177e4 504 spin_unlock_bh(&recent_lock);
404bdbfd 505 return c - buf;
1da177e4 506 }
1da177e4 507
404bdbfd
PM
508 switch (*c) {
509 case '-':
510 add = 0;
511 c++;
512 break;
513 case '+':
514 c++;
515 default:
516 add = 1;
517 break;
1da177e4 518 }
404bdbfd 519 addr = in_aton(c);
1da177e4 520
404bdbfd 521 spin_lock_bh(&recent_lock);
079aa88f 522 e = recent_entry_lookup(t, (const void *)&addr, PF_INET, 0);
404bdbfd
PM
523 if (e == NULL) {
524 if (add)
079aa88f 525 recent_entry_init(t, (const void *)&addr, PF_INET, 0);
404bdbfd
PM
526 } else {
527 if (add)
528 recent_entry_update(t, e);
529 else
530 recent_entry_remove(t, e);
1da177e4 531 }
1da177e4 532 spin_unlock_bh(&recent_lock);
404bdbfd
PM
533 return size;
534}
1da177e4 535
079aa88f
JE
536static const struct file_operations recent_old_fops = {
537 .open = recent_old_seq_open,
404bdbfd 538 .read = seq_read,
079aa88f 539 .write = recent_old_proc_write,
404bdbfd
PM
540 .release = seq_release_private,
541 .owner = THIS_MODULE,
542};
079aa88f
JE
543#endif
544
545static ssize_t
546recent_mt_proc_write(struct file *file, const char __user *input,
547 size_t size, loff_t *loff)
548{
549 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
550 struct recent_table *t = pde->data;
551 struct recent_entry *e;
552 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
553 const char *c = buf;
554 union nf_inet_addr addr;
555 u_int16_t family;
556 bool add, succ;
557
558 if (size == 0)
559 return 0;
560 if (size > sizeof(buf))
561 size = sizeof(buf);
562 if (copy_from_user(buf, input, size) != 0)
563 return -EFAULT;
564
565 /* Strict protocol! */
566 if (*loff != 0)
567 return -ESPIPE;
568 switch (*c) {
569 case '/': /* flush table */
570 spin_lock_bh(&recent_lock);
571 recent_table_flush(t);
572 spin_unlock_bh(&recent_lock);
573 return size;
574 case '-': /* remove address */
575 add = false;
576 break;
577 case '+': /* add address */
578 add = true;
579 break;
580 default:
581 printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
582 return -EINVAL;
583 }
584
585 ++c;
586 --size;
587 if (strnchr(c, size, ':') != NULL) {
588 family = AF_INET6;
589 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
590 } else {
591 family = AF_INET;
592 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
593 }
594
595 if (!succ) {
596 printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
597 "to procfs\n");
598 return -EINVAL;
599 }
600
601 spin_lock_bh(&recent_lock);
602 e = recent_entry_lookup(t, &addr, family, 0);
603 if (e == NULL) {
604 if (add)
605 recent_entry_init(t, &addr, family, 0);
606 } else {
607 if (add)
608 recent_entry_update(t, e);
609 else
610 recent_entry_remove(t, e);
611 }
612 spin_unlock_bh(&recent_lock);
613 /* Note we removed one above */
614 *loff += size + 1;
615 return size + 1;
616}
617
618static const struct file_operations recent_mt_fops = {
619 .open = recent_seq_open,
620 .read = seq_read,
621 .write = recent_mt_proc_write,
622 .release = seq_release_private,
623 .owner = THIS_MODULE,
624};
1da177e4 625#endif /* CONFIG_PROC_FS */
1da177e4 626
079aa88f
JE
627static struct xt_match recent_mt_reg[] __read_mostly = {
628 {
629 .name = "recent",
630 .revision = 0,
631 .family = AF_INET,
632 .match = recent_mt,
633 .matchsize = sizeof(struct xt_recent_mtinfo),
634 .checkentry = recent_mt_check,
635 .destroy = recent_mt_destroy,
636 .me = THIS_MODULE,
637 },
638 {
639 .name = "recent",
640 .revision = 0,
641 .family = AF_INET6,
642 .match = recent_mt,
643 .matchsize = sizeof(struct xt_recent_mtinfo),
644 .checkentry = recent_mt_check,
645 .destroy = recent_mt_destroy,
646 .me = THIS_MODULE,
647 },
1da177e4
LT
648};
649
d3c5ee6d 650static int __init recent_mt_init(void)
1da177e4 651{
404bdbfd 652 int err;
1da177e4 653
404bdbfd
PM
654 if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
655 return -EINVAL;
656 ip_list_hash_size = 1 << fls(ip_list_tot);
1da177e4 657
079aa88f 658 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
404bdbfd 659#ifdef CONFIG_PROC_FS
1da177e4 660 if (err)
404bdbfd 661 return err;
079aa88f
JE
662 recent_proc_dir = proc_mkdir("xt_recent", init_net.proc_net);
663 if (recent_proc_dir == NULL) {
664 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
665 err = -ENOMEM;
666 }
667#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
668 if (err < 0)
669 return err;
670 proc_old_dir = proc_mkdir("ipt_recent", init_net.proc_net);
671 if (proc_old_dir == NULL) {
672 remove_proc_entry("xt_recent", init_net.proc_net);
673 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
404bdbfd
PM
674 err = -ENOMEM;
675 }
079aa88f 676#endif
404bdbfd 677#endif
1da177e4
LT
678 return err;
679}
680
d3c5ee6d 681static void __exit recent_mt_exit(void)
1da177e4 682{
404bdbfd 683 BUG_ON(!list_empty(&tables));
079aa88f 684 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
404bdbfd 685#ifdef CONFIG_PROC_FS
079aa88f 686#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
457c4cbc 687 remove_proc_entry("ipt_recent", init_net.proc_net);
079aa88f
JE
688#endif
689 remove_proc_entry("xt_recent", init_net.proc_net);
404bdbfd 690#endif
1da177e4
LT
691}
692
d3c5ee6d
JE
693module_init(recent_mt_init);
694module_exit(recent_mt_exit);
This page took 0.471502 seconds and 5 git commands to generate.