[NETFILTER]: nf_conntrack_tcp: catch invalid state updates over ctnetlink
[deliverable/linux.git] / net / netfilter / nf_conntrack_standalone.c
CommitLineData
9fb9cbb1
YK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
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.
9fb9cbb1
YK
7 */
8
9fb9cbb1
YK
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>
457c4cbc 17#include <net/net_namespace.h>
9fb9cbb1
YK
18#ifdef CONFIG_SYSCTL
19#include <linux/sysctl.h>
20#endif
21
9fb9cbb1 22#include <net/netfilter/nf_conntrack.h>
f6180121 23#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 24#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 25#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 26#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 27#include <net/netfilter/nf_conntrack_helper.h>
9fb9cbb1 28
9fb9cbb1
YK
29MODULE_LICENSE("GPL");
30
9fb9cbb1 31#ifdef CONFIG_PROC_FS
77ab9cff 32int
9fb9cbb1 33print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
32948588
JE
34 const struct nf_conntrack_l3proto *l3proto,
35 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 36{
605dcad6 37 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
9fb9cbb1 38}
e4bd8bce 39EXPORT_SYMBOL_GPL(print_tuple);
9fb9cbb1
YK
40
41#ifdef CONFIG_NF_CT_ACCT
42static unsigned int
43seq_print_counters(struct seq_file *s,
44 const struct ip_conntrack_counter *counter)
45{
46 return seq_printf(s, "packets=%llu bytes=%llu ",
47 (unsigned long long)counter->packets,
48 (unsigned long long)counter->bytes);
49}
50#else
51#define seq_print_counters(x, y) 0
52#endif
53
54struct ct_iter_state {
55 unsigned int bucket;
56};
57
f205c5e0 58static struct hlist_node *ct_get_first(struct seq_file *seq)
9fb9cbb1
YK
59{
60 struct ct_iter_state *st = seq->private;
76507f69 61 struct hlist_node *n;
9fb9cbb1
YK
62
63 for (st->bucket = 0;
64 st->bucket < nf_conntrack_htable_size;
65 st->bucket++) {
76507f69
PM
66 n = rcu_dereference(nf_conntrack_hash[st->bucket].first);
67 if (n)
68 return n;
9fb9cbb1
YK
69 }
70 return NULL;
71}
72
f205c5e0
PM
73static struct hlist_node *ct_get_next(struct seq_file *seq,
74 struct hlist_node *head)
9fb9cbb1
YK
75{
76 struct ct_iter_state *st = seq->private;
77
76507f69 78 head = rcu_dereference(head->next);
f205c5e0 79 while (head == NULL) {
9fb9cbb1
YK
80 if (++st->bucket >= nf_conntrack_htable_size)
81 return NULL;
76507f69 82 head = rcu_dereference(nf_conntrack_hash[st->bucket].first);
9fb9cbb1
YK
83 }
84 return head;
85}
86
f205c5e0 87static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
9fb9cbb1 88{
f205c5e0 89 struct hlist_node *head = ct_get_first(seq);
9fb9cbb1
YK
90
91 if (head)
92 while (pos && (head = ct_get_next(seq, head)))
93 pos--;
94 return pos ? NULL : head;
95}
96
97static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 98 __acquires(RCU)
9fb9cbb1 99{
76507f69 100 rcu_read_lock();
9fb9cbb1
YK
101 return ct_get_idx(seq, *pos);
102}
103
104static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
105{
106 (*pos)++;
107 return ct_get_next(s, v);
108}
109
110static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 111 __releases(RCU)
9fb9cbb1 112{
76507f69 113 rcu_read_unlock();
9fb9cbb1
YK
114}
115
116/* return 0 on success, 1 in case of error */
117static int ct_seq_show(struct seq_file *s, void *v)
118{
119 const struct nf_conntrack_tuple_hash *hash = v;
c88130bc 120 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
121 const struct nf_conntrack_l3proto *l3proto;
122 const struct nf_conntrack_l4proto *l4proto;
9fb9cbb1 123
c88130bc 124 NF_CT_ASSERT(ct);
9fb9cbb1
YK
125
126 /* we only want to print DIR_ORIGINAL */
127 if (NF_CT_DIRECTION(hash))
128 return 0;
129
c88130bc 130 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
c1d10adb 131 .tuple.src.l3num);
9fb9cbb1
YK
132
133 NF_CT_ASSERT(l3proto);
c88130bc 134 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
c1d10adb 135 .tuple.src.l3num,
c88130bc 136 ct->tuplehash[IP_CT_DIR_ORIGINAL]
c1d10adb 137 .tuple.dst.protonum);
605dcad6 138 NF_CT_ASSERT(l4proto);
9fb9cbb1
YK
139
140 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
141 l3proto->name,
c88130bc 142 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
605dcad6 143 l4proto->name,
c88130bc
PM
144 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
145 timer_pending(&ct->timeout)
146 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
9fb9cbb1
YK
147 return -ENOSPC;
148
c88130bc 149 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
9fb9cbb1
YK
150 return -ENOSPC;
151
c88130bc 152 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
605dcad6 153 l3proto, l4proto))
9fb9cbb1
YK
154 return -ENOSPC;
155
c88130bc 156 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
9fb9cbb1
YK
157 return -ENOSPC;
158
c88130bc 159 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
9fb9cbb1
YK
160 if (seq_printf(s, "[UNREPLIED] "))
161 return -ENOSPC;
162
c88130bc 163 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
605dcad6 164 l3proto, l4proto))
9fb9cbb1
YK
165 return -ENOSPC;
166
c88130bc 167 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
9fb9cbb1
YK
168 return -ENOSPC;
169
c88130bc 170 if (test_bit(IPS_ASSURED_BIT, &ct->status))
9fb9cbb1
YK
171 if (seq_printf(s, "[ASSURED] "))
172 return -ENOSPC;
173
174#if defined(CONFIG_NF_CONNTRACK_MARK)
c88130bc 175 if (seq_printf(s, "mark=%u ", ct->mark))
9fb9cbb1
YK
176 return -ENOSPC;
177#endif
178
7c9728c3 179#ifdef CONFIG_NF_CONNTRACK_SECMARK
c88130bc 180 if (seq_printf(s, "secmark=%u ", ct->secmark))
7c9728c3
JM
181 return -ENOSPC;
182#endif
183
c88130bc 184 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
9fb9cbb1 185 return -ENOSPC;
a5d29264 186
9fb9cbb1
YK
187 return 0;
188}
189
56b3d975 190static const struct seq_operations ct_seq_ops = {
9fb9cbb1
YK
191 .start = ct_seq_start,
192 .next = ct_seq_next,
193 .stop = ct_seq_stop,
194 .show = ct_seq_show
195};
196
197static int ct_open(struct inode *inode, struct file *file)
198{
e2da5913
PE
199 return seq_open_private(file, &ct_seq_ops,
200 sizeof(struct ct_iter_state));
9fb9cbb1
YK
201}
202
da7071d7 203static const struct file_operations ct_file_ops = {
9fb9cbb1
YK
204 .owner = THIS_MODULE,
205 .open = ct_open,
206 .read = seq_read,
207 .llseek = seq_lseek,
208 .release = seq_release_private,
209};
210
9fb9cbb1
YK
211static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
212{
213 int cpu;
214
215 if (*pos == 0)
216 return SEQ_START_TOKEN;
217
218 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
219 if (!cpu_possible(cpu))
220 continue;
221 *pos = cpu + 1;
222 return &per_cpu(nf_conntrack_stat, cpu);
223 }
224
225 return NULL;
226}
227
228static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
229{
230 int cpu;
231
232 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
233 if (!cpu_possible(cpu))
234 continue;
235 *pos = cpu + 1;
236 return &per_cpu(nf_conntrack_stat, cpu);
237 }
238
239 return NULL;
240}
241
242static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
243{
244}
245
246static int ct_cpu_seq_show(struct seq_file *seq, void *v)
247{
248 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
32948588 249 const struct ip_conntrack_stat *st = v;
9fb9cbb1
YK
250
251 if (v == SEQ_START_TOKEN) {
252 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");
253 return 0;
254 }
255
256 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
257 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
258 nr_conntracks,
259 st->searched,
260 st->found,
261 st->new,
262 st->invalid,
263 st->ignore,
264 st->delete,
265 st->delete_list,
266 st->insert,
267 st->insert_failed,
268 st->drop,
269 st->early_drop,
270 st->error,
271
272 st->expect_new,
273 st->expect_create,
274 st->expect_delete
275 );
276 return 0;
277}
278
56b3d975 279static const struct seq_operations ct_cpu_seq_ops = {
9fb9cbb1
YK
280 .start = ct_cpu_seq_start,
281 .next = ct_cpu_seq_next,
282 .stop = ct_cpu_seq_stop,
283 .show = ct_cpu_seq_show,
284};
285
286static int ct_cpu_seq_open(struct inode *inode, struct file *file)
287{
288 return seq_open(file, &ct_cpu_seq_ops);
289}
290
da7071d7 291static const struct file_operations ct_cpu_seq_fops = {
9fb9cbb1
YK
292 .owner = THIS_MODULE,
293 .open = ct_cpu_seq_open,
294 .read = seq_read,
295 .llseek = seq_lseek,
665bba10 296 .release = seq_release,
9fb9cbb1 297};
b916f7d4
AD
298
299static int nf_conntrack_standalone_init_proc(void)
300{
301 struct proc_dir_entry *pde;
302
303 pde = proc_net_fops_create(&init_net, "nf_conntrack", 0440, &ct_file_ops);
304 if (!pde)
305 goto out_nf_conntrack;
306 pde = create_proc_entry("nf_conntrack", S_IRUGO, init_net.proc_net_stat);
307 if (!pde)
308 goto out_stat_nf_conntrack;
309 pde->proc_fops = &ct_cpu_seq_fops;
310 pde->owner = THIS_MODULE;
311 return 0;
312
313out_stat_nf_conntrack:
314 proc_net_remove(&init_net, "nf_conntrack");
315out_nf_conntrack:
316 return -ENOMEM;
317}
318
319static void nf_conntrack_standalone_fini_proc(void)
320{
321 remove_proc_entry("nf_conntrack", init_net.proc_net_stat);
322 proc_net_remove(&init_net, "nf_conntrack");
323}
324#else
325static int nf_conntrack_standalone_init_proc(void)
326{
327 return 0;
328}
329
330static void nf_conntrack_standalone_fini_proc(void)
331{
332}
9fb9cbb1
YK
333#endif /* CONFIG_PROC_FS */
334
335/* Sysctl support */
336
94aec08e 337int nf_conntrack_checksum __read_mostly = 1;
13b18339 338EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
72b55823 339
9fb9cbb1 340#ifdef CONFIG_SYSCTL
9fb9cbb1
YK
341/* Log invalid packets of a given protocol */
342static int log_invalid_proto_min = 0;
343static int log_invalid_proto_max = 255;
344
345static struct ctl_table_header *nf_ct_sysctl_header;
346
347static ctl_table nf_ct_sysctl_table[] = {
348 {
349 .ctl_name = NET_NF_CONNTRACK_MAX,
350 .procname = "nf_conntrack_max",
351 .data = &nf_conntrack_max,
352 .maxlen = sizeof(int),
353 .mode = 0644,
354 .proc_handler = &proc_dointvec,
355 },
356 {
357 .ctl_name = NET_NF_CONNTRACK_COUNT,
358 .procname = "nf_conntrack_count",
359 .data = &nf_conntrack_count,
360 .maxlen = sizeof(int),
361 .mode = 0444,
362 .proc_handler = &proc_dointvec,
363 },
364 {
365 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
366 .procname = "nf_conntrack_buckets",
367 .data = &nf_conntrack_htable_size,
368 .maxlen = sizeof(unsigned int),
369 .mode = 0444,
370 .proc_handler = &proc_dointvec,
371 },
39a27a35
PM
372 {
373 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
374 .procname = "nf_conntrack_checksum",
375 .data = &nf_conntrack_checksum,
376 .maxlen = sizeof(unsigned int),
377 .mode = 0644,
378 .proc_handler = &proc_dointvec,
379 },
9fb9cbb1
YK
380 {
381 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
382 .procname = "nf_conntrack_log_invalid",
383 .data = &nf_ct_log_invalid,
384 .maxlen = sizeof(unsigned int),
385 .mode = 0644,
386 .proc_handler = &proc_dointvec_minmax,
387 .strategy = &sysctl_intvec,
388 .extra1 = &log_invalid_proto_min,
389 .extra2 = &log_invalid_proto_max,
390 },
f264a7df
PM
391 {
392 .ctl_name = CTL_UNNUMBERED,
393 .procname = "nf_conntrack_expect_max",
394 .data = &nf_ct_expect_max,
395 .maxlen = sizeof(int),
396 .mode = 0644,
397 .proc_handler = &proc_dointvec,
398 },
9fb9cbb1
YK
399 { .ctl_name = 0 }
400};
401
402#define NET_NF_CONNTRACK_MAX 2089
403
404static ctl_table nf_ct_netfilter_table[] = {
405 {
406 .ctl_name = NET_NETFILTER,
407 .procname = "netfilter",
408 .mode = 0555,
409 .child = nf_ct_sysctl_table,
410 },
411 {
412 .ctl_name = NET_NF_CONNTRACK_MAX,
413 .procname = "nf_conntrack_max",
414 .data = &nf_conntrack_max,
415 .maxlen = sizeof(int),
416 .mode = 0644,
417 .proc_handler = &proc_dointvec,
418 },
419 { .ctl_name = 0 }
420};
421
9e232495 422static struct ctl_path nf_ct_path[] = {
3d7cc2ba
PE
423 { .procname = "net", .ctl_name = CTL_NET, },
424 { }
9fb9cbb1 425};
3d7cc2ba 426
13b18339 427EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
b916f7d4
AD
428
429static int nf_conntrack_standalone_init_sysctl(void)
430{
431 nf_ct_sysctl_header =
432 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
433 if (nf_ct_sysctl_header == NULL) {
434 printk("nf_conntrack: can't register to sysctl.\n");
435 return -ENOMEM;
436 }
437 return 0;
438
439}
440
441static void nf_conntrack_standalone_fini_sysctl(void)
442{
443 unregister_sysctl_table(nf_ct_sysctl_header);
444}
445#else
446static int nf_conntrack_standalone_init_sysctl(void)
447{
448 return 0;
449}
450
451static void nf_conntrack_standalone_fini_sysctl(void)
452{
453}
9fb9cbb1
YK
454#endif /* CONFIG_SYSCTL */
455
65b4b4e8 456static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 457{
b916f7d4 458 int ret;
32292a7f
PM
459
460 ret = nf_conntrack_init();
461 if (ret < 0)
b916f7d4
AD
462 goto out;
463 ret = nf_conntrack_standalone_init_proc();
464 if (ret < 0)
465 goto out_proc;
466 ret = nf_conntrack_standalone_init_sysctl();
467 if (ret < 0)
468 goto out_sysctl;
469 return 0;
32292a7f 470
b916f7d4
AD
471out_sysctl:
472 nf_conntrack_standalone_fini_proc();
473out_proc:
32292a7f 474 nf_conntrack_cleanup();
b916f7d4 475out:
32292a7f 476 return ret;
9fb9cbb1
YK
477}
478
65b4b4e8 479static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 480{
b916f7d4
AD
481 nf_conntrack_standalone_fini_sysctl();
482 nf_conntrack_standalone_fini_proc();
32292a7f 483 nf_conntrack_cleanup();
9fb9cbb1
YK
484}
485
65b4b4e8
AM
486module_init(nf_conntrack_standalone_init);
487module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
488
489/* Some modules need us, but don't depend directly on any symbol.
490 They should call this. */
2e4e6a17 491void need_conntrack(void)
9fb9cbb1
YK
492{
493}
13b18339 494EXPORT_SYMBOL_GPL(need_conntrack);
This page took 0.350454 seconds and 5 git commands to generate.