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