include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
5a0e3ad6 11#include <linux/slab.h>
9fb9cbb1
YK
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/proc_fs.h>
15#include <linux/seq_file.h>
16#include <linux/percpu.h>
17#include <linux/netdevice.h>
457c4cbc 18#include <net/net_namespace.h>
9fb9cbb1
YK
19#ifdef CONFIG_SYSCTL
20#include <linux/sysctl.h>
21#endif
22
9fb9cbb1 23#include <net/netfilter/nf_conntrack.h>
f6180121 24#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 25#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 26#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 27#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 28#include <net/netfilter/nf_conntrack_helper.h>
58401572 29#include <net/netfilter/nf_conntrack_acct.h>
5d0aa2cc 30#include <net/netfilter/nf_conntrack_zones.h>
9fb9cbb1 31
9fb9cbb1
YK
32MODULE_LICENSE("GPL");
33
9fb9cbb1 34#ifdef CONFIG_PROC_FS
77ab9cff 35int
9fb9cbb1 36print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
32948588
JE
37 const struct nf_conntrack_l3proto *l3proto,
38 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 39{
605dcad6 40 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
9fb9cbb1 41}
e4bd8bce 42EXPORT_SYMBOL_GPL(print_tuple);
9fb9cbb1 43
9fb9cbb1 44struct ct_iter_state {
b2ce2c74 45 struct seq_net_private p;
9fb9cbb1
YK
46 unsigned int bucket;
47};
48
ea781f19 49static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
9fb9cbb1 50{
b2ce2c74 51 struct net *net = seq_file_net(seq);
9fb9cbb1 52 struct ct_iter_state *st = seq->private;
ea781f19 53 struct hlist_nulls_node *n;
9fb9cbb1
YK
54
55 for (st->bucket = 0;
d696c7bd 56 st->bucket < net->ct.htable_size;
9fb9cbb1 57 st->bucket++) {
b2ce2c74 58 n = rcu_dereference(net->ct.hash[st->bucket].first);
ea781f19 59 if (!is_a_nulls(n))
76507f69 60 return n;
9fb9cbb1
YK
61 }
62 return NULL;
63}
64
ea781f19
ED
65static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
66 struct hlist_nulls_node *head)
9fb9cbb1 67{
b2ce2c74 68 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
69 struct ct_iter_state *st = seq->private;
70
76507f69 71 head = rcu_dereference(head->next);
ea781f19
ED
72 while (is_a_nulls(head)) {
73 if (likely(get_nulls_value(head) == st->bucket)) {
d696c7bd 74 if (++st->bucket >= net->ct.htable_size)
ea781f19
ED
75 return NULL;
76 }
b2ce2c74 77 head = rcu_dereference(net->ct.hash[st->bucket].first);
9fb9cbb1
YK
78 }
79 return head;
80}
81
ea781f19 82static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
9fb9cbb1 83{
ea781f19 84 struct hlist_nulls_node *head = ct_get_first(seq);
9fb9cbb1
YK
85
86 if (head)
87 while (pos && (head = ct_get_next(seq, head)))
88 pos--;
89 return pos ? NULL : head;
90}
91
92static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 93 __acquires(RCU)
9fb9cbb1 94{
76507f69 95 rcu_read_lock();
9fb9cbb1
YK
96 return ct_get_idx(seq, *pos);
97}
98
99static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
100{
101 (*pos)++;
102 return ct_get_next(s, v);
103}
104
105static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 106 __releases(RCU)
9fb9cbb1 107{
76507f69 108 rcu_read_unlock();
9fb9cbb1
YK
109}
110
111/* return 0 on success, 1 in case of error */
112static int ct_seq_show(struct seq_file *s, void *v)
113{
ea781f19
ED
114 struct nf_conntrack_tuple_hash *hash = v;
115 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
116 const struct nf_conntrack_l3proto *l3proto;
117 const struct nf_conntrack_l4proto *l4proto;
ea781f19 118 int ret = 0;
9fb9cbb1 119
c88130bc 120 NF_CT_ASSERT(ct);
ea781f19
ED
121 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
122 return 0;
9fb9cbb1
YK
123
124 /* we only want to print DIR_ORIGINAL */
125 if (NF_CT_DIRECTION(hash))
ea781f19 126 goto release;
9fb9cbb1 127
5e8fbe2a 128 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
9fb9cbb1 129 NF_CT_ASSERT(l3proto);
5e8fbe2a 130 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
605dcad6 131 NF_CT_ASSERT(l4proto);
9fb9cbb1 132
ea781f19 133 ret = -ENOSPC;
9fb9cbb1 134 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
5e8fbe2a
PM
135 l3proto->name, nf_ct_l3num(ct),
136 l4proto->name, nf_ct_protonum(ct),
c88130bc
PM
137 timer_pending(&ct->timeout)
138 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
ea781f19 139 goto release;
9fb9cbb1 140
c88130bc 141 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
ea781f19 142 goto release;
9fb9cbb1 143
c88130bc 144 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
605dcad6 145 l3proto, l4proto))
ea781f19 146 goto release;
9fb9cbb1 147
58401572 148 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
ea781f19 149 goto release;
9fb9cbb1 150
c88130bc 151 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
9fb9cbb1 152 if (seq_printf(s, "[UNREPLIED] "))
ea781f19 153 goto release;
9fb9cbb1 154
c88130bc 155 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
605dcad6 156 l3proto, l4proto))
ea781f19 157 goto release;
9fb9cbb1 158
58401572 159 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
ea781f19 160 goto release;
9fb9cbb1 161
c88130bc 162 if (test_bit(IPS_ASSURED_BIT, &ct->status))
9fb9cbb1 163 if (seq_printf(s, "[ASSURED] "))
ea781f19 164 goto release;
9fb9cbb1
YK
165
166#if defined(CONFIG_NF_CONNTRACK_MARK)
c88130bc 167 if (seq_printf(s, "mark=%u ", ct->mark))
ea781f19 168 goto release;
9fb9cbb1
YK
169#endif
170
7c9728c3 171#ifdef CONFIG_NF_CONNTRACK_SECMARK
c88130bc 172 if (seq_printf(s, "secmark=%u ", ct->secmark))
ea781f19 173 goto release;
7c9728c3
JM
174#endif
175
5d0aa2cc
PM
176#ifdef CONFIG_NF_CONNTRACK_ZONES
177 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
178 goto release;
179#endif
180
c88130bc 181 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
ea781f19 182 goto release;
a5d29264 183
ea781f19
ED
184 ret = 0;
185release:
186 nf_ct_put(ct);
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{
b2ce2c74 199 return seq_open_net(inode, file, &ct_seq_ops,
e2da5913 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,
b2ce2c74 208 .release = seq_release_net,
9fb9cbb1
YK
209};
210
9fb9cbb1
YK
211static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
212{
8e9df801 213 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
214 int cpu;
215
216 if (*pos == 0)
217 return SEQ_START_TOKEN;
218
0f23174a 219 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
220 if (!cpu_possible(cpu))
221 continue;
222 *pos = cpu + 1;
8e9df801 223 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
224 }
225
226 return NULL;
227}
228
229static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
230{
8e9df801 231 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
232 int cpu;
233
0f23174a 234 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
235 if (!cpu_possible(cpu))
236 continue;
237 *pos = cpu + 1;
8e9df801 238 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
239 }
240
241 return NULL;
242}
243
244static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
245{
246}
247
248static int ct_cpu_seq_show(struct seq_file *seq, void *v)
249{
8e9df801
AD
250 struct net *net = seq_file_net(seq);
251 unsigned int nr_conntracks = atomic_read(&net->ct.count);
32948588 252 const struct ip_conntrack_stat *st = v;
9fb9cbb1
YK
253
254 if (v == SEQ_START_TOKEN) {
255 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");
256 return 0;
257 }
258
259 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
260 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
261 nr_conntracks,
262 st->searched,
263 st->found,
264 st->new,
265 st->invalid,
266 st->ignore,
267 st->delete,
268 st->delete_list,
269 st->insert,
270 st->insert_failed,
271 st->drop,
272 st->early_drop,
273 st->error,
274
275 st->expect_new,
276 st->expect_create,
277 st->expect_delete
278 );
279 return 0;
280}
281
56b3d975 282static const struct seq_operations ct_cpu_seq_ops = {
9fb9cbb1
YK
283 .start = ct_cpu_seq_start,
284 .next = ct_cpu_seq_next,
285 .stop = ct_cpu_seq_stop,
286 .show = ct_cpu_seq_show,
287};
288
289static int ct_cpu_seq_open(struct inode *inode, struct file *file)
290{
8e9df801
AD
291 return seq_open_net(inode, file, &ct_cpu_seq_ops,
292 sizeof(struct seq_net_private));
9fb9cbb1
YK
293}
294
da7071d7 295static const struct file_operations ct_cpu_seq_fops = {
9fb9cbb1
YK
296 .owner = THIS_MODULE,
297 .open = ct_cpu_seq_open,
298 .read = seq_read,
299 .llseek = seq_lseek,
8e9df801 300 .release = seq_release_net,
9fb9cbb1 301};
b916f7d4 302
b2ce2c74 303static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
304{
305 struct proc_dir_entry *pde;
306
b2ce2c74 307 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
b916f7d4
AD
308 if (!pde)
309 goto out_nf_conntrack;
52c0e111 310
b2ce2c74 311 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
52c0e111 312 &ct_cpu_seq_fops);
b916f7d4
AD
313 if (!pde)
314 goto out_stat_nf_conntrack;
b916f7d4
AD
315 return 0;
316
317out_stat_nf_conntrack:
b2ce2c74 318 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
319out_nf_conntrack:
320 return -ENOMEM;
321}
322
b2ce2c74 323static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4 324{
b2ce2c74
AD
325 remove_proc_entry("nf_conntrack", net->proc_net_stat);
326 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
327}
328#else
b2ce2c74 329static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
330{
331 return 0;
332}
333
b2ce2c74 334static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4
AD
335{
336}
9fb9cbb1
YK
337#endif /* CONFIG_PROC_FS */
338
339/* Sysctl support */
340
341#ifdef CONFIG_SYSCTL
9fb9cbb1
YK
342/* Log invalid packets of a given protocol */
343static int log_invalid_proto_min = 0;
344static int log_invalid_proto_max = 255;
345
9714be7d 346static struct ctl_table_header *nf_ct_netfilter_header;
9fb9cbb1
YK
347
348static ctl_table nf_ct_sysctl_table[] = {
349 {
9fb9cbb1
YK
350 .procname = "nf_conntrack_max",
351 .data = &nf_conntrack_max,
352 .maxlen = sizeof(int),
353 .mode = 0644,
6d9f239a 354 .proc_handler = proc_dointvec,
9fb9cbb1
YK
355 },
356 {
9fb9cbb1 357 .procname = "nf_conntrack_count",
49ac8713 358 .data = &init_net.ct.count,
9fb9cbb1
YK
359 .maxlen = sizeof(int),
360 .mode = 0444,
6d9f239a 361 .proc_handler = proc_dointvec,
9fb9cbb1
YK
362 },
363 {
9fb9cbb1 364 .procname = "nf_conntrack_buckets",
d696c7bd 365 .data = &init_net.ct.htable_size,
9fb9cbb1
YK
366 .maxlen = sizeof(unsigned int),
367 .mode = 0444,
6d9f239a 368 .proc_handler = proc_dointvec,
9fb9cbb1 369 },
39a27a35 370 {
39a27a35 371 .procname = "nf_conntrack_checksum",
c04d0552 372 .data = &init_net.ct.sysctl_checksum,
39a27a35
PM
373 .maxlen = sizeof(unsigned int),
374 .mode = 0644,
6d9f239a 375 .proc_handler = proc_dointvec,
39a27a35 376 },
9fb9cbb1 377 {
9fb9cbb1 378 .procname = "nf_conntrack_log_invalid",
c2a2c7e0 379 .data = &init_net.ct.sysctl_log_invalid,
9fb9cbb1
YK
380 .maxlen = sizeof(unsigned int),
381 .mode = 0644,
6d9f239a 382 .proc_handler = proc_dointvec_minmax,
9fb9cbb1
YK
383 .extra1 = &log_invalid_proto_min,
384 .extra2 = &log_invalid_proto_max,
385 },
f264a7df 386 {
f264a7df
PM
387 .procname = "nf_conntrack_expect_max",
388 .data = &nf_ct_expect_max,
389 .maxlen = sizeof(int),
390 .mode = 0644,
6d9f239a 391 .proc_handler = proc_dointvec,
f264a7df 392 },
f8572d8f 393 { }
9fb9cbb1
YK
394};
395
396#define NET_NF_CONNTRACK_MAX 2089
397
398static ctl_table nf_ct_netfilter_table[] = {
9fb9cbb1 399 {
9fb9cbb1
YK
400 .procname = "nf_conntrack_max",
401 .data = &nf_conntrack_max,
402 .maxlen = sizeof(int),
403 .mode = 0644,
6d9f239a 404 .proc_handler = proc_dointvec,
9fb9cbb1 405 },
f8572d8f 406 { }
9fb9cbb1
YK
407};
408
9e232495 409static struct ctl_path nf_ct_path[] = {
f8572d8f 410 { .procname = "net", },
3d7cc2ba 411 { }
9fb9cbb1 412};
3d7cc2ba 413
80250707 414static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4 415{
80250707
AD
416 struct ctl_table *table;
417
418 if (net_eq(net, &init_net)) {
419 nf_ct_netfilter_header =
420 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
421 if (!nf_ct_netfilter_header)
422 goto out;
423 }
424
425 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
426 GFP_KERNEL);
427 if (!table)
428 goto out_kmemdup;
429
430 table[1].data = &net->ct.count;
d696c7bd 431 table[2].data = &net->ct.htable_size;
c04d0552 432 table[3].data = &net->ct.sysctl_checksum;
c2a2c7e0 433 table[4].data = &net->ct.sysctl_log_invalid;
80250707
AD
434
435 net->ct.sysctl_header = register_net_sysctl_table(net,
436 nf_net_netfilter_sysctl_path, table);
437 if (!net->ct.sysctl_header)
9714be7d
KPO
438 goto out_unregister_netfilter;
439
b916f7d4
AD
440 return 0;
441
9714be7d 442out_unregister_netfilter:
80250707
AD
443 kfree(table);
444out_kmemdup:
445 if (net_eq(net, &init_net))
446 unregister_sysctl_table(nf_ct_netfilter_header);
9714be7d
KPO
447out:
448 printk("nf_conntrack: can't register to sysctl.\n");
449 return -ENOMEM;
b916f7d4
AD
450}
451
80250707 452static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4 453{
80250707
AD
454 struct ctl_table *table;
455
456 if (net_eq(net, &init_net))
457 unregister_sysctl_table(nf_ct_netfilter_header);
458 table = net->ct.sysctl_header->ctl_table_arg;
459 unregister_net_sysctl_table(net->ct.sysctl_header);
460 kfree(table);
b916f7d4
AD
461}
462#else
80250707 463static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4
AD
464{
465 return 0;
466}
467
80250707 468static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4
AD
469{
470}
9fb9cbb1
YK
471#endif /* CONFIG_SYSCTL */
472
dfdb8d79
AD
473static int nf_conntrack_net_init(struct net *net)
474{
b2ce2c74
AD
475 int ret;
476
477 ret = nf_conntrack_init(net);
478 if (ret < 0)
479 goto out_init;
480 ret = nf_conntrack_standalone_init_proc(net);
481 if (ret < 0)
482 goto out_proc;
c04d0552 483 net->ct.sysctl_checksum = 1;
c2a2c7e0 484 net->ct.sysctl_log_invalid = 0;
80250707
AD
485 ret = nf_conntrack_standalone_init_sysctl(net);
486 if (ret < 0)
487 goto out_sysctl;
b2ce2c74
AD
488 return 0;
489
80250707
AD
490out_sysctl:
491 nf_conntrack_standalone_fini_proc(net);
b2ce2c74
AD
492out_proc:
493 nf_conntrack_cleanup(net);
494out_init:
495 return ret;
dfdb8d79
AD
496}
497
498static void nf_conntrack_net_exit(struct net *net)
499{
80250707 500 nf_conntrack_standalone_fini_sysctl(net);
b2ce2c74 501 nf_conntrack_standalone_fini_proc(net);
dfdb8d79
AD
502 nf_conntrack_cleanup(net);
503}
504
505static struct pernet_operations nf_conntrack_net_ops = {
506 .init = nf_conntrack_net_init,
507 .exit = nf_conntrack_net_exit,
508};
509
65b4b4e8 510static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 511{
80250707 512 return register_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
513}
514
65b4b4e8 515static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 516{
dfdb8d79 517 unregister_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
518}
519
65b4b4e8
AM
520module_init(nf_conntrack_standalone_init);
521module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
522
523/* Some modules need us, but don't depend directly on any symbol.
524 They should call this. */
2e4e6a17 525void need_conntrack(void)
9fb9cbb1
YK
526{
527}
13b18339 528EXPORT_SYMBOL_GPL(need_conntrack);