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
{
46 static struct hlist_node
*ct_get_first(struct seq_file
*seq
)
48 struct ct_iter_state
*st
= seq
->private;
52 st
->bucket
< nf_conntrack_htable_size
;
54 n
= rcu_dereference(nf_conntrack_hash
[st
->bucket
].first
);
61 static struct hlist_node
*ct_get_next(struct seq_file
*seq
,
62 struct hlist_node
*head
)
64 struct ct_iter_state
*st
= seq
->private;
66 head
= rcu_dereference(head
->next
);
67 while (head
== NULL
) {
68 if (++st
->bucket
>= nf_conntrack_htable_size
)
70 head
= rcu_dereference(nf_conntrack_hash
[st
->bucket
].first
);
75 static struct hlist_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
77 struct hlist_node
*head
= ct_get_first(seq
);
80 while (pos
&& (head
= ct_get_next(seq
, head
)))
82 return pos
? NULL
: head
;
85 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
89 return ct_get_idx(seq
, *pos
);
92 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
95 return ct_get_next(s
, v
);
98 static void ct_seq_stop(struct seq_file
*s
, void *v
)
104 /* return 0 on success, 1 in case of error */
105 static int ct_seq_show(struct seq_file
*s
, void *v
)
107 const struct nf_conntrack_tuple_hash
*hash
= v
;
108 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
109 const struct nf_conntrack_l3proto
*l3proto
;
110 const struct nf_conntrack_l4proto
*l4proto
;
114 /* we only want to print DIR_ORIGINAL */
115 if (NF_CT_DIRECTION(hash
))
118 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
119 NF_CT_ASSERT(l3proto
);
120 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
121 NF_CT_ASSERT(l4proto
);
123 if (seq_printf(s
, "%-8s %u %-8s %u %ld ",
124 l3proto
->name
, nf_ct_l3num(ct
),
125 l4proto
->name
, nf_ct_protonum(ct
),
126 timer_pending(&ct
->timeout
)
127 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
130 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
133 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
137 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
140 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
141 if (seq_printf(s
, "[UNREPLIED] "))
144 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
148 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
151 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
152 if (seq_printf(s
, "[ASSURED] "))
155 #if defined(CONFIG_NF_CONNTRACK_MARK)
156 if (seq_printf(s
, "mark=%u ", ct
->mark
))
160 #ifdef CONFIG_NF_CONNTRACK_SECMARK
161 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
165 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
171 static const struct seq_operations ct_seq_ops
= {
172 .start
= ct_seq_start
,
178 static int ct_open(struct inode
*inode
, struct file
*file
)
180 return seq_open_private(file
, &ct_seq_ops
,
181 sizeof(struct ct_iter_state
));
184 static const struct file_operations ct_file_ops
= {
185 .owner
= THIS_MODULE
,
189 .release
= seq_release_private
,
192 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
197 return SEQ_START_TOKEN
;
199 for (cpu
= *pos
-1; cpu
< NR_CPUS
; ++cpu
) {
200 if (!cpu_possible(cpu
))
203 return &per_cpu(nf_conntrack_stat
, cpu
);
209 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
213 for (cpu
= *pos
; cpu
< NR_CPUS
; ++cpu
) {
214 if (!cpu_possible(cpu
))
217 return &per_cpu(nf_conntrack_stat
, cpu
);
223 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
227 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
229 unsigned int nr_conntracks
= atomic_read(&nf_conntrack_count
);
230 const struct ip_conntrack_stat
*st
= v
;
232 if (v
== SEQ_START_TOKEN
) {
233 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");
237 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
238 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
260 static const struct seq_operations ct_cpu_seq_ops
= {
261 .start
= ct_cpu_seq_start
,
262 .next
= ct_cpu_seq_next
,
263 .stop
= ct_cpu_seq_stop
,
264 .show
= ct_cpu_seq_show
,
267 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
269 return seq_open(file
, &ct_cpu_seq_ops
);
272 static const struct file_operations ct_cpu_seq_fops
= {
273 .owner
= THIS_MODULE
,
274 .open
= ct_cpu_seq_open
,
277 .release
= seq_release
,
280 static int nf_conntrack_standalone_init_proc(void)
282 struct proc_dir_entry
*pde
;
284 pde
= proc_net_fops_create(&init_net
, "nf_conntrack", 0440, &ct_file_ops
);
286 goto out_nf_conntrack
;
288 pde
= proc_create("nf_conntrack", S_IRUGO
, init_net
.proc_net_stat
,
291 goto out_stat_nf_conntrack
;
294 out_stat_nf_conntrack
:
295 proc_net_remove(&init_net
, "nf_conntrack");
300 static void nf_conntrack_standalone_fini_proc(void)
302 remove_proc_entry("nf_conntrack", init_net
.proc_net_stat
);
303 proc_net_remove(&init_net
, "nf_conntrack");
306 static int nf_conntrack_standalone_init_proc(void)
311 static void nf_conntrack_standalone_fini_proc(void)
314 #endif /* CONFIG_PROC_FS */
318 int nf_conntrack_checksum __read_mostly
= 1;
319 EXPORT_SYMBOL_GPL(nf_conntrack_checksum
);
322 /* Log invalid packets of a given protocol */
323 static int log_invalid_proto_min
= 0;
324 static int log_invalid_proto_max
= 255;
326 static struct ctl_table_header
*nf_ct_sysctl_header
;
328 static ctl_table nf_ct_sysctl_table
[] = {
330 .ctl_name
= NET_NF_CONNTRACK_MAX
,
331 .procname
= "nf_conntrack_max",
332 .data
= &nf_conntrack_max
,
333 .maxlen
= sizeof(int),
335 .proc_handler
= &proc_dointvec
,
338 .ctl_name
= NET_NF_CONNTRACK_COUNT
,
339 .procname
= "nf_conntrack_count",
340 .data
= &nf_conntrack_count
,
341 .maxlen
= sizeof(int),
343 .proc_handler
= &proc_dointvec
,
346 .ctl_name
= NET_NF_CONNTRACK_BUCKETS
,
347 .procname
= "nf_conntrack_buckets",
348 .data
= &nf_conntrack_htable_size
,
349 .maxlen
= sizeof(unsigned int),
351 .proc_handler
= &proc_dointvec
,
354 .ctl_name
= NET_NF_CONNTRACK_CHECKSUM
,
355 .procname
= "nf_conntrack_checksum",
356 .data
= &nf_conntrack_checksum
,
357 .maxlen
= sizeof(unsigned int),
359 .proc_handler
= &proc_dointvec
,
362 .ctl_name
= NET_NF_CONNTRACK_LOG_INVALID
,
363 .procname
= "nf_conntrack_log_invalid",
364 .data
= &nf_ct_log_invalid
,
365 .maxlen
= sizeof(unsigned int),
367 .proc_handler
= &proc_dointvec_minmax
,
368 .strategy
= &sysctl_intvec
,
369 .extra1
= &log_invalid_proto_min
,
370 .extra2
= &log_invalid_proto_max
,
373 .ctl_name
= CTL_UNNUMBERED
,
374 .procname
= "nf_conntrack_expect_max",
375 .data
= &nf_ct_expect_max
,
376 .maxlen
= sizeof(int),
378 .proc_handler
= &proc_dointvec
,
383 #define NET_NF_CONNTRACK_MAX 2089
385 static ctl_table nf_ct_netfilter_table
[] = {
387 .ctl_name
= NET_NETFILTER
,
388 .procname
= "netfilter",
390 .child
= nf_ct_sysctl_table
,
393 .ctl_name
= NET_NF_CONNTRACK_MAX
,
394 .procname
= "nf_conntrack_max",
395 .data
= &nf_conntrack_max
,
396 .maxlen
= sizeof(int),
398 .proc_handler
= &proc_dointvec
,
403 static struct ctl_path nf_ct_path
[] = {
404 { .procname
= "net", .ctl_name
= CTL_NET
, },
408 EXPORT_SYMBOL_GPL(nf_ct_log_invalid
);
410 static int nf_conntrack_standalone_init_sysctl(void)
412 nf_ct_sysctl_header
=
413 register_sysctl_paths(nf_ct_path
, nf_ct_netfilter_table
);
414 if (nf_ct_sysctl_header
== NULL
) {
415 printk("nf_conntrack: can't register to sysctl.\n");
422 static void nf_conntrack_standalone_fini_sysctl(void)
424 unregister_sysctl_table(nf_ct_sysctl_header
);
427 static int nf_conntrack_standalone_init_sysctl(void)
432 static void nf_conntrack_standalone_fini_sysctl(void)
435 #endif /* CONFIG_SYSCTL */
437 static int __init
nf_conntrack_standalone_init(void)
441 ret
= nf_conntrack_init();
444 ret
= nf_conntrack_standalone_init_proc();
447 ret
= nf_conntrack_standalone_init_sysctl();
453 nf_conntrack_standalone_fini_proc();
455 nf_conntrack_cleanup();
460 static void __exit
nf_conntrack_standalone_fini(void)
462 nf_conntrack_standalone_fini_sysctl();
463 nf_conntrack_standalone_fini_proc();
464 nf_conntrack_cleanup();
467 module_init(nf_conntrack_standalone_init
);
468 module_exit(nf_conntrack_standalone_fini
);
470 /* Some modules need us, but don't depend directly on any symbol.
471 They should call this. */
472 void need_conntrack(void)
475 EXPORT_SYMBOL_GPL(need_conntrack
);