]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/xt_recent.c
Merge tag 'imx-fixes-4.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/shawngu...
[mirror_ubuntu-artful-kernel.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 */
8bee4bad 15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
404bdbfd 16#include <linux/init.h>
6709dbbb 17#include <linux/ip.h>
079aa88f
JE
18#include <linux/ipv6.h>
19#include <linux/module.h>
404bdbfd 20#include <linux/moduleparam.h>
1da177e4 21#include <linux/proc_fs.h>
404bdbfd
PM
22#include <linux/seq_file.h>
23#include <linux/string.h>
1da177e4 24#include <linux/ctype.h>
404bdbfd
PM
25#include <linux/list.h>
26#include <linux/random.h>
27#include <linux/jhash.h>
28#include <linux/bitops.h>
29#include <linux/skbuff.h>
30#include <linux/inet.h>
5a0e3ad6 31#include <linux/slab.h>
2727de76 32#include <linux/vmalloc.h>
457c4cbc 33#include <net/net_namespace.h>
7d07d563 34#include <net/netns/generic.h>
1da177e4 35
6709dbbb 36#include <linux/netfilter/x_tables.h>
e948b20a 37#include <linux/netfilter/xt_recent.h>
1da177e4 38
404bdbfd 39MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
408ffaa4 40MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
06bf514e 41MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
404bdbfd 42MODULE_LICENSE("GPL");
e948b20a 43MODULE_ALIAS("ipt_recent");
079aa88f 44MODULE_ALIAS("ip6t_recent");
1da177e4 45
abc86d0f
FW
46static unsigned int ip_list_tot __read_mostly = 100;
47static unsigned int ip_list_hash_size __read_mostly;
48static unsigned int ip_list_perms __read_mostly = 0644;
49static unsigned int ip_list_uid __read_mostly;
50static unsigned int ip_list_gid __read_mostly;
e7be6994 51module_param(ip_list_tot, uint, 0400);
e7be6994
PM
52module_param(ip_list_hash_size, uint, 0400);
53module_param(ip_list_perms, uint, 0400);
5dc7a6d5
JE
54module_param(ip_list_uid, uint, S_IRUGO | S_IWUSR);
55module_param(ip_list_gid, uint, S_IRUGO | S_IWUSR);
404bdbfd 56MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
404bdbfd 57MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
079aa88f 58MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
5dc7a6d5
JE
59MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files");
60MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files");
404bdbfd 61
abc86d0f
FW
62/* retained for backwards compatibility */
63static unsigned int ip_pkt_list_tot __read_mostly;
64module_param(ip_pkt_list_tot, uint, 0400);
65MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
66
67#define XT_RECENT_MAX_NSTAMPS 256
68
404bdbfd
PM
69struct recent_entry {
70 struct list_head list;
71 struct list_head lru_list;
079aa88f
JE
72 union nf_inet_addr addr;
73 u_int16_t family;
404bdbfd
PM
74 u_int8_t ttl;
75 u_int8_t index;
76 u_int16_t nstamps;
77 unsigned long stamps[0];
1da177e4
LT
78};
79
404bdbfd
PM
80struct recent_table {
81 struct list_head list;
e948b20a 82 char name[XT_RECENT_NAME_LEN];
efdedd54 83 union nf_inet_addr mask;
404bdbfd
PM
84 unsigned int refcnt;
85 unsigned int entries;
abc86d0f 86 u8 nstamps_max_mask;
404bdbfd
PM
87 struct list_head lru_list;
88 struct list_head iphash[0];
1da177e4
LT
89};
90
7d07d563
AD
91struct recent_net {
92 struct list_head tables;
93#ifdef CONFIG_PROC_FS
94 struct proc_dir_entry *xt_recent;
7d07d563
AD
95#endif
96};
97
c7d03a00 98static unsigned int recent_net_id __read_mostly;
abc86d0f 99
7d07d563
AD
100static inline struct recent_net *recent_pernet(struct net *net)
101{
102 return net_generic(net, recent_net_id);
103}
104
1da177e4 105static DEFINE_SPINLOCK(recent_lock);
a0e889bb 106static DEFINE_MUTEX(recent_mutex);
1da177e4
LT
107
108#ifdef CONFIG_PROC_FS
079aa88f 109static const struct file_operations recent_old_fops, recent_mt_fops;
1da177e4
LT
110#endif
111
294188ae 112static u_int32_t hash_rnd __read_mostly;
079aa88f 113
294188ae 114static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
079aa88f 115{
079aa88f
JE
116 return jhash_1word((__force u32)addr->ip, hash_rnd) &
117 (ip_list_hash_size - 1);
118}
1da177e4 119
294188ae 120static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
1da177e4 121{
079aa88f
JE
122 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
123 (ip_list_hash_size - 1);
1da177e4
LT
124}
125
404bdbfd 126static struct recent_entry *
079aa88f
JE
127recent_entry_lookup(const struct recent_table *table,
128 const union nf_inet_addr *addrp, u_int16_t family,
129 u_int8_t ttl)
1da177e4 130{
404bdbfd
PM
131 struct recent_entry *e;
132 unsigned int h;
133
ee999d8b 134 if (family == NFPROTO_IPV4)
079aa88f
JE
135 h = recent_entry_hash4(addrp);
136 else
137 h = recent_entry_hash6(addrp);
138
404bdbfd 139 list_for_each_entry(e, &table->iphash[h], list)
079aa88f
JE
140 if (e->family == family &&
141 memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
142 (ttl == e->ttl || ttl == 0 || e->ttl == 0))
404bdbfd
PM
143 return e;
144 return NULL;
145}
1da177e4 146
404bdbfd
PM
147static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
148{
149 list_del(&e->list);
150 list_del(&e->lru_list);
151 kfree(e);
152 t->entries--;
153}
1da177e4 154
0079c5ae
TG
155/*
156 * Drop entries with timestamps older then 'time'.
157 */
158static void recent_entry_reap(struct recent_table *t, unsigned long time)
159{
160 struct recent_entry *e;
161
162 /*
163 * The head of the LRU list is always the oldest entry.
164 */
165 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
166
167 /*
168 * The last time stamp is the most recent.
169 */
170 if (time_after(time, e->stamps[e->index-1]))
171 recent_entry_remove(t, e);
172}
173
404bdbfd 174static struct recent_entry *
079aa88f
JE
175recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
176 u_int16_t family, u_int8_t ttl)
404bdbfd
PM
177{
178 struct recent_entry *e;
abc86d0f 179 unsigned int nstamps_max = t->nstamps_max_mask;
1da177e4 180
404bdbfd
PM
181 if (t->entries >= ip_list_tot) {
182 e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
183 recent_entry_remove(t, e);
1da177e4 184 }
abc86d0f
FW
185
186 nstamps_max += 1;
187 e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * nstamps_max,
404bdbfd
PM
188 GFP_ATOMIC);
189 if (e == NULL)
190 return NULL;
079aa88f 191 memcpy(&e->addr, addr, sizeof(e->addr));
404bdbfd
PM
192 e->ttl = ttl;
193 e->stamps[0] = jiffies;
194 e->nstamps = 1;
195 e->index = 1;
079aa88f 196 e->family = family;
ee999d8b 197 if (family == NFPROTO_IPV4)
079aa88f
JE
198 list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
199 else
200 list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
404bdbfd
PM
201 list_add_tail(&e->lru_list, &t->lru_list);
202 t->entries++;
203 return e;
204}
1da177e4 205
404bdbfd
PM
206static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
207{
abc86d0f 208 e->index &= t->nstamps_max_mask;
404bdbfd
PM
209 e->stamps[e->index++] = jiffies;
210 if (e->index > e->nstamps)
211 e->nstamps = e->index;
404bdbfd
PM
212 list_move_tail(&e->lru_list, &t->lru_list);
213}
1da177e4 214
7d07d563
AD
215static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
216 const char *name)
404bdbfd
PM
217{
218 struct recent_table *t;
1da177e4 219
7d07d563 220 list_for_each_entry(t, &recent_net->tables, list)
404bdbfd
PM
221 if (!strcmp(t->name, name))
222 return t;
223 return NULL;
224}
1da177e4 225
404bdbfd
PM
226static void recent_table_flush(struct recent_table *t)
227{
228 struct recent_entry *e, *next;
229 unsigned int i;
1da177e4 230
7c4e36bc 231 for (i = 0; i < ip_list_hash_size; i++)
404bdbfd
PM
232 list_for_each_entry_safe(e, next, &t->iphash[i], list)
233 recent_entry_remove(t, e);
1da177e4
LT
234}
235
1d93a9cb 236static bool
62fc8051 237recent_mt(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 238{
613dbd95 239 struct net *net = xt_net(par);
7d07d563 240 struct recent_net *recent_net = recent_pernet(net);
efdedd54 241 const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
404bdbfd
PM
242 struct recent_table *t;
243 struct recent_entry *e;
efdedd54 244 union nf_inet_addr addr = {}, addr_mask;
404bdbfd 245 u_int8_t ttl;
1d93a9cb 246 bool ret = info->invert;
1da177e4 247
613dbd95 248 if (xt_family(par) == NFPROTO_IPV4) {
079aa88f
JE
249 const struct iphdr *iph = ip_hdr(skb);
250
251 if (info->side == XT_RECENT_DEST)
252 addr.ip = iph->daddr;
253 else
254 addr.ip = iph->saddr;
255
256 ttl = iph->ttl;
257 } else {
258 const struct ipv6hdr *iph = ipv6_hdr(skb);
259
260 if (info->side == XT_RECENT_DEST)
261 memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
262 else
263 memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
264
265 ttl = iph->hop_limit;
266 }
1da177e4 267
404bdbfd 268 /* use TTL as seen before forwarding */
613dbd95 269 if (xt_out(par) != NULL && skb->sk == NULL)
404bdbfd 270 ttl++;
1da177e4 271
1da177e4 272 spin_lock_bh(&recent_lock);
7d07d563 273 t = recent_table_lookup(recent_net, info->name);
efdedd54
DF
274
275 nf_inet_addr_mask(&addr, &addr_mask, &t->mask);
276
613dbd95 277 e = recent_entry_lookup(t, &addr_mask, xt_family(par),
079aa88f 278 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
404bdbfd 279 if (e == NULL) {
e948b20a 280 if (!(info->check_set & XT_RECENT_SET))
404bdbfd 281 goto out;
613dbd95 282 e = recent_entry_init(t, &addr_mask, xt_family(par), ttl);
404bdbfd 283 if (e == NULL)
b4ba2611 284 par->hotdrop = true;
1d93a9cb 285 ret = !ret;
404bdbfd 286 goto out;
1da177e4
LT
287 }
288
e948b20a 289 if (info->check_set & XT_RECENT_SET)
1d93a9cb 290 ret = !ret;
e948b20a 291 else if (info->check_set & XT_RECENT_REMOVE) {
404bdbfd 292 recent_entry_remove(t, e);
1d93a9cb 293 ret = !ret;
e948b20a 294 } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
855304af 295 unsigned long time = jiffies - info->seconds * HZ;
404bdbfd
PM
296 unsigned int i, hits = 0;
297
298 for (i = 0; i < e->nstamps; i++) {
855304af 299 if (info->seconds && time_after(time, e->stamps[i]))
404bdbfd 300 continue;
ef169150 301 if (!info->hit_count || ++hits >= info->hit_count) {
1d93a9cb 302 ret = !ret;
404bdbfd 303 break;
1da177e4 304 }
1da177e4 305 }
0079c5ae
TG
306
307 /* info->seconds must be non-zero */
308 if (info->check_set & XT_RECENT_REAP)
309 recent_entry_reap(t, time);
1da177e4
LT
310 }
311
e948b20a
JE
312 if (info->check_set & XT_RECENT_SET ||
313 (info->check_set & XT_RECENT_UPDATE && ret)) {
404bdbfd
PM
314 recent_entry_update(t, e);
315 e->ttl = ttl;
316 }
317out:
318 spin_unlock_bh(&recent_lock);
319 return ret;
1da177e4
LT
320}
321
2727de76
ED
322static void recent_table_free(void *addr)
323{
4cb28970 324 kvfree(addr);
2727de76
ED
325}
326
efdedd54
DF
327static int recent_mt_check(const struct xt_mtchk_param *par,
328 const struct xt_recent_mtinfo_v1 *info)
1da177e4 329{
7d07d563 330 struct recent_net *recent_net = recent_pernet(par->net);
404bdbfd 331 struct recent_table *t;
b0ceb560
AD
332#ifdef CONFIG_PROC_FS
333 struct proc_dir_entry *pde;
da742808
EB
334 kuid_t uid;
335 kgid_t gid;
b0ceb560 336#endif
abc86d0f 337 unsigned int nstamp_mask;
95c96174 338 unsigned int i;
bd414ee6 339 int ret = -EINVAL;
2727de76 340 size_t sz;
1da177e4 341
7bdc6624
GF
342 net_get_random_once(&hash_rnd, sizeof(hash_rnd));
343
606a9a02 344 if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
ff67e4e4
JE
345 pr_info("Unsupported user space flags (%08x)\n",
346 info->check_set);
bd414ee6 347 return -EINVAL;
606a9a02 348 }
404bdbfd 349 if (hweight8(info->check_set &
e948b20a
JE
350 (XT_RECENT_SET | XT_RECENT_REMOVE |
351 XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
bd414ee6 352 return -EINVAL;
e948b20a 353 if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
0079c5ae
TG
354 (info->seconds || info->hit_count ||
355 (info->check_set & XT_RECENT_MODIFIERS)))
bd414ee6 356 return -EINVAL;
0079c5ae 357 if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
bd414ee6 358 return -EINVAL;
abc86d0f
FW
359 if (info->hit_count >= XT_RECENT_MAX_NSTAMPS) {
360 pr_info("hitcount (%u) is larger than allowed maximum (%u)\n",
361 info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
bd414ee6 362 return -EINVAL;
98e6d2d5 363 }
404bdbfd 364 if (info->name[0] == '\0' ||
e948b20a 365 strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
bd414ee6 366 return -EINVAL;
1da177e4 367
abc86d0f
FW
368 if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot)
369 nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1;
370 else if (info->hit_count)
371 nstamp_mask = roundup_pow_of_two(info->hit_count) - 1;
372 else
373 nstamp_mask = 32 - 1;
374
a0e889bb 375 mutex_lock(&recent_mutex);
7d07d563 376 t = recent_table_lookup(recent_net, info->name);
404bdbfd 377 if (t != NULL) {
cef9ed86
FW
378 if (nstamp_mask > t->nstamps_max_mask) {
379 spin_lock_bh(&recent_lock);
380 recent_table_flush(t);
381 t->nstamps_max_mask = nstamp_mask;
382 spin_unlock_bh(&recent_lock);
abc86d0f
FW
383 }
384
404bdbfd 385 t->refcnt++;
bd414ee6 386 ret = 0;
404bdbfd 387 goto out;
1da177e4
LT
388 }
389
2727de76 390 sz = sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size;
752ade68 391 t = kvzalloc(sz, GFP_KERNEL);
4a5a5c73
JE
392 if (t == NULL) {
393 ret = -ENOMEM;
404bdbfd 394 goto out;
4a5a5c73 395 }
2b2283d0 396 t->refcnt = 1;
abc86d0f 397 t->nstamps_max_mask = nstamp_mask;
efdedd54
DF
398
399 memcpy(&t->mask, &info->mask, sizeof(t->mask));
404bdbfd
PM
400 strcpy(t->name, info->name);
401 INIT_LIST_HEAD(&t->lru_list);
402 for (i = 0; i < ip_list_hash_size; i++)
403 INIT_LIST_HEAD(&t->iphash[i]);
404#ifdef CONFIG_PROC_FS
da742808
EB
405 uid = make_kuid(&init_user_ns, ip_list_uid);
406 gid = make_kgid(&init_user_ns, ip_list_gid);
407 if (!uid_valid(uid) || !gid_valid(gid)) {
2727de76 408 recent_table_free(t);
da742808
EB
409 ret = -EINVAL;
410 goto out;
411 }
7d07d563 412 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
b09eec16 413 &recent_mt_fops, t);
b0ceb560 414 if (pde == NULL) {
2727de76 415 recent_table_free(t);
4a5a5c73 416 ret = -ENOMEM;
404bdbfd 417 goto out;
1da177e4 418 }
271a15ea 419 proc_set_user(pde, uid, gid);
1da177e4 420#endif
a0e889bb 421 spin_lock_bh(&recent_lock);
7d07d563 422 list_add_tail(&t->list, &recent_net->tables);
a0e889bb 423 spin_unlock_bh(&recent_lock);
bd414ee6 424 ret = 0;
404bdbfd 425out:
a0e889bb 426 mutex_unlock(&recent_mutex);
404bdbfd
PM
427 return ret;
428}
1da177e4 429
efdedd54
DF
430static int recent_mt_check_v0(const struct xt_mtchk_param *par)
431{
432 const struct xt_recent_mtinfo_v0 *info_v0 = par->matchinfo;
433 struct xt_recent_mtinfo_v1 info_v1;
434
435 /* Copy revision 0 structure to revision 1 */
436 memcpy(&info_v1, info_v0, sizeof(struct xt_recent_mtinfo));
437 /* Set default mask to ensure backward compatible behaviour */
438 memset(info_v1.mask.all, 0xFF, sizeof(info_v1.mask.all));
439
440 return recent_mt_check(par, &info_v1);
441}
442
443static int recent_mt_check_v1(const struct xt_mtchk_param *par)
444{
445 return recent_mt_check(par, par->matchinfo);
446}
447
6be3d859 448static void recent_mt_destroy(const struct xt_mtdtor_param *par)
404bdbfd 449{
7d07d563 450 struct recent_net *recent_net = recent_pernet(par->net);
efdedd54 451 const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
404bdbfd 452 struct recent_table *t;
1da177e4 453
a0e889bb 454 mutex_lock(&recent_mutex);
7d07d563 455 t = recent_table_lookup(recent_net, info->name);
404bdbfd 456 if (--t->refcnt == 0) {
a0e889bb 457 spin_lock_bh(&recent_lock);
404bdbfd 458 list_del(&t->list);
a0e889bb 459 spin_unlock_bh(&recent_lock);
404bdbfd 460#ifdef CONFIG_PROC_FS
665e205c
VL
461 if (recent_net->xt_recent != NULL)
462 remove_proc_entry(t->name, recent_net->xt_recent);
1da177e4 463#endif
a8ddc916 464 recent_table_flush(t);
2727de76 465 recent_table_free(t);
1da177e4 466 }
a0e889bb 467 mutex_unlock(&recent_mutex);
404bdbfd 468}
1da177e4 469
404bdbfd
PM
470#ifdef CONFIG_PROC_FS
471struct recent_iter_state {
079aa88f 472 const struct recent_table *table;
404bdbfd
PM
473 unsigned int bucket;
474};
1da177e4 475
404bdbfd 476static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
855304af 477 __acquires(recent_lock)
404bdbfd
PM
478{
479 struct recent_iter_state *st = seq->private;
a47362a2 480 const struct recent_table *t = st->table;
404bdbfd
PM
481 struct recent_entry *e;
482 loff_t p = *pos;
1da177e4 483
1da177e4 484 spin_lock_bh(&recent_lock);
1da177e4 485
7c4e36bc
JE
486 for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
487 list_for_each_entry(e, &t->iphash[st->bucket], list)
404bdbfd
PM
488 if (p-- == 0)
489 return e;
404bdbfd
PM
490 return NULL;
491}
1da177e4 492
404bdbfd
PM
493static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
494{
495 struct recent_iter_state *st = seq->private;
3cf93c96 496 const struct recent_table *t = st->table;
079aa88f
JE
497 const struct recent_entry *e = v;
498 const struct list_head *head = e->list.next;
404bdbfd
PM
499
500 while (head == &t->iphash[st->bucket]) {
501 if (++st->bucket >= ip_list_hash_size)
502 return NULL;
503 head = t->iphash[st->bucket].next;
504 }
505 (*pos)++;
506 return list_entry(head, struct recent_entry, list);
1da177e4
LT
507}
508
404bdbfd 509static void recent_seq_stop(struct seq_file *s, void *v)
855304af 510 __releases(recent_lock)
1da177e4 511{
404bdbfd
PM
512 spin_unlock_bh(&recent_lock);
513}
1da177e4 514
404bdbfd
PM
515static int recent_seq_show(struct seq_file *seq, void *v)
516{
3cf93c96 517 const struct recent_entry *e = v;
abc86d0f
FW
518 struct recent_iter_state *st = seq->private;
519 const struct recent_table *t = st->table;
404bdbfd
PM
520 unsigned int i;
521
abc86d0f
FW
522 i = (e->index - 1) & t->nstamps_max_mask;
523
ee999d8b 524 if (e->family == NFPROTO_IPV4)
14d5e834
HH
525 seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
526 &e->addr.ip, e->ttl, e->stamps[i], e->index);
079aa88f 527 else
5b095d98 528 seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
38ff4fa4 529 &e->addr.in6, e->ttl, e->stamps[i], e->index);
404bdbfd
PM
530 for (i = 0; i < e->nstamps; i++)
531 seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
cdec2685 532 seq_putc(seq, '\n');
404bdbfd
PM
533 return 0;
534}
1da177e4 535
56b3d975 536static const struct seq_operations recent_seq_ops = {
404bdbfd
PM
537 .start = recent_seq_start,
538 .next = recent_seq_next,
539 .stop = recent_seq_stop,
540 .show = recent_seq_show,
541};
1da177e4 542
404bdbfd
PM
543static int recent_seq_open(struct inode *inode, struct file *file)
544{
404bdbfd 545 struct recent_iter_state *st;
404bdbfd 546
e2da5913 547 st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
404bdbfd
PM
548 if (st == NULL)
549 return -ENOMEM;
3af8e31c 550
d9dda78b 551 st->table = PDE_DATA(inode);
e2da5913 552 return 0;
404bdbfd 553}
1da177e4 554
079aa88f
JE
555static ssize_t
556recent_mt_proc_write(struct file *file, const char __user *input,
557 size_t size, loff_t *loff)
558{
d9dda78b 559 struct recent_table *t = PDE_DATA(file_inode(file));
079aa88f
JE
560 struct recent_entry *e;
561 char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
562 const char *c = buf;
325fb5b4 563 union nf_inet_addr addr = {};
079aa88f
JE
564 u_int16_t family;
565 bool add, succ;
566
567 if (size == 0)
568 return 0;
569 if (size > sizeof(buf))
570 size = sizeof(buf);
571 if (copy_from_user(buf, input, size) != 0)
572 return -EFAULT;
573
574 /* Strict protocol! */
575 if (*loff != 0)
576 return -ESPIPE;
577 switch (*c) {
578 case '/': /* flush table */
579 spin_lock_bh(&recent_lock);
580 recent_table_flush(t);
581 spin_unlock_bh(&recent_lock);
582 return size;
583 case '-': /* remove address */
584 add = false;
585 break;
586 case '+': /* add address */
587 add = true;
588 break;
589 default:
8bee4bad 590 pr_info("Need \"+ip\", \"-ip\" or \"/\"\n");
079aa88f
JE
591 return -EINVAL;
592 }
593
594 ++c;
595 --size;
596 if (strnchr(c, size, ':') != NULL) {
ee999d8b 597 family = NFPROTO_IPV6;
079aa88f
JE
598 succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
599 } else {
ee999d8b 600 family = NFPROTO_IPV4;
079aa88f
JE
601 succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
602 }
603
604 if (!succ) {
8bee4bad 605 pr_info("illegal address written to procfs\n");
079aa88f
JE
606 return -EINVAL;
607 }
608
609 spin_lock_bh(&recent_lock);
610 e = recent_entry_lookup(t, &addr, family, 0);
611 if (e == NULL) {
612 if (add)
613 recent_entry_init(t, &addr, family, 0);
614 } else {
615 if (add)
616 recent_entry_update(t, e);
617 else
618 recent_entry_remove(t, e);
619 }
620 spin_unlock_bh(&recent_lock);
621 /* Note we removed one above */
622 *loff += size + 1;
623 return size + 1;
624}
625
626static const struct file_operations recent_mt_fops = {
627 .open = recent_seq_open,
628 .read = seq_read,
629 .write = recent_mt_proc_write,
630 .release = seq_release_private,
631 .owner = THIS_MODULE,
6038f373 632 .llseek = seq_lseek,
079aa88f 633};
7d07d563
AD
634
635static int __net_init recent_proc_net_init(struct net *net)
636{
637 struct recent_net *recent_net = recent_pernet(net);
638
639 recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
640 if (!recent_net->xt_recent)
641 return -ENOMEM;
7d07d563
AD
642 return 0;
643}
644
645static void __net_exit recent_proc_net_exit(struct net *net)
646{
665e205c
VL
647 struct recent_net *recent_net = recent_pernet(net);
648 struct recent_table *t;
649
650 /* recent_net_exit() is called before recent_mt_destroy(). Make sure
651 * that the parent xt_recent proc entry is is empty before trying to
652 * remove it.
653 */
654 spin_lock_bh(&recent_lock);
655 list_for_each_entry(t, &recent_net->tables, list)
656 remove_proc_entry(t->name, recent_net->xt_recent);
657
658 recent_net->xt_recent = NULL;
659 spin_unlock_bh(&recent_lock);
660
ece31ffd 661 remove_proc_entry("xt_recent", net->proc_net);
7d07d563
AD
662}
663#else
664static inline int recent_proc_net_init(struct net *net)
665{
666 return 0;
667}
668
669static inline void recent_proc_net_exit(struct net *net)
670{
671}
1da177e4 672#endif /* CONFIG_PROC_FS */
1da177e4 673
7d07d563
AD
674static int __net_init recent_net_init(struct net *net)
675{
676 struct recent_net *recent_net = recent_pernet(net);
677
678 INIT_LIST_HEAD(&recent_net->tables);
679 return recent_proc_net_init(net);
680}
681
682static void __net_exit recent_net_exit(struct net *net)
683{
7d07d563
AD
684 recent_proc_net_exit(net);
685}
686
687static struct pernet_operations recent_net_ops = {
688 .init = recent_net_init,
689 .exit = recent_net_exit,
690 .id = &recent_net_id,
691 .size = sizeof(struct recent_net),
692};
693
079aa88f
JE
694static struct xt_match recent_mt_reg[] __read_mostly = {
695 {
696 .name = "recent",
697 .revision = 0,
ee999d8b 698 .family = NFPROTO_IPV4,
079aa88f
JE
699 .match = recent_mt,
700 .matchsize = sizeof(struct xt_recent_mtinfo),
efdedd54 701 .checkentry = recent_mt_check_v0,
079aa88f
JE
702 .destroy = recent_mt_destroy,
703 .me = THIS_MODULE,
704 },
705 {
706 .name = "recent",
707 .revision = 0,
ee999d8b 708 .family = NFPROTO_IPV6,
079aa88f
JE
709 .match = recent_mt,
710 .matchsize = sizeof(struct xt_recent_mtinfo),
efdedd54
DF
711 .checkentry = recent_mt_check_v0,
712 .destroy = recent_mt_destroy,
713 .me = THIS_MODULE,
714 },
715 {
716 .name = "recent",
717 .revision = 1,
718 .family = NFPROTO_IPV4,
719 .match = recent_mt,
720 .matchsize = sizeof(struct xt_recent_mtinfo_v1),
721 .checkentry = recent_mt_check_v1,
079aa88f
JE
722 .destroy = recent_mt_destroy,
723 .me = THIS_MODULE,
724 },
efdedd54
DF
725 {
726 .name = "recent",
727 .revision = 1,
728 .family = NFPROTO_IPV6,
729 .match = recent_mt,
730 .matchsize = sizeof(struct xt_recent_mtinfo_v1),
731 .checkentry = recent_mt_check_v1,
732 .destroy = recent_mt_destroy,
733 .me = THIS_MODULE,
734 }
1da177e4
LT
735};
736
d3c5ee6d 737static int __init recent_mt_init(void)
1da177e4 738{
404bdbfd 739 int err;
1da177e4 740
abc86d0f
FW
741 BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS);
742
743 if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
404bdbfd
PM
744 return -EINVAL;
745 ip_list_hash_size = 1 << fls(ip_list_tot);
1da177e4 746
7d07d563 747 err = register_pernet_subsys(&recent_net_ops);
1da177e4 748 if (err)
404bdbfd 749 return err;
7d07d563
AD
750 err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
751 if (err)
752 unregister_pernet_subsys(&recent_net_ops);
1da177e4
LT
753 return err;
754}
755
d3c5ee6d 756static void __exit recent_mt_exit(void)
1da177e4 757{
079aa88f 758 xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
7d07d563 759 unregister_pernet_subsys(&recent_net_ops);
1da177e4
LT
760}
761
d3c5ee6d
JE
762module_init(recent_mt_init);
763module_exit(recent_mt_exit);