allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / nf_conntrack_standalone.c
blob99369c816ee32e1c48d9c9e722ea5e5719abec05
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/types.h>
10 #include <linux/netfilter.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/percpu.h>
16 #include <linux/netdevice.h>
17 #ifdef CONFIG_SYSCTL
18 #include <linux/sysctl.h>
19 #endif
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_l3proto.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
28 #if 0
29 #define DEBUGP printk
30 #else
31 #define DEBUGP(format, args...)
32 #endif
34 MODULE_LICENSE("GPL");
36 #ifdef CONFIG_PROC_FS
37 int
38 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
39 struct nf_conntrack_l3proto *l3proto,
40 struct nf_conntrack_l4proto *l4proto)
42 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
44 EXPORT_SYMBOL_GPL(print_tuple);
46 #ifdef CONFIG_NF_CT_ACCT
47 static unsigned int
48 seq_print_counters(struct seq_file *s,
49 const struct ip_conntrack_counter *counter)
51 return seq_printf(s, "packets=%llu bytes=%llu ",
52 (unsigned long long)counter->packets,
53 (unsigned long long)counter->bytes);
55 #else
56 #define seq_print_counters(x, y) 0
57 #endif
59 struct ct_iter_state {
60 unsigned int bucket;
63 static struct list_head *ct_get_first(struct seq_file *seq)
65 struct ct_iter_state *st = seq->private;
67 for (st->bucket = 0;
68 st->bucket < nf_conntrack_htable_size;
69 st->bucket++) {
70 if (!list_empty(&nf_conntrack_hash[st->bucket]))
71 return nf_conntrack_hash[st->bucket].next;
73 return NULL;
76 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
78 struct ct_iter_state *st = seq->private;
80 head = head->next;
81 while (head == &nf_conntrack_hash[st->bucket]) {
82 if (++st->bucket >= nf_conntrack_htable_size)
83 return NULL;
84 head = nf_conntrack_hash[st->bucket].next;
86 return head;
89 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
91 struct list_head *head = ct_get_first(seq);
93 if (head)
94 while (pos && (head = ct_get_next(seq, head)))
95 pos--;
96 return pos ? NULL : head;
99 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
101 read_lock_bh(&nf_conntrack_lock);
102 return ct_get_idx(seq, *pos);
105 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
107 (*pos)++;
108 return ct_get_next(s, v);
111 static void ct_seq_stop(struct seq_file *s, void *v)
113 read_unlock_bh(&nf_conntrack_lock);
116 /* return 0 on success, 1 in case of error */
117 static int ct_seq_show(struct seq_file *s, void *v)
119 const struct nf_conntrack_tuple_hash *hash = v;
120 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
121 struct nf_conntrack_l3proto *l3proto;
122 struct nf_conntrack_l4proto *l4proto;
124 NF_CT_ASSERT(conntrack);
126 /* we only want to print DIR_ORIGINAL */
127 if (NF_CT_DIRECTION(hash))
128 return 0;
130 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
131 .tuple.src.l3num);
133 NF_CT_ASSERT(l3proto);
134 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
135 .tuple.src.l3num,
136 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
137 .tuple.dst.protonum);
138 NF_CT_ASSERT(l4proto);
140 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
141 l3proto->name,
142 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
143 l4proto->name,
144 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
145 timer_pending(&conntrack->timeout)
146 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
147 return -ENOSPC;
149 if (l4proto->print_conntrack && l4proto->print_conntrack(s, conntrack))
150 return -ENOSPC;
152 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
153 l3proto, l4proto))
154 return -ENOSPC;
156 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
157 return -ENOSPC;
159 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
160 if (seq_printf(s, "[UNREPLIED] "))
161 return -ENOSPC;
163 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
164 l3proto, l4proto))
165 return -ENOSPC;
167 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
168 return -ENOSPC;
170 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
171 if (seq_printf(s, "[ASSURED] "))
172 return -ENOSPC;
174 #if defined(CONFIG_NF_CONNTRACK_MARK)
175 if (seq_printf(s, "mark=%u ", conntrack->mark))
176 return -ENOSPC;
177 #endif
179 #ifdef CONFIG_NF_CONNTRACK_SECMARK
180 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
181 return -ENOSPC;
182 #endif
184 #if defined(CONFIG_NETFILTER_XT_MATCH_LAYER7) || defined(CONFIG_NETFILTER_XT_MATCH_LAYER7_MODULE)
185 if(conntrack->layer7.app_proto)
186 if(seq_printf(s, "l7proto=%s ", conntrack->layer7.app_proto))
187 return -ENOSPC;
188 #endif
190 #if defined(CONFIG_IP_NF_TARGET_MACSAVE) || defined(CONFIG_IP_NF_TARGET_MACSAVE_MODULE)
191 if ((*((u32 *)conntrack->macsave) != 0) || (*((u16*)(conntrack->macsave + 4)) != 0)) {
192 if (seq_printf(s, "macsave=%02X:%02X:%02X:%02X:%02X:%02X ",
193 conntrack->macsave[0], conntrack->macsave[1], conntrack->macsave[2],
194 conntrack->macsave[3], conntrack->macsave[4], conntrack->macsave[5]))
195 return -ENOSPC;
197 #endif
198 #if defined(CONFIG_IP_NF_TARGET_BCOUNT) || defined(CONFIG_IP_NF_TARGET_BCOUNT_MODULE)
199 #if 0
200 if (conntrack->bcount != 0) {
201 if (seq_printf(s, "bcount=%ldK ", conntrack->bcount / 1024))
202 return -ENOSPC;
204 #endif
205 #endif
207 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
208 return -ENOSPC;
210 return 0;
213 static struct seq_operations ct_seq_ops = {
214 .start = ct_seq_start,
215 .next = ct_seq_next,
216 .stop = ct_seq_stop,
217 .show = ct_seq_show
220 static int ct_open(struct inode *inode, struct file *file)
222 return seq_open_private(file, &ct_seq_ops,
223 sizeof(struct ct_iter_state));
226 static const struct file_operations ct_file_ops = {
227 .owner = THIS_MODULE,
228 .open = ct_open,
229 .read = seq_read,
230 .llseek = seq_lseek,
231 .release = seq_release_private,
234 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
236 int cpu;
238 if (*pos == 0)
239 return SEQ_START_TOKEN;
241 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
242 if (!cpu_possible(cpu))
243 continue;
244 *pos = cpu + 1;
245 return &per_cpu(nf_conntrack_stat, cpu);
248 return NULL;
251 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
253 int cpu;
255 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
256 if (!cpu_possible(cpu))
257 continue;
258 *pos = cpu + 1;
259 return &per_cpu(nf_conntrack_stat, cpu);
262 return NULL;
265 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
269 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
271 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
272 struct ip_conntrack_stat *st = v;
274 if (v == SEQ_START_TOKEN) {
275 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");
276 return 0;
279 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
280 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
281 nr_conntracks,
282 st->searched,
283 st->found,
284 st->new,
285 st->invalid,
286 st->ignore,
287 st->delete,
288 st->delete_list,
289 st->insert,
290 st->insert_failed,
291 st->drop,
292 st->early_drop,
293 st->error,
295 st->expect_new,
296 st->expect_create,
297 st->expect_delete
299 return 0;
302 static struct seq_operations ct_cpu_seq_ops = {
303 .start = ct_cpu_seq_start,
304 .next = ct_cpu_seq_next,
305 .stop = ct_cpu_seq_stop,
306 .show = ct_cpu_seq_show,
309 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
311 return seq_open(file, &ct_cpu_seq_ops);
314 static const struct file_operations ct_cpu_seq_fops = {
315 .owner = THIS_MODULE,
316 .open = ct_cpu_seq_open,
317 .read = seq_read,
318 .llseek = seq_lseek,
319 .release = seq_release_private,
321 #endif /* CONFIG_PROC_FS */
323 /* Sysctl support */
325 int nf_conntrack_checksum __read_mostly = 1;
326 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
328 #ifdef CONFIG_SYSCTL
329 /* Log invalid packets of a given protocol */
330 static int log_invalid_proto_min = 0;
331 static int log_invalid_proto_max = 255;
333 static struct ctl_table_header *nf_ct_sysctl_header;
335 static ctl_table nf_ct_sysctl_table[] = {
337 .ctl_name = NET_NF_CONNTRACK_MAX,
338 .procname = "nf_conntrack_max",
339 .data = &nf_conntrack_max,
340 .maxlen = sizeof(int),
341 .mode = 0644,
342 .proc_handler = &proc_dointvec,
345 .ctl_name = NET_NF_CONNTRACK_COUNT,
346 .procname = "nf_conntrack_count",
347 .data = &nf_conntrack_count,
348 .maxlen = sizeof(int),
349 .mode = 0444,
350 .proc_handler = &proc_dointvec,
353 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
354 .procname = "nf_conntrack_buckets",
355 .data = &nf_conntrack_htable_size,
356 .maxlen = sizeof(unsigned int),
357 .mode = 0444,
358 .proc_handler = &proc_dointvec,
361 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
362 .procname = "nf_conntrack_checksum",
363 .data = &nf_conntrack_checksum,
364 .maxlen = sizeof(unsigned int),
365 .mode = 0644,
366 .proc_handler = &proc_dointvec,
369 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
370 .procname = "nf_conntrack_log_invalid",
371 .data = &nf_ct_log_invalid,
372 .maxlen = sizeof(unsigned int),
373 .mode = 0644,
374 .proc_handler = &proc_dointvec_minmax,
375 .strategy = &sysctl_intvec,
376 .extra1 = &log_invalid_proto_min,
377 .extra2 = &log_invalid_proto_max,
380 { .ctl_name = 0 }
383 #define NET_NF_CONNTRACK_MAX 2089
385 static ctl_table nf_ct_netfilter_table[] = {
387 .ctl_name = NET_NETFILTER,
388 .procname = "netfilter",
389 .mode = 0555,
390 .child = nf_ct_sysctl_table,
393 .ctl_name = NET_NF_CONNTRACK_MAX,
394 .procname = "nf_conntrack_max",
395 .data = &nf_conntrack_max,
396 .maxlen = sizeof(int),
397 .mode = 0644,
398 .proc_handler = &proc_dointvec,
400 { .ctl_name = 0 }
403 static ctl_table nf_ct_net_table[] = {
405 .ctl_name = CTL_NET,
406 .procname = "net",
407 .mode = 0555,
408 .child = nf_ct_netfilter_table,
410 { .ctl_name = 0 }
412 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
413 #endif /* CONFIG_SYSCTL */
415 static int __init nf_conntrack_standalone_init(void)
417 #ifdef CONFIG_PROC_FS
418 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
419 #endif
420 int ret = 0;
422 ret = nf_conntrack_init();
423 if (ret < 0)
424 return ret;
426 #ifdef CONFIG_PROC_FS
427 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
428 if (!proc) goto cleanup_init;
430 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
431 &exp_file_ops);
432 if (!proc_exp) goto cleanup_proc;
434 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
435 if (!proc_stat)
436 goto cleanup_proc_exp;
438 proc_stat->proc_fops = &ct_cpu_seq_fops;
439 proc_stat->owner = THIS_MODULE;
440 #endif
441 #ifdef CONFIG_SYSCTL
442 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table);
443 if (nf_ct_sysctl_header == NULL) {
444 printk("nf_conntrack: can't register to sysctl.\n");
445 ret = -ENOMEM;
446 goto cleanup_proc_stat;
448 #endif
449 return ret;
451 #ifdef CONFIG_SYSCTL
452 cleanup_proc_stat:
453 #endif
454 #ifdef CONFIG_PROC_FS
455 remove_proc_entry("nf_conntrack", proc_net_stat);
456 cleanup_proc_exp:
457 proc_net_remove("nf_conntrack_expect");
458 cleanup_proc:
459 proc_net_remove("nf_conntrack");
460 cleanup_init:
461 #endif /* CNFIG_PROC_FS */
462 nf_conntrack_cleanup();
463 return ret;
466 static void __exit nf_conntrack_standalone_fini(void)
468 #ifdef CONFIG_SYSCTL
469 unregister_sysctl_table(nf_ct_sysctl_header);
470 #endif
471 #ifdef CONFIG_PROC_FS
472 remove_proc_entry("nf_conntrack", proc_net_stat);
473 proc_net_remove("nf_conntrack_expect");
474 proc_net_remove("nf_conntrack");
475 #endif /* CNFIG_PROC_FS */
476 nf_conntrack_cleanup();
479 module_init(nf_conntrack_standalone_init);
480 module_exit(nf_conntrack_standalone_fini);
482 /* Some modules need us, but don't depend directly on any symbol.
483 They should call this. */
484 void need_conntrack(void)
487 EXPORT_SYMBOL_GPL(need_conntrack);