]> err.no Git - linux-2.6/blob - net/netfilter/nf_conntrack_standalone.c
[NETFILTER]: nf_conntrack: split out the event cache
[linux-2.6] / net / netfilter / nf_conntrack_standalone.c
1 /* This file contains all the functions required for the standalone
2    nf_conntrack module.
3
4    These are not required by the compatibility layer.
5 */
6
7 /* (C) 1999-2001 Paul `Rusty' Russell
8  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
9  *
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.
13  *
14  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
15  *      - generalize L3 protocol dependent part.
16  *
17  * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
18  */
19
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
31
32 #define ASSERT_READ_LOCK(x)
33 #define ASSERT_WRITE_LOCK(x)
34
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>
41
42 #if 0
43 #define DEBUGP printk
44 #else
45 #define DEBUGP(format, args...)
46 #endif
47
48 MODULE_LICENSE("GPL");
49
50 extern atomic_t nf_conntrack_count;
51 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
52
53 static int kill_l3proto(struct nf_conn *i, void *data)
54 {
55         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num == 
56                         ((struct nf_conntrack_l3proto *)data)->l3proto);
57 }
58
59 static int kill_proto(struct nf_conn *i, void *data)
60 {
61         struct nf_conntrack_protocol *proto;
62         proto = (struct nf_conntrack_protocol *)data;
63         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum == 
64                         proto->proto) &&
65                (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
66                         proto->l3proto);
67 }
68
69 #ifdef CONFIG_PROC_FS
70 int
71 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
72             struct nf_conntrack_l3proto *l3proto,
73             struct nf_conntrack_protocol *proto)
74 {
75         return l3proto->print_tuple(s, tuple) || proto->print_tuple(s, tuple);
76 }
77
78 #ifdef CONFIG_NF_CT_ACCT
79 static unsigned int
80 seq_print_counters(struct seq_file *s,
81                    const struct ip_conntrack_counter *counter)
82 {
83         return seq_printf(s, "packets=%llu bytes=%llu ",
84                           (unsigned long long)counter->packets,
85                           (unsigned long long)counter->bytes);
86 }
87 #else
88 #define seq_print_counters(x, y)        0
89 #endif
90
91 struct ct_iter_state {
92         unsigned int bucket;
93 };
94
95 static struct list_head *ct_get_first(struct seq_file *seq)
96 {
97         struct ct_iter_state *st = seq->private;
98
99         for (st->bucket = 0;
100              st->bucket < nf_conntrack_htable_size;
101              st->bucket++) {
102                 if (!list_empty(&nf_conntrack_hash[st->bucket]))
103                         return nf_conntrack_hash[st->bucket].next;
104         }
105         return NULL;
106 }
107
108 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
109 {
110         struct ct_iter_state *st = seq->private;
111
112         head = head->next;
113         while (head == &nf_conntrack_hash[st->bucket]) {
114                 if (++st->bucket >= nf_conntrack_htable_size)
115                         return NULL;
116                 head = nf_conntrack_hash[st->bucket].next;
117         }
118         return head;
119 }
120
121 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
122 {
123         struct list_head *head = ct_get_first(seq);
124
125         if (head)
126                 while (pos && (head = ct_get_next(seq, head)))
127                         pos--;
128         return pos ? NULL : head;
129 }
130
131 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
132 {
133         read_lock_bh(&nf_conntrack_lock);
134         return ct_get_idx(seq, *pos);
135 }
136
137 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
138 {
139         (*pos)++;
140         return ct_get_next(s, v);
141 }
142
143 static void ct_seq_stop(struct seq_file *s, void *v)
144 {
145         read_unlock_bh(&nf_conntrack_lock);
146 }
147
148 /* return 0 on success, 1 in case of error */
149 static int ct_seq_show(struct seq_file *s, void *v)
150 {
151         const struct nf_conntrack_tuple_hash *hash = v;
152         const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
153         struct nf_conntrack_l3proto *l3proto;
154         struct nf_conntrack_protocol *proto;
155
156         ASSERT_READ_LOCK(&nf_conntrack_lock);
157         NF_CT_ASSERT(conntrack);
158
159         /* we only want to print DIR_ORIGINAL */
160         if (NF_CT_DIRECTION(hash))
161                 return 0;
162
163         l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
164                                        .tuple.src.l3num);
165
166         NF_CT_ASSERT(l3proto);
167         proto = __nf_ct_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
168                                    .tuple.src.l3num,
169                                    conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
170                                    .tuple.dst.protonum);
171         NF_CT_ASSERT(proto);
172
173         if (seq_printf(s, "%-8s %u %-8s %u %ld ",
174                        l3proto->name,
175                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
176                        proto->name,
177                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
178                        timer_pending(&conntrack->timeout)
179                        ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
180                 return -ENOSPC;
181
182         if (l3proto->print_conntrack(s, conntrack))
183                 return -ENOSPC;
184
185         if (proto->print_conntrack(s, conntrack))
186                 return -ENOSPC;
187
188         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
189                         l3proto, proto))
190                 return -ENOSPC;
191
192         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
193                 return -ENOSPC;
194
195         if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
196                 if (seq_printf(s, "[UNREPLIED] "))
197                         return -ENOSPC;
198
199         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
200                         l3proto, proto))
201                 return -ENOSPC;
202
203         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
204                 return -ENOSPC;
205
206         if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
207                 if (seq_printf(s, "[ASSURED] "))
208                         return -ENOSPC;
209
210 #if defined(CONFIG_NF_CONNTRACK_MARK)
211         if (seq_printf(s, "mark=%u ", conntrack->mark))
212                 return -ENOSPC;
213 #endif
214
215 #ifdef CONFIG_NF_CONNTRACK_SECMARK
216         if (seq_printf(s, "secmark=%u ", conntrack->secmark))
217                 return -ENOSPC;
218 #endif
219
220         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
221                 return -ENOSPC;
222         
223         return 0;
224 }
225
226 static struct seq_operations ct_seq_ops = {
227         .start = ct_seq_start,
228         .next  = ct_seq_next,
229         .stop  = ct_seq_stop,
230         .show  = ct_seq_show
231 };
232
233 static int ct_open(struct inode *inode, struct file *file)
234 {
235         struct seq_file *seq;
236         struct ct_iter_state *st;
237         int ret;
238
239         st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
240         if (st == NULL)
241                 return -ENOMEM;
242         ret = seq_open(file, &ct_seq_ops);
243         if (ret)
244                 goto out_free;
245         seq          = file->private_data;
246         seq->private = st;
247         memset(st, 0, sizeof(struct ct_iter_state));
248         return ret;
249 out_free:
250         kfree(st);
251         return ret;
252 }
253
254 static struct file_operations ct_file_ops = {
255         .owner   = THIS_MODULE,
256         .open    = ct_open,
257         .read    = seq_read,
258         .llseek  = seq_lseek,
259         .release = seq_release_private,
260 };
261
262 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
263 {
264         int cpu;
265
266         if (*pos == 0)
267                 return SEQ_START_TOKEN;
268
269         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
270                 if (!cpu_possible(cpu))
271                         continue;
272                 *pos = cpu + 1;
273                 return &per_cpu(nf_conntrack_stat, cpu);
274         }
275
276         return NULL;
277 }
278
279 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
280 {
281         int cpu;
282
283         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
284                 if (!cpu_possible(cpu))
285                         continue;
286                 *pos = cpu + 1;
287                 return &per_cpu(nf_conntrack_stat, cpu);
288         }
289
290         return NULL;
291 }
292
293 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
294 {
295 }
296
297 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
298 {
299         unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
300         struct ip_conntrack_stat *st = v;
301
302         if (v == SEQ_START_TOKEN) {
303                 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");
304                 return 0;
305         }
306
307         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
308                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
309                    nr_conntracks,
310                    st->searched,
311                    st->found,
312                    st->new,
313                    st->invalid,
314                    st->ignore,
315                    st->delete,
316                    st->delete_list,
317                    st->insert,
318                    st->insert_failed,
319                    st->drop,
320                    st->early_drop,
321                    st->error,
322
323                    st->expect_new,
324                    st->expect_create,
325                    st->expect_delete
326                 );
327         return 0;
328 }
329
330 static struct seq_operations ct_cpu_seq_ops = {
331         .start  = ct_cpu_seq_start,
332         .next   = ct_cpu_seq_next,
333         .stop   = ct_cpu_seq_stop,
334         .show   = ct_cpu_seq_show,
335 };
336
337 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
338 {
339         return seq_open(file, &ct_cpu_seq_ops);
340 }
341
342 static struct file_operations ct_cpu_seq_fops = {
343         .owner   = THIS_MODULE,
344         .open    = ct_cpu_seq_open,
345         .read    = seq_read,
346         .llseek  = seq_lseek,
347         .release = seq_release_private,
348 };
349 #endif /* CONFIG_PROC_FS */
350
351 /* Sysctl support */
352
353 int nf_conntrack_checksum __read_mostly = 1;
354
355 #ifdef CONFIG_SYSCTL
356
357 /* From nf_conntrack_core.c */
358 extern int nf_conntrack_max;
359 extern unsigned int nf_conntrack_htable_size;
360
361 /* From nf_conntrack_proto_tcp.c */
362 extern unsigned int nf_ct_tcp_timeout_syn_sent;
363 extern unsigned int nf_ct_tcp_timeout_syn_recv;
364 extern unsigned int nf_ct_tcp_timeout_established;
365 extern unsigned int nf_ct_tcp_timeout_fin_wait;
366 extern unsigned int nf_ct_tcp_timeout_close_wait;
367 extern unsigned int nf_ct_tcp_timeout_last_ack;
368 extern unsigned int nf_ct_tcp_timeout_time_wait;
369 extern unsigned int nf_ct_tcp_timeout_close;
370 extern unsigned int nf_ct_tcp_timeout_max_retrans;
371 extern int nf_ct_tcp_loose;
372 extern int nf_ct_tcp_be_liberal;
373 extern int nf_ct_tcp_max_retrans;
374
375 /* From nf_conntrack_proto_udp.c */
376 extern unsigned int nf_ct_udp_timeout;
377 extern unsigned int nf_ct_udp_timeout_stream;
378
379 /* From nf_conntrack_proto_generic.c */
380 extern unsigned int nf_ct_generic_timeout;
381
382 /* Log invalid packets of a given protocol */
383 static int log_invalid_proto_min = 0;
384 static int log_invalid_proto_max = 255;
385
386 static struct ctl_table_header *nf_ct_sysctl_header;
387
388 static ctl_table nf_ct_sysctl_table[] = {
389         {
390                 .ctl_name       = NET_NF_CONNTRACK_MAX,
391                 .procname       = "nf_conntrack_max",
392                 .data           = &nf_conntrack_max,
393                 .maxlen         = sizeof(int),
394                 .mode           = 0644,
395                 .proc_handler   = &proc_dointvec,
396         },
397         {
398                 .ctl_name       = NET_NF_CONNTRACK_COUNT,
399                 .procname       = "nf_conntrack_count",
400                 .data           = &nf_conntrack_count,
401                 .maxlen         = sizeof(int),
402                 .mode           = 0444,
403                 .proc_handler   = &proc_dointvec,
404         },
405         {
406                 .ctl_name       = NET_NF_CONNTRACK_BUCKETS,
407                 .procname       = "nf_conntrack_buckets",
408                 .data           = &nf_conntrack_htable_size,
409                 .maxlen         = sizeof(unsigned int),
410                 .mode           = 0444,
411                 .proc_handler   = &proc_dointvec,
412         },
413         {
414                 .ctl_name       = NET_NF_CONNTRACK_CHECKSUM,
415                 .procname       = "nf_conntrack_checksum",
416                 .data           = &nf_conntrack_checksum,
417                 .maxlen         = sizeof(unsigned int),
418                 .mode           = 0644,
419                 .proc_handler   = &proc_dointvec,
420         },
421         {
422                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
423                 .procname       = "nf_conntrack_tcp_timeout_syn_sent",
424                 .data           = &nf_ct_tcp_timeout_syn_sent,
425                 .maxlen         = sizeof(unsigned int),
426                 .mode           = 0644,
427                 .proc_handler   = &proc_dointvec_jiffies,
428         },
429         {
430                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
431                 .procname       = "nf_conntrack_tcp_timeout_syn_recv",
432                 .data           = &nf_ct_tcp_timeout_syn_recv,
433                 .maxlen         = sizeof(unsigned int),
434                 .mode           = 0644,
435                 .proc_handler   = &proc_dointvec_jiffies,
436         },
437         {
438                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
439                 .procname       = "nf_conntrack_tcp_timeout_established",
440                 .data           = &nf_ct_tcp_timeout_established,
441                 .maxlen         = sizeof(unsigned int),
442                 .mode           = 0644,
443                 .proc_handler   = &proc_dointvec_jiffies,
444         },
445         {
446                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
447                 .procname       = "nf_conntrack_tcp_timeout_fin_wait",
448                 .data           = &nf_ct_tcp_timeout_fin_wait,
449                 .maxlen         = sizeof(unsigned int),
450                 .mode           = 0644,
451                 .proc_handler   = &proc_dointvec_jiffies,
452         },
453         {
454                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
455                 .procname       = "nf_conntrack_tcp_timeout_close_wait",
456                 .data           = &nf_ct_tcp_timeout_close_wait,
457                 .maxlen         = sizeof(unsigned int),
458                 .mode           = 0644,
459                 .proc_handler   = &proc_dointvec_jiffies,
460         },
461         {
462                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
463                 .procname       = "nf_conntrack_tcp_timeout_last_ack",
464                 .data           = &nf_ct_tcp_timeout_last_ack,
465                 .maxlen         = sizeof(unsigned int),
466                 .mode           = 0644,
467                 .proc_handler   = &proc_dointvec_jiffies,
468         },
469         {
470                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
471                 .procname       = "nf_conntrack_tcp_timeout_time_wait",
472                 .data           = &nf_ct_tcp_timeout_time_wait,
473                 .maxlen         = sizeof(unsigned int),
474                 .mode           = 0644,
475                 .proc_handler   = &proc_dointvec_jiffies,
476         },
477         {
478                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
479                 .procname       = "nf_conntrack_tcp_timeout_close",
480                 .data           = &nf_ct_tcp_timeout_close,
481                 .maxlen         = sizeof(unsigned int),
482                 .mode           = 0644,
483                 .proc_handler   = &proc_dointvec_jiffies,
484         },
485         {
486                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT,
487                 .procname       = "nf_conntrack_udp_timeout",
488                 .data           = &nf_ct_udp_timeout,
489                 .maxlen         = sizeof(unsigned int),
490                 .mode           = 0644,
491                 .proc_handler   = &proc_dointvec_jiffies,
492         },
493         {
494                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
495                 .procname       = "nf_conntrack_udp_timeout_stream",
496                 .data           = &nf_ct_udp_timeout_stream,
497                 .maxlen         = sizeof(unsigned int),
498                 .mode           = 0644,
499                 .proc_handler   = &proc_dointvec_jiffies,
500         },
501         {
502                 .ctl_name       = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
503                 .procname       = "nf_conntrack_generic_timeout",
504                 .data           = &nf_ct_generic_timeout,
505                 .maxlen         = sizeof(unsigned int),
506                 .mode           = 0644,
507                 .proc_handler   = &proc_dointvec_jiffies,
508         },
509         {
510                 .ctl_name       = NET_NF_CONNTRACK_LOG_INVALID,
511                 .procname       = "nf_conntrack_log_invalid",
512                 .data           = &nf_ct_log_invalid,
513                 .maxlen         = sizeof(unsigned int),
514                 .mode           = 0644,
515                 .proc_handler   = &proc_dointvec_minmax,
516                 .strategy       = &sysctl_intvec,
517                 .extra1         = &log_invalid_proto_min,
518                 .extra2         = &log_invalid_proto_max,
519         },
520         {
521                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
522                 .procname       = "nf_conntrack_tcp_timeout_max_retrans",
523                 .data           = &nf_ct_tcp_timeout_max_retrans,
524                 .maxlen         = sizeof(unsigned int),
525                 .mode           = 0644,
526                 .proc_handler   = &proc_dointvec_jiffies,
527         },
528         {
529                 .ctl_name       = NET_NF_CONNTRACK_TCP_LOOSE,
530                 .procname       = "nf_conntrack_tcp_loose",
531                 .data           = &nf_ct_tcp_loose,
532                 .maxlen         = sizeof(unsigned int),
533                 .mode           = 0644,
534                 .proc_handler   = &proc_dointvec,
535         },
536         {
537                 .ctl_name       = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
538                 .procname       = "nf_conntrack_tcp_be_liberal",
539                 .data           = &nf_ct_tcp_be_liberal,
540                 .maxlen         = sizeof(unsigned int),
541                 .mode           = 0644,
542                 .proc_handler   = &proc_dointvec,
543         },
544         {
545                 .ctl_name       = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
546                 .procname       = "nf_conntrack_tcp_max_retrans",
547                 .data           = &nf_ct_tcp_max_retrans,
548                 .maxlen         = sizeof(unsigned int),
549                 .mode           = 0644,
550                 .proc_handler   = &proc_dointvec,
551         },
552
553         { .ctl_name = 0 }
554 };
555
556 #define NET_NF_CONNTRACK_MAX 2089
557
558 static ctl_table nf_ct_netfilter_table[] = {
559         {
560                 .ctl_name       = NET_NETFILTER,
561                 .procname       = "netfilter",
562                 .mode           = 0555,
563                 .child          = nf_ct_sysctl_table,
564         },
565         {
566                 .ctl_name       = NET_NF_CONNTRACK_MAX,
567                 .procname       = "nf_conntrack_max",
568                 .data           = &nf_conntrack_max,
569                 .maxlen         = sizeof(int),
570                 .mode           = 0644,
571                 .proc_handler   = &proc_dointvec,
572         },
573         { .ctl_name = 0 }
574 };
575
576 static ctl_table nf_ct_net_table[] = {
577         {
578                 .ctl_name       = CTL_NET,
579                 .procname       = "net",
580                 .mode           = 0555,
581                 .child          = nf_ct_netfilter_table,
582         },
583         { .ctl_name = 0 }
584 };
585 EXPORT_SYMBOL(nf_ct_log_invalid);
586 #endif /* CONFIG_SYSCTL */
587
588 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
589 {
590         int ret = 0;
591
592         write_lock_bh(&nf_conntrack_lock);
593         if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_generic_l3proto) {
594                 ret = -EBUSY;
595                 goto out;
596         }
597         nf_ct_l3protos[proto->l3proto] = proto;
598 out:
599         write_unlock_bh(&nf_conntrack_lock);
600
601         return ret;
602 }
603
604 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
605 {
606         write_lock_bh(&nf_conntrack_lock);
607         nf_ct_l3protos[proto->l3proto] = &nf_conntrack_generic_l3proto;
608         write_unlock_bh(&nf_conntrack_lock);
609         
610         /* Somebody could be still looking at the proto in bh. */
611         synchronize_net();
612
613         /* Remove all contrack entries for this protocol */
614         nf_ct_iterate_cleanup(kill_l3proto, proto);
615 }
616
617 /* FIXME: Allow NULL functions and sub in pointers to generic for
618    them. --RR */
619 int nf_conntrack_protocol_register(struct nf_conntrack_protocol *proto)
620 {
621         int ret = 0;
622
623 retry:
624         write_lock_bh(&nf_conntrack_lock);
625         if (nf_ct_protos[proto->l3proto]) {
626                 if (nf_ct_protos[proto->l3proto][proto->proto]
627                                 != &nf_conntrack_generic_protocol) {
628                         ret = -EBUSY;
629                         goto out_unlock;
630                 }
631         } else {
632                 /* l3proto may be loaded latter. */
633                 struct nf_conntrack_protocol **proto_array;
634                 int i;
635
636                 write_unlock_bh(&nf_conntrack_lock);
637
638                 proto_array = (struct nf_conntrack_protocol **)
639                                 kmalloc(MAX_NF_CT_PROTO *
640                                          sizeof(struct nf_conntrack_protocol *),
641                                         GFP_KERNEL);
642                 if (proto_array == NULL) {
643                         ret = -ENOMEM;
644                         goto out;
645                 }
646                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
647                         proto_array[i] = &nf_conntrack_generic_protocol;
648
649                 write_lock_bh(&nf_conntrack_lock);
650                 if (nf_ct_protos[proto->l3proto]) {
651                         /* bad timing, but no problem */
652                         write_unlock_bh(&nf_conntrack_lock);
653                         kfree(proto_array);
654                 } else {
655                         nf_ct_protos[proto->l3proto] = proto_array;
656                         write_unlock_bh(&nf_conntrack_lock);
657                 }
658
659                 /*
660                  * Just once because array is never freed until unloading
661                  * nf_conntrack.ko
662                  */
663                 goto retry;
664         }
665
666         nf_ct_protos[proto->l3proto][proto->proto] = proto;
667
668 out_unlock:
669         write_unlock_bh(&nf_conntrack_lock);
670 out:
671         return ret;
672 }
673
674 void nf_conntrack_protocol_unregister(struct nf_conntrack_protocol *proto)
675 {
676         write_lock_bh(&nf_conntrack_lock);
677         nf_ct_protos[proto->l3proto][proto->proto]
678                 = &nf_conntrack_generic_protocol;
679         write_unlock_bh(&nf_conntrack_lock);
680         
681         /* Somebody could be still looking at the proto in bh. */
682         synchronize_net();
683
684         /* Remove all contrack entries for this protocol */
685         nf_ct_iterate_cleanup(kill_proto, proto);
686 }
687
688 static int __init nf_conntrack_standalone_init(void)
689 {
690 #ifdef CONFIG_PROC_FS
691         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
692 #endif
693         int ret = 0;
694
695         ret = nf_conntrack_init();
696         if (ret < 0)
697                 return ret;
698
699 #ifdef CONFIG_PROC_FS
700         proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
701         if (!proc) goto cleanup_init;
702
703         proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
704                                         &exp_file_ops);
705         if (!proc_exp) goto cleanup_proc;
706
707         proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
708         if (!proc_stat)
709                 goto cleanup_proc_exp;
710
711         proc_stat->proc_fops = &ct_cpu_seq_fops;
712         proc_stat->owner = THIS_MODULE;
713 #endif
714 #ifdef CONFIG_SYSCTL
715         nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
716         if (nf_ct_sysctl_header == NULL) {
717                 printk("nf_conntrack: can't register to sysctl.\n");
718                 ret = -ENOMEM;
719                 goto cleanup_proc_stat;
720         }
721 #endif
722         return ret;
723
724 #ifdef CONFIG_SYSCTL
725  cleanup_proc_stat:
726 #endif
727 #ifdef CONFIG_PROC_FS
728         remove_proc_entry("nf_conntrack", proc_net_stat);
729  cleanup_proc_exp:
730         proc_net_remove("nf_conntrack_expect");
731  cleanup_proc:
732         proc_net_remove("nf_conntrack");
733  cleanup_init:
734 #endif /* CNFIG_PROC_FS */
735         nf_conntrack_cleanup();
736         return ret;
737 }
738
739 static void __exit nf_conntrack_standalone_fini(void)
740 {
741 #ifdef CONFIG_SYSCTL
742         unregister_sysctl_table(nf_ct_sysctl_header);
743 #endif
744 #ifdef CONFIG_PROC_FS
745         remove_proc_entry("nf_conntrack", proc_net_stat);
746         proc_net_remove("nf_conntrack_expect");
747         proc_net_remove("nf_conntrack");
748 #endif /* CNFIG_PROC_FS */
749         nf_conntrack_cleanup();
750 }
751
752 module_init(nf_conntrack_standalone_init);
753 module_exit(nf_conntrack_standalone_fini);
754
755 /* Some modules need us, but don't depend directly on any symbol.
756    They should call this. */
757 void need_conntrack(void)
758 {
759 }
760
761 #ifdef CONFIG_NF_CONNTRACK_EVENTS
762 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
763 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
764 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
765 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
766 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
767 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
768 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
769 #endif
770 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
771 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
772 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
773 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
774 EXPORT_SYMBOL(nf_conntrack_protocol_register);
775 EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
776 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
777 EXPORT_SYMBOL(nf_conntrack_destroyed);
778 EXPORT_SYMBOL(need_conntrack);
779 EXPORT_SYMBOL(nf_conntrack_helper_register);
780 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
781 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
782 EXPORT_SYMBOL(__nf_ct_refresh_acct);
783 EXPORT_SYMBOL(nf_ct_protos);
784 EXPORT_SYMBOL(__nf_ct_proto_find);
785 EXPORT_SYMBOL(nf_ct_proto_find_get);
786 EXPORT_SYMBOL(nf_ct_proto_put);
787 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
788 EXPORT_SYMBOL(nf_ct_l3proto_put);
789 EXPORT_SYMBOL(nf_ct_l3protos);
790 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
791 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
792 EXPORT_SYMBOL(nf_conntrack_expect_put);
793 EXPORT_SYMBOL(nf_conntrack_expect_related);
794 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
795 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
796 EXPORT_SYMBOL(nf_conntrack_htable_size);
797 EXPORT_SYMBOL(nf_conntrack_lock);
798 EXPORT_SYMBOL(nf_conntrack_hash);
799 EXPORT_SYMBOL(nf_conntrack_untracked);
800 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
801 #ifdef CONFIG_IP_NF_NAT_NEEDED
802 EXPORT_SYMBOL(nf_conntrack_tcp_update);
803 #endif
804 EXPORT_SYMBOL(__nf_conntrack_confirm);
805 EXPORT_SYMBOL(nf_ct_get_tuple);
806 EXPORT_SYMBOL(nf_ct_invert_tuple);
807 EXPORT_SYMBOL(nf_conntrack_in);
808 EXPORT_SYMBOL(__nf_conntrack_attach);
809 EXPORT_SYMBOL(nf_conntrack_alloc);
810 EXPORT_SYMBOL(nf_conntrack_free);
811 EXPORT_SYMBOL(nf_conntrack_flush);
812 EXPORT_SYMBOL(nf_ct_remove_expectations);
813 EXPORT_SYMBOL(nf_ct_helper_find_get);
814 EXPORT_SYMBOL(nf_ct_helper_put);
815 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
816 EXPORT_SYMBOL(__nf_conntrack_find);
817 EXPORT_SYMBOL(nf_ct_unlink_expect);
818 EXPORT_SYMBOL(nf_conntrack_hash_insert);
819 EXPORT_SYMBOL(__nf_conntrack_expect_find);
820 EXPORT_SYMBOL(nf_conntrack_expect_find);
821 EXPORT_SYMBOL(nf_conntrack_expect_list);
822 #if defined(CONFIG_NF_CT_NETLINK) || \
823     defined(CONFIG_NF_CT_NETLINK_MODULE)
824 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
825 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
826 #endif