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