1 /* This file contains all the functions required for the standalone
4 These are not required by the compatibility layer.
7 /* (C) 1999-2001 Paul `Rusty' Russell
8 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
15 * - generalize L3 protocol dependent part.
17 * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
20 #include <linux/types.h>
21 #include <linux/netfilter.h>
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/percpu.h>
27 #include <linux/netdevice.h>
29 #include <linux/sysctl.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_l3proto.h>
35 #include <net/netfilter/nf_conntrack_l4proto.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
42 #define DEBUGP(format, args...)
45 MODULE_LICENSE("GPL");
47 extern atomic_t nf_conntrack_count;
48 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
52 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
53 struct nf_conntrack_l3proto *l3proto,
54 struct nf_conntrack_l4proto *l4proto)
56 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
59 #ifdef CONFIG_NF_CT_ACCT
61 seq_print_counters(struct seq_file *s,
62 const struct ip_conntrack_counter *counter)
64 return seq_printf(s, "packets=%llu bytes=%llu ",
65 (unsigned long long)counter->packets,
66 (unsigned long long)counter->bytes);
69 #define seq_print_counters(x, y) 0
72 struct ct_iter_state {
76 static struct list_head *ct_get_first(struct seq_file *seq)
78 struct ct_iter_state *st = seq->private;
81 st->bucket < nf_conntrack_htable_size;
83 if (!list_empty(&nf_conntrack_hash[st->bucket]))
84 return nf_conntrack_hash[st->bucket].next;
89 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
91 struct ct_iter_state *st = seq->private;
94 while (head == &nf_conntrack_hash[st->bucket]) {
95 if (++st->bucket >= nf_conntrack_htable_size)
97 head = nf_conntrack_hash[st->bucket].next;
102 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
104 struct list_head *head = ct_get_first(seq);
107 while (pos && (head = ct_get_next(seq, head)))
109 return pos ? NULL : head;
112 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
114 read_lock_bh(&nf_conntrack_lock);
115 return ct_get_idx(seq, *pos);
118 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
121 return ct_get_next(s, v);
124 static void ct_seq_stop(struct seq_file *s, void *v)
126 read_unlock_bh(&nf_conntrack_lock);
129 /* return 0 on success, 1 in case of error */
130 static int ct_seq_show(struct seq_file *s, void *v)
132 const struct nf_conntrack_tuple_hash *hash = v;
133 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
134 struct nf_conntrack_l3proto *l3proto;
135 struct nf_conntrack_l4proto *l4proto;
137 NF_CT_ASSERT(conntrack);
139 /* we only want to print DIR_ORIGINAL */
140 if (NF_CT_DIRECTION(hash))
143 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
146 NF_CT_ASSERT(l3proto);
147 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
149 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
150 .tuple.dst.protonum);
151 NF_CT_ASSERT(l4proto);
153 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
155 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
157 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
158 timer_pending(&conntrack->timeout)
159 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
162 if (l3proto->print_conntrack(s, conntrack))
165 if (l4proto->print_conntrack(s, conntrack))
168 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
172 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
175 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
176 if (seq_printf(s, "[UNREPLIED] "))
179 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
183 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
186 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
187 if (seq_printf(s, "[ASSURED] "))
190 #if defined(CONFIG_NF_CONNTRACK_MARK)
191 if (seq_printf(s, "mark=%u ", conntrack->mark))
195 #ifdef CONFIG_NF_CONNTRACK_SECMARK
196 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
200 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
206 static struct seq_operations ct_seq_ops = {
207 .start = ct_seq_start,
213 static int ct_open(struct inode *inode, struct file *file)
215 struct seq_file *seq;
216 struct ct_iter_state *st;
219 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
222 ret = seq_open(file, &ct_seq_ops);
225 seq = file->private_data;
227 memset(st, 0, sizeof(struct ct_iter_state));
234 static struct file_operations ct_file_ops = {
235 .owner = THIS_MODULE,
239 .release = seq_release_private,
242 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
247 return SEQ_START_TOKEN;
249 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
250 if (!cpu_possible(cpu))
253 return &per_cpu(nf_conntrack_stat, cpu);
259 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
263 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
264 if (!cpu_possible(cpu))
267 return &per_cpu(nf_conntrack_stat, cpu);
273 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
277 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
279 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
280 struct ip_conntrack_stat *st = v;
282 if (v == SEQ_START_TOKEN) {
283 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");
287 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
288 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
310 static struct seq_operations ct_cpu_seq_ops = {
311 .start = ct_cpu_seq_start,
312 .next = ct_cpu_seq_next,
313 .stop = ct_cpu_seq_stop,
314 .show = ct_cpu_seq_show,
317 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
319 return seq_open(file, &ct_cpu_seq_ops);
322 static struct file_operations ct_cpu_seq_fops = {
323 .owner = THIS_MODULE,
324 .open = ct_cpu_seq_open,
327 .release = seq_release_private,
329 #endif /* CONFIG_PROC_FS */
333 int nf_conntrack_checksum __read_mostly = 1;
337 /* From nf_conntrack_core.c */
338 extern int nf_conntrack_max;
339 extern unsigned int nf_conntrack_htable_size;
341 /* From nf_conntrack_proto_tcp.c */
342 extern unsigned int nf_ct_tcp_timeout_syn_sent;
343 extern unsigned int nf_ct_tcp_timeout_syn_recv;
344 extern unsigned int nf_ct_tcp_timeout_established;
345 extern unsigned int nf_ct_tcp_timeout_fin_wait;
346 extern unsigned int nf_ct_tcp_timeout_close_wait;
347 extern unsigned int nf_ct_tcp_timeout_last_ack;
348 extern unsigned int nf_ct_tcp_timeout_time_wait;
349 extern unsigned int nf_ct_tcp_timeout_close;
350 extern unsigned int nf_ct_tcp_timeout_max_retrans;
351 extern int nf_ct_tcp_loose;
352 extern int nf_ct_tcp_be_liberal;
353 extern int nf_ct_tcp_max_retrans;
355 /* From nf_conntrack_proto_udp.c */
356 extern unsigned int nf_ct_udp_timeout;
357 extern unsigned int nf_ct_udp_timeout_stream;
359 /* From nf_conntrack_proto_generic.c */
360 extern unsigned int nf_ct_generic_timeout;
362 /* Log invalid packets of a given protocol */
363 static int log_invalid_proto_min = 0;
364 static int log_invalid_proto_max = 255;
366 static struct ctl_table_header *nf_ct_sysctl_header;
368 static ctl_table nf_ct_sysctl_table[] = {
370 .ctl_name = NET_NF_CONNTRACK_MAX,
371 .procname = "nf_conntrack_max",
372 .data = &nf_conntrack_max,
373 .maxlen = sizeof(int),
375 .proc_handler = &proc_dointvec,
378 .ctl_name = NET_NF_CONNTRACK_COUNT,
379 .procname = "nf_conntrack_count",
380 .data = &nf_conntrack_count,
381 .maxlen = sizeof(int),
383 .proc_handler = &proc_dointvec,
386 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
387 .procname = "nf_conntrack_buckets",
388 .data = &nf_conntrack_htable_size,
389 .maxlen = sizeof(unsigned int),
391 .proc_handler = &proc_dointvec,
394 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
395 .procname = "nf_conntrack_checksum",
396 .data = &nf_conntrack_checksum,
397 .maxlen = sizeof(unsigned int),
399 .proc_handler = &proc_dointvec,
402 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
403 .procname = "nf_conntrack_tcp_timeout_syn_sent",
404 .data = &nf_ct_tcp_timeout_syn_sent,
405 .maxlen = sizeof(unsigned int),
407 .proc_handler = &proc_dointvec_jiffies,
410 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
411 .procname = "nf_conntrack_tcp_timeout_syn_recv",
412 .data = &nf_ct_tcp_timeout_syn_recv,
413 .maxlen = sizeof(unsigned int),
415 .proc_handler = &proc_dointvec_jiffies,
418 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
419 .procname = "nf_conntrack_tcp_timeout_established",
420 .data = &nf_ct_tcp_timeout_established,
421 .maxlen = sizeof(unsigned int),
423 .proc_handler = &proc_dointvec_jiffies,
426 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
427 .procname = "nf_conntrack_tcp_timeout_fin_wait",
428 .data = &nf_ct_tcp_timeout_fin_wait,
429 .maxlen = sizeof(unsigned int),
431 .proc_handler = &proc_dointvec_jiffies,
434 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
435 .procname = "nf_conntrack_tcp_timeout_close_wait",
436 .data = &nf_ct_tcp_timeout_close_wait,
437 .maxlen = sizeof(unsigned int),
439 .proc_handler = &proc_dointvec_jiffies,
442 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
443 .procname = "nf_conntrack_tcp_timeout_last_ack",
444 .data = &nf_ct_tcp_timeout_last_ack,
445 .maxlen = sizeof(unsigned int),
447 .proc_handler = &proc_dointvec_jiffies,
450 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
451 .procname = "nf_conntrack_tcp_timeout_time_wait",
452 .data = &nf_ct_tcp_timeout_time_wait,
453 .maxlen = sizeof(unsigned int),
455 .proc_handler = &proc_dointvec_jiffies,
458 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
459 .procname = "nf_conntrack_tcp_timeout_close",
460 .data = &nf_ct_tcp_timeout_close,
461 .maxlen = sizeof(unsigned int),
463 .proc_handler = &proc_dointvec_jiffies,
466 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT,
467 .procname = "nf_conntrack_udp_timeout",
468 .data = &nf_ct_udp_timeout,
469 .maxlen = sizeof(unsigned int),
471 .proc_handler = &proc_dointvec_jiffies,
474 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
475 .procname = "nf_conntrack_udp_timeout_stream",
476 .data = &nf_ct_udp_timeout_stream,
477 .maxlen = sizeof(unsigned int),
479 .proc_handler = &proc_dointvec_jiffies,
482 .ctl_name = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
483 .procname = "nf_conntrack_generic_timeout",
484 .data = &nf_ct_generic_timeout,
485 .maxlen = sizeof(unsigned int),
487 .proc_handler = &proc_dointvec_jiffies,
490 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
491 .procname = "nf_conntrack_log_invalid",
492 .data = &nf_ct_log_invalid,
493 .maxlen = sizeof(unsigned int),
495 .proc_handler = &proc_dointvec_minmax,
496 .strategy = &sysctl_intvec,
497 .extra1 = &log_invalid_proto_min,
498 .extra2 = &log_invalid_proto_max,
501 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
502 .procname = "nf_conntrack_tcp_timeout_max_retrans",
503 .data = &nf_ct_tcp_timeout_max_retrans,
504 .maxlen = sizeof(unsigned int),
506 .proc_handler = &proc_dointvec_jiffies,
509 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
510 .procname = "nf_conntrack_tcp_loose",
511 .data = &nf_ct_tcp_loose,
512 .maxlen = sizeof(unsigned int),
514 .proc_handler = &proc_dointvec,
517 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
518 .procname = "nf_conntrack_tcp_be_liberal",
519 .data = &nf_ct_tcp_be_liberal,
520 .maxlen = sizeof(unsigned int),
522 .proc_handler = &proc_dointvec,
525 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
526 .procname = "nf_conntrack_tcp_max_retrans",
527 .data = &nf_ct_tcp_max_retrans,
528 .maxlen = sizeof(unsigned int),
530 .proc_handler = &proc_dointvec,
536 #define NET_NF_CONNTRACK_MAX 2089
538 static ctl_table nf_ct_netfilter_table[] = {
540 .ctl_name = NET_NETFILTER,
541 .procname = "netfilter",
543 .child = nf_ct_sysctl_table,
546 .ctl_name = NET_NF_CONNTRACK_MAX,
547 .procname = "nf_conntrack_max",
548 .data = &nf_conntrack_max,
549 .maxlen = sizeof(int),
551 .proc_handler = &proc_dointvec,
556 static ctl_table nf_ct_net_table[] = {
561 .child = nf_ct_netfilter_table,
565 EXPORT_SYMBOL(nf_ct_log_invalid);
566 #endif /* CONFIG_SYSCTL */
568 static int __init nf_conntrack_standalone_init(void)
570 #ifdef CONFIG_PROC_FS
571 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
575 ret = nf_conntrack_init();
579 #ifdef CONFIG_PROC_FS
580 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
581 if (!proc) goto cleanup_init;
583 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
585 if (!proc_exp) goto cleanup_proc;
587 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
589 goto cleanup_proc_exp;
591 proc_stat->proc_fops = &ct_cpu_seq_fops;
592 proc_stat->owner = THIS_MODULE;
595 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
596 if (nf_ct_sysctl_header == NULL) {
597 printk("nf_conntrack: can't register to sysctl.\n");
599 goto cleanup_proc_stat;
607 #ifdef CONFIG_PROC_FS
608 remove_proc_entry("nf_conntrack", proc_net_stat);
610 proc_net_remove("nf_conntrack_expect");
612 proc_net_remove("nf_conntrack");
614 #endif /* CNFIG_PROC_FS */
615 nf_conntrack_cleanup();
619 static void __exit nf_conntrack_standalone_fini(void)
622 unregister_sysctl_table(nf_ct_sysctl_header);
624 #ifdef CONFIG_PROC_FS
625 remove_proc_entry("nf_conntrack", proc_net_stat);
626 proc_net_remove("nf_conntrack_expect");
627 proc_net_remove("nf_conntrack");
628 #endif /* CNFIG_PROC_FS */
629 nf_conntrack_cleanup();
632 module_init(nf_conntrack_standalone_init);
633 module_exit(nf_conntrack_standalone_fini);
635 /* Some modules need us, but don't depend directly on any symbol.
636 They should call this. */
637 void need_conntrack(void)
641 #ifdef CONFIG_NF_CONNTRACK_EVENTS
642 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
643 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
644 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
645 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
646 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
647 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
648 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
650 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
651 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
652 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
653 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
654 EXPORT_SYMBOL(nf_conntrack_l4proto_register);
655 EXPORT_SYMBOL(nf_conntrack_l4proto_unregister);
656 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
657 EXPORT_SYMBOL(nf_conntrack_destroyed);
658 EXPORT_SYMBOL(need_conntrack);
659 EXPORT_SYMBOL(nf_conntrack_helper_register);
660 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
661 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
662 EXPORT_SYMBOL(__nf_ct_refresh_acct);
663 EXPORT_SYMBOL(nf_ct_protos);
664 EXPORT_SYMBOL(__nf_ct_l4proto_find);
665 EXPORT_SYMBOL(nf_ct_l4proto_find_get);
666 EXPORT_SYMBOL(nf_ct_l4proto_put);
667 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
668 EXPORT_SYMBOL(nf_ct_l3proto_put);
669 EXPORT_SYMBOL(nf_ct_l3protos);
670 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
671 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
672 EXPORT_SYMBOL(nf_conntrack_expect_put);
673 EXPORT_SYMBOL(nf_conntrack_expect_related);
674 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
675 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
676 EXPORT_SYMBOL(nf_conntrack_htable_size);
677 EXPORT_SYMBOL(nf_conntrack_lock);
678 EXPORT_SYMBOL(nf_conntrack_hash);
679 EXPORT_SYMBOL(nf_conntrack_untracked);
680 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
681 #ifdef CONFIG_IP_NF_NAT_NEEDED
682 EXPORT_SYMBOL(nf_conntrack_tcp_update);
684 EXPORT_SYMBOL(__nf_conntrack_confirm);
685 EXPORT_SYMBOL(nf_ct_get_tuple);
686 EXPORT_SYMBOL(nf_ct_invert_tuple);
687 EXPORT_SYMBOL(nf_conntrack_in);
688 EXPORT_SYMBOL(__nf_conntrack_attach);
689 EXPORT_SYMBOL(nf_conntrack_alloc);
690 EXPORT_SYMBOL(nf_conntrack_free);
691 EXPORT_SYMBOL(nf_conntrack_flush);
692 EXPORT_SYMBOL(nf_ct_remove_expectations);
693 EXPORT_SYMBOL(nf_ct_helper_find_get);
694 EXPORT_SYMBOL(nf_ct_helper_put);
695 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
696 EXPORT_SYMBOL(__nf_conntrack_find);
697 EXPORT_SYMBOL(nf_ct_unlink_expect);
698 EXPORT_SYMBOL(nf_conntrack_hash_insert);
699 EXPORT_SYMBOL(__nf_conntrack_expect_find);
700 EXPORT_SYMBOL(nf_conntrack_expect_find);
701 EXPORT_SYMBOL(nf_conntrack_expect_list);
702 #if defined(CONFIG_NF_CT_NETLINK) || \
703 defined(CONFIG_NF_CT_NETLINK_MODULE)
704 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
705 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);