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