1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
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>
17 #include <net/net_namespace.h>
19 #include <linux/sysctl.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_l3proto.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_expect.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_acct.h>
30 MODULE_LICENSE("GPL");
34 print_tuple(struct seq_file
*s
, const struct nf_conntrack_tuple
*tuple
,
35 const struct nf_conntrack_l3proto
*l3proto
,
36 const struct nf_conntrack_l4proto
*l4proto
)
38 return l3proto
->print_tuple(s
, tuple
) || l4proto
->print_tuple(s
, tuple
);
40 EXPORT_SYMBOL_GPL(print_tuple
);
42 struct ct_iter_state
{
43 struct seq_net_private p
;
47 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
49 struct net
*net
= seq_file_net(seq
);
50 struct ct_iter_state
*st
= seq
->private;
51 struct hlist_nulls_node
*n
;
54 st
->bucket
< nf_conntrack_htable_size
;
56 n
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
63 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
64 struct hlist_nulls_node
*head
)
66 struct net
*net
= seq_file_net(seq
);
67 struct ct_iter_state
*st
= seq
->private;
69 head
= rcu_dereference(head
->next
);
70 while (is_a_nulls(head
)) {
71 if (likely(get_nulls_value(head
) == st
->bucket
)) {
72 if (++st
->bucket
>= nf_conntrack_htable_size
)
75 head
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
80 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
82 struct hlist_nulls_node
*head
= ct_get_first(seq
);
85 while (pos
&& (head
= ct_get_next(seq
, head
)))
87 return pos
? NULL
: head
;
90 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
94 return ct_get_idx(seq
, *pos
);
97 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
100 return ct_get_next(s
, v
);
103 static void ct_seq_stop(struct seq_file
*s
, void *v
)
109 /* return 0 on success, 1 in case of error */
110 static int ct_seq_show(struct seq_file
*s
, void *v
)
112 struct nf_conntrack_tuple_hash
*hash
= v
;
113 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
114 const struct nf_conntrack_l3proto
*l3proto
;
115 const struct nf_conntrack_l4proto
*l4proto
;
119 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
122 /* we only want to print DIR_ORIGINAL */
123 if (NF_CT_DIRECTION(hash
))
126 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
127 NF_CT_ASSERT(l3proto
);
128 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
129 NF_CT_ASSERT(l4proto
);
132 if (seq_printf(s
, "%-8s %u %-8s %u %ld ",
133 l3proto
->name
, nf_ct_l3num(ct
),
134 l4proto
->name
, nf_ct_protonum(ct
),
135 timer_pending(&ct
->timeout
)
136 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
139 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
142 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
146 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
149 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
150 if (seq_printf(s
, "[UNREPLIED] "))
153 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
157 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
160 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
161 if (seq_printf(s
, "[ASSURED] "))
164 #if defined(CONFIG_NF_CONNTRACK_MARK)
165 if (seq_printf(s
, "mark=%u ", ct
->mark
))
169 #ifdef CONFIG_NF_CONNTRACK_SECMARK
170 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
174 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
183 static const struct seq_operations ct_seq_ops
= {
184 .start
= ct_seq_start
,
190 static int ct_open(struct inode
*inode
, struct file
*file
)
192 return seq_open_net(inode
, file
, &ct_seq_ops
,
193 sizeof(struct ct_iter_state
));
196 static const struct file_operations ct_file_ops
= {
197 .owner
= THIS_MODULE
,
201 .release
= seq_release_net
,
204 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
206 struct net
*net
= seq_file_net(seq
);
210 return SEQ_START_TOKEN
;
212 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
213 if (!cpu_possible(cpu
))
216 return per_cpu_ptr(net
->ct
.stat
, cpu
);
222 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
224 struct net
*net
= seq_file_net(seq
);
227 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
228 if (!cpu_possible(cpu
))
231 return per_cpu_ptr(net
->ct
.stat
, cpu
);
237 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
241 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
243 struct net
*net
= seq_file_net(seq
);
244 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
245 const struct ip_conntrack_stat
*st
= v
;
247 if (v
== SEQ_START_TOKEN
) {
248 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");
252 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
253 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
275 static const struct seq_operations ct_cpu_seq_ops
= {
276 .start
= ct_cpu_seq_start
,
277 .next
= ct_cpu_seq_next
,
278 .stop
= ct_cpu_seq_stop
,
279 .show
= ct_cpu_seq_show
,
282 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
284 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
285 sizeof(struct seq_net_private
));
288 static const struct file_operations ct_cpu_seq_fops
= {
289 .owner
= THIS_MODULE
,
290 .open
= ct_cpu_seq_open
,
293 .release
= seq_release_net
,
296 static int nf_conntrack_standalone_init_proc(struct net
*net
)
298 struct proc_dir_entry
*pde
;
300 pde
= proc_net_fops_create(net
, "nf_conntrack", 0440, &ct_file_ops
);
302 goto out_nf_conntrack
;
304 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
307 goto out_stat_nf_conntrack
;
310 out_stat_nf_conntrack
:
311 proc_net_remove(net
, "nf_conntrack");
316 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
318 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
319 proc_net_remove(net
, "nf_conntrack");
322 static int nf_conntrack_standalone_init_proc(struct net
*net
)
327 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
330 #endif /* CONFIG_PROC_FS */
335 /* Log invalid packets of a given protocol */
336 static int log_invalid_proto_min
= 0;
337 static int log_invalid_proto_max
= 255;
339 static struct ctl_table_header
*nf_ct_netfilter_header
;
341 static ctl_table nf_ct_sysctl_table
[] = {
343 .ctl_name
= NET_NF_CONNTRACK_MAX
,
344 .procname
= "nf_conntrack_max",
345 .data
= &nf_conntrack_max
,
346 .maxlen
= sizeof(int),
348 .proc_handler
= proc_dointvec
,
351 .ctl_name
= NET_NF_CONNTRACK_COUNT
,
352 .procname
= "nf_conntrack_count",
353 .data
= &init_net
.ct
.count
,
354 .maxlen
= sizeof(int),
356 .proc_handler
= proc_dointvec
,
359 .ctl_name
= NET_NF_CONNTRACK_BUCKETS
,
360 .procname
= "nf_conntrack_buckets",
361 .data
= &nf_conntrack_htable_size
,
362 .maxlen
= sizeof(unsigned int),
364 .proc_handler
= proc_dointvec
,
367 .ctl_name
= NET_NF_CONNTRACK_CHECKSUM
,
368 .procname
= "nf_conntrack_checksum",
369 .data
= &init_net
.ct
.sysctl_checksum
,
370 .maxlen
= sizeof(unsigned int),
372 .proc_handler
= proc_dointvec
,
375 .ctl_name
= NET_NF_CONNTRACK_LOG_INVALID
,
376 .procname
= "nf_conntrack_log_invalid",
377 .data
= &init_net
.ct
.sysctl_log_invalid
,
378 .maxlen
= sizeof(unsigned int),
380 .proc_handler
= proc_dointvec_minmax
,
381 .strategy
= sysctl_intvec
,
382 .extra1
= &log_invalid_proto_min
,
383 .extra2
= &log_invalid_proto_max
,
386 .ctl_name
= CTL_UNNUMBERED
,
387 .procname
= "nf_conntrack_expect_max",
388 .data
= &nf_ct_expect_max
,
389 .maxlen
= sizeof(int),
391 .proc_handler
= proc_dointvec
,
396 #define NET_NF_CONNTRACK_MAX 2089
398 static ctl_table nf_ct_netfilter_table
[] = {
400 .ctl_name
= NET_NF_CONNTRACK_MAX
,
401 .procname
= "nf_conntrack_max",
402 .data
= &nf_conntrack_max
,
403 .maxlen
= sizeof(int),
405 .proc_handler
= proc_dointvec
,
410 static struct ctl_path nf_ct_path
[] = {
411 { .procname
= "net", .ctl_name
= CTL_NET
, },
415 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
417 struct ctl_table
*table
;
419 if (net_eq(net
, &init_net
)) {
420 nf_ct_netfilter_header
=
421 register_sysctl_paths(nf_ct_path
, nf_ct_netfilter_table
);
422 if (!nf_ct_netfilter_header
)
426 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
431 table
[1].data
= &net
->ct
.count
;
432 table
[3].data
= &net
->ct
.sysctl_checksum
;
433 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
435 net
->ct
.sysctl_header
= register_net_sysctl_table(net
,
436 nf_net_netfilter_sysctl_path
, table
);
437 if (!net
->ct
.sysctl_header
)
438 goto out_unregister_netfilter
;
442 out_unregister_netfilter
:
445 if (net_eq(net
, &init_net
))
446 unregister_sysctl_table(nf_ct_netfilter_header
);
448 printk("nf_conntrack: can't register to sysctl.\n");
452 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
454 struct ctl_table
*table
;
456 if (net_eq(net
, &init_net
))
457 unregister_sysctl_table(nf_ct_netfilter_header
);
458 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
459 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
463 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
468 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
471 #endif /* CONFIG_SYSCTL */
473 static int nf_conntrack_net_init(struct net
*net
)
477 ret
= nf_conntrack_init(net
);
480 ret
= nf_conntrack_standalone_init_proc(net
);
483 net
->ct
.sysctl_checksum
= 1;
484 net
->ct
.sysctl_log_invalid
= 0;
485 ret
= nf_conntrack_standalone_init_sysctl(net
);
491 nf_conntrack_standalone_fini_proc(net
);
493 nf_conntrack_cleanup(net
);
498 static void nf_conntrack_net_exit(struct net
*net
)
500 nf_conntrack_standalone_fini_sysctl(net
);
501 nf_conntrack_standalone_fini_proc(net
);
502 nf_conntrack_cleanup(net
);
505 static struct pernet_operations nf_conntrack_net_ops
= {
506 .init
= nf_conntrack_net_init
,
507 .exit
= nf_conntrack_net_exit
,
510 static int __init
nf_conntrack_standalone_init(void)
512 return register_pernet_subsys(&nf_conntrack_net_ops
);
515 static void __exit
nf_conntrack_standalone_fini(void)
517 unregister_pernet_subsys(&nf_conntrack_net_ops
);
520 module_init(nf_conntrack_standalone_init
);
521 module_exit(nf_conntrack_standalone_fini
);
523 /* Some modules need us, but don't depend directly on any symbol.
524 They should call this. */
525 void need_conntrack(void)
528 EXPORT_SYMBOL_GPL(need_conntrack
);