blob: 9a1de0ca475b7b51802bb8098c726c62f43b8995 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* 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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#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>
39#include <net/netfilter/nf_conntrack_helper.h>
40#include <linux/netfilter_ipv4/listhelp.h>
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
70static int
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
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800163 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
164 .tuple.src.l3num);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800165
166 NF_CT_ASSERT(l3proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800167 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);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 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
James Morris7c9728c2006-06-09 00:31:46 -0700215#ifdef CONFIG_NF_CONNTRACK_SECMARK
216 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
217 return -ENOSPC;
218#endif
219
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 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
262/* expects */
263static void *exp_seq_start(struct seq_file *s, loff_t *pos)
264{
265 struct list_head *e = &nf_conntrack_expect_list;
266 loff_t i;
267
268 /* strange seq_file api calls stop even if we fail,
269 * thus we need to grab lock since stop unlocks */
270 read_lock_bh(&nf_conntrack_lock);
271
272 if (list_empty(e))
273 return NULL;
274
275 for (i = 0; i <= *pos; i++) {
276 e = e->next;
277 if (e == &nf_conntrack_expect_list)
278 return NULL;
279 }
280 return e;
281}
282
283static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
284{
285 struct list_head *e = v;
286
287 ++*pos;
288 e = e->next;
289
290 if (e == &nf_conntrack_expect_list)
291 return NULL;
292
293 return e;
294}
295
296static void exp_seq_stop(struct seq_file *s, void *v)
297{
298 read_unlock_bh(&nf_conntrack_lock);
299}
300
301static int exp_seq_show(struct seq_file *s, void *v)
302{
303 struct nf_conntrack_expect *expect = v;
304
305 if (expect->timeout.function)
306 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
307 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
308 else
309 seq_printf(s, "- ");
310 seq_printf(s, "l3proto = %u proto=%u ",
311 expect->tuple.src.l3num,
312 expect->tuple.dst.protonum);
313 print_tuple(s, &expect->tuple,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800314 __nf_ct_l3proto_find(expect->tuple.src.l3num),
315 __nf_ct_proto_find(expect->tuple.src.l3num,
316 expect->tuple.dst.protonum));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800317 return seq_putc(s, '\n');
318}
319
320static struct seq_operations exp_seq_ops = {
321 .start = exp_seq_start,
322 .next = exp_seq_next,
323 .stop = exp_seq_stop,
324 .show = exp_seq_show
325};
326
327static int exp_open(struct inode *inode, struct file *file)
328{
329 return seq_open(file, &exp_seq_ops);
330}
331
332static struct file_operations exp_file_ops = {
333 .owner = THIS_MODULE,
334 .open = exp_open,
335 .read = seq_read,
336 .llseek = seq_lseek,
337 .release = seq_release
338};
339
340static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
341{
342 int cpu;
343
344 if (*pos == 0)
345 return SEQ_START_TOKEN;
346
347 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
348 if (!cpu_possible(cpu))
349 continue;
350 *pos = cpu + 1;
351 return &per_cpu(nf_conntrack_stat, cpu);
352 }
353
354 return NULL;
355}
356
357static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
358{
359 int cpu;
360
361 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
362 if (!cpu_possible(cpu))
363 continue;
364 *pos = cpu + 1;
365 return &per_cpu(nf_conntrack_stat, cpu);
366 }
367
368 return NULL;
369}
370
371static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
372{
373}
374
375static int ct_cpu_seq_show(struct seq_file *seq, void *v)
376{
377 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
378 struct ip_conntrack_stat *st = v;
379
380 if (v == SEQ_START_TOKEN) {
381 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");
382 return 0;
383 }
384
385 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
386 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
387 nr_conntracks,
388 st->searched,
389 st->found,
390 st->new,
391 st->invalid,
392 st->ignore,
393 st->delete,
394 st->delete_list,
395 st->insert,
396 st->insert_failed,
397 st->drop,
398 st->early_drop,
399 st->error,
400
401 st->expect_new,
402 st->expect_create,
403 st->expect_delete
404 );
405 return 0;
406}
407
408static struct seq_operations ct_cpu_seq_ops = {
409 .start = ct_cpu_seq_start,
410 .next = ct_cpu_seq_next,
411 .stop = ct_cpu_seq_stop,
412 .show = ct_cpu_seq_show,
413};
414
415static int ct_cpu_seq_open(struct inode *inode, struct file *file)
416{
417 return seq_open(file, &ct_cpu_seq_ops);
418}
419
420static struct file_operations ct_cpu_seq_fops = {
421 .owner = THIS_MODULE,
422 .open = ct_cpu_seq_open,
423 .read = seq_read,
424 .llseek = seq_lseek,
425 .release = seq_release_private,
426};
427#endif /* CONFIG_PROC_FS */
428
429/* Sysctl support */
430
Brian Haley94aec082006-09-18 00:05:22 -0700431int nf_conntrack_checksum __read_mostly = 1;
Adrian Bunk72b55822006-07-24 22:53:12 -0700432
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800433#ifdef CONFIG_SYSCTL
434
435/* From nf_conntrack_core.c */
436extern int nf_conntrack_max;
437extern unsigned int nf_conntrack_htable_size;
438
439/* From nf_conntrack_proto_tcp.c */
Patrick McHardybabbdb12006-01-09 17:48:09 -0800440extern unsigned int nf_ct_tcp_timeout_syn_sent;
441extern unsigned int nf_ct_tcp_timeout_syn_recv;
442extern unsigned int nf_ct_tcp_timeout_established;
443extern unsigned int nf_ct_tcp_timeout_fin_wait;
444extern unsigned int nf_ct_tcp_timeout_close_wait;
445extern unsigned int nf_ct_tcp_timeout_last_ack;
446extern unsigned int nf_ct_tcp_timeout_time_wait;
447extern unsigned int nf_ct_tcp_timeout_close;
448extern unsigned int nf_ct_tcp_timeout_max_retrans;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449extern int nf_ct_tcp_loose;
450extern int nf_ct_tcp_be_liberal;
451extern int nf_ct_tcp_max_retrans;
452
453/* From nf_conntrack_proto_udp.c */
Patrick McHardybabbdb12006-01-09 17:48:09 -0800454extern unsigned int nf_ct_udp_timeout;
455extern unsigned int nf_ct_udp_timeout_stream;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456
457/* From nf_conntrack_proto_generic.c */
Patrick McHardybabbdb12006-01-09 17:48:09 -0800458extern unsigned int nf_ct_generic_timeout;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459
460/* Log invalid packets of a given protocol */
461static int log_invalid_proto_min = 0;
462static int log_invalid_proto_max = 255;
463
464static struct ctl_table_header *nf_ct_sysctl_header;
465
466static ctl_table nf_ct_sysctl_table[] = {
467 {
468 .ctl_name = NET_NF_CONNTRACK_MAX,
469 .procname = "nf_conntrack_max",
470 .data = &nf_conntrack_max,
471 .maxlen = sizeof(int),
472 .mode = 0644,
473 .proc_handler = &proc_dointvec,
474 },
475 {
476 .ctl_name = NET_NF_CONNTRACK_COUNT,
477 .procname = "nf_conntrack_count",
478 .data = &nf_conntrack_count,
479 .maxlen = sizeof(int),
480 .mode = 0444,
481 .proc_handler = &proc_dointvec,
482 },
483 {
484 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
485 .procname = "nf_conntrack_buckets",
486 .data = &nf_conntrack_htable_size,
487 .maxlen = sizeof(unsigned int),
488 .mode = 0444,
489 .proc_handler = &proc_dointvec,
490 },
491 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700492 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
493 .procname = "nf_conntrack_checksum",
494 .data = &nf_conntrack_checksum,
495 .maxlen = sizeof(unsigned int),
496 .mode = 0644,
497 .proc_handler = &proc_dointvec,
498 },
499 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800500 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
501 .procname = "nf_conntrack_tcp_timeout_syn_sent",
502 .data = &nf_ct_tcp_timeout_syn_sent,
503 .maxlen = sizeof(unsigned int),
504 .mode = 0644,
505 .proc_handler = &proc_dointvec_jiffies,
506 },
507 {
508 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
509 .procname = "nf_conntrack_tcp_timeout_syn_recv",
510 .data = &nf_ct_tcp_timeout_syn_recv,
511 .maxlen = sizeof(unsigned int),
512 .mode = 0644,
513 .proc_handler = &proc_dointvec_jiffies,
514 },
515 {
516 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
517 .procname = "nf_conntrack_tcp_timeout_established",
518 .data = &nf_ct_tcp_timeout_established,
519 .maxlen = sizeof(unsigned int),
520 .mode = 0644,
521 .proc_handler = &proc_dointvec_jiffies,
522 },
523 {
524 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
525 .procname = "nf_conntrack_tcp_timeout_fin_wait",
526 .data = &nf_ct_tcp_timeout_fin_wait,
527 .maxlen = sizeof(unsigned int),
528 .mode = 0644,
529 .proc_handler = &proc_dointvec_jiffies,
530 },
531 {
532 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
533 .procname = "nf_conntrack_tcp_timeout_close_wait",
534 .data = &nf_ct_tcp_timeout_close_wait,
535 .maxlen = sizeof(unsigned int),
536 .mode = 0644,
537 .proc_handler = &proc_dointvec_jiffies,
538 },
539 {
540 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
541 .procname = "nf_conntrack_tcp_timeout_last_ack",
542 .data = &nf_ct_tcp_timeout_last_ack,
543 .maxlen = sizeof(unsigned int),
544 .mode = 0644,
545 .proc_handler = &proc_dointvec_jiffies,
546 },
547 {
548 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
549 .procname = "nf_conntrack_tcp_timeout_time_wait",
550 .data = &nf_ct_tcp_timeout_time_wait,
551 .maxlen = sizeof(unsigned int),
552 .mode = 0644,
553 .proc_handler = &proc_dointvec_jiffies,
554 },
555 {
556 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
557 .procname = "nf_conntrack_tcp_timeout_close",
558 .data = &nf_ct_tcp_timeout_close,
559 .maxlen = sizeof(unsigned int),
560 .mode = 0644,
561 .proc_handler = &proc_dointvec_jiffies,
562 },
563 {
564 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT,
565 .procname = "nf_conntrack_udp_timeout",
566 .data = &nf_ct_udp_timeout,
567 .maxlen = sizeof(unsigned int),
568 .mode = 0644,
569 .proc_handler = &proc_dointvec_jiffies,
570 },
571 {
572 .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
573 .procname = "nf_conntrack_udp_timeout_stream",
574 .data = &nf_ct_udp_timeout_stream,
575 .maxlen = sizeof(unsigned int),
576 .mode = 0644,
577 .proc_handler = &proc_dointvec_jiffies,
578 },
579 {
580 .ctl_name = NET_NF_CONNTRACK_GENERIC_TIMEOUT,
581 .procname = "nf_conntrack_generic_timeout",
582 .data = &nf_ct_generic_timeout,
583 .maxlen = sizeof(unsigned int),
584 .mode = 0644,
585 .proc_handler = &proc_dointvec_jiffies,
586 },
587 {
588 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
589 .procname = "nf_conntrack_log_invalid",
590 .data = &nf_ct_log_invalid,
591 .maxlen = sizeof(unsigned int),
592 .mode = 0644,
593 .proc_handler = &proc_dointvec_minmax,
594 .strategy = &sysctl_intvec,
595 .extra1 = &log_invalid_proto_min,
596 .extra2 = &log_invalid_proto_max,
597 },
598 {
599 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
600 .procname = "nf_conntrack_tcp_timeout_max_retrans",
601 .data = &nf_ct_tcp_timeout_max_retrans,
602 .maxlen = sizeof(unsigned int),
603 .mode = 0644,
604 .proc_handler = &proc_dointvec_jiffies,
605 },
606 {
607 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
608 .procname = "nf_conntrack_tcp_loose",
609 .data = &nf_ct_tcp_loose,
610 .maxlen = sizeof(unsigned int),
611 .mode = 0644,
612 .proc_handler = &proc_dointvec,
613 },
614 {
615 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
616 .procname = "nf_conntrack_tcp_be_liberal",
617 .data = &nf_ct_tcp_be_liberal,
618 .maxlen = sizeof(unsigned int),
619 .mode = 0644,
620 .proc_handler = &proc_dointvec,
621 },
622 {
623 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
624 .procname = "nf_conntrack_tcp_max_retrans",
625 .data = &nf_ct_tcp_max_retrans,
626 .maxlen = sizeof(unsigned int),
627 .mode = 0644,
628 .proc_handler = &proc_dointvec,
629 },
630
631 { .ctl_name = 0 }
632};
633
634#define NET_NF_CONNTRACK_MAX 2089
635
636static ctl_table nf_ct_netfilter_table[] = {
637 {
638 .ctl_name = NET_NETFILTER,
639 .procname = "netfilter",
640 .mode = 0555,
641 .child = nf_ct_sysctl_table,
642 },
643 {
644 .ctl_name = NET_NF_CONNTRACK_MAX,
645 .procname = "nf_conntrack_max",
646 .data = &nf_conntrack_max,
647 .maxlen = sizeof(int),
648 .mode = 0644,
649 .proc_handler = &proc_dointvec,
650 },
651 { .ctl_name = 0 }
652};
653
654static ctl_table nf_ct_net_table[] = {
655 {
656 .ctl_name = CTL_NET,
657 .procname = "net",
658 .mode = 0555,
659 .child = nf_ct_netfilter_table,
660 },
661 { .ctl_name = 0 }
662};
663EXPORT_SYMBOL(nf_ct_log_invalid);
664#endif /* CONFIG_SYSCTL */
665
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800666int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
667{
668 int ret = 0;
669
670 write_lock_bh(&nf_conntrack_lock);
671 if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_generic_l3proto) {
672 ret = -EBUSY;
673 goto out;
674 }
675 nf_ct_l3protos[proto->l3proto] = proto;
676out:
677 write_unlock_bh(&nf_conntrack_lock);
678
679 return ret;
680}
681
682void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
683{
684 write_lock_bh(&nf_conntrack_lock);
685 nf_ct_l3protos[proto->l3proto] = &nf_conntrack_generic_l3proto;
686 write_unlock_bh(&nf_conntrack_lock);
687
688 /* Somebody could be still looking at the proto in bh. */
689 synchronize_net();
690
691 /* Remove all contrack entries for this protocol */
692 nf_ct_iterate_cleanup(kill_l3proto, proto);
693}
694
695/* FIXME: Allow NULL functions and sub in pointers to generic for
696 them. --RR */
697int nf_conntrack_protocol_register(struct nf_conntrack_protocol *proto)
698{
699 int ret = 0;
700
701retry:
702 write_lock_bh(&nf_conntrack_lock);
703 if (nf_ct_protos[proto->l3proto]) {
704 if (nf_ct_protos[proto->l3proto][proto->proto]
705 != &nf_conntrack_generic_protocol) {
706 ret = -EBUSY;
707 goto out_unlock;
708 }
709 } else {
710 /* l3proto may be loaded latter. */
711 struct nf_conntrack_protocol **proto_array;
712 int i;
713
714 write_unlock_bh(&nf_conntrack_lock);
715
716 proto_array = (struct nf_conntrack_protocol **)
717 kmalloc(MAX_NF_CT_PROTO *
718 sizeof(struct nf_conntrack_protocol *),
719 GFP_KERNEL);
720 if (proto_array == NULL) {
721 ret = -ENOMEM;
722 goto out;
723 }
724 for (i = 0; i < MAX_NF_CT_PROTO; i++)
725 proto_array[i] = &nf_conntrack_generic_protocol;
726
727 write_lock_bh(&nf_conntrack_lock);
728 if (nf_ct_protos[proto->l3proto]) {
729 /* bad timing, but no problem */
730 write_unlock_bh(&nf_conntrack_lock);
731 kfree(proto_array);
732 } else {
733 nf_ct_protos[proto->l3proto] = proto_array;
734 write_unlock_bh(&nf_conntrack_lock);
735 }
736
737 /*
738 * Just once because array is never freed until unloading
739 * nf_conntrack.ko
740 */
741 goto retry;
742 }
743
744 nf_ct_protos[proto->l3proto][proto->proto] = proto;
745
746out_unlock:
747 write_unlock_bh(&nf_conntrack_lock);
748out:
749 return ret;
750}
751
752void nf_conntrack_protocol_unregister(struct nf_conntrack_protocol *proto)
753{
754 write_lock_bh(&nf_conntrack_lock);
755 nf_ct_protos[proto->l3proto][proto->proto]
756 = &nf_conntrack_generic_protocol;
757 write_unlock_bh(&nf_conntrack_lock);
758
759 /* Somebody could be still looking at the proto in bh. */
760 synchronize_net();
761
762 /* Remove all contrack entries for this protocol */
763 nf_ct_iterate_cleanup(kill_proto, proto);
764}
765
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800766static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800767{
Patrick McHardy32292a72006-04-06 14:11:30 -0700768#ifdef CONFIG_PROC_FS
769 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
770#endif
771 int ret = 0;
772
773 ret = nf_conntrack_init();
774 if (ret < 0)
775 return ret;
776
777#ifdef CONFIG_PROC_FS
778 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
779 if (!proc) goto cleanup_init;
780
781 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
782 &exp_file_ops);
783 if (!proc_exp) goto cleanup_proc;
784
785 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
786 if (!proc_stat)
787 goto cleanup_proc_exp;
788
789 proc_stat->proc_fops = &ct_cpu_seq_fops;
790 proc_stat->owner = THIS_MODULE;
791#endif
792#ifdef CONFIG_SYSCTL
793 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
794 if (nf_ct_sysctl_header == NULL) {
795 printk("nf_conntrack: can't register to sysctl.\n");
796 ret = -ENOMEM;
797 goto cleanup_proc_stat;
798 }
799#endif
800 return ret;
801
802#ifdef CONFIG_SYSCTL
803 cleanup_proc_stat:
804#endif
805#ifdef CONFIG_PROC_FS
806 remove_proc_entry("nf_conntrack", proc_net_stat);
807 cleanup_proc_exp:
808 proc_net_remove("nf_conntrack_expect");
809 cleanup_proc:
810 proc_net_remove("nf_conntrack");
811 cleanup_init:
812#endif /* CNFIG_PROC_FS */
813 nf_conntrack_cleanup();
814 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800815}
816
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800817static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800818{
Patrick McHardy32292a72006-04-06 14:11:30 -0700819#ifdef CONFIG_SYSCTL
820 unregister_sysctl_table(nf_ct_sysctl_header);
821#endif
822#ifdef CONFIG_PROC_FS
823 remove_proc_entry("nf_conntrack", proc_net_stat);
824 proc_net_remove("nf_conntrack_expect");
825 proc_net_remove("nf_conntrack");
826#endif /* CNFIG_PROC_FS */
827 nf_conntrack_cleanup();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800828}
829
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800830module_init(nf_conntrack_standalone_init);
831module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800832
833/* Some modules need us, but don't depend directly on any symbol.
834 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800835void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800836{
837}
838
839#ifdef CONFIG_NF_CONNTRACK_EVENTS
840EXPORT_SYMBOL_GPL(nf_conntrack_chain);
841EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
842EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
843EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
844EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
845EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
846EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
847#endif
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800848EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
849EXPORT_SYMBOL(nf_ct_l3proto_module_put);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800850EXPORT_SYMBOL(nf_conntrack_l3proto_register);
851EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
852EXPORT_SYMBOL(nf_conntrack_protocol_register);
853EXPORT_SYMBOL(nf_conntrack_protocol_unregister);
854EXPORT_SYMBOL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800855EXPORT_SYMBOL(nf_conntrack_destroyed);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800856EXPORT_SYMBOL(need_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800857EXPORT_SYMBOL(nf_conntrack_helper_register);
858EXPORT_SYMBOL(nf_conntrack_helper_unregister);
859EXPORT_SYMBOL(nf_ct_iterate_cleanup);
860EXPORT_SYMBOL(__nf_ct_refresh_acct);
861EXPORT_SYMBOL(nf_ct_protos);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800862EXPORT_SYMBOL(__nf_ct_proto_find);
863EXPORT_SYMBOL(nf_ct_proto_find_get);
864EXPORT_SYMBOL(nf_ct_proto_put);
865EXPORT_SYMBOL(nf_ct_l3proto_find_get);
866EXPORT_SYMBOL(nf_ct_l3proto_put);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800867EXPORT_SYMBOL(nf_ct_l3protos);
Patrick McHardy39a27a32006-05-29 18:23:54 -0700868EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800869EXPORT_SYMBOL(nf_conntrack_expect_alloc);
870EXPORT_SYMBOL(nf_conntrack_expect_put);
871EXPORT_SYMBOL(nf_conntrack_expect_related);
872EXPORT_SYMBOL(nf_conntrack_unexpect_related);
873EXPORT_SYMBOL(nf_conntrack_tuple_taken);
874EXPORT_SYMBOL(nf_conntrack_htable_size);
875EXPORT_SYMBOL(nf_conntrack_lock);
876EXPORT_SYMBOL(nf_conntrack_hash);
877EXPORT_SYMBOL(nf_conntrack_untracked);
878EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
879#ifdef CONFIG_IP_NF_NAT_NEEDED
880EXPORT_SYMBOL(nf_conntrack_tcp_update);
881#endif
882EXPORT_SYMBOL(__nf_conntrack_confirm);
883EXPORT_SYMBOL(nf_ct_get_tuple);
884EXPORT_SYMBOL(nf_ct_invert_tuple);
885EXPORT_SYMBOL(nf_conntrack_in);
886EXPORT_SYMBOL(__nf_conntrack_attach);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800887EXPORT_SYMBOL(nf_conntrack_alloc);
888EXPORT_SYMBOL(nf_conntrack_free);
889EXPORT_SYMBOL(nf_conntrack_flush);
890EXPORT_SYMBOL(nf_ct_remove_expectations);
891EXPORT_SYMBOL(nf_ct_helper_find_get);
892EXPORT_SYMBOL(nf_ct_helper_put);
893EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
894EXPORT_SYMBOL(__nf_conntrack_find);
895EXPORT_SYMBOL(nf_ct_unlink_expect);
896EXPORT_SYMBOL(nf_conntrack_hash_insert);
897EXPORT_SYMBOL(__nf_conntrack_expect_find);
898EXPORT_SYMBOL(nf_conntrack_expect_find);
899EXPORT_SYMBOL(nf_conntrack_expect_list);
900#if defined(CONFIG_NF_CT_NETLINK) || \
901 defined(CONFIG_NF_CT_NETLINK_MODULE)
902EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
903EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
904#endif