netfilter: netns nf_conntrack: per-netns net.netfilter.nf_conntrack_count sysctl
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_standalone.c
CommitLineData
9fb9cbb1
YK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9fb9cbb1
YK
7 */
8
9fb9cbb1
YK
9#include <linux/types.h>
10#include <linux/netfilter.h>
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/proc_fs.h>
14#include <linux/seq_file.h>
15#include <linux/percpu.h>
16#include <linux/netdevice.h>
457c4cbc 17#include <net/net_namespace.h>
9fb9cbb1
YK
18#ifdef CONFIG_SYSCTL
19#include <linux/sysctl.h>
20#endif
21
9fb9cbb1 22#include <net/netfilter/nf_conntrack.h>
f6180121 23#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 24#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 25#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 26#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 27#include <net/netfilter/nf_conntrack_helper.h>
58401572 28#include <net/netfilter/nf_conntrack_acct.h>
9fb9cbb1 29
9fb9cbb1
YK
30MODULE_LICENSE("GPL");
31
9fb9cbb1 32#ifdef CONFIG_PROC_FS
77ab9cff 33int
9fb9cbb1 34print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
32948588
JE
35 const struct nf_conntrack_l3proto *l3proto,
36 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 37{
605dcad6 38 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
9fb9cbb1 39}
e4bd8bce 40EXPORT_SYMBOL_GPL(print_tuple);
9fb9cbb1 41
9fb9cbb1 42struct ct_iter_state {
b2ce2c74 43 struct seq_net_private p;
9fb9cbb1
YK
44 unsigned int bucket;
45};
46
f205c5e0 47static struct hlist_node *ct_get_first(struct seq_file *seq)
9fb9cbb1 48{
b2ce2c74 49 struct net *net = seq_file_net(seq);
9fb9cbb1 50 struct ct_iter_state *st = seq->private;
76507f69 51 struct hlist_node *n;
9fb9cbb1
YK
52
53 for (st->bucket = 0;
54 st->bucket < nf_conntrack_htable_size;
55 st->bucket++) {
b2ce2c74 56 n = rcu_dereference(net->ct.hash[st->bucket].first);
76507f69
PM
57 if (n)
58 return n;
9fb9cbb1
YK
59 }
60 return NULL;
61}
62
f205c5e0
PM
63static struct hlist_node *ct_get_next(struct seq_file *seq,
64 struct hlist_node *head)
9fb9cbb1 65{
b2ce2c74 66 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
67 struct ct_iter_state *st = seq->private;
68
76507f69 69 head = rcu_dereference(head->next);
f205c5e0 70 while (head == NULL) {
9fb9cbb1
YK
71 if (++st->bucket >= nf_conntrack_htable_size)
72 return NULL;
b2ce2c74 73 head = rcu_dereference(net->ct.hash[st->bucket].first);
9fb9cbb1
YK
74 }
75 return head;
76}
77
f205c5e0 78static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
9fb9cbb1 79{
f205c5e0 80 struct hlist_node *head = ct_get_first(seq);
9fb9cbb1
YK
81
82 if (head)
83 while (pos && (head = ct_get_next(seq, head)))
84 pos--;
85 return pos ? NULL : head;
86}
87
88static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 89 __acquires(RCU)
9fb9cbb1 90{
76507f69 91 rcu_read_lock();
9fb9cbb1
YK
92 return ct_get_idx(seq, *pos);
93}
94
95static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
96{
97 (*pos)++;
98 return ct_get_next(s, v);
99}
100
101static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 102 __releases(RCU)
9fb9cbb1 103{
76507f69 104 rcu_read_unlock();
9fb9cbb1
YK
105}
106
107/* return 0 on success, 1 in case of error */
108static int ct_seq_show(struct seq_file *s, void *v)
109{
110 const struct nf_conntrack_tuple_hash *hash = v;
c88130bc 111 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
112 const struct nf_conntrack_l3proto *l3proto;
113 const struct nf_conntrack_l4proto *l4proto;
9fb9cbb1 114
c88130bc 115 NF_CT_ASSERT(ct);
9fb9cbb1
YK
116
117 /* we only want to print DIR_ORIGINAL */
118 if (NF_CT_DIRECTION(hash))
119 return 0;
120
5e8fbe2a 121 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
9fb9cbb1 122 NF_CT_ASSERT(l3proto);
5e8fbe2a 123 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
605dcad6 124 NF_CT_ASSERT(l4proto);
9fb9cbb1
YK
125
126 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
5e8fbe2a
PM
127 l3proto->name, nf_ct_l3num(ct),
128 l4proto->name, nf_ct_protonum(ct),
c88130bc
PM
129 timer_pending(&ct->timeout)
130 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
9fb9cbb1
YK
131 return -ENOSPC;
132
c88130bc 133 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
9fb9cbb1
YK
134 return -ENOSPC;
135
c88130bc 136 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
605dcad6 137 l3proto, l4proto))
9fb9cbb1
YK
138 return -ENOSPC;
139
58401572 140 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
9fb9cbb1
YK
141 return -ENOSPC;
142
c88130bc 143 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
9fb9cbb1
YK
144 if (seq_printf(s, "[UNREPLIED] "))
145 return -ENOSPC;
146
c88130bc 147 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
605dcad6 148 l3proto, l4proto))
9fb9cbb1
YK
149 return -ENOSPC;
150
58401572 151 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
9fb9cbb1
YK
152 return -ENOSPC;
153
c88130bc 154 if (test_bit(IPS_ASSURED_BIT, &ct->status))
9fb9cbb1
YK
155 if (seq_printf(s, "[ASSURED] "))
156 return -ENOSPC;
157
158#if defined(CONFIG_NF_CONNTRACK_MARK)
c88130bc 159 if (seq_printf(s, "mark=%u ", ct->mark))
9fb9cbb1
YK
160 return -ENOSPC;
161#endif
162
7c9728c3 163#ifdef CONFIG_NF_CONNTRACK_SECMARK
c88130bc 164 if (seq_printf(s, "secmark=%u ", ct->secmark))
7c9728c3
JM
165 return -ENOSPC;
166#endif
167
c88130bc 168 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
9fb9cbb1 169 return -ENOSPC;
a5d29264 170
9fb9cbb1
YK
171 return 0;
172}
173
56b3d975 174static const struct seq_operations ct_seq_ops = {
9fb9cbb1
YK
175 .start = ct_seq_start,
176 .next = ct_seq_next,
177 .stop = ct_seq_stop,
178 .show = ct_seq_show
179};
180
181static int ct_open(struct inode *inode, struct file *file)
182{
b2ce2c74 183 return seq_open_net(inode, file, &ct_seq_ops,
e2da5913 184 sizeof(struct ct_iter_state));
9fb9cbb1
YK
185}
186
da7071d7 187static const struct file_operations ct_file_ops = {
9fb9cbb1
YK
188 .owner = THIS_MODULE,
189 .open = ct_open,
190 .read = seq_read,
191 .llseek = seq_lseek,
b2ce2c74 192 .release = seq_release_net,
9fb9cbb1
YK
193};
194
9fb9cbb1
YK
195static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
196{
8e9df801 197 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
198 int cpu;
199
200 if (*pos == 0)
201 return SEQ_START_TOKEN;
202
203 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
204 if (!cpu_possible(cpu))
205 continue;
206 *pos = cpu + 1;
8e9df801 207 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
208 }
209
210 return NULL;
211}
212
213static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
214{
8e9df801 215 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
216 int cpu;
217
218 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
219 if (!cpu_possible(cpu))
220 continue;
221 *pos = cpu + 1;
8e9df801 222 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
223 }
224
225 return NULL;
226}
227
228static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
229{
230}
231
232static int ct_cpu_seq_show(struct seq_file *seq, void *v)
233{
8e9df801
AD
234 struct net *net = seq_file_net(seq);
235 unsigned int nr_conntracks = atomic_read(&net->ct.count);
32948588 236 const struct ip_conntrack_stat *st = v;
9fb9cbb1
YK
237
238 if (v == SEQ_START_TOKEN) {
239 seq_printf(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete\n");
240 return 0;
241 }
242
243 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
244 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
245 nr_conntracks,
246 st->searched,
247 st->found,
248 st->new,
249 st->invalid,
250 st->ignore,
251 st->delete,
252 st->delete_list,
253 st->insert,
254 st->insert_failed,
255 st->drop,
256 st->early_drop,
257 st->error,
258
259 st->expect_new,
260 st->expect_create,
261 st->expect_delete
262 );
263 return 0;
264}
265
56b3d975 266static const struct seq_operations ct_cpu_seq_ops = {
9fb9cbb1
YK
267 .start = ct_cpu_seq_start,
268 .next = ct_cpu_seq_next,
269 .stop = ct_cpu_seq_stop,
270 .show = ct_cpu_seq_show,
271};
272
273static int ct_cpu_seq_open(struct inode *inode, struct file *file)
274{
8e9df801
AD
275 return seq_open_net(inode, file, &ct_cpu_seq_ops,
276 sizeof(struct seq_net_private));
9fb9cbb1
YK
277}
278
da7071d7 279static const struct file_operations ct_cpu_seq_fops = {
9fb9cbb1
YK
280 .owner = THIS_MODULE,
281 .open = ct_cpu_seq_open,
282 .read = seq_read,
283 .llseek = seq_lseek,
8e9df801 284 .release = seq_release_net,
9fb9cbb1 285};
b916f7d4 286
b2ce2c74 287static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
288{
289 struct proc_dir_entry *pde;
290
b2ce2c74 291 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
b916f7d4
AD
292 if (!pde)
293 goto out_nf_conntrack;
52c0e111 294
b2ce2c74 295 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
52c0e111 296 &ct_cpu_seq_fops);
b916f7d4
AD
297 if (!pde)
298 goto out_stat_nf_conntrack;
b916f7d4
AD
299 return 0;
300
301out_stat_nf_conntrack:
b2ce2c74 302 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
303out_nf_conntrack:
304 return -ENOMEM;
305}
306
b2ce2c74 307static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4 308{
b2ce2c74
AD
309 remove_proc_entry("nf_conntrack", net->proc_net_stat);
310 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
311}
312#else
b2ce2c74 313static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
314{
315 return 0;
316}
317
b2ce2c74 318static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4
AD
319{
320}
9fb9cbb1
YK
321#endif /* CONFIG_PROC_FS */
322
323/* Sysctl support */
324
94aec08e 325int nf_conntrack_checksum __read_mostly = 1;
13b18339 326EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
72b55823 327
9fb9cbb1 328#ifdef CONFIG_SYSCTL
9fb9cbb1
YK
329/* Log invalid packets of a given protocol */
330static int log_invalid_proto_min = 0;
331static int log_invalid_proto_max = 255;
332
9714be7d 333static struct ctl_table_header *nf_ct_netfilter_header;
9fb9cbb1
YK
334
335static ctl_table nf_ct_sysctl_table[] = {
336 {
337 .ctl_name = NET_NF_CONNTRACK_MAX,
338 .procname = "nf_conntrack_max",
339 .data = &nf_conntrack_max,
340 .maxlen = sizeof(int),
341 .mode = 0644,
342 .proc_handler = &proc_dointvec,
343 },
344 {
345 .ctl_name = NET_NF_CONNTRACK_COUNT,
346 .procname = "nf_conntrack_count",
49ac8713 347 .data = &init_net.ct.count,
9fb9cbb1
YK
348 .maxlen = sizeof(int),
349 .mode = 0444,
350 .proc_handler = &proc_dointvec,
351 },
352 {
353 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
354 .procname = "nf_conntrack_buckets",
355 .data = &nf_conntrack_htable_size,
356 .maxlen = sizeof(unsigned int),
357 .mode = 0444,
358 .proc_handler = &proc_dointvec,
359 },
39a27a35
PM
360 {
361 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
362 .procname = "nf_conntrack_checksum",
363 .data = &nf_conntrack_checksum,
364 .maxlen = sizeof(unsigned int),
365 .mode = 0644,
366 .proc_handler = &proc_dointvec,
367 },
9fb9cbb1
YK
368 {
369 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
370 .procname = "nf_conntrack_log_invalid",
371 .data = &nf_ct_log_invalid,
372 .maxlen = sizeof(unsigned int),
373 .mode = 0644,
374 .proc_handler = &proc_dointvec_minmax,
375 .strategy = &sysctl_intvec,
376 .extra1 = &log_invalid_proto_min,
377 .extra2 = &log_invalid_proto_max,
378 },
f264a7df
PM
379 {
380 .ctl_name = CTL_UNNUMBERED,
381 .procname = "nf_conntrack_expect_max",
382 .data = &nf_ct_expect_max,
383 .maxlen = sizeof(int),
384 .mode = 0644,
385 .proc_handler = &proc_dointvec,
386 },
9fb9cbb1
YK
387 { .ctl_name = 0 }
388};
389
390#define NET_NF_CONNTRACK_MAX 2089
391
392static ctl_table nf_ct_netfilter_table[] = {
9fb9cbb1
YK
393 {
394 .ctl_name = NET_NF_CONNTRACK_MAX,
395 .procname = "nf_conntrack_max",
396 .data = &nf_conntrack_max,
397 .maxlen = sizeof(int),
398 .mode = 0644,
399 .proc_handler = &proc_dointvec,
400 },
401 { .ctl_name = 0 }
402};
403
9e232495 404static struct ctl_path nf_ct_path[] = {
3d7cc2ba
PE
405 { .procname = "net", .ctl_name = CTL_NET, },
406 { }
9fb9cbb1 407};
3d7cc2ba 408
13b18339 409EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
b916f7d4 410
80250707 411static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4 412{
80250707
AD
413 struct ctl_table *table;
414
415 if (net_eq(net, &init_net)) {
416 nf_ct_netfilter_header =
417 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
418 if (!nf_ct_netfilter_header)
419 goto out;
420 }
421
422 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
423 GFP_KERNEL);
424 if (!table)
425 goto out_kmemdup;
426
427 table[1].data = &net->ct.count;
428
429 net->ct.sysctl_header = register_net_sysctl_table(net,
430 nf_net_netfilter_sysctl_path, table);
431 if (!net->ct.sysctl_header)
9714be7d
KPO
432 goto out_unregister_netfilter;
433
b916f7d4
AD
434 return 0;
435
9714be7d 436out_unregister_netfilter:
80250707
AD
437 kfree(table);
438out_kmemdup:
439 if (net_eq(net, &init_net))
440 unregister_sysctl_table(nf_ct_netfilter_header);
9714be7d
KPO
441out:
442 printk("nf_conntrack: can't register to sysctl.\n");
443 return -ENOMEM;
b916f7d4
AD
444}
445
80250707 446static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4 447{
80250707
AD
448 struct ctl_table *table;
449
450 if (net_eq(net, &init_net))
451 unregister_sysctl_table(nf_ct_netfilter_header);
452 table = net->ct.sysctl_header->ctl_table_arg;
453 unregister_net_sysctl_table(net->ct.sysctl_header);
454 kfree(table);
b916f7d4
AD
455}
456#else
80250707 457static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4
AD
458{
459 return 0;
460}
461
80250707 462static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4
AD
463{
464}
9fb9cbb1
YK
465#endif /* CONFIG_SYSCTL */
466
dfdb8d79
AD
467static int nf_conntrack_net_init(struct net *net)
468{
b2ce2c74
AD
469 int ret;
470
471 ret = nf_conntrack_init(net);
472 if (ret < 0)
473 goto out_init;
474 ret = nf_conntrack_standalone_init_proc(net);
475 if (ret < 0)
476 goto out_proc;
80250707
AD
477 ret = nf_conntrack_standalone_init_sysctl(net);
478 if (ret < 0)
479 goto out_sysctl;
b2ce2c74
AD
480 return 0;
481
80250707
AD
482out_sysctl:
483 nf_conntrack_standalone_fini_proc(net);
b2ce2c74
AD
484out_proc:
485 nf_conntrack_cleanup(net);
486out_init:
487 return ret;
dfdb8d79
AD
488}
489
490static void nf_conntrack_net_exit(struct net *net)
491{
80250707 492 nf_conntrack_standalone_fini_sysctl(net);
b2ce2c74 493 nf_conntrack_standalone_fini_proc(net);
dfdb8d79
AD
494 nf_conntrack_cleanup(net);
495}
496
497static struct pernet_operations nf_conntrack_net_ops = {
498 .init = nf_conntrack_net_init,
499 .exit = nf_conntrack_net_exit,
500};
501
65b4b4e8 502static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 503{
80250707 504 return register_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
505}
506
65b4b4e8 507static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 508{
dfdb8d79 509 unregister_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
510}
511
65b4b4e8
AM
512module_init(nf_conntrack_standalone_init);
513module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
514
515/* Some modules need us, but don't depend directly on any symbol.
516 They should call this. */
2e4e6a17 517void need_conntrack(void)
9fb9cbb1
YK
518{
519}
13b18339 520EXPORT_SYMBOL_GPL(need_conntrack);