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