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 #define ASSERT_READ_LOCK(x)
33 #define ASSERT_WRITE_LOCK(x)
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_protocol.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
45 #define DEBUGP(format, args...)
48 MODULE_LICENSE("GPL");
50 extern atomic_t nf_conntrack_count;
51 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
55 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
56 struct nf_conntrack_l3proto *l3proto,
57 struct nf_conntrack_protocol *proto)
59 return l3proto->print_tuple(s, tuple) || proto->print_tuple(s, tuple);
62 #ifdef CONFIG_NF_CT_ACCT
64 seq_print_counters(struct seq_file *s,
65 const struct ip_conntrack_counter *counter)
67 return seq_printf(s, "packets=%llu bytes=%llu ",
68 (unsigned long long)counter->packets,
69 (unsigned long long)counter->bytes);
72 #define seq_print_counters(x, y) 0
75 struct ct_iter_state {
79 static struct list_head *ct_get_first(struct seq_file *seq)
81 struct ct_iter_state *st = seq->private;
84 st->bucket < nf_conntrack_htable_size;
86 if (!list_empty(&nf_conntrack_hash[st->bucket]))
87 return nf_conntrack_hash[st->bucket].next;
92 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
94 struct ct_iter_state *st = seq->private;
97 while (head == &nf_conntrack_hash[st->bucket]) {
98 if (++st->bucket >= nf_conntrack_htable_size)
100 head = nf_conntrack_hash[st->bucket].next;
105 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
107 struct list_head *head = ct_get_first(seq);
110 while (pos && (head = ct_get_next(seq, head)))
112 return pos ? NULL : head;
115 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
117 read_lock_bh(&nf_conntrack_lock);
118 return ct_get_idx(seq, *pos);
121 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
124 return ct_get_next(s, v);
127 static void ct_seq_stop(struct seq_file *s, void *v)
129 read_unlock_bh(&nf_conntrack_lock);
132 /* return 0 on success, 1 in case of error */
133 static int ct_seq_show(struct seq_file *s, void *v)
135 const struct nf_conntrack_tuple_hash *hash = v;
136 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
137 struct nf_conntrack_l3proto *l3proto;
138 struct nf_conntrack_protocol *proto;
140 ASSERT_READ_LOCK(&nf_conntrack_lock);
141 NF_CT_ASSERT(conntrack);
143 /* we only want to print DIR_ORIGINAL */
144 if (NF_CT_DIRECTION(hash))
147 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
150 NF_CT_ASSERT(l3proto);
151 proto = __nf_ct_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
153 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
154 .tuple.dst.protonum);
157 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
159 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
161 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
162 timer_pending(&conntrack->timeout)
163 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
166 if (l3proto->print_conntrack(s, conntrack))
169 if (proto->print_conntrack(s, conntrack))
172 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
176 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
179 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
180 if (seq_printf(s, "[UNREPLIED] "))
183 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
187 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
190 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
191 if (seq_printf(s, "[ASSURED] "))
194 #if defined(CONFIG_NF_CONNTRACK_MARK)
195 if (seq_printf(s, "mark=%u ", conntrack->mark))
199 #ifdef CONFIG_NF_CONNTRACK_SECMARK
200 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
204 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
210 static struct seq_operations ct_seq_ops = {
211 .start = ct_seq_start,
217 static int ct_open(struct inode *inode, struct file *file)
219 struct seq_file *seq;
220 struct ct_iter_state *st;
223 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
226 ret = seq_open(file, &ct_seq_ops);
229 seq = file->private_data;
231 memset(st, 0, sizeof(struct ct_iter_state));
238 static struct file_operations ct_file_ops = {
239 .owner = THIS_MODULE,
243 .release = seq_release_private,
246 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
251 return SEQ_START_TOKEN;
253 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
254 if (!cpu_possible(cpu))
257 return &per_cpu(nf_conntrack_stat, cpu);
263 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
267 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
268 if (!cpu_possible(cpu))
271 return &per_cpu(nf_conntrack_stat, cpu);
277 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
281 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
283 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
284 struct ip_conntrack_stat *st = v;
286 if (v == SEQ_START_TOKEN) {
287 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");
291 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
292 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
314 static struct seq_operations ct_cpu_seq_ops = {
315 .start = ct_cpu_seq_start,
316 .next = ct_cpu_seq_next,
317 .stop = ct_cpu_seq_stop,
318 .show = ct_cpu_seq_show,
321 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
323 return seq_open(file, &ct_cpu_seq_ops);
326 static struct file_operations ct_cpu_seq_fops = {
327 .owner = THIS_MODULE,
328 .open = ct_cpu_seq_open,
331 .release = seq_release_private,
333 #endif /* CONFIG_PROC_FS */
337 int nf_conntrack_checksum __read_mostly = 1;
341 /* From nf_conntrack_core.c */
342 extern int nf_conntrack_max;
343 extern unsigned int nf_conntrack_htable_size;
345 /* From nf_conntrack_proto_tcp.c */
346 extern unsigned int nf_ct_tcp_timeout_syn_sent;
347 extern unsigned int nf_ct_tcp_timeout_syn_recv;
348 extern unsigned int nf_ct_tcp_timeout_established;
349 extern unsigned int nf_ct_tcp_timeout_fin_wait;
350 extern unsigned int nf_ct_tcp_timeout_close_wait;
351 extern unsigned int nf_ct_tcp_timeout_last_ack;
352 extern unsigned int nf_ct_tcp_timeout_time_wait;
353 extern unsigned int nf_ct_tcp_timeout_close;
354 extern unsigned int nf_ct_tcp_timeout_max_retrans;
355 extern int nf_ct_tcp_loose;
356 extern int nf_ct_tcp_be_liberal;
357 extern int nf_ct_tcp_max_retrans;
359 /* From nf_conntrack_proto_udp.c */
360 extern unsigned int nf_ct_udp_timeout;
361 extern unsigned int nf_ct_udp_timeout_stream;
363 /* From nf_conntrack_proto_generic.c */
364 extern unsigned int nf_ct_generic_timeout;
366 /* Log invalid packets of a given protocol */
367 static int log_invalid_proto_min = 0;
368 static int log_invalid_proto_max = 255;
370 static struct ctl_table_header *nf_ct_sysctl_header;
372 static ctl_table nf_ct_sysctl_table[] = {
374 .ctl_name = NET_NF_CONNTRACK_MAX,
375 .procname = "nf_conntrack_max",
376 .data = &nf_conntrack_max,
377 .maxlen = sizeof(int),
379 .proc_handler = &proc_dointvec,
382 .ctl_name = NET_NF_CONNTRACK_COUNT,
383 .procname = "nf_conntrack_count",
384 .data = &nf_conntrack_count,
385 .maxlen = sizeof(int),
387 .proc_handler = &proc_dointvec,
390 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
391 .procname = "nf_conntrack_buckets",
392 .data = &nf_conntrack_htable_size,
393 .maxlen = sizeof(unsigned int),
395 .proc_handler = &proc_dointvec,
398 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
399 .procname = "nf_conntrack_checksum",
400 .data = &nf_conntrack_checksum,
401 .maxlen = sizeof(unsigned int),
403 .proc_handler = &proc_dointvec,
406 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
407 .procname = "nf_conntrack_tcp_timeout_syn_sent",
408 .data = &nf_ct_tcp_timeout_syn_sent,
409 .maxlen = sizeof(unsigned int),
411 .proc_handler = &proc_dointvec_jiffies,
414 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
415 .procname = "nf_conntrack_tcp_timeout_syn_recv",
416 .data = &nf_ct_tcp_timeout_syn_recv,
417 .maxlen = sizeof(unsigned int),
419 .proc_handler = &proc_dointvec_jiffies,
422 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
423 .procname = "nf_conntrack_tcp_timeout_established",
424 .data = &nf_ct_tcp_timeout_established,
425 .maxlen = sizeof(unsigned int),
427 .proc_handler = &proc_dointvec_jiffies,
430 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
431 .procname = "nf_conntrack_tcp_timeout_fin_wait",
432 .data = &nf_ct_tcp_timeout_fin_wait,
433 .maxlen = sizeof(unsigned int),
435 .proc_handler = &proc_dointvec_jiffies,
438 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
439 .procname = "nf_conntrack_tcp_timeout_close_wait",
440 .data = &nf_ct_tcp_timeout_close_wait,
441 .maxlen = sizeof(unsigned int),
443 .proc_handler = &proc_dointvec_jiffies,
446 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
447 .procname = "nf_conntrack_tcp_timeout_last_ack",
448 .data = &nf_ct_tcp_timeout_last_ack,
449 .maxlen = sizeof(unsigned int),
451 .proc_handler = &proc_dointvec_jiffies,
454 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
455 .procname = "nf_conntrack_tcp_timeout_time_wait",
456 .data = &nf_ct_tcp_timeout_time_wait,
457 .maxlen = sizeof(unsigned int),
459 .proc_handler = &proc_dointvec_jiffies,
462 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
463 .procname = "nf_conntrack_tcp_timeout_close",
464 .data = &nf_ct_tcp_timeout_close,
465 .maxlen = sizeof(unsigned int),
467 .proc_handler = &proc_dointvec_jiffies,
470 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT,
471 .procname = "nf_conntrack_udp_timeout",
472 .data = &nf_ct_udp_timeout,
473 .maxlen = sizeof(unsigned int),
475 .proc_handler = &proc_dointvec_jiffies,
478 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
479 .procname = "nf_conntrack_udp_timeout_stream",
480 .data = &nf_ct_udp_timeout_stream,
481 .maxlen = sizeof(unsigned int),
483 .proc_handler = &proc_dointvec_jiffies,
486 .ctl_name = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
487 .procname = "nf_conntrack_generic_timeout",
488 .data = &nf_ct_generic_timeout,
489 .maxlen = sizeof(unsigned int),
491 .proc_handler = &proc_dointvec_jiffies,
494 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
495 .procname = "nf_conntrack_log_invalid",
496 .data = &nf_ct_log_invalid,
497 .maxlen = sizeof(unsigned int),
499 .proc_handler = &proc_dointvec_minmax,
500 .strategy = &sysctl_intvec,
501 .extra1 = &log_invalid_proto_min,
502 .extra2 = &log_invalid_proto_max,
505 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
506 .procname = "nf_conntrack_tcp_timeout_max_retrans",
507 .data = &nf_ct_tcp_timeout_max_retrans,
508 .maxlen = sizeof(unsigned int),
510 .proc_handler = &proc_dointvec_jiffies,
513 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
514 .procname = "nf_conntrack_tcp_loose",
515 .data = &nf_ct_tcp_loose,
516 .maxlen = sizeof(unsigned int),
518 .proc_handler = &proc_dointvec,
521 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
522 .procname = "nf_conntrack_tcp_be_liberal",
523 .data = &nf_ct_tcp_be_liberal,
524 .maxlen = sizeof(unsigned int),
526 .proc_handler = &proc_dointvec,
529 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
530 .procname = "nf_conntrack_tcp_max_retrans",
531 .data = &nf_ct_tcp_max_retrans,
532 .maxlen = sizeof(unsigned int),
534 .proc_handler = &proc_dointvec,
540 #define NET_NF_CONNTRACK_MAX 2089
542 static ctl_table nf_ct_netfilter_table[] = {
544 .ctl_name = NET_NETFILTER,
545 .procname = "netfilter",
547 .child = nf_ct_sysctl_table,
550 .ctl_name = NET_NF_CONNTRACK_MAX,
551 .procname = "nf_conntrack_max",
552 .data = &nf_conntrack_max,
553 .maxlen = sizeof(int),
555 .proc_handler = &proc_dointvec,
560 static ctl_table nf_ct_net_table[] = {
565 .child = nf_ct_netfilter_table,
569 EXPORT_SYMBOL(nf_ct_log_invalid);
570 #endif /* CONFIG_SYSCTL */
572 static int __init nf_conntrack_standalone_init(void)
574 #ifdef CONFIG_PROC_FS
575 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
579 ret = nf_conntrack_init();
583 #ifdef CONFIG_PROC_FS
584 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
585 if (!proc) goto cleanup_init;
587 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
589 if (!proc_exp) goto cleanup_proc;
591 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
593 goto cleanup_proc_exp;
595 proc_stat->proc_fops = &ct_cpu_seq_fops;
596 proc_stat->owner = THIS_MODULE;
599 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
600 if (nf_ct_sysctl_header == NULL) {
601 printk("nf_conntrack: can't register to sysctl.\n");
603 goto cleanup_proc_stat;
611 #ifdef CONFIG_PROC_FS
612 remove_proc_entry("nf_conntrack", proc_net_stat);
614 proc_net_remove("nf_conntrack_expect");
616 proc_net_remove("nf_conntrack");
618 #endif /* CNFIG_PROC_FS */
619 nf_conntrack_cleanup();
623 static void __exit nf_conntrack_standalone_fini(void)
626 unregister_sysctl_table(nf_ct_sysctl_header);
628 #ifdef CONFIG_PROC_FS
629 remove_proc_entry("nf_conntrack", proc_net_stat);
630 proc_net_remove("nf_conntrack_expect");
631 proc_net_remove("nf_conntrack");
632 #endif /* CNFIG_PROC_FS */
633 nf_conntrack_cleanup();
636 module_init(nf_conntrack_standalone_init);
637 module_exit(nf_conntrack_standalone_fini);
639 /* Some modules need us, but don't depend directly on any symbol.
640 They should call this. */
641 void need_conntrack(void)
645 #ifdef CONFIG_NF_CONNTRACK_EVENTS
646 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
647 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
648 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
649 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
650 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
651 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
652 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
654 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
655 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
656 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
657 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
658 EXPORT_SYMBOL(nf_conntrack_protocol_register);
659 EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
660 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
661 EXPORT_SYMBOL(nf_conntrack_destroyed);
662 EXPORT_SYMBOL(need_conntrack);
663 EXPORT_SYMBOL(nf_conntrack_helper_register);
664 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
665 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
666 EXPORT_SYMBOL(__nf_ct_refresh_acct);
667 EXPORT_SYMBOL(nf_ct_protos);
668 EXPORT_SYMBOL(__nf_ct_proto_find);
669 EXPORT_SYMBOL(nf_ct_proto_find_get);
670 EXPORT_SYMBOL(nf_ct_proto_put);
671 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
672 EXPORT_SYMBOL(nf_ct_l3proto_put);
673 EXPORT_SYMBOL(nf_ct_l3protos);
674 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
675 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
676 EXPORT_SYMBOL(nf_conntrack_expect_put);
677 EXPORT_SYMBOL(nf_conntrack_expect_related);
678 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
679 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
680 EXPORT_SYMBOL(nf_conntrack_htable_size);
681 EXPORT_SYMBOL(nf_conntrack_lock);
682 EXPORT_SYMBOL(nf_conntrack_hash);
683 EXPORT_SYMBOL(nf_conntrack_untracked);
684 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
685 #ifdef CONFIG_IP_NF_NAT_NEEDED
686 EXPORT_SYMBOL(nf_conntrack_tcp_update);
688 EXPORT_SYMBOL(__nf_conntrack_confirm);
689 EXPORT_SYMBOL(nf_ct_get_tuple);
690 EXPORT_SYMBOL(nf_ct_invert_tuple);
691 EXPORT_SYMBOL(nf_conntrack_in);
692 EXPORT_SYMBOL(__nf_conntrack_attach);
693 EXPORT_SYMBOL(nf_conntrack_alloc);
694 EXPORT_SYMBOL(nf_conntrack_free);
695 EXPORT_SYMBOL(nf_conntrack_flush);
696 EXPORT_SYMBOL(nf_ct_remove_expectations);
697 EXPORT_SYMBOL(nf_ct_helper_find_get);
698 EXPORT_SYMBOL(nf_ct_helper_put);
699 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
700 EXPORT_SYMBOL(__nf_conntrack_find);
701 EXPORT_SYMBOL(nf_ct_unlink_expect);
702 EXPORT_SYMBOL(nf_conntrack_hash_insert);
703 EXPORT_SYMBOL(__nf_conntrack_expect_find);
704 EXPORT_SYMBOL(nf_conntrack_expect_find);
705 EXPORT_SYMBOL(nf_conntrack_expect_list);
706 #if defined(CONFIG_NF_CT_NETLINK) || \
707 defined(CONFIG_NF_CT_NETLINK_MODULE)
708 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
709 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);