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