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