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