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