Merge to Fedora kernel-2.6.18-1.2224_FC5-vs2.0.2.2-rc4 patched with stable patch...
[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/types.h>
16 #include <linux/ip.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter_ipv4.h>
19 #include <linux/module.h>
20 #include <linux/skbuff.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/percpu.h>
24 #ifdef CONFIG_SYSCTL
25 #include <linux/sysctl.h>
26 #endif
27 #include <net/checksum.h>
28 #include <net/ip.h>
29 #include <net/route.h>
30
31 #define ASSERT_READ_LOCK(x)
32 #define ASSERT_WRITE_LOCK(x)
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_bh(&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_bh(&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         ASSERT_READ_LOCK(&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_conntrack_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
151         IP_NF_ASSERT(proto);
152
153         if (seq_printf(s, "%-8s %u %ld ",
154                       proto->name,
155                       conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
156                       timer_pending(&conntrack->timeout)
157                       ? (long)(conntrack->timeout.expires - jiffies)/HZ
158                       : 0) != 0)
159                 return -ENOSPC;
160
161         if (proto->print_conntrack(s, conntrack))
162                 return -ENOSPC;
163   
164         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
165                         proto))
166                 return -ENOSPC;
167
168 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
169         if (seq_printf(s, "xid=%d\n", conntrack->xid[IP_CT_DIR_ORIGINAL]))
170                 return 1;
171 #endif
172
173         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
174                 return -ENOSPC;
175
176         if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
177                 if (seq_printf(s, "[UNREPLIED] "))
178                         return -ENOSPC;
179
180         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
181                         proto))
182                 return -ENOSPC;
183
184 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
185         if (seq_printf(s, "xid=%d\n", conntrack->xid[IP_CT_DIR_REPLY]))
186                 return 1;
187 #endif
188
189         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
190                 return -ENOSPC;
191
192         if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
193                 if (seq_printf(s, "[ASSURED] "))
194                         return -ENOSPC;
195
196 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
197         if (seq_printf(s, "mark=%u ", conntrack->mark))
198                 return -ENOSPC;
199 #endif
200
201 #ifdef CONFIG_IP_NF_CONNTRACK_SECMARK
202         if (seq_printf(s, "secmark=%u ", conntrack->secmark))
203                 return -ENOSPC;
204 #endif
205
206         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
207                 return -ENOSPC;
208
209         return 0;
210 }
211
212 static struct seq_operations ct_seq_ops = {
213         .start = ct_seq_start,
214         .next  = ct_seq_next,
215         .stop  = ct_seq_stop,
216         .show  = ct_seq_show
217 };
218   
219 static int ct_open(struct inode *inode, struct file *file)
220 {
221         struct seq_file *seq;
222         struct ct_iter_state *st;
223         int ret;
224
225         st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
226         if (st == NULL)
227                 return -ENOMEM;
228         ret = seq_open(file, &ct_seq_ops);
229         if (ret)
230                 goto out_free;
231         seq          = file->private_data;
232         seq->private = st;
233         memset(st, 0, sizeof(struct ct_iter_state));
234         return ret;
235 out_free:
236         kfree(st);
237         return ret;
238 }
239
240 static struct file_operations ct_file_ops = {
241         .owner   = THIS_MODULE,
242         .open    = ct_open,
243         .read    = seq_read,
244         .llseek  = seq_lseek,
245         .release = seq_release_private,
246 };
247   
248 /* expects */
249 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
250 {
251         struct list_head *e = &ip_conntrack_expect_list;
252         loff_t i;
253
254         /* strange seq_file api calls stop even if we fail,
255          * thus we need to grab lock since stop unlocks */
256         read_lock_bh(&ip_conntrack_lock);
257
258         if (list_empty(e))
259                 return NULL;
260
261         for (i = 0; i <= *pos; i++) {
262                 e = e->next;
263                 if (e == &ip_conntrack_expect_list)
264                         return NULL;
265         }
266         return e;
267 }
268
269 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
270 {
271         struct list_head *e = v;
272
273         ++*pos;
274         e = e->next;
275
276         if (e == &ip_conntrack_expect_list)
277                 return NULL;
278
279         return e;
280 }
281
282 static void exp_seq_stop(struct seq_file *s, void *v)
283 {
284         read_unlock_bh(&ip_conntrack_lock);
285 }
286
287 static int exp_seq_show(struct seq_file *s, void *v)
288 {
289         struct ip_conntrack_expect *expect = v;
290
291         if (expect->timeout.function)
292                 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
293                            ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
294         else
295                 seq_printf(s, "- ");
296
297         seq_printf(s, "proto=%u ", expect->tuple.dst.protonum);
298
299         print_tuple(s, &expect->tuple,
300                     __ip_conntrack_proto_find(expect->tuple.dst.protonum));
301         return seq_putc(s, '\n');
302 }
303
304 static struct seq_operations exp_seq_ops = {
305         .start = exp_seq_start,
306         .next = exp_seq_next,
307         .stop = exp_seq_stop,
308         .show = exp_seq_show
309 };
310
311 static int exp_open(struct inode *inode, struct file *file)
312 {
313         return seq_open(file, &exp_seq_ops);
314 }
315   
316 static struct file_operations exp_file_ops = {
317         .owner   = THIS_MODULE,
318         .open    = exp_open,
319         .read    = seq_read,
320         .llseek  = seq_lseek,
321         .release = seq_release
322 };
323
324 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
325 {
326         int cpu;
327
328         if (*pos == 0)
329                 return SEQ_START_TOKEN;
330
331         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
332                 if (!cpu_possible(cpu))
333                         continue;
334                 *pos = cpu+1;
335                 return &per_cpu(ip_conntrack_stat, cpu);
336         }
337
338         return NULL;
339 }
340
341 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
342 {
343         int cpu;
344
345         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
346                 if (!cpu_possible(cpu))
347                         continue;
348                 *pos = cpu+1;
349                 return &per_cpu(ip_conntrack_stat, cpu);
350         }
351
352         return NULL;
353 }
354
355 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
356 {
357 }
358
359 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
360 {
361         unsigned int nr_conntracks = atomic_read(&ip_conntrack_count);
362         struct ip_conntrack_stat *st = v;
363
364         if (v == SEQ_START_TOKEN) {
365                 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");
366                 return 0;
367         }
368
369         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
370                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
371                    nr_conntracks,
372                    st->searched,
373                    st->found,
374                    st->new,
375                    st->invalid,
376                    st->ignore,
377                    st->delete,
378                    st->delete_list,
379                    st->insert,
380                    st->insert_failed,
381                    st->drop,
382                    st->early_drop,
383                    st->error,
384
385                    st->expect_new,
386                    st->expect_create,
387                    st->expect_delete
388                 );
389         return 0;
390 }
391
392 static struct seq_operations ct_cpu_seq_ops = {
393         .start  = ct_cpu_seq_start,
394         .next   = ct_cpu_seq_next,
395         .stop   = ct_cpu_seq_stop,
396         .show   = ct_cpu_seq_show,
397 };
398
399 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
400 {
401         return seq_open(file, &ct_cpu_seq_ops);
402 }
403
404 static struct file_operations ct_cpu_seq_fops = {
405         .owner   = THIS_MODULE,
406         .open    = ct_cpu_seq_open,
407         .read    = seq_read,
408         .llseek  = seq_lseek,
409         .release = seq_release_private,
410 };
411 #endif
412
413 static unsigned int ip_confirm(unsigned int hooknum,
414                                struct sk_buff **pskb,
415                                const struct net_device *in,
416                                const struct net_device *out,
417                                int (*okfn)(struct sk_buff *))
418 {
419         /* We've seen it coming out the other side: confirm it */
420         return ip_conntrack_confirm(pskb);
421 }
422
423 static unsigned int ip_conntrack_help(unsigned int hooknum,
424                                       struct sk_buff **pskb,
425                                       const struct net_device *in,
426                                       const struct net_device *out,
427                                       int (*okfn)(struct sk_buff *))
428 {
429         struct ip_conntrack *ct;
430         enum ip_conntrack_info ctinfo;
431
432         /* This is where we call the helper: as the packet goes out. */
433         ct = ip_conntrack_get(*pskb, &ctinfo);
434         if (ct && ct->helper && ctinfo != IP_CT_RELATED + IP_CT_IS_REPLY) {
435                 unsigned int ret;
436                 ret = ct->helper->help(pskb, ct, ctinfo);
437                 if (ret != NF_ACCEPT)
438                         return ret;
439         }
440         return NF_ACCEPT;
441 }
442
443 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
444                                         struct sk_buff **pskb,
445                                         const struct net_device *in,
446                                         const struct net_device *out,
447                                         int (*okfn)(struct sk_buff *))
448 {
449 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
450         /* Previously seen (loopback)?  Ignore.  Do this before
451            fragment check. */
452         if ((*pskb)->nfct)
453                 return NF_ACCEPT;
454 #endif
455
456         /* Gather fragments. */
457         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
458                 *pskb = ip_ct_gather_frags(*pskb,
459                                            hooknum == NF_IP_PRE_ROUTING ? 
460                                            IP_DEFRAG_CONNTRACK_IN :
461                                            IP_DEFRAG_CONNTRACK_OUT);
462                 if (!*pskb)
463                         return NF_STOLEN;
464         }
465         return NF_ACCEPT;
466 }
467
468 static unsigned int ip_conntrack_local(unsigned int hooknum,
469                                        struct sk_buff **pskb,
470                                        const struct net_device *in,
471                                        const struct net_device *out,
472                                        int (*okfn)(struct sk_buff *))
473 {
474         /* root is playing with raw sockets. */
475         if ((*pskb)->len < sizeof(struct iphdr)
476             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
477                 if (net_ratelimit())
478                         printk("ipt_hook: happy cracking.\n");
479                 return NF_ACCEPT;
480         }
481         return ip_conntrack_in(hooknum, pskb, in, out, okfn);
482 }
483
484 /* Connection tracking may drop packets, but never alters them, so
485    make it the first hook. */
486 static struct nf_hook_ops ip_conntrack_ops[] = {
487         {
488                 .hook           = ip_conntrack_defrag,
489                 .owner          = THIS_MODULE,
490                 .pf             = PF_INET,
491                 .hooknum        = NF_IP_PRE_ROUTING,
492                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
493         },
494         {
495                 .hook           = ip_conntrack_in,
496                 .owner          = THIS_MODULE,
497                 .pf             = PF_INET,
498                 .hooknum        = NF_IP_PRE_ROUTING,
499                 .priority       = NF_IP_PRI_CONNTRACK,
500         },
501         {
502                 .hook           = ip_conntrack_defrag,
503                 .owner          = THIS_MODULE,
504                 .pf             = PF_INET,
505                 .hooknum        = NF_IP_LOCAL_OUT,
506                 .priority       = NF_IP_PRI_CONNTRACK_DEFRAG,
507         },
508         {
509                 .hook           = ip_conntrack_local,
510                 .owner          = THIS_MODULE,
511                 .pf             = PF_INET,
512                 .hooknum        = NF_IP_LOCAL_OUT,
513                 .priority       = NF_IP_PRI_CONNTRACK,
514         },
515         {
516                 .hook           = ip_conntrack_help,
517                 .owner          = THIS_MODULE,
518                 .pf             = PF_INET,
519                 .hooknum        = NF_IP_POST_ROUTING,
520                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
521         },
522         {
523                 .hook           = ip_conntrack_help,
524                 .owner          = THIS_MODULE,
525                 .pf             = PF_INET,
526                 .hooknum        = NF_IP_LOCAL_IN,
527                 .priority       = NF_IP_PRI_CONNTRACK_HELPER,
528         },
529         {
530                 .hook           = ip_confirm,
531                 .owner          = THIS_MODULE,
532                 .pf             = PF_INET,
533                 .hooknum        = NF_IP_POST_ROUTING,
534                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
535         },
536         {
537                 .hook           = ip_confirm,
538                 .owner          = THIS_MODULE,
539                 .pf             = PF_INET,
540                 .hooknum        = NF_IP_LOCAL_IN,
541                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM,
542         },
543 };
544
545 /* Sysctl support */
546
547 int ip_conntrack_checksum = 1;
548
549 #ifdef CONFIG_SYSCTL
550
551 /* From ip_conntrack_core.c */
552 extern int ip_conntrack_max;
553 extern unsigned int ip_conntrack_htable_size;
554
555 /* From ip_conntrack_proto_tcp.c */
556 extern unsigned int ip_ct_tcp_timeout_syn_sent;
557 extern unsigned int ip_ct_tcp_timeout_syn_recv;
558 extern unsigned int ip_ct_tcp_timeout_established;
559 extern unsigned int ip_ct_tcp_timeout_fin_wait;
560 extern unsigned int ip_ct_tcp_timeout_close_wait;
561 extern unsigned int ip_ct_tcp_timeout_last_ack;
562 extern unsigned int ip_ct_tcp_timeout_time_wait;
563 extern unsigned int ip_ct_tcp_timeout_close;
564 extern unsigned int ip_ct_tcp_timeout_max_retrans;
565 extern int ip_ct_tcp_loose;
566 extern int ip_ct_tcp_be_liberal;
567 extern int ip_ct_tcp_max_retrans;
568
569 /* From ip_conntrack_proto_udp.c */
570 extern unsigned int ip_ct_udp_timeout;
571 extern unsigned int ip_ct_udp_timeout_stream;
572
573 /* From ip_conntrack_proto_icmp.c */
574 extern unsigned int ip_ct_icmp_timeout;
575
576 /* From ip_conntrack_proto_icmp.c */
577 extern unsigned int ip_ct_generic_timeout;
578
579 /* Log invalid packets of a given protocol */
580 static int log_invalid_proto_min = 0;
581 static int log_invalid_proto_max = 255;
582
583 static struct ctl_table_header *ip_ct_sysctl_header;
584
585 static ctl_table ip_ct_sysctl_table[] = {
586         {
587                 .ctl_name       = NET_IPV4_NF_CONNTRACK_MAX,
588                 .procname       = "ip_conntrack_max",
589                 .data           = &ip_conntrack_max,
590                 .maxlen         = sizeof(int),
591                 .mode           = 0644,
592                 .proc_handler   = &proc_dointvec,
593         },
594         {
595                 .ctl_name       = NET_IPV4_NF_CONNTRACK_COUNT,
596                 .procname       = "ip_conntrack_count",
597                 .data           = &ip_conntrack_count,
598                 .maxlen         = sizeof(int),
599                 .mode           = 0444,
600                 .proc_handler   = &proc_dointvec,
601         },
602         {
603                 .ctl_name       = NET_IPV4_NF_CONNTRACK_BUCKETS,
604                 .procname       = "ip_conntrack_buckets",
605                 .data           = &ip_conntrack_htable_size,
606                 .maxlen         = sizeof(unsigned int),
607                 .mode           = 0444,
608                 .proc_handler   = &proc_dointvec,
609         },
610         {
611                 .ctl_name       = NET_IPV4_NF_CONNTRACK_CHECKSUM,
612                 .procname       = "ip_conntrack_checksum",
613                 .data           = &ip_conntrack_checksum,
614                 .maxlen         = sizeof(int),
615                 .mode           = 0644,
616                 .proc_handler   = &proc_dointvec,
617         },
618         {
619                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
620                 .procname       = "ip_conntrack_tcp_timeout_syn_sent",
621                 .data           = &ip_ct_tcp_timeout_syn_sent,
622                 .maxlen         = sizeof(unsigned int),
623                 .mode           = 0644,
624                 .proc_handler   = &proc_dointvec_jiffies,
625         },
626         {
627                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
628                 .procname       = "ip_conntrack_tcp_timeout_syn_recv",
629                 .data           = &ip_ct_tcp_timeout_syn_recv,
630                 .maxlen         = sizeof(unsigned int),
631                 .mode           = 0644,
632                 .proc_handler   = &proc_dointvec_jiffies,
633         },
634         {
635                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
636                 .procname       = "ip_conntrack_tcp_timeout_established",
637                 .data           = &ip_ct_tcp_timeout_established,
638                 .maxlen         = sizeof(unsigned int),
639                 .mode           = 0644,
640                 .proc_handler   = &proc_dointvec_jiffies,
641         },
642         {
643                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
644                 .procname       = "ip_conntrack_tcp_timeout_fin_wait",
645                 .data           = &ip_ct_tcp_timeout_fin_wait,
646                 .maxlen         = sizeof(unsigned int),
647                 .mode           = 0644,
648                 .proc_handler   = &proc_dointvec_jiffies,
649         },
650         {
651                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
652                 .procname       = "ip_conntrack_tcp_timeout_close_wait",
653                 .data           = &ip_ct_tcp_timeout_close_wait,
654                 .maxlen         = sizeof(unsigned int),
655                 .mode           = 0644,
656                 .proc_handler   = &proc_dointvec_jiffies,
657         },
658         {
659                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
660                 .procname       = "ip_conntrack_tcp_timeout_last_ack",
661                 .data           = &ip_ct_tcp_timeout_last_ack,
662                 .maxlen         = sizeof(unsigned int),
663                 .mode           = 0644,
664                 .proc_handler   = &proc_dointvec_jiffies,
665         },
666         {
667                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
668                 .procname       = "ip_conntrack_tcp_timeout_time_wait",
669                 .data           = &ip_ct_tcp_timeout_time_wait,
670                 .maxlen         = sizeof(unsigned int),
671                 .mode           = 0644,
672                 .proc_handler   = &proc_dointvec_jiffies,
673         },
674         {
675                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
676                 .procname       = "ip_conntrack_tcp_timeout_close",
677                 .data           = &ip_ct_tcp_timeout_close,
678                 .maxlen         = sizeof(unsigned int),
679                 .mode           = 0644,
680                 .proc_handler   = &proc_dointvec_jiffies,
681         },
682         {
683                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
684                 .procname       = "ip_conntrack_udp_timeout",
685                 .data           = &ip_ct_udp_timeout,
686                 .maxlen         = sizeof(unsigned int),
687                 .mode           = 0644,
688                 .proc_handler   = &proc_dointvec_jiffies,
689         },
690         {
691                 .ctl_name       = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
692                 .procname       = "ip_conntrack_udp_timeout_stream",
693                 .data           = &ip_ct_udp_timeout_stream,
694                 .maxlen         = sizeof(unsigned int),
695                 .mode           = 0644,
696                 .proc_handler   = &proc_dointvec_jiffies,
697         },
698         {
699                 .ctl_name       = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
700                 .procname       = "ip_conntrack_icmp_timeout",
701                 .data           = &ip_ct_icmp_timeout,
702                 .maxlen         = sizeof(unsigned int),
703                 .mode           = 0644,
704                 .proc_handler   = &proc_dointvec_jiffies,
705         },
706         {
707                 .ctl_name       = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
708                 .procname       = "ip_conntrack_generic_timeout",
709                 .data           = &ip_ct_generic_timeout,
710                 .maxlen         = sizeof(unsigned int),
711                 .mode           = 0644,
712                 .proc_handler   = &proc_dointvec_jiffies,
713         },
714         {
715                 .ctl_name       = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
716                 .procname       = "ip_conntrack_log_invalid",
717                 .data           = &ip_ct_log_invalid,
718                 .maxlen         = sizeof(unsigned int),
719                 .mode           = 0644,
720                 .proc_handler   = &proc_dointvec_minmax,
721                 .strategy       = &sysctl_intvec,
722                 .extra1         = &log_invalid_proto_min,
723                 .extra2         = &log_invalid_proto_max,
724         },
725         {
726                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
727                 .procname       = "ip_conntrack_tcp_timeout_max_retrans",
728                 .data           = &ip_ct_tcp_timeout_max_retrans,
729                 .maxlen         = sizeof(unsigned int),
730                 .mode           = 0644,
731                 .proc_handler   = &proc_dointvec_jiffies,
732         },
733         {
734                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
735                 .procname       = "ip_conntrack_tcp_loose",
736                 .data           = &ip_ct_tcp_loose,
737                 .maxlen         = sizeof(unsigned int),
738                 .mode           = 0644,
739                 .proc_handler   = &proc_dointvec,
740         },
741         {
742                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
743                 .procname       = "ip_conntrack_tcp_be_liberal",
744                 .data           = &ip_ct_tcp_be_liberal,
745                 .maxlen         = sizeof(unsigned int),
746                 .mode           = 0644,
747                 .proc_handler   = &proc_dointvec,
748         },
749         {
750                 .ctl_name       = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
751                 .procname       = "ip_conntrack_tcp_max_retrans",
752                 .data           = &ip_ct_tcp_max_retrans,
753                 .maxlen         = sizeof(unsigned int),
754                 .mode           = 0644,
755                 .proc_handler   = &proc_dointvec,
756         },
757         { .ctl_name = 0 }
758 };
759
760 #define NET_IP_CONNTRACK_MAX 2089
761
762 static ctl_table ip_ct_netfilter_table[] = {
763         {
764                 .ctl_name       = NET_IPV4_NETFILTER,
765                 .procname       = "netfilter",
766                 .mode           = 0555,
767                 .child          = ip_ct_sysctl_table,
768         },
769         {
770                 .ctl_name       = NET_IP_CONNTRACK_MAX,
771                 .procname       = "ip_conntrack_max",
772                 .data           = &ip_conntrack_max,
773                 .maxlen         = sizeof(int),
774                 .mode           = 0644,
775                 .proc_handler   = &proc_dointvec
776         },
777         { .ctl_name = 0 }
778 };
779
780 static ctl_table ip_ct_ipv4_table[] = {
781         {
782                 .ctl_name       = NET_IPV4,
783                 .procname       = "ipv4",
784                 .mode           = 0555,
785                 .child          = ip_ct_netfilter_table,
786         },
787         { .ctl_name = 0 }
788 };
789
790 static ctl_table ip_ct_net_table[] = {
791         {
792                 .ctl_name       = CTL_NET,
793                 .procname       = "net",
794                 .mode           = 0555, 
795                 .child          = ip_ct_ipv4_table,
796         },
797         { .ctl_name = 0 }
798 };
799
800 EXPORT_SYMBOL(ip_ct_log_invalid);
801 #endif /* CONFIG_SYSCTL */
802
803 /* FIXME: Allow NULL functions and sub in pointers to generic for
804    them. --RR */
805 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
806 {
807         int ret = 0;
808
809         write_lock_bh(&ip_conntrack_lock);
810         if (ip_ct_protos[proto->proto] != &ip_conntrack_generic_protocol) {
811                 ret = -EBUSY;
812                 goto out;
813         }
814         ip_ct_protos[proto->proto] = proto;
815  out:
816         write_unlock_bh(&ip_conntrack_lock);
817         return ret;
818 }
819
820 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
821 {
822         write_lock_bh(&ip_conntrack_lock);
823         ip_ct_protos[proto->proto] = &ip_conntrack_generic_protocol;
824         write_unlock_bh(&ip_conntrack_lock);
825
826         /* Somebody could be still looking at the proto in bh. */
827         synchronize_net();
828
829         /* Remove all contrack entries for this protocol */
830         ip_ct_iterate_cleanup(kill_proto, &proto->proto);
831 }
832
833 static int __init ip_conntrack_standalone_init(void)
834 {
835 #ifdef CONFIG_PROC_FS
836         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
837 #endif
838         int ret = 0;
839
840         ret = ip_conntrack_init();
841         if (ret < 0)
842                 return ret;
843
844 #ifdef CONFIG_PROC_FS
845         ret = -ENOMEM;
846         proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
847         if (!proc) goto cleanup_init;
848
849         proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
850                                         &exp_file_ops);
851         if (!proc_exp) goto cleanup_proc;
852
853         proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
854         if (!proc_stat)
855                 goto cleanup_proc_exp;
856
857         proc_stat->proc_fops = &ct_cpu_seq_fops;
858         proc_stat->owner = THIS_MODULE;
859 #endif
860
861         ret = nf_register_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
862         if (ret < 0) {
863                 printk("ip_conntrack: can't register hooks.\n");
864                 goto cleanup_proc_stat;
865         }
866 #ifdef CONFIG_SYSCTL
867         ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
868         if (ip_ct_sysctl_header == NULL) {
869                 printk("ip_conntrack: can't register to sysctl.\n");
870                 ret = -ENOMEM;
871                 goto cleanup_hooks;
872         }
873 #endif
874         return ret;
875
876 #ifdef CONFIG_SYSCTL
877  cleanup_hooks:
878         nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
879 #endif
880  cleanup_proc_stat:
881 #ifdef CONFIG_PROC_FS
882         remove_proc_entry("ip_conntrack", proc_net_stat);
883  cleanup_proc_exp:
884         proc_net_remove("ip_conntrack_expect");
885  cleanup_proc:
886         proc_net_remove("ip_conntrack");
887  cleanup_init:
888 #endif /* CONFIG_PROC_FS */
889         ip_conntrack_cleanup();
890         return ret;
891 }
892
893 static void __exit ip_conntrack_standalone_fini(void)
894 {
895         synchronize_net();
896 #ifdef CONFIG_SYSCTL
897         unregister_sysctl_table(ip_ct_sysctl_header);
898 #endif
899         nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops));
900 #ifdef CONFIG_PROC_FS
901         remove_proc_entry("ip_conntrack", proc_net_stat);
902         proc_net_remove("ip_conntrack_expect");
903         proc_net_remove("ip_conntrack");
904 #endif /* CONFIG_PROC_FS */
905         ip_conntrack_cleanup();
906 }
907
908 module_init(ip_conntrack_standalone_init);
909 module_exit(ip_conntrack_standalone_fini);
910
911 /* Some modules need us, but don't depend directly on any symbol.
912    They should call this. */
913 void need_conntrack(void)
914 {
915 }
916
917 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
918 EXPORT_SYMBOL_GPL(ip_conntrack_chain);
919 EXPORT_SYMBOL_GPL(ip_conntrack_expect_chain);
920 EXPORT_SYMBOL_GPL(ip_conntrack_register_notifier);
921 EXPORT_SYMBOL_GPL(ip_conntrack_unregister_notifier);
922 EXPORT_SYMBOL_GPL(__ip_ct_event_cache_init);
923 EXPORT_PER_CPU_SYMBOL_GPL(ip_conntrack_ecache);
924 #endif
925 EXPORT_SYMBOL(ip_conntrack_protocol_register);
926 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
927 EXPORT_SYMBOL(ip_ct_get_tuple);
928 EXPORT_SYMBOL(invert_tuplepr);
929 EXPORT_SYMBOL(ip_conntrack_alter_reply);
930 EXPORT_SYMBOL(ip_conntrack_destroyed);
931 EXPORT_SYMBOL(need_conntrack);
932 EXPORT_SYMBOL(ip_conntrack_helper_register);
933 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
934 EXPORT_SYMBOL(ip_ct_iterate_cleanup);
935 EXPORT_SYMBOL(__ip_ct_refresh_acct);
936
937 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
938 EXPORT_SYMBOL(ip_conntrack_expect_put);
939 EXPORT_SYMBOL_GPL(__ip_conntrack_expect_find);
940 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find);
941 EXPORT_SYMBOL(ip_conntrack_expect_related);
942 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
943 EXPORT_SYMBOL_GPL(ip_conntrack_expect_list);
944 EXPORT_SYMBOL_GPL(ip_ct_unlink_expect);
945
946 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
947 EXPORT_SYMBOL(ip_ct_gather_frags);
948 EXPORT_SYMBOL(ip_conntrack_htable_size);
949 EXPORT_SYMBOL(ip_conntrack_lock);
950 EXPORT_SYMBOL(ip_conntrack_hash);
951 EXPORT_SYMBOL(ip_conntrack_untracked);
952 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
953 #ifdef CONFIG_IP_NF_NAT_NEEDED
954 EXPORT_SYMBOL(ip_conntrack_tcp_update);
955 #endif
956
957 EXPORT_SYMBOL_GPL(ip_conntrack_flush);
958 EXPORT_SYMBOL_GPL(__ip_conntrack_find);
959
960 EXPORT_SYMBOL_GPL(ip_conntrack_alloc);
961 EXPORT_SYMBOL_GPL(ip_conntrack_free);
962 EXPORT_SYMBOL_GPL(ip_conntrack_hash_insert);
963
964 EXPORT_SYMBOL_GPL(ip_ct_remove_expectations);
965
966 EXPORT_SYMBOL_GPL(ip_conntrack_helper_find_get);
967 EXPORT_SYMBOL_GPL(ip_conntrack_helper_put);
968 EXPORT_SYMBOL_GPL(__ip_conntrack_helper_find_byname);
969
970 EXPORT_SYMBOL_GPL(ip_conntrack_proto_find_get);
971 EXPORT_SYMBOL_GPL(ip_conntrack_proto_put);
972 EXPORT_SYMBOL_GPL(__ip_conntrack_proto_find);
973 EXPORT_SYMBOL_GPL(ip_conntrack_checksum);
974 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
975     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
976 EXPORT_SYMBOL_GPL(ip_ct_port_tuple_to_nfattr);
977 EXPORT_SYMBOL_GPL(ip_ct_port_nfattr_to_tuple);
978 #endif