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