Revert to Fedora kernel-2.6.17-1.2187_FC5 patched with vs2.0.2.1; there are too many...
[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-2005 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 #include <net/route.h>
31
32 #define ASSERT_READ_LOCK(x)
33 #define ASSERT_WRITE_LOCK(x)
34
35 #include <linux/netfilter_ipv4/ip_conntrack.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
38 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
39 #include <linux/netfilter_ipv4/listhelp.h>
40
41 #if 0
42 #define DEBUGP printk
43 #else
44 #define DEBUGP(format, args...)
45 #endif
46
47 MODULE_LICENSE("GPL");
48
49 extern atomic_t ip_conntrack_count;
50 DECLARE_PER_CPU(struct ip_conntrack_stat, ip_conntrack_stat);
51
52 static int kill_proto(struct ip_conntrack *i, void *data)
53 {
54         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum == 
55                         *((u_int8_t *) data));
56 }
57
58 #ifdef CONFIG_PROC_FS
59 static int
60 print_tuple(struct seq_file *s, const struct ip_conntrack_tuple *tuple,
61             struct ip_conntrack_protocol *proto)
62 {
63         seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
64                    NIPQUAD(tuple->src.ip), NIPQUAD(tuple->dst.ip));
65         return proto->print_tuple(s, tuple);
66 }
67
68 #ifdef CONFIG_IP_NF_CT_ACCT
69 static unsigned int
70 seq_print_counters(struct seq_file *s,
71                    const struct ip_conntrack_counter *counter)
72 {
73         return seq_printf(s, "packets=%llu bytes=%llu ",
74                           (unsigned long long)counter->packets,
75                           (unsigned long long)counter->bytes);
76 }
77 #else
78 #define seq_print_counters(x, y)        0
79 #endif
80
81 struct ct_iter_state {
82         unsigned int bucket;
83 };
84
85 static struct list_head *ct_get_first(struct seq_file *seq)
86 {
87         struct ct_iter_state *st = seq->private;
88
89         for (st->bucket = 0;
90              st->bucket < ip_conntrack_htable_size;
91              st->bucket++) {
92                 if (!list_empty(&ip_conntrack_hash[st->bucket]))
93                         return ip_conntrack_hash[st->bucket].next;
94         }
95         return NULL;
96 }
97
98 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
99 {
100         struct ct_iter_state *st = seq->private;
101
102         head = head->next;
103         while (head == &ip_conntrack_hash[st->bucket]) {
104                 if (++st->bucket >= ip_conntrack_htable_size)
105                         return NULL;
106                 head = ip_conntrack_hash[st->bucket].next;
107         }
108         return head;
109 }
110
111 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
112 {
113         struct list_head *head = ct_get_first(seq);
114
115         if (head)
116                 while (pos && (head = ct_get_next(seq, head)))
117                         pos--;
118         return pos ? NULL : head;
119 }
120
121 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
122 {
123         read_lock_bh(&ip_conntrack_lock);
124         return ct_get_idx(seq, *pos);
125 }
126
127 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
128 {
129         (*pos)++;
130         return ct_get_next(s, v);
131 }
132   
133 static void ct_seq_stop(struct seq_file *s, void *v)
134 {
135         read_unlock_bh(&ip_conntrack_lock);
136 }
137  
138 static int ct_seq_show(struct seq_file *s, void *v)
139 {
140         const struct ip_conntrack_tuple_hash *hash = v;
141         const struct ip_conntrack *conntrack = tuplehash_to_ctrack(hash);
142         struct ip_conntrack_protocol *proto;
143
144         ASSERT_READ_LOCK(&ip_conntrack_lock);
145         IP_NF_ASSERT(conntrack);
146
147         /* we only want to print DIR_ORIGINAL */
148         if (DIRECTION(hash))
149                 return 0;
150
151         proto = __ip_conntrack_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL].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=%u ", 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_bh(&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_bh(&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_conntrack_proto_find(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 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
446         /* Previously seen (loopback)?  Ignore.  Do this before
447            fragment check. */
448         if ((*pskb)->nfct)
449                 return NF_ACCEPT;
450 #endif
451
452         /* Gather fragments. */
453         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
454                 *pskb = ip_ct_gather_frags(*pskb,
455                                            hooknum == NF_IP_PRE_ROUTING ? 
456                                            IP_DEFRAG_CONNTRACK_IN :
457                                            IP_DEFRAG_CONNTRACK_OUT);
458                 if (!*pskb)
459                         return NF_STOLEN;
460         }
461         return NF_ACCEPT;
462 }
463
464 static unsigned int ip_conntrack_local(unsigned int hooknum,
465                                        struct sk_buff **pskb,
466                                        const struct net_device *in,
467                                        const struct net_device *out,
468                                        int (*okfn)(struct sk_buff *))
469 {
470         /* root is playing with raw sockets. */
471         if ((*pskb)->len < sizeof(struct iphdr)
472             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
473                 if (net_ratelimit())
474                         printk("ipt_hook: happy cracking.\n");
475                 return NF_ACCEPT;
476         }
477         return ip_conntrack_in(hooknum, pskb, in, out, okfn);
478 }
479
480 /* Connection tracking may drop packets, but never alters them, so
481    make it the first hook. */
482 static struct nf_hook_ops ip_conntrack_ops[] = {
483         {
484                 .hook           = ip_conntrack_defrag,
485                 .owner          = THIS_MODULE,
486                 .pf             = PF_INET,
487                 .hooknum        = NF_IP_PRE_ROUTING,
488                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
489         },
490         {
491                 .hook           = ip_conntrack_in,
492                 .owner          = THIS_MODULE,
493                 .pf             = PF_INET,
494                 .hooknum        = NF_IP_PRE_ROUTING,
495                 .priority       = NF_IP_PRI_CONNTRACK,
496         },
497         {
498                 .hook           = ip_conntrack_defrag,
499                 .owner          = THIS_MODULE,
500                 .pf             = PF_INET,
501                 .hooknum        = NF_IP_LOCAL_OUT,
502                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
503         },
504         {
505                 .hook           = ip_conntrack_local,
506                 .owner          = THIS_MODULE,
507                 .pf             = PF_INET,
508                 .hooknum        = NF_IP_LOCAL_OUT,
509                 .priority       = NF_IP_PRI_CONNTRACK,
510         },
511         {
512                 .hook           = ip_conntrack_help,
513                 .owner          = THIS_MODULE,
514                 .pf             = PF_INET,
515                 .hooknum        = NF_IP_POST_ROUTING,
516                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
517         },
518         {
519                 .hook           = ip_conntrack_help,
520                 .owner          = THIS_MODULE,
521                 .pf             = PF_INET,
522                 .hooknum        = NF_IP_LOCAL_IN,
523                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
524         },
525         {
526                 .hook           = ip_confirm,
527                 .owner          = THIS_MODULE,
528                 .pf             = PF_INET,
529                 .hooknum        = NF_IP_POST_ROUTING,
530                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
531         },
532         {
533                 .hook           = ip_confirm,
534                 .owner          = THIS_MODULE,
535                 .pf             = PF_INET,
536                 .hooknum        = NF_IP_LOCAL_IN,
537                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
538         },
539 };
540
541 /* Sysctl support */
542
543 #ifdef CONFIG_SYSCTL
544
545 /* From ip_conntrack_core.c */
546 extern int ip_conntrack_max;
547 extern unsigned int ip_conntrack_htable_size;
548
549 /* From ip_conntrack_proto_tcp.c */
550 extern unsigned int ip_ct_tcp_timeout_syn_sent;
551 extern unsigned int ip_ct_tcp_timeout_syn_recv;
552 extern unsigned int ip_ct_tcp_timeout_established;
553 extern unsigned int ip_ct_tcp_timeout_fin_wait;
554 extern unsigned int ip_ct_tcp_timeout_close_wait;
555 extern unsigned int ip_ct_tcp_timeout_last_ack;
556 extern unsigned int ip_ct_tcp_timeout_time_wait;
557 extern unsigned int ip_ct_tcp_timeout_close;
558 extern unsigned int ip_ct_tcp_timeout_max_retrans;
559 extern int ip_ct_tcp_loose;
560 extern int ip_ct_tcp_be_liberal;
561 extern int ip_ct_tcp_max_retrans;
562
563 /* From ip_conntrack_proto_udp.c */
564 extern unsigned int ip_ct_udp_timeout;
565 extern unsigned int ip_ct_udp_timeout_stream;
566
567 /* From ip_conntrack_proto_icmp.c */
568 extern unsigned int ip_ct_icmp_timeout;
569
570 /* From ip_conntrack_proto_icmp.c */
571 extern unsigned int ip_ct_generic_timeout;
572
573 /* Log invalid packets of a given protocol */
574 static int log_invalid_proto_min = 0;
575 static int log_invalid_proto_max = 255;
576
577 static struct ctl_table_header *ip_ct_sysctl_header;
578
579 static ctl_table ip_ct_sysctl_table[] = {
580         {
581                 .ctl_name       = NET_IPV4_NF_CONNTRACK_MAX,
582                 .procname       = "ip_conntrack_max",
583                 .data           = &ip_conntrack_max,
584                 .maxlen         = sizeof(int),
585                 .mode           = 0644,
586                 .proc_handler   = &proc_dointvec,
587         },
588         {
589                 .ctl_name       = NET_IPV4_NF_CONNTRACK_COUNT,
590                 .procname       = "ip_conntrack_count",
591                 .data           = &ip_conntrack_count,
592                 .maxlen         = sizeof(int),
593                 .mode           = 0444,
594                 .proc_handler   = &proc_dointvec,
595         },
596         {
597                 .ctl_name       = NET_IPV4_NF_CONNTRACK_BUCKETS,
598                 .procname       = "ip_conntrack_buckets",
599                 .data           = &ip_conntrack_htable_size,
600                 .maxlen         = sizeof(unsigned int),
601                 .mode           = 0444,
602                 .proc_handler   = &proc_dointvec,
603         },
604         {
605                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
606                 .procname       = "ip_conntrack_tcp_timeout_syn_sent",
607                 .data           = &ip_ct_tcp_timeout_syn_sent,
608                 .maxlen         = sizeof(unsigned int),
609                 .mode           = 0644,
610                 .proc_handler   = &proc_dointvec_jiffies,
611         },
612         {
613                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
614                 .procname       = "ip_conntrack_tcp_timeout_syn_recv",
615                 .data           = &ip_ct_tcp_timeout_syn_recv,
616                 .maxlen         = sizeof(unsigned int),
617                 .mode           = 0644,
618                 .proc_handler   = &proc_dointvec_jiffies,
619         },
620         {
621                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
622                 .procname       = "ip_conntrack_tcp_timeout_established",
623                 .data           = &ip_ct_tcp_timeout_established,
624                 .maxlen         = sizeof(unsigned int),
625                 .mode           = 0644,
626                 .proc_handler   = &proc_dointvec_jiffies,
627         },
628         {
629                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
630                 .procname       = "ip_conntrack_tcp_timeout_fin_wait",
631                 .data           = &ip_ct_tcp_timeout_fin_wait,
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_CLOSE_WAIT,
638                 .procname       = "ip_conntrack_tcp_timeout_close_wait",
639                 .data           = &ip_ct_tcp_timeout_close_wait,
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_LAST_ACK,
646                 .procname       = "ip_conntrack_tcp_timeout_last_ack",
647                 .data           = &ip_ct_tcp_timeout_last_ack,
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_TIME_WAIT,
654                 .procname       = "ip_conntrack_tcp_timeout_time_wait",
655                 .data           = &ip_ct_tcp_timeout_time_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,
662                 .procname       = "ip_conntrack_tcp_timeout_close",
663                 .data           = &ip_ct_tcp_timeout_close,
664                 .maxlen         = sizeof(unsigned int),
665                 .mode           = 0644,
666                 .proc_handler   = &proc_dointvec_jiffies,
667         },
668         {
669                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
670                 .procname       = "ip_conntrack_udp_timeout",
671                 .data           = &ip_ct_udp_timeout,
672                 .maxlen         = sizeof(unsigned int),
673                 .mode           = 0644,
674                 .proc_handler   = &proc_dointvec_jiffies,
675         },
676         {
677                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
678                 .procname       = "ip_conntrack_udp_timeout_stream",
679                 .data           = &ip_ct_udp_timeout_stream,
680                 .maxlen         = sizeof(unsigned int),
681                 .mode           = 0644,
682                 .proc_handler   = &proc_dointvec_jiffies,
683         },
684         {
685                 .ctl_name       = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
686                 .procname       = "ip_conntrack_icmp_timeout",
687                 .data           = &ip_ct_icmp_timeout,
688                 .maxlen         = sizeof(unsigned int),
689                 .mode           = 0644,
690                 .proc_handler   = &proc_dointvec_jiffies,
691         },
692         {
693                 .ctl_name       = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
694                 .procname       = "ip_conntrack_generic_timeout",
695                 .data           = &ip_ct_generic_timeout,
696                 .maxlen         = sizeof(unsigned int),
697                 .mode           = 0644,
698                 .proc_handler   = &proc_dointvec_jiffies,
699         },
700         {
701                 .ctl_name       = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
702                 .procname       = "ip_conntrack_log_invalid",
703                 .data           = &ip_ct_log_invalid,
704                 .maxlen         = sizeof(unsigned int),
705                 .mode           = 0644,
706                 .proc_handler   = &proc_dointvec_minmax,
707                 .strategy       = &sysctl_intvec,
708                 .extra1         = &log_invalid_proto_min,
709                 .extra2         = &log_invalid_proto_max,
710         },
711         {
712                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
713                 .procname       = "ip_conntrack_tcp_timeout_max_retrans",
714                 .data           = &ip_ct_tcp_timeout_max_retrans,
715                 .maxlen         = sizeof(unsigned int),
716                 .mode           = 0644,
717                 .proc_handler   = &proc_dointvec_jiffies,
718         },
719         {
720                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
721                 .procname       = "ip_conntrack_tcp_loose",
722                 .data           = &ip_ct_tcp_loose,
723                 .maxlen         = sizeof(unsigned int),
724                 .mode           = 0644,
725                 .proc_handler   = &proc_dointvec,
726         },
727         {
728                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
729                 .procname       = "ip_conntrack_tcp_be_liberal",
730                 .data           = &ip_ct_tcp_be_liberal,
731                 .maxlen         = sizeof(unsigned int),
732                 .mode           = 0644,
733                 .proc_handler   = &proc_dointvec,
734         },
735         {
736                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
737                 .procname       = "ip_conntrack_tcp_max_retrans",
738                 .data           = &ip_ct_tcp_max_retrans,
739                 .maxlen         = sizeof(unsigned int),
740                 .mode           = 0644,
741                 .proc_handler   = &proc_dointvec,
742         },
743         { .ctl_name = 0 }
744 };
745
746 #define NET_IP_CONNTRACK_MAX 2089
747
748 static ctl_table ip_ct_netfilter_table[] = {
749         {
750                 .ctl_name       = NET_IPV4_NETFILTER,
751                 .procname       = "netfilter",
752                 .mode           = 0555,
753                 .child          = ip_ct_sysctl_table,
754         },
755         {
756                 .ctl_name       = NET_IP_CONNTRACK_MAX,
757                 .procname       = "ip_conntrack_max",
758                 .data           = &ip_conntrack_max,
759                 .maxlen         = sizeof(int),
760                 .mode           = 0644,
761                 .proc_handler   = &proc_dointvec
762         },
763         { .ctl_name = 0 }
764 };
765
766 static ctl_table ip_ct_ipv4_table[] = {
767         {
768                 .ctl_name       = NET_IPV4,
769                 .procname       = "ipv4",
770                 .mode           = 0555,
771                 .child          = ip_ct_netfilter_table,
772         },
773         { .ctl_name = 0 }
774 };
775
776 static ctl_table ip_ct_net_table[] = {
777         {
778                 .ctl_name       = CTL_NET,
779                 .procname       = "net",
780                 .mode           = 0555, 
781                 .child          = ip_ct_ipv4_table,
782         },
783         { .ctl_name = 0 }
784 };
785
786 EXPORT_SYMBOL(ip_ct_log_invalid);
787 #endif /* CONFIG_SYSCTL */
788
789 /* FIXME: Allow NULL functions and sub in pointers to generic for
790    them. --RR */
791 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
792 {
793         int ret = 0;
794
795         write_lock_bh(&ip_conntrack_lock);
796         if (ip_ct_protos[proto->proto] != &ip_conntrack_generic_protocol) {
797                 ret = -EBUSY;
798                 goto out;
799         }
800         ip_ct_protos[proto->proto] = proto;
801  out:
802         write_unlock_bh(&ip_conntrack_lock);
803         return ret;
804 }
805
806 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
807 {
808         write_lock_bh(&ip_conntrack_lock);
809         ip_ct_protos[proto->proto] = &ip_conntrack_generic_protocol;
810         write_unlock_bh(&ip_conntrack_lock);
811
812         /* Somebody could be still looking at the proto in bh. */
813         synchronize_net();
814
815         /* Remove all contrack entries for this protocol */
816         ip_ct_iterate_cleanup(kill_proto, &proto->proto);
817 }
818
819 static int __init ip_conntrack_standalone_init(void)
820 {
821 #ifdef CONFIG_PROC_FS
822         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
823 #endif
824         int ret = 0;
825
826         ret = ip_conntrack_init();
827         if (ret < 0)
828                 return ret;
829
830 #ifdef CONFIG_PROC_FS
831         ret = -ENOMEM;
832         proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
833         if (!proc) goto cleanup_init;
834
835         proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
836                                         &exp_file_ops);
837         if (!proc_exp) goto cleanup_proc;
838
839         proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
840         if (!proc_stat)
841                 goto cleanup_proc_exp;
842
843         proc_stat->proc_fops = &ct_cpu_seq_fops;
844         proc_stat->owner = THIS_MODULE;
845 #endif
846
847         ret = nf_register_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
848         if (ret < 0) {
849                 printk("ip_conntrack: can't register hooks.\n");
850                 goto cleanup_proc_stat;
851         }
852 #ifdef CONFIG_SYSCTL
853         ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
854         if (ip_ct_sysctl_header == NULL) {
855                 printk("ip_conntrack: can't register to sysctl.\n");
856                 ret = -ENOMEM;
857                 goto cleanup_hooks;
858         }
859 #endif
860         return ret;
861
862 #ifdef CONFIG_SYSCTL
863  cleanup_hooks:
864         nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
865 #endif
866  cleanup_proc_stat:
867 #ifdef CONFIG_PROC_FS
868         remove_proc_entry("ip_conntrack", proc_net_stat);
869  cleanup_proc_exp:
870         proc_net_remove("ip_conntrack_expect");
871  cleanup_proc:
872         proc_net_remove("ip_conntrack");
873  cleanup_init:
874 #endif /* CONFIG_PROC_FS */
875         ip_conntrack_cleanup();
876         return ret;
877 }
878
879 static void __exit ip_conntrack_standalone_fini(void)
880 {
881         synchronize_net();
882 #ifdef CONFIG_SYSCTL
883         unregister_sysctl_table(ip_ct_sysctl_header);
884 #endif
885         nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
886 #ifdef CONFIG_PROC_FS
887         remove_proc_entry("ip_conntrack", proc_net_stat);
888         proc_net_remove("ip_conntrack_expect");
889         proc_net_remove("ip_conntrack");
890 #endif /* CONFIG_PROC_FS */
891         ip_conntrack_cleanup();
892 }
893
894 module_init(ip_conntrack_standalone_init);
895 module_exit(ip_conntrack_standalone_fini);
896
897 /* Some modules need us, but don't depend directly on any symbol.
898    They should call this. */
899 void need_conntrack(void)
900 {
901 }
902
903 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
904 EXPORT_SYMBOL_GPL(ip_conntrack_chain);
905 EXPORT_SYMBOL_GPL(ip_conntrack_expect_chain);
906 EXPORT_SYMBOL_GPL(ip_conntrack_register_notifier);
907 EXPORT_SYMBOL_GPL(ip_conntrack_unregister_notifier);
908 EXPORT_SYMBOL_GPL(__ip_ct_event_cache_init);
909 EXPORT_PER_CPU_SYMBOL_GPL(ip_conntrack_ecache);
910 #endif
911 EXPORT_SYMBOL(ip_conntrack_protocol_register);
912 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
913 EXPORT_SYMBOL(ip_ct_get_tuple);
914 EXPORT_SYMBOL(invert_tuplepr);
915 EXPORT_SYMBOL(ip_conntrack_alter_reply);
916 EXPORT_SYMBOL(ip_conntrack_destroyed);
917 EXPORT_SYMBOL(need_conntrack);
918 EXPORT_SYMBOL(ip_conntrack_helper_register);
919 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
920 EXPORT_SYMBOL(ip_ct_iterate_cleanup);
921 EXPORT_SYMBOL(__ip_ct_refresh_acct);
922
923 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
924 EXPORT_SYMBOL(ip_conntrack_expect_put);
925 EXPORT_SYMBOL_GPL(__ip_conntrack_expect_find);
926 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find);
927 EXPORT_SYMBOL(ip_conntrack_expect_related);
928 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
929 EXPORT_SYMBOL_GPL(ip_conntrack_expect_list);
930 EXPORT_SYMBOL_GPL(ip_ct_unlink_expect);
931
932 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
933 EXPORT_SYMBOL(ip_ct_gather_frags);
934 EXPORT_SYMBOL(ip_conntrack_htable_size);
935 EXPORT_SYMBOL(ip_conntrack_lock);
936 EXPORT_SYMBOL(ip_conntrack_hash);
937 EXPORT_SYMBOL(ip_conntrack_untracked);
938 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
939 #ifdef CONFIG_IP_NF_NAT_NEEDED
940 EXPORT_SYMBOL(ip_conntrack_tcp_update);
941 #endif
942
943 EXPORT_SYMBOL_GPL(ip_conntrack_flush);
944 EXPORT_SYMBOL_GPL(__ip_conntrack_find);
945
946 EXPORT_SYMBOL_GPL(ip_conntrack_alloc);
947 EXPORT_SYMBOL_GPL(ip_conntrack_free);
948 EXPORT_SYMBOL_GPL(ip_conntrack_hash_insert);
949
950 EXPORT_SYMBOL_GPL(ip_ct_remove_expectations);
951
952 EXPORT_SYMBOL_GPL(ip_conntrack_helper_find_get);
953 EXPORT_SYMBOL_GPL(ip_conntrack_helper_put);
954 EXPORT_SYMBOL_GPL(__ip_conntrack_helper_find_byname);
955
956 EXPORT_SYMBOL_GPL(ip_conntrack_proto_find_get);
957 EXPORT_SYMBOL_GPL(ip_conntrack_proto_put);
958 EXPORT_SYMBOL_GPL(__ip_conntrack_proto_find);
959 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
960     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
961 EXPORT_SYMBOL_GPL(ip_ct_port_tuple_to_nfattr);
962 EXPORT_SYMBOL_GPL(ip_ct_port_nfattr_to_tuple);
963 #endif