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>
29 #include <net/netfilter/nf_conntrack_zones.h>
31 MODULE_LICENSE("GPL");
35 print_tuple(struct seq_file
*s
, const struct nf_conntrack_tuple
*tuple
,
36 const struct nf_conntrack_l3proto
*l3proto
,
37 const struct nf_conntrack_l4proto
*l4proto
)
39 return l3proto
->print_tuple(s
, tuple
) || l4proto
->print_tuple(s
, tuple
);
41 EXPORT_SYMBOL_GPL(print_tuple
);
43 struct ct_iter_state
{
44 struct seq_net_private p
;
48 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
50 struct net
*net
= seq_file_net(seq
);
51 struct ct_iter_state
*st
= seq
->private;
52 struct hlist_nulls_node
*n
;
55 st
->bucket
< net
->ct
.htable_size
;
57 n
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
64 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
65 struct hlist_nulls_node
*head
)
67 struct net
*net
= seq_file_net(seq
);
68 struct ct_iter_state
*st
= seq
->private;
70 head
= rcu_dereference(head
->next
);
71 while (is_a_nulls(head
)) {
72 if (likely(get_nulls_value(head
) == st
->bucket
)) {
73 if (++st
->bucket
>= net
->ct
.htable_size
)
76 head
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
81 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
83 struct hlist_nulls_node
*head
= ct_get_first(seq
);
86 while (pos
&& (head
= ct_get_next(seq
, head
)))
88 return pos
? NULL
: head
;
91 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
95 return ct_get_idx(seq
, *pos
);
98 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
101 return ct_get_next(s
, v
);
104 static void ct_seq_stop(struct seq_file
*s
, void *v
)
110 /* return 0 on success, 1 in case of error */
111 static int ct_seq_show(struct seq_file
*s
, void *v
)
113 struct nf_conntrack_tuple_hash
*hash
= v
;
114 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
115 const struct nf_conntrack_l3proto
*l3proto
;
116 const struct nf_conntrack_l4proto
*l4proto
;
120 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
123 /* we only want to print DIR_ORIGINAL */
124 if (NF_CT_DIRECTION(hash
))
127 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
128 NF_CT_ASSERT(l3proto
);
129 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
130 NF_CT_ASSERT(l4proto
);
133 if (seq_printf(s
, "%-8s %u %-8s %u %ld ",
134 l3proto
->name
, nf_ct_l3num(ct
),
135 l4proto
->name
, nf_ct_protonum(ct
),
136 timer_pending(&ct
->timeout
)
137 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
140 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
143 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
147 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
150 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
151 if (seq_printf(s
, "[UNREPLIED] "))
154 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
158 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
161 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
162 if (seq_printf(s
, "[ASSURED] "))
165 #if defined(CONFIG_NF_CONNTRACK_MARK)
166 if (seq_printf(s
, "mark=%u ", ct
->mark
))
170 #ifdef CONFIG_NF_CONNTRACK_SECMARK
171 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
175 #ifdef CONFIG_NF_CONNTRACK_ZONES
176 if (seq_printf(s
, "zone=%u ", nf_ct_zone(ct
)))
180 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
189 static const struct seq_operations ct_seq_ops
= {
190 .start
= ct_seq_start
,
196 static int ct_open(struct inode
*inode
, struct file
*file
)
198 return seq_open_net(inode
, file
, &ct_seq_ops
,
199 sizeof(struct ct_iter_state
));
202 static const struct file_operations ct_file_ops
= {
203 .owner
= THIS_MODULE
,
207 .release
= seq_release_net
,
210 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
212 struct net
*net
= seq_file_net(seq
);
216 return SEQ_START_TOKEN
;
218 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
219 if (!cpu_possible(cpu
))
222 return per_cpu_ptr(net
->ct
.stat
, cpu
);
228 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
230 struct net
*net
= seq_file_net(seq
);
233 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
234 if (!cpu_possible(cpu
))
237 return per_cpu_ptr(net
->ct
.stat
, cpu
);
243 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
247 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
249 struct net
*net
= seq_file_net(seq
);
250 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
251 const struct ip_conntrack_stat
*st
= v
;
253 if (v
== SEQ_START_TOKEN
) {
254 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");
258 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
259 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
281 static const struct seq_operations ct_cpu_seq_ops
= {
282 .start
= ct_cpu_seq_start
,
283 .next
= ct_cpu_seq_next
,
284 .stop
= ct_cpu_seq_stop
,
285 .show
= ct_cpu_seq_show
,
288 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
290 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
291 sizeof(struct seq_net_private
));
294 static const struct file_operations ct_cpu_seq_fops
= {
295 .owner
= THIS_MODULE
,
296 .open
= ct_cpu_seq_open
,
299 .release
= seq_release_net
,
302 static int nf_conntrack_standalone_init_proc(struct net
*net
)
304 struct proc_dir_entry
*pde
;
306 pde
= proc_net_fops_create(net
, "nf_conntrack", 0440, &ct_file_ops
);
308 goto out_nf_conntrack
;
310 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
313 goto out_stat_nf_conntrack
;
316 out_stat_nf_conntrack
:
317 proc_net_remove(net
, "nf_conntrack");
322 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
324 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
325 proc_net_remove(net
, "nf_conntrack");
328 static int nf_conntrack_standalone_init_proc(struct net
*net
)
333 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
336 #endif /* CONFIG_PROC_FS */
341 /* Log invalid packets of a given protocol */
342 static int log_invalid_proto_min
= 0;
343 static int log_invalid_proto_max
= 255;
345 static struct ctl_table_header
*nf_ct_netfilter_header
;
347 static ctl_table nf_ct_sysctl_table
[] = {
349 .procname
= "nf_conntrack_max",
350 .data
= &nf_conntrack_max
,
351 .maxlen
= sizeof(int),
353 .proc_handler
= proc_dointvec
,
356 .procname
= "nf_conntrack_count",
357 .data
= &init_net
.ct
.count
,
358 .maxlen
= sizeof(int),
360 .proc_handler
= proc_dointvec
,
363 .procname
= "nf_conntrack_buckets",
364 .data
= &init_net
.ct
.htable_size
,
365 .maxlen
= sizeof(unsigned int),
367 .proc_handler
= proc_dointvec
,
370 .procname
= "nf_conntrack_checksum",
371 .data
= &init_net
.ct
.sysctl_checksum
,
372 .maxlen
= sizeof(unsigned int),
374 .proc_handler
= proc_dointvec
,
377 .procname
= "nf_conntrack_log_invalid",
378 .data
= &init_net
.ct
.sysctl_log_invalid
,
379 .maxlen
= sizeof(unsigned int),
381 .proc_handler
= proc_dointvec_minmax
,
382 .extra1
= &log_invalid_proto_min
,
383 .extra2
= &log_invalid_proto_max
,
386 .procname
= "nf_conntrack_expect_max",
387 .data
= &nf_ct_expect_max
,
388 .maxlen
= sizeof(int),
390 .proc_handler
= proc_dointvec
,
395 #define NET_NF_CONNTRACK_MAX 2089
397 static ctl_table nf_ct_netfilter_table
[] = {
399 .procname
= "nf_conntrack_max",
400 .data
= &nf_conntrack_max
,
401 .maxlen
= sizeof(int),
403 .proc_handler
= proc_dointvec
,
408 static struct ctl_path nf_ct_path
[] = {
409 { .procname
= "net", },
413 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
415 struct ctl_table
*table
;
417 if (net_eq(net
, &init_net
)) {
418 nf_ct_netfilter_header
=
419 register_sysctl_paths(nf_ct_path
, nf_ct_netfilter_table
);
420 if (!nf_ct_netfilter_header
)
424 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
429 table
[1].data
= &net
->ct
.count
;
430 table
[2].data
= &net
->ct
.htable_size
;
431 table
[3].data
= &net
->ct
.sysctl_checksum
;
432 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
434 net
->ct
.sysctl_header
= register_net_sysctl_table(net
,
435 nf_net_netfilter_sysctl_path
, table
);
436 if (!net
->ct
.sysctl_header
)
437 goto out_unregister_netfilter
;
441 out_unregister_netfilter
:
444 if (net_eq(net
, &init_net
))
445 unregister_sysctl_table(nf_ct_netfilter_header
);
447 printk("nf_conntrack: can't register to sysctl.\n");
451 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
453 struct ctl_table
*table
;
455 if (net_eq(net
, &init_net
))
456 unregister_sysctl_table(nf_ct_netfilter_header
);
457 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
458 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
462 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
467 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
470 #endif /* CONFIG_SYSCTL */
472 static int nf_conntrack_net_init(struct net
*net
)
476 ret
= nf_conntrack_init(net
);
479 ret
= nf_conntrack_standalone_init_proc(net
);
482 net
->ct
.sysctl_checksum
= 1;
483 net
->ct
.sysctl_log_invalid
= 0;
484 ret
= nf_conntrack_standalone_init_sysctl(net
);
490 nf_conntrack_standalone_fini_proc(net
);
492 nf_conntrack_cleanup(net
);
497 static void nf_conntrack_net_exit(struct net
*net
)
499 nf_conntrack_standalone_fini_sysctl(net
);
500 nf_conntrack_standalone_fini_proc(net
);
501 nf_conntrack_cleanup(net
);
504 static struct pernet_operations nf_conntrack_net_ops
= {
505 .init
= nf_conntrack_net_init
,
506 .exit
= nf_conntrack_net_exit
,
509 static int __init
nf_conntrack_standalone_init(void)
511 return register_pernet_subsys(&nf_conntrack_net_ops
);
514 static void __exit
nf_conntrack_standalone_fini(void)
516 unregister_pernet_subsys(&nf_conntrack_net_ops
);
519 module_init(nf_conntrack_standalone_init
);
520 module_exit(nf_conntrack_standalone_fini
);
522 /* Some modules need us, but don't depend directly on any symbol.
523 They should call this. */
524 void need_conntrack(void)
527 EXPORT_SYMBOL_GPL(need_conntrack
);