This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_standalone.c
1 /* This file contains all the functions required for the standalone
2    ip_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
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/ip.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/percpu.h>
25 #ifdef CONFIG_SYSCTL
26 #include <linux/sysctl.h>
27 #endif
28 #include <net/checksum.h>
29 #include <net/ip.h>
30
31 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
32 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_conntrack_lock)
33
34 #include <linux/netfilter_ipv4/ip_conntrack.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
38 #include <linux/netfilter_ipv4/listhelp.h>
39
40 #if 0
41 #define DEBUGP printk
42 #else
43 #define DEBUGP(format, args...)
44 #endif
45
46 MODULE_LICENSE("GPL");
47
48 extern atomic_t ip_conntrack_count;
49 DECLARE_PER_CPU(struct ip_conntrack_stat, ip_conntrack_stat);
50
51 static int kill_proto(const struct ip_conntrack *i, void *data)
52 {
53         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum == 
54                         *((u_int8_t *) data));
55 }
56
57 #ifdef CONFIG_PROC_FS
58 static int
59 print_tuple(struct seq_file *s, const struct ip_conntrack_tuple *tuple,
60             struct ip_conntrack_protocol *proto)
61 {
62         seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
63                    NIPQUAD(tuple->src.ip), NIPQUAD(tuple->dst.ip));
64         return proto->print_tuple(s, tuple);
65 }
66
67 #ifdef CONFIG_IP_NF_CT_ACCT
68 static unsigned int
69 seq_print_counters(struct seq_file *s, struct ip_conntrack_counter *counter)
70 {
71         return seq_printf(s, "packets=%llu bytes=%llu ",
72                           (unsigned long long)counter->packets,
73                           (unsigned long long)counter->bytes);
74 }
75 #else
76 #define seq_print_counters(x, y)        0
77 #endif
78
79 static void *ct_seq_start(struct seq_file *s, loff_t *pos)
80 {
81         if (*pos >= ip_conntrack_htable_size)
82                 return NULL;
83         return &ip_conntrack_hash[*pos];
84 }
85   
86 static void ct_seq_stop(struct seq_file *s, void *v)
87 {
88 }
89
90 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
91 {
92         (*pos)++;
93         if (*pos >= ip_conntrack_htable_size)
94                 return NULL;
95         return &ip_conntrack_hash[*pos];
96 }
97   
98 /* return 0 on success, 1 in case of error */
99 static int ct_seq_real_show(const struct ip_conntrack_tuple_hash *hash,
100                             struct seq_file *s)
101 {
102         struct ip_conntrack *conntrack = hash->ctrack;
103         struct ip_conntrack_protocol *proto;
104
105         MUST_BE_READ_LOCKED(&ip_conntrack_lock);
106
107         IP_NF_ASSERT(conntrack);
108
109         /* we only want to print DIR_ORIGINAL */
110         if (DIRECTION(hash))
111                 return 0;
112
113         proto = ip_ct_find_proto(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
114                                .tuple.dst.protonum);
115         IP_NF_ASSERT(proto);
116
117         if (seq_printf(s, "%-8s %u %lu ",
118                       proto->name,
119                       conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
120                       timer_pending(&conntrack->timeout)
121                       ? (conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
122                 return 1;
123
124         if (proto->print_conntrack(s, conntrack))
125                 return 1;
126   
127         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
128                         proto))
129                 return 1;
130
131 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
132         if (seq_printf(s, "xid=%d\n", conntrack->xid[IP_CT_DIR_ORIGINAL]))
133                 return 1;
134 #endif
135
136         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
137                 return 1;
138
139         if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
140                 if (seq_printf(s, "[UNREPLIED] "))
141                         return 1;
142
143         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
144                         proto))
145                 return 1;
146
147 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
148         if (seq_printf(s, "xid=%d\n", conntrack->xid[IP_CT_DIR_REPLY]))
149                 return 1;
150 #endif
151
152         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
153                 return 1;
154
155         if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
156                 if (seq_printf(s, "[ASSURED] "))
157                         return 1;
158
159         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
160                 return 1;
161
162         return 0;
163 }
164
165 static int ct_seq_show(struct seq_file *s, void *v)
166 {
167         struct list_head *list = v;
168         int ret = 0;
169
170         /* FIXME: Simply truncates if hash chain too long. */
171         READ_LOCK(&ip_conntrack_lock);
172         if (LIST_FIND(list, ct_seq_real_show,
173                       struct ip_conntrack_tuple_hash *, s))
174                 ret = -ENOSPC;
175         READ_UNLOCK(&ip_conntrack_lock);
176         return ret;
177 }
178         
179 static struct seq_operations ct_seq_ops = {
180         .start = ct_seq_start,
181         .next  = ct_seq_next,
182         .stop  = ct_seq_stop,
183         .show  = ct_seq_show
184 };
185   
186 static int ct_open(struct inode *inode, struct file *file)
187 {
188         return seq_open(file, &ct_seq_ops);
189 }
190
191 static struct file_operations ct_file_ops = {
192         .owner   = THIS_MODULE,
193         .open    = ct_open,
194         .read    = seq_read,
195         .llseek  = seq_lseek,
196         .release = seq_release
197 };
198   
199 /* expects */
200 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
201 {
202         struct list_head *e = &ip_conntrack_expect_list;
203         loff_t i;
204
205         /* strange seq_file api calls stop even if we fail,
206          * thus we need to grab lock since stop unlocks */
207         READ_LOCK(&ip_conntrack_lock);
208         READ_LOCK(&ip_conntrack_expect_tuple_lock);
209
210         if (list_empty(e))
211                 return NULL;
212
213         for (i = 0; i <= *pos; i++) {
214                 e = e->next;
215                 if (e == &ip_conntrack_expect_list)
216                         return NULL;
217         }
218         return e;
219 }
220
221 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
222 {
223         struct list_head *e = v;
224
225         e = e->next;
226
227         if (e == &ip_conntrack_expect_list)
228                 return NULL;
229
230         return e;
231 }
232
233 static void exp_seq_stop(struct seq_file *s, void *v)
234 {
235         READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
236         READ_UNLOCK(&ip_conntrack_lock);
237 }
238
239 static int exp_seq_show(struct seq_file *s, void *v)
240 {
241         struct ip_conntrack_expect *expect = v;
242
243         if (expect->expectant->helper->timeout)
244                 seq_printf(s, "%lu ", timer_pending(&expect->timeout)
245                            ? (expect->timeout.expires - jiffies)/HZ : 0);
246         else
247                 seq_printf(s, "- ");
248
249         seq_printf(s, "use=%u proto=%u ", atomic_read(&expect->use),
250                    expect->tuple.dst.protonum);
251
252         print_tuple(s, &expect->tuple,
253                     ip_ct_find_proto(expect->tuple.dst.protonum));
254         return seq_putc(s, '\n');
255 }
256
257 static struct seq_operations exp_seq_ops = {
258         .start = exp_seq_start,
259         .next = exp_seq_next,
260         .stop = exp_seq_stop,
261         .show = exp_seq_show
262 };
263
264 static int exp_open(struct inode *inode, struct file *file)
265 {
266         return seq_open(file, &exp_seq_ops);
267 }
268   
269 static struct file_operations exp_file_ops = {
270         .owner   = THIS_MODULE,
271         .open    = exp_open,
272         .read    = seq_read,
273         .llseek  = seq_lseek,
274         .release = seq_release
275 };
276
277 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
278 {
279         int cpu;
280
281         if (*pos == 0)
282                 return SEQ_START_TOKEN;
283
284         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
285                 if (!cpu_possible(cpu))
286                         continue;
287                 *pos = cpu+1;
288                 return &per_cpu(ip_conntrack_stat, cpu);
289         }
290
291         return NULL;
292 }
293
294 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
295 {
296         int cpu;
297
298         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
299                 if (!cpu_possible(cpu))
300                         continue;
301                 *pos = cpu+1;
302                 return &per_cpu(ip_conntrack_stat, cpu);
303         }
304
305         return NULL;
306 }
307
308 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
309 {
310 }
311
312 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
313 {
314         unsigned int nr_conntracks = atomic_read(&ip_conntrack_count);
315         struct ip_conntrack_stat *st = v;
316
317         if (v == SEQ_START_TOKEN) {
318                 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");
319                 return 0;
320         }
321
322         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
323                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
324                    nr_conntracks,
325                    st->searched,
326                    st->found,
327                    st->new,
328                    st->invalid,
329                    st->ignore,
330                    st->delete,
331                    st->delete_list,
332                    st->insert,
333                    st->insert_failed,
334                    st->drop,
335                    st->early_drop,
336                    st->error,
337
338                    st->expect_new,
339                    st->expect_create,
340                    st->expect_delete
341                 );
342         return 0;
343 }
344
345 static struct seq_operations ct_cpu_seq_ops = {
346         .start  = ct_cpu_seq_start,
347         .next   = ct_cpu_seq_next,
348         .stop   = ct_cpu_seq_stop,
349         .show   = ct_cpu_seq_show,
350 };
351
352 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
353 {
354         return seq_open(file, &ct_cpu_seq_ops);
355 }
356
357 static struct file_operations ct_cpu_seq_fops = {
358         .owner   = THIS_MODULE,
359         .open    = ct_cpu_seq_open,
360         .read    = seq_read,
361         .llseek  = seq_lseek,
362         .release = seq_release_private,
363 };
364 #endif
365
366 static unsigned int ip_confirm(unsigned int hooknum,
367                                struct sk_buff **pskb,
368                                const struct net_device *in,
369                                const struct net_device *out,
370                                int (*okfn)(struct sk_buff *))
371 {
372         /* We've seen it coming out the other side: confirm it */
373         return ip_conntrack_confirm(*pskb);
374 }
375
376 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
377                                         struct sk_buff **pskb,
378                                         const struct net_device *in,
379                                         const struct net_device *out,
380                                         int (*okfn)(struct sk_buff *))
381 {
382         /* Previously seen (loopback)?  Ignore.  Do this before
383            fragment check. */
384         if ((*pskb)->nfct)
385                 return NF_ACCEPT;
386
387         /* Gather fragments. */
388         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
389                 *pskb = ip_ct_gather_frags(*pskb);
390                 if (!*pskb)
391                         return NF_STOLEN;
392         }
393         return NF_ACCEPT;
394 }
395
396 static unsigned int ip_refrag(unsigned int hooknum,
397                               struct sk_buff **pskb,
398                               const struct net_device *in,
399                               const struct net_device *out,
400                               int (*okfn)(struct sk_buff *))
401 {
402         struct rtable *rt = (struct rtable *)(*pskb)->dst;
403
404         /* We've seen it coming out the other side: confirm */
405         if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
406                 return NF_DROP;
407
408         /* Local packets are never produced too large for their
409            interface.  We degfragment them at LOCAL_OUT, however,
410            so we have to refragment them here. */
411         if ((*pskb)->len > dst_pmtu(&rt->u.dst) &&
412             !skb_shinfo(*pskb)->tso_size) {
413                 /* No hook can be after us, so this should be OK. */
414                 ip_fragment(*pskb, okfn);
415                 return NF_STOLEN;
416         }
417         return NF_ACCEPT;
418 }
419
420 static unsigned int ip_conntrack_local(unsigned int hooknum,
421                                        struct sk_buff **pskb,
422                                        const struct net_device *in,
423                                        const struct net_device *out,
424                                        int (*okfn)(struct sk_buff *))
425 {
426         /* root is playing with raw sockets. */
427         if ((*pskb)->len < sizeof(struct iphdr)
428             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
429                 if (net_ratelimit())
430                         printk("ipt_hook: happy cracking.\n");
431                 return NF_ACCEPT;
432         }
433         return ip_conntrack_in(hooknum, pskb, in, out, okfn);
434 }
435
436 /* Connection tracking may drop packets, but never alters them, so
437    make it the first hook. */
438 static struct nf_hook_ops ip_conntrack_defrag_ops = {
439         .hook           = ip_conntrack_defrag,
440         .owner          = THIS_MODULE,
441         .pf             = PF_INET,
442         .hooknum        = NF_IP_PRE_ROUTING,
443         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
444 };
445
446 static struct nf_hook_ops ip_conntrack_in_ops = {
447         .hook           = ip_conntrack_in,
448         .owner          = THIS_MODULE,
449         .pf             = PF_INET,
450         .hooknum        = NF_IP_PRE_ROUTING,
451         .priority       = NF_IP_PRI_CONNTRACK,
452 };
453
454 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
455         .hook           = ip_conntrack_defrag,
456         .owner          = THIS_MODULE,
457         .pf             = PF_INET,
458         .hooknum        = NF_IP_LOCAL_OUT,
459         .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
460 };
461
462 static struct nf_hook_ops ip_conntrack_local_out_ops = {
463         .hook           = ip_conntrack_local,
464         .owner          = THIS_MODULE,
465         .pf             = PF_INET,
466         .hooknum        = NF_IP_LOCAL_OUT,
467         .priority       = NF_IP_PRI_CONNTRACK,
468 };
469
470 /* Refragmenter; last chance. */
471 static struct nf_hook_ops ip_conntrack_out_ops = {
472         .hook           = ip_refrag,
473         .owner          = THIS_MODULE,
474         .pf             = PF_INET,
475         .hooknum        = NF_IP_POST_ROUTING,
476         .priority       = NF_IP_PRI_LAST,
477 };
478
479 static struct nf_hook_ops ip_conntrack_local_in_ops = {
480         .hook           = ip_confirm,
481         .owner          = THIS_MODULE,
482         .pf             = PF_INET,
483         .hooknum        = NF_IP_LOCAL_IN,
484         .priority       = NF_IP_PRI_LAST-1,
485 };
486
487 /* Sysctl support */
488
489 #ifdef CONFIG_SYSCTL
490
491 /* From ip_conntrack_core.c */
492 extern int ip_conntrack_max;
493 extern unsigned int ip_conntrack_htable_size;
494
495 /* From ip_conntrack_proto_tcp.c */
496 extern unsigned long ip_ct_tcp_timeout_syn_sent;
497 extern unsigned long ip_ct_tcp_timeout_syn_recv;
498 extern unsigned long ip_ct_tcp_timeout_established;
499 extern unsigned long ip_ct_tcp_timeout_fin_wait;
500 extern unsigned long ip_ct_tcp_timeout_close_wait;
501 extern unsigned long ip_ct_tcp_timeout_last_ack;
502 extern unsigned long ip_ct_tcp_timeout_time_wait;
503 extern unsigned long ip_ct_tcp_timeout_close;
504 extern unsigned long ip_ct_tcp_timeout_max_retrans;
505 extern int ip_ct_tcp_loose;
506 extern int ip_ct_tcp_be_liberal;
507 extern int ip_ct_tcp_max_retrans;
508
509 /* From ip_conntrack_proto_udp.c */
510 extern unsigned long ip_ct_udp_timeout;
511 extern unsigned long ip_ct_udp_timeout_stream;
512
513 /* From ip_conntrack_proto_icmp.c */
514 extern unsigned long ip_ct_icmp_timeout;
515
516 /* From ip_conntrack_proto_icmp.c */
517 extern unsigned long ip_ct_generic_timeout;
518
519 /* Log invalid packets of a given protocol */
520 static int log_invalid_proto_min = 0;
521 static int log_invalid_proto_max = 255;
522
523 static struct ctl_table_header *ip_ct_sysctl_header;
524
525 static ctl_table ip_ct_sysctl_table[] = {
526         {
527                 .ctl_name       = NET_IPV4_NF_CONNTRACK_MAX,
528                 .procname       = "ip_conntrack_max",
529                 .data           = &ip_conntrack_max,
530                 .maxlen         = sizeof(int),
531                 .mode           = 0644,
532                 .proc_handler   = &proc_dointvec,
533         },
534         {
535                 .ctl_name       = NET_IPV4_NF_CONNTRACK_COUNT,
536                 .procname       = "ip_conntrack_count",
537                 .data           = &ip_conntrack_count,
538                 .maxlen         = sizeof(int),
539                 .mode           = 0444,
540                 .proc_handler   = &proc_dointvec,
541         },
542         {
543                 .ctl_name       = NET_IPV4_NF_CONNTRACK_BUCKETS,
544                 .procname       = "ip_conntrack_buckets",
545                 .data           = &ip_conntrack_htable_size,
546                 .maxlen         = sizeof(unsigned int),
547                 .mode           = 0444,
548                 .proc_handler   = &proc_dointvec,
549         },
550         {
551                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
552                 .procname       = "ip_conntrack_tcp_timeout_syn_sent",
553                 .data           = &ip_ct_tcp_timeout_syn_sent,
554                 .maxlen         = sizeof(unsigned int),
555                 .mode           = 0644,
556                 .proc_handler   = &proc_dointvec_jiffies,
557         },
558         {
559                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
560                 .procname       = "ip_conntrack_tcp_timeout_syn_recv",
561                 .data           = &ip_ct_tcp_timeout_syn_recv,
562                 .maxlen         = sizeof(unsigned int),
563                 .mode           = 0644,
564                 .proc_handler   = &proc_dointvec_jiffies,
565         },
566         {
567                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
568                 .procname       = "ip_conntrack_tcp_timeout_established",
569                 .data           = &ip_ct_tcp_timeout_established,
570                 .maxlen         = sizeof(unsigned int),
571                 .mode           = 0644,
572                 .proc_handler   = &proc_dointvec_jiffies,
573         },
574         {
575                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
576                 .procname       = "ip_conntrack_tcp_timeout_fin_wait",
577                 .data           = &ip_ct_tcp_timeout_fin_wait,
578                 .maxlen         = sizeof(unsigned int),
579                 .mode           = 0644,
580                 .proc_handler   = &proc_dointvec_jiffies,
581         },
582         {
583                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
584                 .procname       = "ip_conntrack_tcp_timeout_close_wait",
585                 .data           = &ip_ct_tcp_timeout_close_wait,
586                 .maxlen         = sizeof(unsigned int),
587                 .mode           = 0644,
588                 .proc_handler   = &proc_dointvec_jiffies,
589         },
590         {
591                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
592                 .procname       = "ip_conntrack_tcp_timeout_last_ack",
593                 .data           = &ip_ct_tcp_timeout_last_ack,
594                 .maxlen         = sizeof(unsigned int),
595                 .mode           = 0644,
596                 .proc_handler   = &proc_dointvec_jiffies,
597         },
598         {
599                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
600                 .procname       = "ip_conntrack_tcp_timeout_time_wait",
601                 .data           = &ip_ct_tcp_timeout_time_wait,
602                 .maxlen         = sizeof(unsigned int),
603                 .mode           = 0644,
604                 .proc_handler   = &proc_dointvec_jiffies,
605         },
606         {
607                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
608                 .procname       = "ip_conntrack_tcp_timeout_close",
609                 .data           = &ip_ct_tcp_timeout_close,
610                 .maxlen         = sizeof(unsigned int),
611                 .mode           = 0644,
612                 .proc_handler   = &proc_dointvec_jiffies,
613         },
614         {
615                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
616                 .procname       = "ip_conntrack_udp_timeout",
617                 .data           = &ip_ct_udp_timeout,
618                 .maxlen         = sizeof(unsigned int),
619                 .mode           = 0644,
620                 .proc_handler   = &proc_dointvec_jiffies,
621         },
622         {
623                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
624                 .procname       = "ip_conntrack_udp_timeout_stream",
625                 .data           = &ip_ct_udp_timeout_stream,
626                 .maxlen         = sizeof(unsigned int),
627                 .mode           = 0644,
628                 .proc_handler   = &proc_dointvec_jiffies,
629         },
630         {
631                 .ctl_name       = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
632                 .procname       = "ip_conntrack_icmp_timeout",
633                 .data           = &ip_ct_icmp_timeout,
634                 .maxlen         = sizeof(unsigned int),
635                 .mode           = 0644,
636                 .proc_handler   = &proc_dointvec_jiffies,
637         },
638         {
639                 .ctl_name       = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
640                 .procname       = "ip_conntrack_generic_timeout",
641                 .data           = &ip_ct_generic_timeout,
642                 .maxlen         = sizeof(unsigned int),
643                 .mode           = 0644,
644                 .proc_handler   = &proc_dointvec_jiffies,
645         },
646         {
647                 .ctl_name       = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
648                 .procname       = "ip_conntrack_log_invalid",
649                 .data           = &ip_ct_log_invalid,
650                 .maxlen         = sizeof(unsigned int),
651                 .mode           = 0644,
652                 .proc_handler   = &proc_dointvec_minmax,
653                 .strategy       = &sysctl_intvec,
654                 .extra1         = &log_invalid_proto_min,
655                 .extra2         = &log_invalid_proto_max,
656         },
657         {
658                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
659                 .procname       = "ip_conntrack_tcp_timeout_max_retrans",
660                 .data           = &ip_ct_tcp_timeout_max_retrans,
661                 .maxlen         = sizeof(unsigned int),
662                 .mode           = 0644,
663                 .proc_handler   = &proc_dointvec_jiffies,
664         },
665         {
666                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
667                 .procname       = "ip_conntrack_tcp_loose",
668                 .data           = &ip_ct_tcp_loose,
669                 .maxlen         = sizeof(unsigned int),
670                 .mode           = 0644,
671                 .proc_handler   = &proc_dointvec,
672         },
673         {
674                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
675                 .procname       = "ip_conntrack_tcp_be_liberal",
676                 .data           = &ip_ct_tcp_be_liberal,
677                 .maxlen         = sizeof(unsigned int),
678                 .mode           = 0644,
679                 .proc_handler   = &proc_dointvec,
680         },
681         {
682                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
683                 .procname       = "ip_conntrack_tcp_max_retrans",
684                 .data           = &ip_ct_tcp_max_retrans,
685                 .maxlen         = sizeof(unsigned int),
686                 .mode           = 0644,
687                 .proc_handler   = &proc_dointvec,
688         },
689         { .ctl_name = 0 }
690 };
691
692 #define NET_IP_CONNTRACK_MAX 2089
693
694 static ctl_table ip_ct_netfilter_table[] = {
695         {
696                 .ctl_name       = NET_IPV4_NETFILTER,
697                 .procname       = "netfilter",
698                 .mode           = 0555,
699                 .child          = ip_ct_sysctl_table,
700         },
701         {
702                 .ctl_name       = NET_IP_CONNTRACK_MAX,
703                 .procname       = "ip_conntrack_max",
704                 .data           = &ip_conntrack_max,
705                 .maxlen         = sizeof(int),
706                 .mode           = 0644,
707                 .proc_handler   = &proc_dointvec
708         },
709         { .ctl_name = 0 }
710 };
711
712 static ctl_table ip_ct_ipv4_table[] = {
713         {
714                 .ctl_name       = NET_IPV4,
715                 .procname       = "ipv4",
716                 .mode           = 0555,
717                 .child          = ip_ct_netfilter_table,
718         },
719         { .ctl_name = 0 }
720 };
721
722 static ctl_table ip_ct_net_table[] = {
723         {
724                 .ctl_name       = CTL_NET,
725                 .procname       = "net",
726                 .mode           = 0555, 
727                 .child          = ip_ct_ipv4_table,
728         },
729         { .ctl_name = 0 }
730 };
731
732 EXPORT_SYMBOL(ip_ct_log_invalid);
733 #endif /* CONFIG_SYSCTL */
734
735 static int init_or_cleanup(int init)
736 {
737 #ifdef CONFIG_PROC_FS
738         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
739 #endif
740         int ret = 0;
741
742         if (!init) goto cleanup;
743
744         ret = ip_conntrack_init();
745         if (ret < 0)
746                 goto cleanup_nothing;
747
748 #ifdef CONFIG_PROC_FS
749         proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
750         if (!proc) goto cleanup_init;
751
752         proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
753                                         &exp_file_ops);
754         if (!proc_exp) goto cleanup_proc;
755
756         proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
757         if (!proc_stat)
758                 goto cleanup_proc_exp;
759
760         proc_stat->proc_fops = &ct_cpu_seq_fops;
761         proc_stat->owner = THIS_MODULE;
762 #endif
763
764         ret = nf_register_hook(&ip_conntrack_defrag_ops);
765         if (ret < 0) {
766                 printk("ip_conntrack: can't register pre-routing defrag hook.\n");
767                 goto cleanup_proc_stat;
768         }
769         ret = nf_register_hook(&ip_conntrack_defrag_local_out_ops);
770         if (ret < 0) {
771                 printk("ip_conntrack: can't register local_out defrag hook.\n");
772                 goto cleanup_defragops;
773         }
774         ret = nf_register_hook(&ip_conntrack_in_ops);
775         if (ret < 0) {
776                 printk("ip_conntrack: can't register pre-routing hook.\n");
777                 goto cleanup_defraglocalops;
778         }
779         ret = nf_register_hook(&ip_conntrack_local_out_ops);
780         if (ret < 0) {
781                 printk("ip_conntrack: can't register local out hook.\n");
782                 goto cleanup_inops;
783         }
784         ret = nf_register_hook(&ip_conntrack_out_ops);
785         if (ret < 0) {
786                 printk("ip_conntrack: can't register post-routing hook.\n");
787                 goto cleanup_inandlocalops;
788         }
789         ret = nf_register_hook(&ip_conntrack_local_in_ops);
790         if (ret < 0) {
791                 printk("ip_conntrack: can't register local in hook.\n");
792                 goto cleanup_inoutandlocalops;
793         }
794 #ifdef CONFIG_SYSCTL
795         ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
796         if (ip_ct_sysctl_header == NULL) {
797                 printk("ip_conntrack: can't register to sysctl.\n");
798                 goto cleanup;
799         }
800 #endif
801
802         return ret;
803
804  cleanup:
805 #ifdef CONFIG_SYSCTL
806         unregister_sysctl_table(ip_ct_sysctl_header);
807 #endif
808         nf_unregister_hook(&ip_conntrack_local_in_ops);
809  cleanup_inoutandlocalops:
810         nf_unregister_hook(&ip_conntrack_out_ops);
811  cleanup_inandlocalops:
812         nf_unregister_hook(&ip_conntrack_local_out_ops);
813  cleanup_inops:
814         nf_unregister_hook(&ip_conntrack_in_ops);
815  cleanup_defraglocalops:
816         nf_unregister_hook(&ip_conntrack_defrag_local_out_ops);
817  cleanup_defragops:
818         /* Frag queues may hold fragments with skb->dst == NULL */
819         ip_ct_no_defrag = 1;
820         synchronize_net();
821         local_bh_disable();
822         ipfrag_flush();
823         local_bh_enable();
824         nf_unregister_hook(&ip_conntrack_defrag_ops);
825  cleanup_proc_stat:
826 #ifdef CONFIG_PROC_FS
827         proc_net_remove("ip_conntrack_stat");
828 cleanup_proc_exp:
829         proc_net_remove("ip_conntrack_expect");
830  cleanup_proc:
831         proc_net_remove("ip_conntrack");
832  cleanup_init:
833 #endif /* CONFIG_PROC_FS */
834         ip_conntrack_cleanup();
835  cleanup_nothing:
836         return ret;
837 }
838
839 /* FIXME: Allow NULL functions and sub in pointers to generic for
840    them. --RR */
841 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
842 {
843         int ret = 0;
844
845         WRITE_LOCK(&ip_conntrack_lock);
846         if (ip_ct_protos[proto->proto] != &ip_conntrack_generic_protocol) {
847                 ret = -EBUSY;
848                 goto out;
849         }
850         ip_ct_protos[proto->proto] = proto;
851  out:
852         WRITE_UNLOCK(&ip_conntrack_lock);
853         return ret;
854 }
855
856 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
857 {
858         WRITE_LOCK(&ip_conntrack_lock);
859         ip_ct_protos[proto->proto] = &ip_conntrack_generic_protocol;
860         WRITE_UNLOCK(&ip_conntrack_lock);
861         
862         /* Somebody could be still looking at the proto in bh. */
863         synchronize_net();
864
865         /* Remove all contrack entries for this protocol */
866         ip_ct_selective_cleanup(kill_proto, &proto->proto);
867 }
868
869 static int __init init(void)
870 {
871         return init_or_cleanup(1);
872 }
873
874 static void __exit fini(void)
875 {
876         init_or_cleanup(0);
877 }
878
879 module_init(init);
880 module_exit(fini);
881
882 /* Some modules need us, but don't depend directly on any symbol.
883    They should call this. */
884 void need_ip_conntrack(void)
885 {
886 }
887
888 EXPORT_SYMBOL(ip_conntrack_protocol_register);
889 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
890 EXPORT_SYMBOL(invert_tuplepr);
891 EXPORT_SYMBOL(ip_conntrack_alter_reply);
892 EXPORT_SYMBOL(ip_conntrack_destroyed);
893 EXPORT_SYMBOL(need_ip_conntrack);
894 EXPORT_SYMBOL(ip_conntrack_helper_register);
895 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
896 EXPORT_SYMBOL(ip_ct_selective_cleanup);
897 EXPORT_SYMBOL(ip_ct_refresh_acct);
898 EXPORT_SYMBOL(ip_ct_protos);
899 EXPORT_SYMBOL(ip_ct_find_proto);
900 EXPORT_SYMBOL(ip_ct_find_helper);
901 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
902 EXPORT_SYMBOL(ip_conntrack_expect_related);
903 EXPORT_SYMBOL(ip_conntrack_change_expect);
904 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
905 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find_get);
906 EXPORT_SYMBOL_GPL(ip_conntrack_expect_put);
907 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
908 EXPORT_SYMBOL(ip_ct_gather_frags);
909 EXPORT_SYMBOL(ip_conntrack_htable_size);
910 EXPORT_SYMBOL(ip_conntrack_expect_list);
911 EXPORT_SYMBOL(ip_conntrack_lock);
912 EXPORT_SYMBOL(ip_conntrack_hash);
913 EXPORT_SYMBOL(ip_conntrack_untracked);
914 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
915 EXPORT_SYMBOL_GPL(ip_conntrack_put);