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