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>
29 MODULE_LICENSE("GPL");
33 print_tuple(struct seq_file
*s
, const struct nf_conntrack_tuple
*tuple
,
34 const struct nf_conntrack_l3proto
*l3proto
,
35 const struct nf_conntrack_l4proto
*l4proto
)
37 return l3proto
->print_tuple(s
, tuple
) || l4proto
->print_tuple(s
, tuple
);
39 EXPORT_SYMBOL_GPL(print_tuple
);
41 #ifdef CONFIG_NF_CT_ACCT
43 seq_print_counters(struct seq_file
*s
,
44 const struct ip_conntrack_counter
*counter
)
46 return seq_printf(s
, "packets=%llu bytes=%llu ",
47 (unsigned long long)counter
->packets
,
48 (unsigned long long)counter
->bytes
);
51 #define seq_print_counters(x, y) 0
54 struct ct_iter_state
{
58 static struct hlist_node
*ct_get_first(struct seq_file
*seq
)
60 struct ct_iter_state
*st
= seq
->private;
64 st
->bucket
< nf_conntrack_htable_size
;
66 n
= rcu_dereference(nf_conntrack_hash
[st
->bucket
].first
);
73 static struct hlist_node
*ct_get_next(struct seq_file
*seq
,
74 struct hlist_node
*head
)
76 struct ct_iter_state
*st
= seq
->private;
78 head
= rcu_dereference(head
->next
);
79 while (head
== NULL
) {
80 if (++st
->bucket
>= nf_conntrack_htable_size
)
82 head
= rcu_dereference(nf_conntrack_hash
[st
->bucket
].first
);
87 static struct hlist_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
89 struct hlist_node
*head
= ct_get_first(seq
);
92 while (pos
&& (head
= ct_get_next(seq
, head
)))
94 return pos
? NULL
: head
;
97 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
101 return ct_get_idx(seq
, *pos
);
104 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
107 return ct_get_next(s
, v
);
110 static void ct_seq_stop(struct seq_file
*s
, void *v
)
116 /* return 0 on success, 1 in case of error */
117 static int ct_seq_show(struct seq_file
*s
, void *v
)
119 const struct nf_conntrack_tuple_hash
*hash
= v
;
120 const struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
121 const struct nf_conntrack_l3proto
*l3proto
;
122 const struct nf_conntrack_l4proto
*l4proto
;
126 /* we only want to print DIR_ORIGINAL */
127 if (NF_CT_DIRECTION(hash
))
130 l3proto
= __nf_ct_l3proto_find(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
133 NF_CT_ASSERT(l3proto
);
134 l4proto
= __nf_ct_l4proto_find(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
136 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
137 .tuple
.dst
.protonum
);
138 NF_CT_ASSERT(l4proto
);
140 if (seq_printf(s
, "%-8s %u %-8s %u %ld ",
142 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
,
144 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
,
145 timer_pending(&ct
->timeout
)
146 ? (long)(ct
->timeout
.expires
- jiffies
)/HZ
: 0) != 0)
149 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
152 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
156 if (seq_print_counters(s
, &ct
->counters
[IP_CT_DIR_ORIGINAL
]))
159 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
160 if (seq_printf(s
, "[UNREPLIED] "))
163 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
167 if (seq_print_counters(s
, &ct
->counters
[IP_CT_DIR_REPLY
]))
170 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
171 if (seq_printf(s
, "[ASSURED] "))
174 #if defined(CONFIG_NF_CONNTRACK_MARK)
175 if (seq_printf(s
, "mark=%u ", ct
->mark
))
179 #ifdef CONFIG_NF_CONNTRACK_SECMARK
180 if (seq_printf(s
, "secmark=%u ", ct
->secmark
))
184 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
190 static const struct seq_operations ct_seq_ops
= {
191 .start
= ct_seq_start
,
197 static int ct_open(struct inode
*inode
, struct file
*file
)
199 return seq_open_private(file
, &ct_seq_ops
,
200 sizeof(struct ct_iter_state
));
203 static const struct file_operations ct_file_ops
= {
204 .owner
= THIS_MODULE
,
208 .release
= seq_release_private
,
211 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
216 return SEQ_START_TOKEN
;
218 for (cpu
= *pos
-1; cpu
< NR_CPUS
; ++cpu
) {
219 if (!cpu_possible(cpu
))
222 return &per_cpu(nf_conntrack_stat
, cpu
);
228 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
232 for (cpu
= *pos
; cpu
< NR_CPUS
; ++cpu
) {
233 if (!cpu_possible(cpu
))
236 return &per_cpu(nf_conntrack_stat
, cpu
);
242 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
246 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
248 unsigned int nr_conntracks
= atomic_read(&nf_conntrack_count
);
249 const struct ip_conntrack_stat
*st
= v
;
251 if (v
== SEQ_START_TOKEN
) {
252 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");
256 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
257 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
279 static const struct seq_operations ct_cpu_seq_ops
= {
280 .start
= ct_cpu_seq_start
,
281 .next
= ct_cpu_seq_next
,
282 .stop
= ct_cpu_seq_stop
,
283 .show
= ct_cpu_seq_show
,
286 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
288 return seq_open(file
, &ct_cpu_seq_ops
);
291 static const struct file_operations ct_cpu_seq_fops
= {
292 .owner
= THIS_MODULE
,
293 .open
= ct_cpu_seq_open
,
296 .release
= seq_release_private
,
298 #endif /* CONFIG_PROC_FS */
302 int nf_conntrack_checksum __read_mostly
= 1;
303 EXPORT_SYMBOL_GPL(nf_conntrack_checksum
);
306 /* Log invalid packets of a given protocol */
307 static int log_invalid_proto_min
= 0;
308 static int log_invalid_proto_max
= 255;
310 static struct ctl_table_header
*nf_ct_sysctl_header
;
312 static ctl_table nf_ct_sysctl_table
[] = {
314 .ctl_name
= NET_NF_CONNTRACK_MAX
,
315 .procname
= "nf_conntrack_max",
316 .data
= &nf_conntrack_max
,
317 .maxlen
= sizeof(int),
319 .proc_handler
= &proc_dointvec
,
322 .ctl_name
= NET_NF_CONNTRACK_COUNT
,
323 .procname
= "nf_conntrack_count",
324 .data
= &nf_conntrack_count
,
325 .maxlen
= sizeof(int),
327 .proc_handler
= &proc_dointvec
,
330 .ctl_name
= NET_NF_CONNTRACK_BUCKETS
,
331 .procname
= "nf_conntrack_buckets",
332 .data
= &nf_conntrack_htable_size
,
333 .maxlen
= sizeof(unsigned int),
335 .proc_handler
= &proc_dointvec
,
338 .ctl_name
= NET_NF_CONNTRACK_CHECKSUM
,
339 .procname
= "nf_conntrack_checksum",
340 .data
= &nf_conntrack_checksum
,
341 .maxlen
= sizeof(unsigned int),
343 .proc_handler
= &proc_dointvec
,
346 .ctl_name
= NET_NF_CONNTRACK_LOG_INVALID
,
347 .procname
= "nf_conntrack_log_invalid",
348 .data
= &nf_ct_log_invalid
,
349 .maxlen
= sizeof(unsigned int),
351 .proc_handler
= &proc_dointvec_minmax
,
352 .strategy
= &sysctl_intvec
,
353 .extra1
= &log_invalid_proto_min
,
354 .extra2
= &log_invalid_proto_max
,
357 .ctl_name
= CTL_UNNUMBERED
,
358 .procname
= "nf_conntrack_expect_max",
359 .data
= &nf_ct_expect_max
,
360 .maxlen
= sizeof(int),
362 .proc_handler
= &proc_dointvec
,
367 #define NET_NF_CONNTRACK_MAX 2089
369 static ctl_table nf_ct_netfilter_table
[] = {
371 .ctl_name
= NET_NETFILTER
,
372 .procname
= "netfilter",
374 .child
= nf_ct_sysctl_table
,
377 .ctl_name
= NET_NF_CONNTRACK_MAX
,
378 .procname
= "nf_conntrack_max",
379 .data
= &nf_conntrack_max
,
380 .maxlen
= sizeof(int),
382 .proc_handler
= &proc_dointvec
,
387 static struct ctl_path nf_ct_path
[] = {
388 { .procname
= "net", .ctl_name
= CTL_NET
, },
392 EXPORT_SYMBOL_GPL(nf_ct_log_invalid
);
393 #endif /* CONFIG_SYSCTL */
395 static int __init
nf_conntrack_standalone_init(void)
397 #ifdef CONFIG_PROC_FS
398 struct proc_dir_entry
*proc
, *proc_stat
;
402 ret
= nf_conntrack_init();
406 #ifdef CONFIG_PROC_FS
407 proc
= proc_net_fops_create(&init_net
, "nf_conntrack", 0440, &ct_file_ops
);
408 if (!proc
) goto cleanup_init
;
410 proc_stat
= create_proc_entry("nf_conntrack", S_IRUGO
, init_net
.proc_net_stat
);
414 proc_stat
->proc_fops
= &ct_cpu_seq_fops
;
415 proc_stat
->owner
= THIS_MODULE
;
418 nf_ct_sysctl_header
= register_sysctl_paths(nf_ct_path
,
419 nf_ct_netfilter_table
);
420 if (nf_ct_sysctl_header
== NULL
) {
421 printk("nf_conntrack: can't register to sysctl.\n");
423 goto cleanup_proc_stat
;
431 #ifdef CONFIG_PROC_FS
432 remove_proc_entry("nf_conntrack", init_net
. proc_net_stat
);
434 proc_net_remove(&init_net
, "nf_conntrack");
436 #endif /* CNFIG_PROC_FS */
437 nf_conntrack_cleanup();
441 static void __exit
nf_conntrack_standalone_fini(void)
444 unregister_sysctl_table(nf_ct_sysctl_header
);
446 #ifdef CONFIG_PROC_FS
447 remove_proc_entry("nf_conntrack", init_net
.proc_net_stat
);
448 proc_net_remove(&init_net
, "nf_conntrack");
449 #endif /* CNFIG_PROC_FS */
450 nf_conntrack_cleanup();
453 module_init(nf_conntrack_standalone_init
);
454 module_exit(nf_conntrack_standalone_fini
);
456 /* Some modules need us, but don't depend directly on any symbol.
457 They should call this. */
458 void need_conntrack(void)
461 EXPORT_SYMBOL_GPL(need_conntrack
);