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