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