]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/nf_conntrack_standalone.c
[NETFILTER]: nf_conntrack: sysctl compatibility with old connection tracking
[net-next-2.6.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
9fb9cbb1 32#include <net/netfilter/nf_conntrack.h>
f6180121 33#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 34#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 35#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 36#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 37#include <net/netfilter/nf_conntrack_helper.h>
9fb9cbb1
YK
38
39#if 0
40#define DEBUGP printk
41#else
42#define DEBUGP(format, args...)
43#endif
44
45MODULE_LICENSE("GPL");
46
9fb9cbb1 47#ifdef CONFIG_PROC_FS
77ab9cff 48int
9fb9cbb1
YK
49print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
50 struct nf_conntrack_l3proto *l3proto,
605dcad6 51 struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 52{
605dcad6 53 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
9fb9cbb1
YK
54}
55
56#ifdef CONFIG_NF_CT_ACCT
57static unsigned int
58seq_print_counters(struct seq_file *s,
59 const struct ip_conntrack_counter *counter)
60{
61 return seq_printf(s, "packets=%llu bytes=%llu ",
62 (unsigned long long)counter->packets,
63 (unsigned long long)counter->bytes);
64}
65#else
66#define seq_print_counters(x, y) 0
67#endif
68
69struct ct_iter_state {
70 unsigned int bucket;
71};
72
73static struct list_head *ct_get_first(struct seq_file *seq)
74{
75 struct ct_iter_state *st = seq->private;
76
77 for (st->bucket = 0;
78 st->bucket < nf_conntrack_htable_size;
79 st->bucket++) {
80 if (!list_empty(&nf_conntrack_hash[st->bucket]))
81 return nf_conntrack_hash[st->bucket].next;
82 }
83 return NULL;
84}
85
86static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
87{
88 struct ct_iter_state *st = seq->private;
89
90 head = head->next;
91 while (head == &nf_conntrack_hash[st->bucket]) {
92 if (++st->bucket >= nf_conntrack_htable_size)
93 return NULL;
94 head = nf_conntrack_hash[st->bucket].next;
95 }
96 return head;
97}
98
99static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
100{
101 struct list_head *head = ct_get_first(seq);
102
103 if (head)
104 while (pos && (head = ct_get_next(seq, head)))
105 pos--;
106 return pos ? NULL : head;
107}
108
109static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
110{
111 read_lock_bh(&nf_conntrack_lock);
112 return ct_get_idx(seq, *pos);
113}
114
115static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
116{
117 (*pos)++;
118 return ct_get_next(s, v);
119}
120
121static void ct_seq_stop(struct seq_file *s, void *v)
122{
123 read_unlock_bh(&nf_conntrack_lock);
124}
125
126/* return 0 on success, 1 in case of error */
127static int ct_seq_show(struct seq_file *s, void *v)
128{
129 const struct nf_conntrack_tuple_hash *hash = v;
130 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
131 struct nf_conntrack_l3proto *l3proto;
605dcad6 132 struct nf_conntrack_l4proto *l4proto;
9fb9cbb1 133
9fb9cbb1
YK
134 NF_CT_ASSERT(conntrack);
135
136 /* we only want to print DIR_ORIGINAL */
137 if (NF_CT_DIRECTION(hash))
138 return 0;
139
c1d10adb
PNA
140 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
141 .tuple.src.l3num);
9fb9cbb1
YK
142
143 NF_CT_ASSERT(l3proto);
605dcad6 144 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
c1d10adb
PNA
145 .tuple.src.l3num,
146 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
147 .tuple.dst.protonum);
605dcad6 148 NF_CT_ASSERT(l4proto);
9fb9cbb1
YK
149
150 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
151 l3proto->name,
152 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
605dcad6 153 l4proto->name,
9fb9cbb1
YK
154 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
155 timer_pending(&conntrack->timeout)
156 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
157 return -ENOSPC;
158
159 if (l3proto->print_conntrack(s, conntrack))
160 return -ENOSPC;
161
605dcad6 162 if (l4proto->print_conntrack(s, conntrack))
9fb9cbb1
YK
163 return -ENOSPC;
164
165 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
605dcad6 166 l3proto, l4proto))
9fb9cbb1
YK
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,
605dcad6 177 l3proto, l4proto))
9fb9cbb1
YK
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_NF_CONNTRACK_MARK)
188 if (seq_printf(s, "mark=%u ", conntrack->mark))
189 return -ENOSPC;
190#endif
191
7c9728c3
JM
192#ifdef CONFIG_NF_CONNTRACK_SECMARK
193 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
194 return -ENOSPC;
195#endif
196
9fb9cbb1
YK
197 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
198 return -ENOSPC;
199
200 return 0;
201}
202
203static struct seq_operations ct_seq_ops = {
204 .start = ct_seq_start,
205 .next = ct_seq_next,
206 .stop = ct_seq_stop,
207 .show = ct_seq_show
208};
209
210static int ct_open(struct inode *inode, struct file *file)
211{
212 struct seq_file *seq;
213 struct ct_iter_state *st;
214 int ret;
215
216 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
217 if (st == NULL)
218 return -ENOMEM;
219 ret = seq_open(file, &ct_seq_ops);
220 if (ret)
221 goto out_free;
222 seq = file->private_data;
223 seq->private = st;
224 memset(st, 0, sizeof(struct ct_iter_state));
225 return ret;
226out_free:
227 kfree(st);
228 return ret;
229}
230
231static struct file_operations ct_file_ops = {
232 .owner = THIS_MODULE,
233 .open = ct_open,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = seq_release_private,
237};
238
9fb9cbb1
YK
239static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
240{
241 int cpu;
242
243 if (*pos == 0)
244 return SEQ_START_TOKEN;
245
246 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
247 if (!cpu_possible(cpu))
248 continue;
249 *pos = cpu + 1;
250 return &per_cpu(nf_conntrack_stat, cpu);
251 }
252
253 return NULL;
254}
255
256static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
257{
258 int cpu;
259
260 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
261 if (!cpu_possible(cpu))
262 continue;
263 *pos = cpu + 1;
264 return &per_cpu(nf_conntrack_stat, cpu);
265 }
266
267 return NULL;
268}
269
270static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
271{
272}
273
274static int ct_cpu_seq_show(struct seq_file *seq, void *v)
275{
276 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
277 struct ip_conntrack_stat *st = v;
278
279 if (v == SEQ_START_TOKEN) {
280 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");
281 return 0;
282 }
283
284 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
285 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
286 nr_conntracks,
287 st->searched,
288 st->found,
289 st->new,
290 st->invalid,
291 st->ignore,
292 st->delete,
293 st->delete_list,
294 st->insert,
295 st->insert_failed,
296 st->drop,
297 st->early_drop,
298 st->error,
299
300 st->expect_new,
301 st->expect_create,
302 st->expect_delete
303 );
304 return 0;
305}
306
307static struct seq_operations ct_cpu_seq_ops = {
308 .start = ct_cpu_seq_start,
309 .next = ct_cpu_seq_next,
310 .stop = ct_cpu_seq_stop,
311 .show = ct_cpu_seq_show,
312};
313
314static int ct_cpu_seq_open(struct inode *inode, struct file *file)
315{
316 return seq_open(file, &ct_cpu_seq_ops);
317}
318
319static struct file_operations ct_cpu_seq_fops = {
320 .owner = THIS_MODULE,
321 .open = ct_cpu_seq_open,
322 .read = seq_read,
323 .llseek = seq_lseek,
324 .release = seq_release_private,
325};
326#endif /* CONFIG_PROC_FS */
327
328/* Sysctl support */
329
94aec08e 330int nf_conntrack_checksum __read_mostly = 1;
72b55823 331
9fb9cbb1 332#ifdef CONFIG_SYSCTL
9fb9cbb1
YK
333/* Log invalid packets of a given protocol */
334static int log_invalid_proto_min = 0;
335static int log_invalid_proto_max = 255;
336
337static struct ctl_table_header *nf_ct_sysctl_header;
338
339static ctl_table nf_ct_sysctl_table[] = {
340 {
341 .ctl_name = NET_NF_CONNTRACK_MAX,
342 .procname = "nf_conntrack_max",
343 .data = &nf_conntrack_max,
344 .maxlen = sizeof(int),
345 .mode = 0644,
346 .proc_handler = &proc_dointvec,
347 },
348 {
349 .ctl_name = NET_NF_CONNTRACK_COUNT,
350 .procname = "nf_conntrack_count",
351 .data = &nf_conntrack_count,
352 .maxlen = sizeof(int),
353 .mode = 0444,
354 .proc_handler = &proc_dointvec,
355 },
356 {
357 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
358 .procname = "nf_conntrack_buckets",
359 .data = &nf_conntrack_htable_size,
360 .maxlen = sizeof(unsigned int),
361 .mode = 0444,
362 .proc_handler = &proc_dointvec,
363 },
39a27a35
PM
364 {
365 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
366 .procname = "nf_conntrack_checksum",
367 .data = &nf_conntrack_checksum,
368 .maxlen = sizeof(unsigned int),
369 .mode = 0644,
370 .proc_handler = &proc_dointvec,
371 },
9fb9cbb1
YK
372 {
373 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
374 .procname = "nf_conntrack_log_invalid",
375 .data = &nf_ct_log_invalid,
376 .maxlen = sizeof(unsigned int),
377 .mode = 0644,
378 .proc_handler = &proc_dointvec_minmax,
379 .strategy = &sysctl_intvec,
380 .extra1 = &log_invalid_proto_min,
381 .extra2 = &log_invalid_proto_max,
382 },
9fb9cbb1
YK
383
384 { .ctl_name = 0 }
385};
386
387#define NET_NF_CONNTRACK_MAX 2089
388
389static ctl_table nf_ct_netfilter_table[] = {
390 {
391 .ctl_name = NET_NETFILTER,
392 .procname = "netfilter",
393 .mode = 0555,
394 .child = nf_ct_sysctl_table,
395 },
396 {
397 .ctl_name = NET_NF_CONNTRACK_MAX,
398 .procname = "nf_conntrack_max",
399 .data = &nf_conntrack_max,
400 .maxlen = sizeof(int),
401 .mode = 0644,
402 .proc_handler = &proc_dointvec,
403 },
404 { .ctl_name = 0 }
405};
406
407static ctl_table nf_ct_net_table[] = {
408 {
409 .ctl_name = CTL_NET,
410 .procname = "net",
411 .mode = 0555,
412 .child = nf_ct_netfilter_table,
413 },
414 { .ctl_name = 0 }
415};
416EXPORT_SYMBOL(nf_ct_log_invalid);
417#endif /* CONFIG_SYSCTL */
418
65b4b4e8 419static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 420{
32292a7f
PM
421#ifdef CONFIG_PROC_FS
422 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
423#endif
424 int ret = 0;
425
426 ret = nf_conntrack_init();
427 if (ret < 0)
428 return ret;
429
430#ifdef CONFIG_PROC_FS
431 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
432 if (!proc) goto cleanup_init;
433
434 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
435 &exp_file_ops);
436 if (!proc_exp) goto cleanup_proc;
437
438 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
439 if (!proc_stat)
440 goto cleanup_proc_exp;
441
442 proc_stat->proc_fops = &ct_cpu_seq_fops;
443 proc_stat->owner = THIS_MODULE;
444#endif
445#ifdef CONFIG_SYSCTL
446 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
447 if (nf_ct_sysctl_header == NULL) {
448 printk("nf_conntrack: can't register to sysctl.\n");
449 ret = -ENOMEM;
450 goto cleanup_proc_stat;
451 }
452#endif
453 return ret;
454
455#ifdef CONFIG_SYSCTL
456 cleanup_proc_stat:
457#endif
458#ifdef CONFIG_PROC_FS
459 remove_proc_entry("nf_conntrack", proc_net_stat);
460 cleanup_proc_exp:
461 proc_net_remove("nf_conntrack_expect");
462 cleanup_proc:
463 proc_net_remove("nf_conntrack");
464 cleanup_init:
465#endif /* CNFIG_PROC_FS */
466 nf_conntrack_cleanup();
467 return ret;
9fb9cbb1
YK
468}
469
65b4b4e8 470static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 471{
32292a7f
PM
472#ifdef CONFIG_SYSCTL
473 unregister_sysctl_table(nf_ct_sysctl_header);
474#endif
475#ifdef CONFIG_PROC_FS
476 remove_proc_entry("nf_conntrack", proc_net_stat);
477 proc_net_remove("nf_conntrack_expect");
478 proc_net_remove("nf_conntrack");
479#endif /* CNFIG_PROC_FS */
480 nf_conntrack_cleanup();
9fb9cbb1
YK
481}
482
65b4b4e8
AM
483module_init(nf_conntrack_standalone_init);
484module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
485
486/* Some modules need us, but don't depend directly on any symbol.
487 They should call this. */
2e4e6a17 488void need_conntrack(void)
9fb9cbb1
YK
489{
490}
491
492#ifdef CONFIG_NF_CONNTRACK_EVENTS
493EXPORT_SYMBOL_GPL(nf_conntrack_chain);
494EXPORT_SYMBOL_GPL(nf_conntrack_expect_chain);
495EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
496EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
497EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
498EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache);
499EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
500#endif
b9f78f9f
PNA
501EXPORT_SYMBOL(nf_ct_l3proto_try_module_get);
502EXPORT_SYMBOL(nf_ct_l3proto_module_put);
9fb9cbb1
YK
503EXPORT_SYMBOL(nf_conntrack_l3proto_register);
504EXPORT_SYMBOL(nf_conntrack_l3proto_unregister);
605dcad6
MJ
505EXPORT_SYMBOL(nf_conntrack_l4proto_register);
506EXPORT_SYMBOL(nf_conntrack_l4proto_unregister);
9fb9cbb1 507EXPORT_SYMBOL(nf_ct_invert_tuplepr);
9fb9cbb1 508EXPORT_SYMBOL(nf_conntrack_destroyed);
2e4e6a17 509EXPORT_SYMBOL(need_conntrack);
9fb9cbb1
YK
510EXPORT_SYMBOL(nf_conntrack_helper_register);
511EXPORT_SYMBOL(nf_conntrack_helper_unregister);
512EXPORT_SYMBOL(nf_ct_iterate_cleanup);
513EXPORT_SYMBOL(__nf_ct_refresh_acct);
514EXPORT_SYMBOL(nf_ct_protos);
605dcad6
MJ
515EXPORT_SYMBOL(__nf_ct_l4proto_find);
516EXPORT_SYMBOL(nf_ct_l4proto_find_get);
517EXPORT_SYMBOL(nf_ct_l4proto_put);
c1d10adb
PNA
518EXPORT_SYMBOL(nf_ct_l3proto_find_get);
519EXPORT_SYMBOL(nf_ct_l3proto_put);
9fb9cbb1 520EXPORT_SYMBOL(nf_ct_l3protos);
39a27a35 521EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
9fb9cbb1
YK
522EXPORT_SYMBOL(nf_conntrack_expect_alloc);
523EXPORT_SYMBOL(nf_conntrack_expect_put);
524EXPORT_SYMBOL(nf_conntrack_expect_related);
525EXPORT_SYMBOL(nf_conntrack_unexpect_related);
526EXPORT_SYMBOL(nf_conntrack_tuple_taken);
527EXPORT_SYMBOL(nf_conntrack_htable_size);
528EXPORT_SYMBOL(nf_conntrack_lock);
529EXPORT_SYMBOL(nf_conntrack_hash);
530EXPORT_SYMBOL(nf_conntrack_untracked);
531EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
532#ifdef CONFIG_IP_NF_NAT_NEEDED
533EXPORT_SYMBOL(nf_conntrack_tcp_update);
534#endif
535EXPORT_SYMBOL(__nf_conntrack_confirm);
536EXPORT_SYMBOL(nf_ct_get_tuple);
537EXPORT_SYMBOL(nf_ct_invert_tuple);
538EXPORT_SYMBOL(nf_conntrack_in);
539EXPORT_SYMBOL(__nf_conntrack_attach);
c1d10adb
PNA
540EXPORT_SYMBOL(nf_conntrack_alloc);
541EXPORT_SYMBOL(nf_conntrack_free);
542EXPORT_SYMBOL(nf_conntrack_flush);
543EXPORT_SYMBOL(nf_ct_remove_expectations);
544EXPORT_SYMBOL(nf_ct_helper_find_get);
545EXPORT_SYMBOL(nf_ct_helper_put);
546EXPORT_SYMBOL(__nf_conntrack_helper_find_byname);
547EXPORT_SYMBOL(__nf_conntrack_find);
548EXPORT_SYMBOL(nf_ct_unlink_expect);
549EXPORT_SYMBOL(nf_conntrack_hash_insert);
550EXPORT_SYMBOL(__nf_conntrack_expect_find);
551EXPORT_SYMBOL(nf_conntrack_expect_find);
552EXPORT_SYMBOL(nf_conntrack_expect_list);
553#if defined(CONFIG_NF_CT_NETLINK) || \
554 defined(CONFIG_NF_CT_NETLINK_MODULE)
555EXPORT_SYMBOL(nf_ct_port_tuple_to_nfattr);
556EXPORT_SYMBOL(nf_ct_port_nfattr_to_tuple);
557#endif