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