[NETFILTER]: Fix nf_conntrack_ftp.c build.
[linux-2.6.git] / 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_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>
40 #include <linux/netfilter_ipv4/listhelp.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 static 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 /* expects */
263 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
264 {
265         struct list_head *e = &nf_conntrack_expect_list;
266         loff_t i;
267
268         /* strange seq_file api calls stop even if we fail,
269          * thus we need to grab lock since stop unlocks */
270         read_lock_bh(&nf_conntrack_lock);
271
272         if (list_empty(e))
273                 return NULL;
274
275         for (i = 0; i <= *pos; i++) {
276                 e = e->next;
277                 if (e == &nf_conntrack_expect_list)
278                         return NULL;
279         }
280         return e;
281 }
282
283 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
284 {
285         struct list_head *e = v;
286
287         ++*pos;
288         e = e->next;
289
290         if (e == &nf_conntrack_expect_list)
291                 return NULL;
292
293         return e;
294 }
295
296 static void exp_seq_stop(struct seq_file *s, void *v)
297 {
298         read_unlock_bh(&nf_conntrack_lock);
299 }
300
301 static int exp_seq_show(struct seq_file *s, void *v)
302 {
303         struct nf_conntrack_expect *expect = v;
304
305         if (expect->timeout.function)
306                 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
307                            ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
308         else
309                 seq_printf(s, "- ");
310         seq_printf(s, "l3proto = %u proto=%u ",
311                    expect->tuple.src.l3num,
312                    expect->tuple.dst.protonum);
313         print_tuple(s, &expect->tuple,
314                     __nf_ct_l3proto_find(expect->tuple.src.l3num),
315                     __nf_ct_proto_find(expect->tuple.src.l3num,
316                                        expect->tuple.dst.protonum));
317         return seq_putc(s, '\n');
318 }
319
320 static struct seq_operations exp_seq_ops = {
321         .start = exp_seq_start,
322         .next = exp_seq_next,
323         .stop = exp_seq_stop,
324         .show = exp_seq_show
325 };
326
327 static int exp_open(struct inode *inode, struct file *file)
328 {
329         return seq_open(file, &exp_seq_ops);
330 }
331
332 static struct file_operations exp_file_ops = {
333         .owner   = THIS_MODULE,
334         .open    = exp_open,
335         .read    = seq_read,
336         .llseek  = seq_lseek,
337         .release = seq_release
338 };
339
340 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
341 {
342         int cpu;
343
344         if (*pos == 0)
345                 return SEQ_START_TOKEN;
346
347         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
348                 if (!cpu_possible(cpu))
349                         continue;
350                 *pos = cpu + 1;
351                 return &per_cpu(nf_conntrack_stat, cpu);
352         }
353
354         return NULL;
355 }
356
357 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
358 {
359         int cpu;
360
361         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
362                 if (!cpu_possible(cpu))
363                         continue;
364                 *pos = cpu + 1;
365                 return &per_cpu(nf_conntrack_stat, cpu);
366         }
367
368         return NULL;
369 }
370
371 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
372 {
373 }
374
375 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
376 {
377         unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
378         struct ip_conntrack_stat *st = v;
379
380         if (v == SEQ_START_TOKEN) {
381                 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");
382                 return 0;
383         }
384
385         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
386                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
387                    nr_conntracks,
388                    st->searched,
389                    st->found,
390                    st->new,
391                    st->invalid,
392                    st->ignore,
393                    st->delete,
394                    st->delete_list,
395                    st->insert,
396                    st->insert_failed,
397                    st->drop,
398                    st->early_drop,
399                    st->error,
400
401                    st->expect_new,
402                    st->expect_create,
403                    st->expect_delete
404                 );
405         return 0;
406 }
407
408 static struct seq_operations ct_cpu_seq_ops = {
409         .start  = ct_cpu_seq_start,
410         .next   = ct_cpu_seq_next,
411         .stop   = ct_cpu_seq_stop,
412         .show   = ct_cpu_seq_show,
413 };
414
415 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
416 {
417         return seq_open(file, &ct_cpu_seq_ops);
418 }
419
420 static struct file_operations ct_cpu_seq_fops = {
421         .owner   = THIS_MODULE,
422         .open    = ct_cpu_seq_open,
423         .read    = seq_read,
424         .llseek  = seq_lseek,
425         .release = seq_release_private,
426 };
427 #endif /* CONFIG_PROC_FS */
428
429 /* Sysctl support */
430
431 int nf_conntrack_checksum = 1;
432
433 #ifdef CONFIG_SYSCTL
434
435 /* From nf_conntrack_core.c */
436 extern int nf_conntrack_max;
437 extern unsigned int nf_conntrack_htable_size;
438
439 /* From nf_conntrack_proto_tcp.c */
440 extern unsigned int nf_ct_tcp_timeout_syn_sent;
441 extern unsigned int nf_ct_tcp_timeout_syn_recv;
442 extern unsigned int nf_ct_tcp_timeout_established;
443 extern unsigned int nf_ct_tcp_timeout_fin_wait;
444 extern unsigned int nf_ct_tcp_timeout_close_wait;
445 extern unsigned int nf_ct_tcp_timeout_last_ack;
446 extern unsigned int nf_ct_tcp_timeout_time_wait;
447 extern unsigned int nf_ct_tcp_timeout_close;
448 extern unsigned int nf_ct_tcp_timeout_max_retrans;
449 extern int nf_ct_tcp_loose;
450 extern int nf_ct_tcp_be_liberal;
451 extern int nf_ct_tcp_max_retrans;
452
453 /* From nf_conntrack_proto_udp.c */
454 extern unsigned int nf_ct_udp_timeout;
455 extern unsigned int nf_ct_udp_timeout_stream;
456
457 /* From nf_conntrack_proto_generic.c */
458 extern unsigned int nf_ct_generic_timeout;
459
460 /* Log invalid packets of a given protocol */
461 static int log_invalid_proto_min = 0;
462 static int log_invalid_proto_max = 255;
463
464 static struct ctl_table_header *nf_ct_sysctl_header;
465
466 static ctl_table nf_ct_sysctl_table[] = {
467         {
468                 .ctl_name       = NET_NF_CONNTRACK_MAX,
469                 .procname       = "nf_conntrack_max",
470                 .data           = &nf_conntrack_max,
471                 .maxlen         = sizeof(int),
472                 .mode           = 0644,
473                 .proc_handler   = &proc_dointvec,
474         },
475         {
476                 .ctl_name       = NET_NF_CONNTRACK_COUNT,
477                 .procname       = "nf_conntrack_count",
478                 .data           = &nf_conntrack_count,
479                 .maxlen         = sizeof(int),
480                 .mode           = 0444,
481                 .proc_handler   = &proc_dointvec,
482         },
483         {
484                 .ctl_name       = NET_NF_CONNTRACK_BUCKETS,
485                 .procname       = "nf_conntrack_buckets",
486                 .data           = &nf_conntrack_htable_size,
487                 .maxlen         = sizeof(unsigned int),
488                 .mode           = 0444,
489                 .proc_handler   = &proc_dointvec,
490         },
491         {
492                 .ctl_name       = NET_NF_CONNTRACK_CHECKSUM,
493                 .procname       = "nf_conntrack_checksum",
494                 .data           = &nf_conntrack_checksum,
495                 .maxlen         = sizeof(unsigned int),
496                 .mode           = 0644,
497                 .proc_handler   = &proc_dointvec,
498         },
499         {
500                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
501                 .procname       = "nf_conntrack_tcp_timeout_syn_sent",
502                 .data           = &nf_ct_tcp_timeout_syn_sent,
503                 .maxlen         = sizeof(unsigned int),
504                 .mode           = 0644,
505                 .proc_handler   = &proc_dointvec_jiffies,
506         },
507         {
508                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
509                 .procname       = "nf_conntrack_tcp_timeout_syn_recv",
510                 .data           = &nf_ct_tcp_timeout_syn_recv,
511                 .maxlen         = sizeof(unsigned int),
512                 .mode           = 0644,
513                 .proc_handler   = &proc_dointvec_jiffies,
514         },
515         {
516                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
517                 .procname       = "nf_conntrack_tcp_timeout_established",
518                 .data           = &nf_ct_tcp_timeout_established,
519                 .maxlen         = sizeof(unsigned int),
520                 .mode           = 0644,
521                 .proc_handler   = &proc_dointvec_jiffies,
522         },
523         {
524                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
525                 .procname       = "nf_conntrack_tcp_timeout_fin_wait",
526                 .data           = &nf_ct_tcp_timeout_fin_wait,
527                 .maxlen         = sizeof(unsigned int),
528                 .mode           = 0644,
529                 .proc_handler   = &proc_dointvec_jiffies,
530         },
531         {
532                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
533                 .procname       = "nf_conntrack_tcp_timeout_close_wait",
534                 .data           = &nf_ct_tcp_timeout_close_wait,
535                 .maxlen         = sizeof(unsigned int),
536                 .mode           = 0644,
537                 .proc_handler   = &proc_dointvec_jiffies,
538         },
539         {
540                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
541                 .procname       = "nf_conntrack_tcp_timeout_last_ack",
542                 .data           = &nf_ct_tcp_timeout_last_ack,
543                 .maxlen         = sizeof(unsigned int),
544                 .mode           = 0644,
545                 .proc_handler   = &proc_dointvec_jiffies,
546         },
547         {
548                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
549                 .procname       = "nf_conntrack_tcp_timeout_time_wait",
550                 .data           = &nf_ct_tcp_timeout_time_wait,
551                 .maxlen         = sizeof(unsigned int),
552                 .mode           = 0644,
553                 .proc_handler   = &proc_dointvec_jiffies,
554         },
555         {
556                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
557                 .procname       = "nf_conntrack_tcp_timeout_close",
558                 .data           = &nf_ct_tcp_timeout_close,
559                 .maxlen         = sizeof(unsigned int),
560                 .mode           = 0644,
561                 .proc_handler   = &proc_dointvec_jiffies,
562         },
563         {
564                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT,
565                 .procname       = "nf_conntrack_udp_timeout",
566                 .data           = &nf_ct_udp_timeout,
567                 .maxlen         = sizeof(unsigned int),
568                 .mode           = 0644,
569                 .proc_handler   = &proc_dointvec_jiffies,
570         },
571         {
572                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
573                 .procname       = "nf_conntrack_udp_timeout_stream",
574                 .data           = &nf_ct_udp_timeout_stream,
575                 .maxlen         = sizeof(unsigned int),
576                 .mode           = 0644,
577                 .proc_handler   = &proc_dointvec_jiffies,
578         },
579         {
580                 .ctl_name       = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
581                 .procname       = "nf_conntrack_generic_timeout",
582                 .data           = &nf_ct_generic_timeout,
583                 .maxlen         = sizeof(unsigned int),
584                 .mode           = 0644,
585                 .proc_handler   = &proc_dointvec_jiffies,
586         },
587         {
588                 .ctl_name       = NET_NF_CONNTRACK_LOG_INVALID,
589                 .procname       = "nf_conntrack_log_invalid",
590                 .data           = &nf_ct_log_invalid,
591                 .maxlen         = sizeof(unsigned int),
592                 .mode           = 0644,
593                 .proc_handler   = &proc_dointvec_minmax,
594                 .strategy       = &sysctl_intvec,
595                 .extra1         = &log_invalid_proto_min,
596                 .extra2         = &log_invalid_proto_max,
597         },
598         {
599                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
600                 .procname       = "nf_conntrack_tcp_timeout_max_retrans",
601                 .data           = &nf_ct_tcp_timeout_max_retrans,
602                 .maxlen         = sizeof(unsigned int),
603                 .mode           = 0644,
604                 .proc_handler   = &proc_dointvec_jiffies,
605         },
606         {
607                 .ctl_name       = NET_NF_CONNTRACK_TCP_LOOSE,
608                 .procname       = "nf_conntrack_tcp_loose",
609                 .data           = &nf_ct_tcp_loose,
610                 .maxlen         = sizeof(unsigned int),
611                 .mode           = 0644,
612                 .proc_handler   = &proc_dointvec,
613         },
614         {
615                 .ctl_name       = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
616                 .procname       = "nf_conntrack_tcp_be_liberal",
617                 .data           = &nf_ct_tcp_be_liberal,
618                 .maxlen         = sizeof(unsigned int),
619                 .mode           = 0644,
620                 .proc_handler   = &proc_dointvec,
621         },
622         {
623                 .ctl_name       = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
624                 .procname       = "nf_conntrack_tcp_max_retrans",
625                 .data           = &nf_ct_tcp_max_retrans,
626                 .maxlen         = sizeof(unsigned int),
627                 .mode           = 0644,
628                 .proc_handler   = &proc_dointvec,
629         },
630
631         { .ctl_name = 0 }
632 };
633
634 #define NET_NF_CONNTRACK_MAX 2089
635
636 static ctl_table nf_ct_netfilter_table[] = {
637         {
638                 .ctl_name       = NET_NETFILTER,
639                 .procname       = "netfilter",
640                 .mode           = 0555,
641                 .child          = nf_ct_sysctl_table,
642         },
643         {
644                 .ctl_name       = NET_NF_CONNTRACK_MAX,
645                 .procname       = "nf_conntrack_max",
646                 .data           = &nf_conntrack_max,
647                 .maxlen         = sizeof(int),
648                 .mode           = 0644,
649                 .proc_handler   = &proc_dointvec,
650         },
651         { .ctl_name = 0 }
652 };
653
654 static ctl_table nf_ct_net_table[] = {
655         {
656                 .ctl_name       = CTL_NET,
657                 .procname       = "net",
658                 .mode           = 0555,
659                 .child          = nf_ct_netfilter_table,
660         },
661         { .ctl_name = 0 }
662 };
663 EXPORT_SYMBOL(nf_ct_log_invalid);
664 #endif /* CONFIG_SYSCTL */
665
666 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
667 {
668         int ret = 0;
669
670         write_lock_bh(&nf_conntrack_lock);
671         if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_generic_l3proto) {
672                 ret = -EBUSY;
673                 goto out;
674         }
675         nf_ct_l3protos[proto->l3proto] = proto;
676 out:
677         write_unlock_bh(&nf_conntrack_lock);
678
679         return ret;
680 }
681
682 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
683 {
684         write_lock_bh(&nf_conntrack_lock);
685         nf_ct_l3protos[proto->l3proto] = &nf_conntrack_generic_l3proto;
686         write_unlock_bh(&nf_conntrack_lock);
687         
688         /* Somebody could be still looking at the proto in bh. */
689         synchronize_net();
690
691         /* Remove all contrack entries for this protocol */
692         nf_ct_iterate_cleanup(kill_l3proto, proto);
693 }
694
695 /* FIXME: Allow NULL functions and sub in pointers to generic for
696    them. --RR */
697 int nf_conntrack_protocol_register(struct nf_conntrack_protocol *proto)
698 {
699         int ret = 0;
700
701 retry:
702         write_lock_bh(&nf_conntrack_lock);
703         if (nf_ct_protos[proto->l3proto]) {
704                 if (nf_ct_protos[proto->l3proto][proto->proto]
705                                 != &nf_conntrack_generic_protocol) {
706                         ret = -EBUSY;
707                         goto out_unlock;
708                 }
709         } else {
710                 /* l3proto may be loaded latter. */
711                 struct nf_conntrack_protocol **proto_array;
712                 int i;
713
714                 write_unlock_bh(&nf_conntrack_lock);
715
716                 proto_array = (struct nf_conntrack_protocol **)
717                                 kmalloc(MAX_NF_CT_PROTO *
718                                          sizeof(struct nf_conntrack_protocol *),
719                                         GFP_KERNEL);
720                 if (proto_array == NULL) {
721                         ret = -ENOMEM;
722                         goto out;
723                 }
724                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
725                         proto_array[i] = &nf_conntrack_generic_protocol;
726
727                 write_lock_bh(&nf_conntrack_lock);
728                 if (nf_ct_protos[proto->l3proto]) {
729                         /* bad timing, but no problem */
730                         write_unlock_bh(&nf_conntrack_lock);
731                         kfree(proto_array);
732                 } else {
733                         nf_ct_protos[proto->l3proto] = proto_array;
734                         write_unlock_bh(&nf_conntrack_lock);
735                 }
736
737                 /*
738                  * Just once because array is never freed until unloading
739                  * nf_conntrack.ko
740                  */
741                 goto retry;
742         }
743
744         nf_ct_protos[proto->l3proto][proto->proto] = proto;
745
746 out_unlock:
747         write_unlock_bh(&nf_conntrack_lock);
748 out:
749         return ret;
750 }
751
752 void nf_conntrack_protocol_unregister(struct nf_conntrack_protocol *proto)
753 {
754         write_lock_bh(&nf_conntrack_lock);
755         nf_ct_protos[proto->l3proto][proto->proto]
756                 = &nf_conntrack_generic_protocol;
757         write_unlock_bh(&nf_conntrack_lock);
758         
759         /* Somebody could be still looking at the proto in bh. */
760         synchronize_net();
761
762         /* Remove all contrack entries for this protocol */
763         nf_ct_iterate_cleanup(kill_proto, proto);
764 }
765
766 static int __init nf_conntrack_standalone_init(void)
767 {
768 #ifdef CONFIG_PROC_FS
769         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
770 #endif
771         int ret = 0;
772
773         ret = nf_conntrack_init();
774         if (ret < 0)
775                 return ret;
776
777 #ifdef CONFIG_PROC_FS
778         proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
779         if (!proc) goto cleanup_init;
780
781         proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
782                                         &exp_file_ops);
783         if (!proc_exp) goto cleanup_proc;
784
785         proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
786         if (!proc_stat)
787                 goto cleanup_proc_exp;
788
789         proc_stat->proc_fops = &ct_cpu_seq_fops;
790         proc_stat->owner = THIS_MODULE;
791 #endif
792 #ifdef CONFIG_SYSCTL
793         nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
794         if (nf_ct_sysctl_header == NULL) {
795                 printk("nf_conntrack: can't register to sysctl.\n");
796                 ret = -ENOMEM;
797                 goto cleanup_proc_stat;
798         }
799 #endif
800         return ret;
801
802 #ifdef CONFIG_SYSCTL
803  cleanup_proc_stat:
804 #endif
805 #ifdef CONFIG_PROC_FS
806         remove_proc_entry("nf_conntrack", proc_net_stat);
807  cleanup_proc_exp:
808         proc_net_remove("nf_conntrack_expect");
809  cleanup_proc:
810         proc_net_remove("nf_conntrack");
811  cleanup_init:
812 #endif /* CNFIG_PROC_FS */
813         nf_conntrack_cleanup();
814         return ret;
815 }
816
817 static void __exit nf_conntrack_standalone_fini(void)
818 {
819 #ifdef CONFIG_SYSCTL
820         unregister_sysctl_table(nf_ct_sysctl_header);
821 #endif
822 #ifdef CONFIG_PROC_FS
823         remove_proc_entry("nf_conntrack", proc_net_stat);
824         proc_net_remove("nf_conntrack_expect");
825         proc_net_remove("nf_conntrack");
826 #endif /* CNFIG_PROC_FS */
827         nf_conntrack_cleanup();
828 }
829
830 module_init(nf_conntrack_standalone_init);
831 module_exit(nf_conntrack_standalone_fini);
832
833 /* Some modules need us, but don't depend directly on any symbol.
834    They should call this. */
835 void need_conntrack(void)
836 {
837 }
838
839 #ifdef CONFIG_NF_CONNTRACK_EVENTS
840 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
841 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
842 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
843 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
844 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
845 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
846 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
847 #endif
848 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
849 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
850 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
851 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
852 EXPORT_SYMBOL(nf_conntrack_protocol_register);
853 EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
854 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
855 EXPORT_SYMBOL(nf_conntrack_destroyed);
856 EXPORT_SYMBOL(need_conntrack);
857 EXPORT_SYMBOL(nf_conntrack_helper_register);
858 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
859 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
860 EXPORT_SYMBOL(__nf_ct_refresh_acct);
861 EXPORT_SYMBOL(nf_ct_protos);
862 EXPORT_SYMBOL(__nf_ct_proto_find);
863 EXPORT_SYMBOL(nf_ct_proto_find_get);
864 EXPORT_SYMBOL(nf_ct_proto_put);
865 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
866 EXPORT_SYMBOL(nf_ct_l3proto_put);
867 EXPORT_SYMBOL(nf_ct_l3protos);
868 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
869 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
870 EXPORT_SYMBOL(nf_conntrack_expect_put);
871 EXPORT_SYMBOL(nf_conntrack_expect_related);
872 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
873 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
874 EXPORT_SYMBOL(nf_conntrack_htable_size);
875 EXPORT_SYMBOL(nf_conntrack_lock);
876 EXPORT_SYMBOL(nf_conntrack_hash);
877 EXPORT_SYMBOL(nf_conntrack_untracked);
878 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
879 #ifdef CONFIG_IP_NF_NAT_NEEDED
880 EXPORT_SYMBOL(nf_conntrack_tcp_update);
881 #endif
882 EXPORT_SYMBOL(__nf_conntrack_confirm);
883 EXPORT_SYMBOL(nf_ct_get_tuple);
884 EXPORT_SYMBOL(nf_ct_invert_tuple);
885 EXPORT_SYMBOL(nf_conntrack_in);
886 EXPORT_SYMBOL(__nf_conntrack_attach);
887 EXPORT_SYMBOL(nf_conntrack_alloc);
888 EXPORT_SYMBOL(nf_conntrack_free);
889 EXPORT_SYMBOL(nf_conntrack_flush);
890 EXPORT_SYMBOL(nf_ct_remove_expectations);
891 EXPORT_SYMBOL(nf_ct_helper_find_get);
892 EXPORT_SYMBOL(nf_ct_helper_put);
893 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
894 EXPORT_SYMBOL(__nf_conntrack_find);
895 EXPORT_SYMBOL(nf_ct_unlink_expect);
896 EXPORT_SYMBOL(nf_conntrack_hash_insert);
897 EXPORT_SYMBOL(__nf_conntrack_expect_find);
898 EXPORT_SYMBOL(nf_conntrack_expect_find);
899 EXPORT_SYMBOL(nf_conntrack_expect_list);
900 #if defined(CONFIG_NF_CT_NETLINK) || \
901     defined(CONFIG_NF_CT_NETLINK_MODULE)
902 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
903 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
904 #endif