[PATCH] x86: implement always-locked bit ops, for memory shared with an SMP hypervisor
[linux-2.6/mini2440.git] / net / netfilter / nf_conntrack_standalone.c
blob5954f6773810591e96fffaae245b1a40bf8fdf2d
1 /* This file contains all the functions required for the standalone
2 nf_conntrack module.
4 These are not required by the compatibility layer.
5 */
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>
28 #ifdef CONFIG_SYSCTL
29 #include <linux/sysctl.h>
30 #endif
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_l3proto.h>
37 #include <net/netfilter/nf_conntrack_protocol.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
41 #if 0
42 #define DEBUGP printk
43 #else
44 #define DEBUGP(format, args...)
45 #endif
47 MODULE_LICENSE("GPL");
49 extern atomic_t nf_conntrack_count;
50 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
52 static int kill_l3proto(struct nf_conn *i, void *data)
54 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
55 ((struct nf_conntrack_l3proto *)data)->l3proto);
58 static int kill_proto(struct nf_conn *i, void *data)
60 struct nf_conntrack_protocol *proto;
61 proto = (struct nf_conntrack_protocol *)data;
62 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
63 proto->proto) &&
64 (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
65 proto->l3proto);
68 #ifdef CONFIG_PROC_FS
69 static int
70 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
71 struct nf_conntrack_l3proto *l3proto,
72 struct nf_conntrack_protocol *proto)
74 return l3proto->print_tuple(s, tuple) || proto->print_tuple(s, tuple);
77 #ifdef CONFIG_NF_CT_ACCT
78 static unsigned int
79 seq_print_counters(struct seq_file *s,
80 const struct ip_conntrack_counter *counter)
82 return seq_printf(s, "packets=%llu bytes=%llu ",
83 (unsigned long long)counter->packets,
84 (unsigned long long)counter->bytes);
86 #else
87 #define seq_print_counters(x, y) 0
88 #endif
90 struct ct_iter_state {
91 unsigned int bucket;
94 static struct list_head *ct_get_first(struct seq_file *seq)
96 struct ct_iter_state *st = seq->private;
98 for (st->bucket = 0;
99 st->bucket < nf_conntrack_htable_size;
100 st->bucket++) {
101 if (!list_empty(&nf_conntrack_hash[st->bucket]))
102 return nf_conntrack_hash[st->bucket].next;
104 return NULL;
107 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
109 struct ct_iter_state *st = seq->private;
111 head = head->next;
112 while (head == &nf_conntrack_hash[st->bucket]) {
113 if (++st->bucket >= nf_conntrack_htable_size)
114 return NULL;
115 head = nf_conntrack_hash[st->bucket].next;
117 return head;
120 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
122 struct list_head *head = ct_get_first(seq);
124 if (head)
125 while (pos && (head = ct_get_next(seq, head)))
126 pos--;
127 return pos ? NULL : head;
130 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
132 read_lock_bh(&nf_conntrack_lock);
133 return ct_get_idx(seq, *pos);
136 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
138 (*pos)++;
139 return ct_get_next(s, v);
142 static void ct_seq_stop(struct seq_file *s, void *v)
144 read_unlock_bh(&nf_conntrack_lock);
147 /* return 0 on success, 1 in case of error */
148 static int ct_seq_show(struct seq_file *s, void *v)
150 const struct nf_conntrack_tuple_hash *hash = v;
151 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
152 struct nf_conntrack_l3proto *l3proto;
153 struct nf_conntrack_protocol *proto;
155 ASSERT_READ_LOCK(&nf_conntrack_lock);
156 NF_CT_ASSERT(conntrack);
158 /* we only want to print DIR_ORIGINAL */
159 if (NF_CT_DIRECTION(hash))
160 return 0;
162 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
163 .tuple.src.l3num);
165 NF_CT_ASSERT(l3proto);
166 proto = __nf_ct_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
167 .tuple.src.l3num,
168 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
169 .tuple.dst.protonum);
170 NF_CT_ASSERT(proto);
172 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
173 l3proto->name,
174 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
175 proto->name,
176 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
177 timer_pending(&conntrack->timeout)
178 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
179 return -ENOSPC;
181 if (l3proto->print_conntrack(s, conntrack))
182 return -ENOSPC;
184 if (proto->print_conntrack(s, conntrack))
185 return -ENOSPC;
187 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
188 l3proto, proto))
189 return -ENOSPC;
191 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
192 return -ENOSPC;
194 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
195 if (seq_printf(s, "[UNREPLIED] "))
196 return -ENOSPC;
198 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
199 l3proto, proto))
200 return -ENOSPC;
202 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
203 return -ENOSPC;
205 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
206 if (seq_printf(s, "[ASSURED] "))
207 return -ENOSPC;
209 #if defined(CONFIG_NF_CONNTRACK_MARK)
210 if (seq_printf(s, "mark=%u ", conntrack->mark))
211 return -ENOSPC;
212 #endif
214 #ifdef CONFIG_NF_CONNTRACK_SECMARK
215 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
216 return -ENOSPC;
217 #endif
219 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
220 return -ENOSPC;
222 return 0;
225 static struct seq_operations ct_seq_ops = {
226 .start = ct_seq_start,
227 .next = ct_seq_next,
228 .stop = ct_seq_stop,
229 .show = ct_seq_show
232 static int ct_open(struct inode *inode, struct file *file)
234 struct seq_file *seq;
235 struct ct_iter_state *st;
236 int ret;
238 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
239 if (st == NULL)
240 return -ENOMEM;
241 ret = seq_open(file, &ct_seq_ops);
242 if (ret)
243 goto out_free;
244 seq = file->private_data;
245 seq->private = st;
246 memset(st, 0, sizeof(struct ct_iter_state));
247 return ret;
248 out_free:
249 kfree(st);
250 return ret;
253 static struct file_operations ct_file_ops = {
254 .owner = THIS_MODULE,
255 .open = ct_open,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = seq_release_private,
261 /* expects */
262 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
264 struct list_head *e = &nf_conntrack_expect_list;
265 loff_t i;
267 /* strange seq_file api calls stop even if we fail,
268 * thus we need to grab lock since stop unlocks */
269 read_lock_bh(&nf_conntrack_lock);
271 if (list_empty(e))
272 return NULL;
274 for (i = 0; i <= *pos; i++) {
275 e = e->next;
276 if (e == &nf_conntrack_expect_list)
277 return NULL;
279 return e;
282 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
284 struct list_head *e = v;
286 ++*pos;
287 e = e->next;
289 if (e == &nf_conntrack_expect_list)
290 return NULL;
292 return e;
295 static void exp_seq_stop(struct seq_file *s, void *v)
297 read_unlock_bh(&nf_conntrack_lock);
300 static int exp_seq_show(struct seq_file *s, void *v)
302 struct nf_conntrack_expect *expect = v;
304 if (expect->timeout.function)
305 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
306 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
307 else
308 seq_printf(s, "- ");
309 seq_printf(s, "l3proto = %u proto=%u ",
310 expect->tuple.src.l3num,
311 expect->tuple.dst.protonum);
312 print_tuple(s, &expect->tuple,
313 __nf_ct_l3proto_find(expect->tuple.src.l3num),
314 __nf_ct_proto_find(expect->tuple.src.l3num,
315 expect->tuple.dst.protonum));
316 return seq_putc(s, '\n');
319 static struct seq_operations exp_seq_ops = {
320 .start = exp_seq_start,
321 .next = exp_seq_next,
322 .stop = exp_seq_stop,
323 .show = exp_seq_show
326 static int exp_open(struct inode *inode, struct file *file)
328 return seq_open(file, &exp_seq_ops);
331 static struct file_operations exp_file_ops = {
332 .owner = THIS_MODULE,
333 .open = exp_open,
334 .read = seq_read,
335 .llseek = seq_lseek,
336 .release = seq_release
339 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
341 int cpu;
343 if (*pos == 0)
344 return SEQ_START_TOKEN;
346 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
347 if (!cpu_possible(cpu))
348 continue;
349 *pos = cpu + 1;
350 return &per_cpu(nf_conntrack_stat, cpu);
353 return NULL;
356 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
358 int cpu;
360 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
361 if (!cpu_possible(cpu))
362 continue;
363 *pos = cpu + 1;
364 return &per_cpu(nf_conntrack_stat, cpu);
367 return NULL;
370 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
374 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
376 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
377 struct ip_conntrack_stat *st = v;
379 if (v == SEQ_START_TOKEN) {
380 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");
381 return 0;
384 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
385 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
386 nr_conntracks,
387 st->searched,
388 st->found,
389 st->new,
390 st->invalid,
391 st->ignore,
392 st->delete,
393 st->delete_list,
394 st->insert,
395 st->insert_failed,
396 st->drop,
397 st->early_drop,
398 st->error,
400 st->expect_new,
401 st->expect_create,
402 st->expect_delete
404 return 0;
407 static struct seq_operations ct_cpu_seq_ops = {
408 .start = ct_cpu_seq_start,
409 .next = ct_cpu_seq_next,
410 .stop = ct_cpu_seq_stop,
411 .show = ct_cpu_seq_show,
414 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
416 return seq_open(file, &ct_cpu_seq_ops);
419 static struct file_operations ct_cpu_seq_fops = {
420 .owner = THIS_MODULE,
421 .open = ct_cpu_seq_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
424 .release = seq_release_private,
426 #endif /* CONFIG_PROC_FS */
428 /* Sysctl support */
430 int nf_conntrack_checksum __read_mostly = 1;
432 #ifdef CONFIG_SYSCTL
434 /* From nf_conntrack_core.c */
435 extern int nf_conntrack_max;
436 extern unsigned int nf_conntrack_htable_size;
438 /* From nf_conntrack_proto_tcp.c */
439 extern unsigned int nf_ct_tcp_timeout_syn_sent;
440 extern unsigned int nf_ct_tcp_timeout_syn_recv;
441 extern unsigned int nf_ct_tcp_timeout_established;
442 extern unsigned int nf_ct_tcp_timeout_fin_wait;
443 extern unsigned int nf_ct_tcp_timeout_close_wait;
444 extern unsigned int nf_ct_tcp_timeout_last_ack;
445 extern unsigned int nf_ct_tcp_timeout_time_wait;
446 extern unsigned int nf_ct_tcp_timeout_close;
447 extern unsigned int nf_ct_tcp_timeout_max_retrans;
448 extern int nf_ct_tcp_loose;
449 extern int nf_ct_tcp_be_liberal;
450 extern int nf_ct_tcp_max_retrans;
452 /* From nf_conntrack_proto_udp.c */
453 extern unsigned int nf_ct_udp_timeout;
454 extern unsigned int nf_ct_udp_timeout_stream;
456 /* From nf_conntrack_proto_generic.c */
457 extern unsigned int nf_ct_generic_timeout;
459 /* Log invalid packets of a given protocol */
460 static int log_invalid_proto_min = 0;
461 static int log_invalid_proto_max = 255;
463 static struct ctl_table_header *nf_ct_sysctl_header;
465 static ctl_table nf_ct_sysctl_table[] = {
467 .ctl_name = NET_NF_CONNTRACK_MAX,
468 .procname = "nf_conntrack_max",
469 .data = &nf_conntrack_max,
470 .maxlen = sizeof(int),
471 .mode = 0644,
472 .proc_handler = &proc_dointvec,
475 .ctl_name = NET_NF_CONNTRACK_COUNT,
476 .procname = "nf_conntrack_count",
477 .data = &nf_conntrack_count,
478 .maxlen = sizeof(int),
479 .mode = 0444,
480 .proc_handler = &proc_dointvec,
483 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
484 .procname = "nf_conntrack_buckets",
485 .data = &nf_conntrack_htable_size,
486 .maxlen = sizeof(unsigned int),
487 .mode = 0444,
488 .proc_handler = &proc_dointvec,
491 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
492 .procname = "nf_conntrack_checksum",
493 .data = &nf_conntrack_checksum,
494 .maxlen = sizeof(unsigned int),
495 .mode = 0644,
496 .proc_handler = &proc_dointvec,
499 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
500 .procname = "nf_conntrack_tcp_timeout_syn_sent",
501 .data = &nf_ct_tcp_timeout_syn_sent,
502 .maxlen = sizeof(unsigned int),
503 .mode = 0644,
504 .proc_handler = &proc_dointvec_jiffies,
507 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
508 .procname = "nf_conntrack_tcp_timeout_syn_recv",
509 .data = &nf_ct_tcp_timeout_syn_recv,
510 .maxlen = sizeof(unsigned int),
511 .mode = 0644,
512 .proc_handler = &proc_dointvec_jiffies,
515 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
516 .procname = "nf_conntrack_tcp_timeout_established",
517 .data = &nf_ct_tcp_timeout_established,
518 .maxlen = sizeof(unsigned int),
519 .mode = 0644,
520 .proc_handler = &proc_dointvec_jiffies,
523 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
524 .procname = "nf_conntrack_tcp_timeout_fin_wait",
525 .data = &nf_ct_tcp_timeout_fin_wait,
526 .maxlen = sizeof(unsigned int),
527 .mode = 0644,
528 .proc_handler = &proc_dointvec_jiffies,
531 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
532 .procname = "nf_conntrack_tcp_timeout_close_wait",
533 .data = &nf_ct_tcp_timeout_close_wait,
534 .maxlen = sizeof(unsigned int),
535 .mode = 0644,
536 .proc_handler = &proc_dointvec_jiffies,
539 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
540 .procname = "nf_conntrack_tcp_timeout_last_ack",
541 .data = &nf_ct_tcp_timeout_last_ack,
542 .maxlen = sizeof(unsigned int),
543 .mode = 0644,
544 .proc_handler = &proc_dointvec_jiffies,
547 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
548 .procname = "nf_conntrack_tcp_timeout_time_wait",
549 .data = &nf_ct_tcp_timeout_time_wait,
550 .maxlen = sizeof(unsigned int),
551 .mode = 0644,
552 .proc_handler = &proc_dointvec_jiffies,
555 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
556 .procname = "nf_conntrack_tcp_timeout_close",
557 .data = &nf_ct_tcp_timeout_close,
558 .maxlen = sizeof(unsigned int),
559 .mode = 0644,
560 .proc_handler = &proc_dointvec_jiffies,
563 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT,
564 .procname = "nf_conntrack_udp_timeout",
565 .data = &nf_ct_udp_timeout,
566 .maxlen = sizeof(unsigned int),
567 .mode = 0644,
568 .proc_handler = &proc_dointvec_jiffies,
571 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
572 .procname = "nf_conntrack_udp_timeout_stream",
573 .data = &nf_ct_udp_timeout_stream,
574 .maxlen = sizeof(unsigned int),
575 .mode = 0644,
576 .proc_handler = &proc_dointvec_jiffies,
579 .ctl_name = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
580 .procname = "nf_conntrack_generic_timeout",
581 .data = &nf_ct_generic_timeout,
582 .maxlen = sizeof(unsigned int),
583 .mode = 0644,
584 .proc_handler = &proc_dointvec_jiffies,
587 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
588 .procname = "nf_conntrack_log_invalid",
589 .data = &nf_ct_log_invalid,
590 .maxlen = sizeof(unsigned int),
591 .mode = 0644,
592 .proc_handler = &proc_dointvec_minmax,
593 .strategy = &sysctl_intvec,
594 .extra1 = &log_invalid_proto_min,
595 .extra2 = &log_invalid_proto_max,
598 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
599 .procname = "nf_conntrack_tcp_timeout_max_retrans",
600 .data = &nf_ct_tcp_timeout_max_retrans,
601 .maxlen = sizeof(unsigned int),
602 .mode = 0644,
603 .proc_handler = &proc_dointvec_jiffies,
606 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
607 .procname = "nf_conntrack_tcp_loose",
608 .data = &nf_ct_tcp_loose,
609 .maxlen = sizeof(unsigned int),
610 .mode = 0644,
611 .proc_handler = &proc_dointvec,
614 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
615 .procname = "nf_conntrack_tcp_be_liberal",
616 .data = &nf_ct_tcp_be_liberal,
617 .maxlen = sizeof(unsigned int),
618 .mode = 0644,
619 .proc_handler = &proc_dointvec,
622 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
623 .procname = "nf_conntrack_tcp_max_retrans",
624 .data = &nf_ct_tcp_max_retrans,
625 .maxlen = sizeof(unsigned int),
626 .mode = 0644,
627 .proc_handler = &proc_dointvec,
630 { .ctl_name = 0 }
633 #define NET_NF_CONNTRACK_MAX 2089
635 static ctl_table nf_ct_netfilter_table[] = {
637 .ctl_name = NET_NETFILTER,
638 .procname = "netfilter",
639 .mode = 0555,
640 .child = nf_ct_sysctl_table,
643 .ctl_name = NET_NF_CONNTRACK_MAX,
644 .procname = "nf_conntrack_max",
645 .data = &nf_conntrack_max,
646 .maxlen = sizeof(int),
647 .mode = 0644,
648 .proc_handler = &proc_dointvec,
650 { .ctl_name = 0 }
653 static ctl_table nf_ct_net_table[] = {
655 .ctl_name = CTL_NET,
656 .procname = "net",
657 .mode = 0555,
658 .child = nf_ct_netfilter_table,
660 { .ctl_name = 0 }
662 EXPORT_SYMBOL(nf_ct_log_invalid);
663 #endif /* CONFIG_SYSCTL */
665 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
667 int ret = 0;
669 write_lock_bh(&nf_conntrack_lock);
670 if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_generic_l3proto) {
671 ret = -EBUSY;
672 goto out;
674 nf_ct_l3protos[proto->l3proto] = proto;
675 out:
676 write_unlock_bh(&nf_conntrack_lock);
678 return ret;
681 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
683 write_lock_bh(&nf_conntrack_lock);
684 nf_ct_l3protos[proto->l3proto] = &nf_conntrack_generic_l3proto;
685 write_unlock_bh(&nf_conntrack_lock);
687 /* Somebody could be still looking at the proto in bh. */
688 synchronize_net();
690 /* Remove all contrack entries for this protocol */
691 nf_ct_iterate_cleanup(kill_l3proto, proto);
694 /* FIXME: Allow NULL functions and sub in pointers to generic for
695 them. --RR */
696 int nf_conntrack_protocol_register(struct nf_conntrack_protocol *proto)
698 int ret = 0;
700 retry:
701 write_lock_bh(&nf_conntrack_lock);
702 if (nf_ct_protos[proto->l3proto]) {
703 if (nf_ct_protos[proto->l3proto][proto->proto]
704 != &nf_conntrack_generic_protocol) {
705 ret = -EBUSY;
706 goto out_unlock;
708 } else {
709 /* l3proto may be loaded latter. */
710 struct nf_conntrack_protocol **proto_array;
711 int i;
713 write_unlock_bh(&nf_conntrack_lock);
715 proto_array = (struct nf_conntrack_protocol **)
716 kmalloc(MAX_NF_CT_PROTO *
717 sizeof(struct nf_conntrack_protocol *),
718 GFP_KERNEL);
719 if (proto_array == NULL) {
720 ret = -ENOMEM;
721 goto out;
723 for (i = 0; i < MAX_NF_CT_PROTO; i++)
724 proto_array[i] = &nf_conntrack_generic_protocol;
726 write_lock_bh(&nf_conntrack_lock);
727 if (nf_ct_protos[proto->l3proto]) {
728 /* bad timing, but no problem */
729 write_unlock_bh(&nf_conntrack_lock);
730 kfree(proto_array);
731 } else {
732 nf_ct_protos[proto->l3proto] = proto_array;
733 write_unlock_bh(&nf_conntrack_lock);
737 * Just once because array is never freed until unloading
738 * nf_conntrack.ko
740 goto retry;
743 nf_ct_protos[proto->l3proto][proto->proto] = proto;
745 out_unlock:
746 write_unlock_bh(&nf_conntrack_lock);
747 out:
748 return ret;
751 void nf_conntrack_protocol_unregister(struct nf_conntrack_protocol *proto)
753 write_lock_bh(&nf_conntrack_lock);
754 nf_ct_protos[proto->l3proto][proto->proto]
755 = &nf_conntrack_generic_protocol;
756 write_unlock_bh(&nf_conntrack_lock);
758 /* Somebody could be still looking at the proto in bh. */
759 synchronize_net();
761 /* Remove all contrack entries for this protocol */
762 nf_ct_iterate_cleanup(kill_proto, proto);
765 static int __init nf_conntrack_standalone_init(void)
767 #ifdef CONFIG_PROC_FS
768 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
769 #endif
770 int ret = 0;
772 ret = nf_conntrack_init();
773 if (ret < 0)
774 return ret;
776 #ifdef CONFIG_PROC_FS
777 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
778 if (!proc) goto cleanup_init;
780 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
781 &exp_file_ops);
782 if (!proc_exp) goto cleanup_proc;
784 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
785 if (!proc_stat)
786 goto cleanup_proc_exp;
788 proc_stat->proc_fops = &ct_cpu_seq_fops;
789 proc_stat->owner = THIS_MODULE;
790 #endif
791 #ifdef CONFIG_SYSCTL
792 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
793 if (nf_ct_sysctl_header == NULL) {
794 printk("nf_conntrack: can't register to sysctl.\n");
795 ret = -ENOMEM;
796 goto cleanup_proc_stat;
798 #endif
799 return ret;
801 #ifdef CONFIG_SYSCTL
802 cleanup_proc_stat:
803 #endif
804 #ifdef CONFIG_PROC_FS
805 remove_proc_entry("nf_conntrack", proc_net_stat);
806 cleanup_proc_exp:
807 proc_net_remove("nf_conntrack_expect");
808 cleanup_proc:
809 proc_net_remove("nf_conntrack");
810 cleanup_init:
811 #endif /* CNFIG_PROC_FS */
812 nf_conntrack_cleanup();
813 return ret;
816 static void __exit nf_conntrack_standalone_fini(void)
818 #ifdef CONFIG_SYSCTL
819 unregister_sysctl_table(nf_ct_sysctl_header);
820 #endif
821 #ifdef CONFIG_PROC_FS
822 remove_proc_entry("nf_conntrack", proc_net_stat);
823 proc_net_remove("nf_conntrack_expect");
824 proc_net_remove("nf_conntrack");
825 #endif /* CNFIG_PROC_FS */
826 nf_conntrack_cleanup();
829 module_init(nf_conntrack_standalone_init);
830 module_exit(nf_conntrack_standalone_fini);
832 /* Some modules need us, but don't depend directly on any symbol.
833 They should call this. */
834 void need_conntrack(void)
838 #ifdef CONFIG_NF_CONNTRACK_EVENTS
839 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
840 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
841 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
842 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
843 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
844 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
845 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
846 #endif
847 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
848 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
849 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
850 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
851 EXPORT_SYMBOL(nf_conntrack_protocol_register);
852 EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
853 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
854 EXPORT_SYMBOL(nf_conntrack_destroyed);
855 EXPORT_SYMBOL(need_conntrack);
856 EXPORT_SYMBOL(nf_conntrack_helper_register);
857 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
858 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
859 EXPORT_SYMBOL(__nf_ct_refresh_acct);
860 EXPORT_SYMBOL(nf_ct_protos);
861 EXPORT_SYMBOL(__nf_ct_proto_find);
862 EXPORT_SYMBOL(nf_ct_proto_find_get);
863 EXPORT_SYMBOL(nf_ct_proto_put);
864 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
865 EXPORT_SYMBOL(nf_ct_l3proto_put);
866 EXPORT_SYMBOL(nf_ct_l3protos);
867 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
868 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
869 EXPORT_SYMBOL(nf_conntrack_expect_put);
870 EXPORT_SYMBOL(nf_conntrack_expect_related);
871 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
872 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
873 EXPORT_SYMBOL(nf_conntrack_htable_size);
874 EXPORT_SYMBOL(nf_conntrack_lock);
875 EXPORT_SYMBOL(nf_conntrack_hash);
876 EXPORT_SYMBOL(nf_conntrack_untracked);
877 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
878 #ifdef CONFIG_IP_NF_NAT_NEEDED
879 EXPORT_SYMBOL(nf_conntrack_tcp_update);
880 #endif
881 EXPORT_SYMBOL(__nf_conntrack_confirm);
882 EXPORT_SYMBOL(nf_ct_get_tuple);
883 EXPORT_SYMBOL(nf_ct_invert_tuple);
884 EXPORT_SYMBOL(nf_conntrack_in);
885 EXPORT_SYMBOL(__nf_conntrack_attach);
886 EXPORT_SYMBOL(nf_conntrack_alloc);
887 EXPORT_SYMBOL(nf_conntrack_free);
888 EXPORT_SYMBOL(nf_conntrack_flush);
889 EXPORT_SYMBOL(nf_ct_remove_expectations);
890 EXPORT_SYMBOL(nf_ct_helper_find_get);
891 EXPORT_SYMBOL(nf_ct_helper_put);
892 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
893 EXPORT_SYMBOL(__nf_conntrack_find);
894 EXPORT_SYMBOL(nf_ct_unlink_expect);
895 EXPORT_SYMBOL(nf_conntrack_hash_insert);
896 EXPORT_SYMBOL(__nf_conntrack_expect_find);
897 EXPORT_SYMBOL(nf_conntrack_expect_find);
898 EXPORT_SYMBOL(nf_conntrack_expect_list);
899 #if defined(CONFIG_NF_CT_NETLINK) || \
900 defined(CONFIG_NF_CT_NETLINK_MODULE)
901 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
902 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
903 #endif