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