[SECMARK]: Add secmark support to conntrack
[safe/jmp/linux-2.6] / net / netfilter / nf_conntrack_standalone.c
1 /* This file contains all the functions required for the standalone
2    nf_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  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
15  *      - generalize L3 protocol dependent part.
16  *
17  * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
18  */
19
20 #include <linux/config.h>
21 #include <linux/types.h>
22 #include <linux/netfilter.h>
23 #include <linux/module.h>
24 #include <linux/skbuff.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/percpu.h>
28 #include <linux/netdevice.h>
29 #ifdef CONFIG_SYSCTL
30 #include <linux/sysctl.h>
31 #endif
32
33 #define ASSERT_READ_LOCK(x)
34 #define ASSERT_WRITE_LOCK(x)
35
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_protocol.h>
39 #include <net/netfilter/nf_conntrack_core.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <linux/netfilter_ipv4/listhelp.h>
42
43 #if 0
44 #define DEBUGP printk
45 #else
46 #define DEBUGP(format, args...)
47 #endif
48
49 MODULE_LICENSE("GPL");
50
51 extern atomic_t nf_conntrack_count;
52 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
53
54 static int kill_l3proto(struct nf_conn *i, void *data)
55 {
56         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num == 
57                         ((struct nf_conntrack_l3proto *)data)->l3proto);
58 }
59
60 static int kill_proto(struct nf_conn *i, void *data)
61 {
62         struct nf_conntrack_protocol *proto;
63         proto = (struct nf_conntrack_protocol *)data;
64         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum == 
65                         proto->proto) &&
66                (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
67                         proto->l3proto);
68 }
69
70 #ifdef CONFIG_PROC_FS
71 static int
72 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
73             struct nf_conntrack_l3proto *l3proto,
74             struct nf_conntrack_protocol *proto)
75 {
76         return l3proto->print_tuple(s, tuple) || proto->print_tuple(s, tuple);
77 }
78
79 #ifdef CONFIG_NF_CT_ACCT
80 static unsigned int
81 seq_print_counters(struct seq_file *s,
82                    const struct ip_conntrack_counter *counter)
83 {
84         return seq_printf(s, "packets=%llu bytes=%llu ",
85                           (unsigned long long)counter->packets,
86                           (unsigned long long)counter->bytes);
87 }
88 #else
89 #define seq_print_counters(x, y)        0
90 #endif
91
92 struct ct_iter_state {
93         unsigned int bucket;
94 };
95
96 static struct list_head *ct_get_first(struct seq_file *seq)
97 {
98         struct ct_iter_state *st = seq->private;
99
100         for (st->bucket = 0;
101              st->bucket < nf_conntrack_htable_size;
102              st->bucket++) {
103                 if (!list_empty(&nf_conntrack_hash[st->bucket]))
104                         return nf_conntrack_hash[st->bucket].next;
105         }
106         return NULL;
107 }
108
109 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
110 {
111         struct ct_iter_state *st = seq->private;
112
113         head = head->next;
114         while (head == &nf_conntrack_hash[st->bucket]) {
115                 if (++st->bucket >= nf_conntrack_htable_size)
116                         return NULL;
117                 head = nf_conntrack_hash[st->bucket].next;
118         }
119         return head;
120 }
121
122 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
123 {
124         struct list_head *head = ct_get_first(seq);
125
126         if (head)
127                 while (pos && (head = ct_get_next(seq, head)))
128                         pos--;
129         return pos ? NULL : head;
130 }
131
132 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
133 {
134         read_lock_bh(&nf_conntrack_lock);
135         return ct_get_idx(seq, *pos);
136 }
137
138 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
139 {
140         (*pos)++;
141         return ct_get_next(s, v);
142 }
143
144 static void ct_seq_stop(struct seq_file *s, void *v)
145 {
146         read_unlock_bh(&nf_conntrack_lock);
147 }
148
149 /* return 0 on success, 1 in case of error */
150 static int ct_seq_show(struct seq_file *s, void *v)
151 {
152         const struct nf_conntrack_tuple_hash *hash = v;
153         const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
154         struct nf_conntrack_l3proto *l3proto;
155         struct nf_conntrack_protocol *proto;
156
157         ASSERT_READ_LOCK(&nf_conntrack_lock);
158         NF_CT_ASSERT(conntrack);
159
160         /* we only want to print DIR_ORIGINAL */
161         if (NF_CT_DIRECTION(hash))
162                 return 0;
163
164         l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
165                                        .tuple.src.l3num);
166
167         NF_CT_ASSERT(l3proto);
168         proto = __nf_ct_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
169                                    .tuple.src.l3num,
170                                    conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
171                                    .tuple.dst.protonum);
172         NF_CT_ASSERT(proto);
173
174         if (seq_printf(s, "%-8s %u %-8s %u %ld ",
175                        l3proto->name,
176                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
177                        proto->name,
178                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
179                        timer_pending(&conntrack->timeout)
180                        ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
181                 return -ENOSPC;
182
183         if (l3proto->print_conntrack(s, conntrack))
184                 return -ENOSPC;
185
186         if (proto->print_conntrack(s, conntrack))
187                 return -ENOSPC;
188
189         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
190                         l3proto, proto))
191                 return -ENOSPC;
192
193         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
194                 return -ENOSPC;
195
196         if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
197                 if (seq_printf(s, "[UNREPLIED] "))
198                         return -ENOSPC;
199
200         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
201                         l3proto, proto))
202                 return -ENOSPC;
203
204         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
205                 return -ENOSPC;
206
207         if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
208                 if (seq_printf(s, "[ASSURED] "))
209                         return -ENOSPC;
210
211 #if defined(CONFIG_NF_CONNTRACK_MARK)
212         if (seq_printf(s, "mark=%u ", conntrack->mark))
213                 return -ENOSPC;
214 #endif
215
216 #ifdef CONFIG_NF_CONNTRACK_SECMARK
217         if (seq_printf(s, "secmark=%u ", conntrack->secmark))
218                 return -ENOSPC;
219 #endif
220
221         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
222                 return -ENOSPC;
223         
224         return 0;
225 }
226
227 static struct seq_operations ct_seq_ops = {
228         .start = ct_seq_start,
229         .next  = ct_seq_next,
230         .stop  = ct_seq_stop,
231         .show  = ct_seq_show
232 };
233
234 static int ct_open(struct inode *inode, struct file *file)
235 {
236         struct seq_file *seq;
237         struct ct_iter_state *st;
238         int ret;
239
240         st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
241         if (st == NULL)
242                 return -ENOMEM;
243         ret = seq_open(file, &ct_seq_ops);
244         if (ret)
245                 goto out_free;
246         seq          = file->private_data;
247         seq->private = st;
248         memset(st, 0, sizeof(struct ct_iter_state));
249         return ret;
250 out_free:
251         kfree(st);
252         return ret;
253 }
254
255 static struct file_operations ct_file_ops = {
256         .owner   = THIS_MODULE,
257         .open    = ct_open,
258         .read    = seq_read,
259         .llseek  = seq_lseek,
260         .release = seq_release_private,
261 };
262
263 /* expects */
264 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
265 {
266         struct list_head *e = &nf_conntrack_expect_list;
267         loff_t i;
268
269         /* strange seq_file api calls stop even if we fail,
270          * thus we need to grab lock since stop unlocks */
271         read_lock_bh(&nf_conntrack_lock);
272
273         if (list_empty(e))
274                 return NULL;
275
276         for (i = 0; i <= *pos; i++) {
277                 e = e->next;
278                 if (e == &nf_conntrack_expect_list)
279                         return NULL;
280         }
281         return e;
282 }
283
284 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
285 {
286         struct list_head *e = v;
287
288         ++*pos;
289         e = e->next;
290
291         if (e == &nf_conntrack_expect_list)
292                 return NULL;
293
294         return e;
295 }
296
297 static void exp_seq_stop(struct seq_file *s, void *v)
298 {
299         read_unlock_bh(&nf_conntrack_lock);
300 }
301
302 static int exp_seq_show(struct seq_file *s, void *v)
303 {
304         struct nf_conntrack_expect *expect = v;
305
306         if (expect->timeout.function)
307                 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
308                            ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
309         else
310                 seq_printf(s, "- ");
311         seq_printf(s, "l3proto = %u proto=%u ",
312                    expect->tuple.src.l3num,
313                    expect->tuple.dst.protonum);
314         print_tuple(s, &expect->tuple,
315                     __nf_ct_l3proto_find(expect->tuple.src.l3num),
316                     __nf_ct_proto_find(expect->tuple.src.l3num,
317                                        expect->tuple.dst.protonum));
318         return seq_putc(s, '\n');
319 }
320
321 static struct seq_operations exp_seq_ops = {
322         .start = exp_seq_start,
323         .next = exp_seq_next,
324         .stop = exp_seq_stop,
325         .show = exp_seq_show
326 };
327
328 static int exp_open(struct inode *inode, struct file *file)
329 {
330         return seq_open(file, &exp_seq_ops);
331 }
332
333 static struct file_operations exp_file_ops = {
334         .owner   = THIS_MODULE,
335         .open    = exp_open,
336         .read    = seq_read,
337         .llseek  = seq_lseek,
338         .release = seq_release
339 };
340
341 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
342 {
343         int cpu;
344
345         if (*pos == 0)
346                 return SEQ_START_TOKEN;
347
348         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
349                 if (!cpu_possible(cpu))
350                         continue;
351                 *pos = cpu + 1;
352                 return &per_cpu(nf_conntrack_stat, cpu);
353         }
354
355         return NULL;
356 }
357
358 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
359 {
360         int cpu;
361
362         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
363                 if (!cpu_possible(cpu))
364                         continue;
365                 *pos = cpu + 1;
366                 return &per_cpu(nf_conntrack_stat, cpu);
367         }
368
369         return NULL;
370 }
371
372 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
373 {
374 }
375
376 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
377 {
378         unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
379         struct ip_conntrack_stat *st = v;
380
381         if (v == SEQ_START_TOKEN) {
382                 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");
383                 return 0;
384         }
385
386         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
387                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
388                    nr_conntracks,
389                    st->searched,
390                    st->found,
391                    st->new,
392                    st->invalid,
393                    st->ignore,
394                    st->delete,
395                    st->delete_list,
396                    st->insert,
397                    st->insert_failed,
398                    st->drop,
399                    st->early_drop,
400                    st->error,
401
402                    st->expect_new,
403                    st->expect_create,
404                    st->expect_delete
405                 );
406         return 0;
407 }
408
409 static struct seq_operations ct_cpu_seq_ops = {
410         .start  = ct_cpu_seq_start,
411         .next   = ct_cpu_seq_next,
412         .stop   = ct_cpu_seq_stop,
413         .show   = ct_cpu_seq_show,
414 };
415
416 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
417 {
418         return seq_open(file, &ct_cpu_seq_ops);
419 }
420
421 static struct file_operations ct_cpu_seq_fops = {
422         .owner   = THIS_MODULE,
423         .open    = ct_cpu_seq_open,
424         .read    = seq_read,
425         .llseek  = seq_lseek,
426         .release = seq_release_private,
427 };
428 #endif /* CONFIG_PROC_FS */
429
430 /* Sysctl support */
431
432 #ifdef CONFIG_SYSCTL
433
434 /* From nf_conntrack_core.c */
435 extern int nf_conntrack_max;
436 extern unsigned int nf_conntrack_htable_size;
437
438 /* From nf_conntrack_proto_tcp.c */
439 extern unsigned int nf_ct_tcp_timeout_syn_sent;
440 extern unsigned int nf_ct_tcp_timeout_syn_recv;
441 extern unsigned int nf_ct_tcp_timeout_established;
442 extern unsigned int nf_ct_tcp_timeout_fin_wait;
443 extern unsigned int nf_ct_tcp_timeout_close_wait;
444 extern unsigned int nf_ct_tcp_timeout_last_ack;
445 extern unsigned int nf_ct_tcp_timeout_time_wait;
446 extern unsigned int nf_ct_tcp_timeout_close;
447 extern unsigned int nf_ct_tcp_timeout_max_retrans;
448 extern int nf_ct_tcp_loose;
449 extern int nf_ct_tcp_be_liberal;
450 extern int nf_ct_tcp_max_retrans;
451
452 /* From nf_conntrack_proto_udp.c */
453 extern unsigned int nf_ct_udp_timeout;
454 extern unsigned int nf_ct_udp_timeout_stream;
455
456 /* From nf_conntrack_proto_generic.c */
457 extern unsigned int nf_ct_generic_timeout;
458
459 /* Log invalid packets of a given protocol */
460 static int log_invalid_proto_min = 0;
461 static int log_invalid_proto_max = 255;
462
463 int nf_conntrack_checksum = 1;
464
465 static struct ctl_table_header *nf_ct_sysctl_header;
466
467 static ctl_table nf_ct_sysctl_table[] = {
468         {
469                 .ctl_name       = NET_NF_CONNTRACK_MAX,
470                 .procname       = "nf_conntrack_max",
471                 .data           = &nf_conntrack_max,
472                 .maxlen         = sizeof(int),
473                 .mode           = 0644,
474                 .proc_handler   = &proc_dointvec,
475         },
476         {
477                 .ctl_name       = NET_NF_CONNTRACK_COUNT,
478                 .procname       = "nf_conntrack_count",
479                 .data           = &nf_conntrack_count,
480                 .maxlen         = sizeof(int),
481                 .mode           = 0444,
482                 .proc_handler   = &proc_dointvec,
483         },
484         {
485                 .ctl_name       = NET_NF_CONNTRACK_BUCKETS,
486                 .procname       = "nf_conntrack_buckets",
487                 .data           = &nf_conntrack_htable_size,
488                 .maxlen         = sizeof(unsigned int),
489                 .mode           = 0444,
490                 .proc_handler   = &proc_dointvec,
491         },
492         {
493                 .ctl_name       = NET_NF_CONNTRACK_CHECKSUM,
494                 .procname       = "nf_conntrack_checksum",
495                 .data           = &nf_conntrack_checksum,
496                 .maxlen         = sizeof(unsigned int),
497                 .mode           = 0644,
498                 .proc_handler   = &proc_dointvec,
499         },
500         {
501                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
502                 .procname       = "nf_conntrack_tcp_timeout_syn_sent",
503                 .data           = &nf_ct_tcp_timeout_syn_sent,
504                 .maxlen         = sizeof(unsigned int),
505                 .mode           = 0644,
506                 .proc_handler   = &proc_dointvec_jiffies,
507         },
508         {
509                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
510                 .procname       = "nf_conntrack_tcp_timeout_syn_recv",
511                 .data           = &nf_ct_tcp_timeout_syn_recv,
512                 .maxlen         = sizeof(unsigned int),
513                 .mode           = 0644,
514                 .proc_handler   = &proc_dointvec_jiffies,
515         },
516         {
517                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
518                 .procname       = "nf_conntrack_tcp_timeout_established",
519                 .data           = &nf_ct_tcp_timeout_established,
520                 .maxlen         = sizeof(unsigned int),
521                 .mode           = 0644,
522                 .proc_handler   = &proc_dointvec_jiffies,
523         },
524         {
525                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
526                 .procname       = "nf_conntrack_tcp_timeout_fin_wait",
527                 .data           = &nf_ct_tcp_timeout_fin_wait,
528                 .maxlen         = sizeof(unsigned int),
529                 .mode           = 0644,
530                 .proc_handler   = &proc_dointvec_jiffies,
531         },
532         {
533                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
534                 .procname       = "nf_conntrack_tcp_timeout_close_wait",
535                 .data           = &nf_ct_tcp_timeout_close_wait,
536                 .maxlen         = sizeof(unsigned int),
537                 .mode           = 0644,
538                 .proc_handler   = &proc_dointvec_jiffies,
539         },
540         {
541                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
542                 .procname       = "nf_conntrack_tcp_timeout_last_ack",
543                 .data           = &nf_ct_tcp_timeout_last_ack,
544                 .maxlen         = sizeof(unsigned int),
545                 .mode           = 0644,
546                 .proc_handler   = &proc_dointvec_jiffies,
547         },
548         {
549                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
550                 .procname       = "nf_conntrack_tcp_timeout_time_wait",
551                 .data           = &nf_ct_tcp_timeout_time_wait,
552                 .maxlen         = sizeof(unsigned int),
553                 .mode           = 0644,
554                 .proc_handler   = &proc_dointvec_jiffies,
555         },
556         {
557                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
558                 .procname       = "nf_conntrack_tcp_timeout_close",
559                 .data           = &nf_ct_tcp_timeout_close,
560                 .maxlen         = sizeof(unsigned int),
561                 .mode           = 0644,
562                 .proc_handler   = &proc_dointvec_jiffies,
563         },
564         {
565                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT,
566                 .procname       = "nf_conntrack_udp_timeout",
567                 .data           = &nf_ct_udp_timeout,
568                 .maxlen         = sizeof(unsigned int),
569                 .mode           = 0644,
570                 .proc_handler   = &proc_dointvec_jiffies,
571         },
572         {
573                 .ctl_name       = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
574                 .procname       = "nf_conntrack_udp_timeout_stream",
575                 .data           = &nf_ct_udp_timeout_stream,
576                 .maxlen         = sizeof(unsigned int),
577                 .mode           = 0644,
578                 .proc_handler   = &proc_dointvec_jiffies,
579         },
580         {
581                 .ctl_name       = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
582                 .procname       = "nf_conntrack_generic_timeout",
583                 .data           = &nf_ct_generic_timeout,
584                 .maxlen         = sizeof(unsigned int),
585                 .mode           = 0644,
586                 .proc_handler   = &proc_dointvec_jiffies,
587         },
588         {
589                 .ctl_name       = NET_NF_CONNTRACK_LOG_INVALID,
590                 .procname       = "nf_conntrack_log_invalid",
591                 .data           = &nf_ct_log_invalid,
592                 .maxlen         = sizeof(unsigned int),
593                 .mode           = 0644,
594                 .proc_handler   = &proc_dointvec_minmax,
595                 .strategy       = &sysctl_intvec,
596                 .extra1         = &log_invalid_proto_min,
597                 .extra2         = &log_invalid_proto_max,
598         },
599         {
600                 .ctl_name       = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
601                 .procname       = "nf_conntrack_tcp_timeout_max_retrans",
602                 .data           = &nf_ct_tcp_timeout_max_retrans,
603                 .maxlen         = sizeof(unsigned int),
604                 .mode           = 0644,
605                 .proc_handler   = &proc_dointvec_jiffies,
606         },
607         {
608                 .ctl_name       = NET_NF_CONNTRACK_TCP_LOOSE,
609                 .procname       = "nf_conntrack_tcp_loose",
610                 .data           = &nf_ct_tcp_loose,
611                 .maxlen         = sizeof(unsigned int),
612                 .mode           = 0644,
613                 .proc_handler   = &proc_dointvec,
614         },
615         {
616                 .ctl_name       = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
617                 .procname       = "nf_conntrack_tcp_be_liberal",
618                 .data           = &nf_ct_tcp_be_liberal,
619                 .maxlen         = sizeof(unsigned int),
620                 .mode           = 0644,
621                 .proc_handler   = &proc_dointvec,
622         },
623         {
624                 .ctl_name       = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
625                 .procname       = "nf_conntrack_tcp_max_retrans",
626                 .data           = &nf_ct_tcp_max_retrans,
627                 .maxlen         = sizeof(unsigned int),
628                 .mode           = 0644,
629                 .proc_handler   = &proc_dointvec,
630         },
631
632         { .ctl_name = 0 }
633 };
634
635 #define NET_NF_CONNTRACK_MAX 2089
636
637 static ctl_table nf_ct_netfilter_table[] = {
638         {
639                 .ctl_name       = NET_NETFILTER,
640                 .procname       = "netfilter",
641                 .mode           = 0555,
642                 .child          = nf_ct_sysctl_table,
643         },
644         {
645                 .ctl_name       = NET_NF_CONNTRACK_MAX,
646                 .procname       = "nf_conntrack_max",
647                 .data           = &nf_conntrack_max,
648                 .maxlen         = sizeof(int),
649                 .mode           = 0644,
650                 .proc_handler   = &proc_dointvec,
651         },
652         { .ctl_name = 0 }
653 };
654
655 static ctl_table nf_ct_net_table[] = {
656         {
657                 .ctl_name       = CTL_NET,
658                 .procname       = "net",
659                 .mode           = 0555,
660                 .child          = nf_ct_netfilter_table,
661         },
662         { .ctl_name = 0 }
663 };
664 EXPORT_SYMBOL(nf_ct_log_invalid);
665 #endif /* CONFIG_SYSCTL */
666
667 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
668 {
669         int ret = 0;
670
671         write_lock_bh(&nf_conntrack_lock);
672         if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_generic_l3proto) {
673                 ret = -EBUSY;
674                 goto out;
675         }
676         nf_ct_l3protos[proto->l3proto] = proto;
677 out:
678         write_unlock_bh(&nf_conntrack_lock);
679
680         return ret;
681 }
682
683 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
684 {
685         write_lock_bh(&nf_conntrack_lock);
686         nf_ct_l3protos[proto->l3proto] = &nf_conntrack_generic_l3proto;
687         write_unlock_bh(&nf_conntrack_lock);
688         
689         /* Somebody could be still looking at the proto in bh. */
690         synchronize_net();
691
692         /* Remove all contrack entries for this protocol */
693         nf_ct_iterate_cleanup(kill_l3proto, proto);
694 }
695
696 /* FIXME: Allow NULL functions and sub in pointers to generic for
697    them. --RR */
698 int nf_conntrack_protocol_register(struct nf_conntrack_protocol *proto)
699 {
700         int ret = 0;
701
702 retry:
703         write_lock_bh(&nf_conntrack_lock);
704         if (nf_ct_protos[proto->l3proto]) {
705                 if (nf_ct_protos[proto->l3proto][proto->proto]
706                                 != &nf_conntrack_generic_protocol) {
707                         ret = -EBUSY;
708                         goto out_unlock;
709                 }
710         } else {
711                 /* l3proto may be loaded latter. */
712                 struct nf_conntrack_protocol **proto_array;
713                 int i;
714
715                 write_unlock_bh(&nf_conntrack_lock);
716
717                 proto_array = (struct nf_conntrack_protocol **)
718                                 kmalloc(MAX_NF_CT_PROTO *
719                                          sizeof(struct nf_conntrack_protocol *),
720                                         GFP_KERNEL);
721                 if (proto_array == NULL) {
722                         ret = -ENOMEM;
723                         goto out;
724                 }
725                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
726                         proto_array[i] = &nf_conntrack_generic_protocol;
727
728                 write_lock_bh(&nf_conntrack_lock);
729                 if (nf_ct_protos[proto->l3proto]) {
730                         /* bad timing, but no problem */
731                         write_unlock_bh(&nf_conntrack_lock);
732                         kfree(proto_array);
733                 } else {
734                         nf_ct_protos[proto->l3proto] = proto_array;
735                         write_unlock_bh(&nf_conntrack_lock);
736                 }
737
738                 /*
739                  * Just once because array is never freed until unloading
740                  * nf_conntrack.ko
741                  */
742                 goto retry;
743         }
744
745         nf_ct_protos[proto->l3proto][proto->proto] = proto;
746
747 out_unlock:
748         write_unlock_bh(&nf_conntrack_lock);
749 out:
750         return ret;
751 }
752
753 void nf_conntrack_protocol_unregister(struct nf_conntrack_protocol *proto)
754 {
755         write_lock_bh(&nf_conntrack_lock);
756         nf_ct_protos[proto->l3proto][proto->proto]
757                 = &nf_conntrack_generic_protocol;
758         write_unlock_bh(&nf_conntrack_lock);
759         
760         /* Somebody could be still looking at the proto in bh. */
761         synchronize_net();
762
763         /* Remove all contrack entries for this protocol */
764         nf_ct_iterate_cleanup(kill_proto, proto);
765 }
766
767 static int __init nf_conntrack_standalone_init(void)
768 {
769 #ifdef CONFIG_PROC_FS
770         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
771 #endif
772         int ret = 0;
773
774         ret = nf_conntrack_init();
775         if (ret < 0)
776                 return ret;
777
778 #ifdef CONFIG_PROC_FS
779         proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
780         if (!proc) goto cleanup_init;
781
782         proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
783                                         &exp_file_ops);
784         if (!proc_exp) goto cleanup_proc;
785
786         proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
787         if (!proc_stat)
788                 goto cleanup_proc_exp;
789
790         proc_stat->proc_fops = &ct_cpu_seq_fops;
791         proc_stat->owner = THIS_MODULE;
792 #endif
793 #ifdef CONFIG_SYSCTL
794         nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
795         if (nf_ct_sysctl_header == NULL) {
796                 printk("nf_conntrack: can't register to sysctl.\n");
797                 ret = -ENOMEM;
798                 goto cleanup_proc_stat;
799         }
800 #endif
801         return ret;
802
803 #ifdef CONFIG_SYSCTL
804  cleanup_proc_stat:
805 #endif
806 #ifdef CONFIG_PROC_FS
807         remove_proc_entry("nf_conntrack", proc_net_stat);
808  cleanup_proc_exp:
809         proc_net_remove("nf_conntrack_expect");
810  cleanup_proc:
811         proc_net_remove("nf_conntrack");
812  cleanup_init:
813 #endif /* CNFIG_PROC_FS */
814         nf_conntrack_cleanup();
815         return ret;
816 }
817
818 static void __exit nf_conntrack_standalone_fini(void)
819 {
820 #ifdef CONFIG_SYSCTL
821         unregister_sysctl_table(nf_ct_sysctl_header);
822 #endif
823 #ifdef CONFIG_PROC_FS
824         remove_proc_entry("nf_conntrack", proc_net_stat);
825         proc_net_remove("nf_conntrack_expect");
826         proc_net_remove("nf_conntrack");
827 #endif /* CNFIG_PROC_FS */
828         nf_conntrack_cleanup();
829 }
830
831 module_init(nf_conntrack_standalone_init);
832 module_exit(nf_conntrack_standalone_fini);
833
834 /* Some modules need us, but don't depend directly on any symbol.
835    They should call this. */
836 void need_conntrack(void)
837 {
838 }
839
840 #ifdef CONFIG_NF_CONNTRACK_EVENTS
841 EXPORT_SYMBOL_GPL(nf_conntrack_chain);
842 EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
843 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
844 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
845 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
846 EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
847 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
848 #endif
849 EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
850 EXPORT_SYMBOL(nf_ct_l3proto_module_put);
851 EXPORT_SYMBOL(nf_conntrack_l3proto_register);
852 EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
853 EXPORT_SYMBOL(nf_conntrack_protocol_register);
854 EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
855 EXPORT_SYMBOL(nf_ct_invert_tuplepr);
856 EXPORT_SYMBOL(nf_conntrack_destroyed);
857 EXPORT_SYMBOL(need_conntrack);
858 EXPORT_SYMBOL(nf_conntrack_helper_register);
859 EXPORT_SYMBOL(nf_conntrack_helper_unregister);
860 EXPORT_SYMBOL(nf_ct_iterate_cleanup);
861 EXPORT_SYMBOL(__nf_ct_refresh_acct);
862 EXPORT_SYMBOL(nf_ct_protos);
863 EXPORT_SYMBOL(__nf_ct_proto_find);
864 EXPORT_SYMBOL(nf_ct_proto_find_get);
865 EXPORT_SYMBOL(nf_ct_proto_put);
866 EXPORT_SYMBOL(nf_ct_l3proto_find_get);
867 EXPORT_SYMBOL(nf_ct_l3proto_put);
868 EXPORT_SYMBOL(nf_ct_l3protos);
869 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
870 EXPORT_SYMBOL(nf_conntrack_expect_alloc);
871 EXPORT_SYMBOL(nf_conntrack_expect_put);
872 EXPORT_SYMBOL(nf_conntrack_expect_related);
873 EXPORT_SYMBOL(nf_conntrack_unexpect_related);
874 EXPORT_SYMBOL(nf_conntrack_tuple_taken);
875 EXPORT_SYMBOL(nf_conntrack_htable_size);
876 EXPORT_SYMBOL(nf_conntrack_lock);
877 EXPORT_SYMBOL(nf_conntrack_hash);
878 EXPORT_SYMBOL(nf_conntrack_untracked);
879 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
880 #ifdef CONFIG_IP_NF_NAT_NEEDED
881 EXPORT_SYMBOL(nf_conntrack_tcp_update);
882 #endif
883 EXPORT_SYMBOL(__nf_conntrack_confirm);
884 EXPORT_SYMBOL(nf_ct_get_tuple);
885 EXPORT_SYMBOL(nf_ct_invert_tuple);
886 EXPORT_SYMBOL(nf_conntrack_in);
887 EXPORT_SYMBOL(__nf_conntrack_attach);
888 EXPORT_SYMBOL(nf_conntrack_alloc);
889 EXPORT_SYMBOL(nf_conntrack_free);
890 EXPORT_SYMBOL(nf_conntrack_flush);
891 EXPORT_SYMBOL(nf_ct_remove_expectations);
892 EXPORT_SYMBOL(nf_ct_helper_find_get);
893 EXPORT_SYMBOL(nf_ct_helper_put);
894 EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
895 EXPORT_SYMBOL(__nf_conntrack_find);
896 EXPORT_SYMBOL(nf_ct_unlink_expect);
897 EXPORT_SYMBOL(nf_conntrack_hash_insert);
898 EXPORT_SYMBOL(__nf_conntrack_expect_find);
899 EXPORT_SYMBOL(nf_conntrack_expect_find);
900 EXPORT_SYMBOL(nf_conntrack_expect_list);
901 #if defined(CONFIG_NF_CT_NETLINK) || \
902     defined(CONFIG_NF_CT_NETLINK_MODULE)
903 EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
904 EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
905 #endif