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