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