]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c
some kmalloc/memset ->kzalloc (tree wide)
[mirror_ubuntu-zesty-kernel.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4_compat.c
CommitLineData
e4bd8bce
PM
1/* ip_conntrack proc compat - based on ip_conntrack_standalone.c
2 *
3 * (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/types.h>
11#include <linux/proc_fs.h>
12#include <linux/seq_file.h>
13#include <linux/percpu.h>
14
15#include <linux/netfilter.h>
16#include <net/netfilter/nf_conntrack_core.h>
17#include <net/netfilter/nf_conntrack_l3proto.h>
18#include <net/netfilter/nf_conntrack_l4proto.h>
19#include <net/netfilter/nf_conntrack_expect.h>
20
e4bd8bce
PM
21#ifdef CONFIG_NF_CT_ACCT
22static unsigned int
23seq_print_counters(struct seq_file *s,
24 const struct ip_conntrack_counter *counter)
25{
26 return seq_printf(s, "packets=%llu bytes=%llu ",
27 (unsigned long long)counter->packets,
28 (unsigned long long)counter->bytes);
29}
30#else
31#define seq_print_counters(x, y) 0
32#endif
33
34struct ct_iter_state {
35 unsigned int bucket;
36};
37
f205c5e0 38static struct hlist_node *ct_get_first(struct seq_file *seq)
e4bd8bce
PM
39{
40 struct ct_iter_state *st = seq->private;
41
42 for (st->bucket = 0;
43 st->bucket < nf_conntrack_htable_size;
44 st->bucket++) {
f205c5e0
PM
45 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
46 return nf_conntrack_hash[st->bucket].first;
e4bd8bce
PM
47 }
48 return NULL;
49}
50
f205c5e0
PM
51static struct hlist_node *ct_get_next(struct seq_file *seq,
52 struct hlist_node *head)
e4bd8bce
PM
53{
54 struct ct_iter_state *st = seq->private;
55
56 head = head->next;
f205c5e0 57 while (head == NULL) {
e4bd8bce
PM
58 if (++st->bucket >= nf_conntrack_htable_size)
59 return NULL;
f205c5e0 60 head = nf_conntrack_hash[st->bucket].first;
e4bd8bce
PM
61 }
62 return head;
63}
64
f205c5e0 65static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
e4bd8bce 66{
f205c5e0 67 struct hlist_node *head = ct_get_first(seq);
e4bd8bce
PM
68
69 if (head)
70 while (pos && (head = ct_get_next(seq, head)))
71 pos--;
72 return pos ? NULL : head;
73}
74
75static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76{
77 read_lock_bh(&nf_conntrack_lock);
78 return ct_get_idx(seq, *pos);
79}
80
81static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
82{
83 (*pos)++;
84 return ct_get_next(s, v);
85}
86
87static void ct_seq_stop(struct seq_file *s, void *v)
88{
89 read_unlock_bh(&nf_conntrack_lock);
90}
91
92static int ct_seq_show(struct seq_file *s, void *v)
93{
94 const struct nf_conntrack_tuple_hash *hash = v;
95 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
96 struct nf_conntrack_l3proto *l3proto;
97 struct nf_conntrack_l4proto *l4proto;
98
99 NF_CT_ASSERT(ct);
100
101 /* we only want to print DIR_ORIGINAL */
102 if (NF_CT_DIRECTION(hash))
103 return 0;
104 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
105 return 0;
106
107 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
108 .tuple.src.l3num);
109 NF_CT_ASSERT(l3proto);
110 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
111 .tuple.src.l3num,
112 ct->tuplehash[IP_CT_DIR_ORIGINAL]
113 .tuple.dst.protonum);
114 NF_CT_ASSERT(l4proto);
115
116 if (seq_printf(s, "%-8s %u %ld ",
117 l4proto->name,
118 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
119 timer_pending(&ct->timeout)
120 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
121 return -ENOSPC;
122
123 if (l3proto->print_conntrack(s, ct))
124 return -ENOSPC;
125
126 if (l4proto->print_conntrack(s, ct))
127 return -ENOSPC;
128
129 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
130 l3proto, l4proto))
131 return -ENOSPC;
132
e905a9ed 133 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
e4bd8bce
PM
134 return -ENOSPC;
135
136 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
137 if (seq_printf(s, "[UNREPLIED] "))
138 return -ENOSPC;
139
140 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
141 l3proto, l4proto))
142 return -ENOSPC;
143
e905a9ed 144 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
e4bd8bce
PM
145 return -ENOSPC;
146
147 if (test_bit(IPS_ASSURED_BIT, &ct->status))
148 if (seq_printf(s, "[ASSURED] "))
149 return -ENOSPC;
150
151#ifdef CONFIG_NF_CONNTRACK_MARK
152 if (seq_printf(s, "mark=%u ", ct->mark))
153 return -ENOSPC;
154#endif
155
156#ifdef CONFIG_NF_CONNTRACK_SECMARK
157 if (seq_printf(s, "secmark=%u ", ct->secmark))
158 return -ENOSPC;
159#endif
160
161 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
162 return -ENOSPC;
163
164 return 0;
165}
166
56b3d975 167static const struct seq_operations ct_seq_ops = {
e4bd8bce
PM
168 .start = ct_seq_start,
169 .next = ct_seq_next,
170 .stop = ct_seq_stop,
171 .show = ct_seq_show
172};
173
174static int ct_open(struct inode *inode, struct file *file)
175{
176 struct seq_file *seq;
177 struct ct_iter_state *st;
178 int ret;
179
dd00cc48 180 st = kzalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
e4bd8bce
PM
181 if (st == NULL)
182 return -ENOMEM;
183 ret = seq_open(file, &ct_seq_ops);
184 if (ret)
185 goto out_free;
186 seq = file->private_data;
187 seq->private = st;
e4bd8bce
PM
188 return ret;
189out_free:
190 kfree(st);
191 return ret;
192}
193
9a32144e 194static const struct file_operations ct_file_ops = {
e4bd8bce
PM
195 .owner = THIS_MODULE,
196 .open = ct_open,
197 .read = seq_read,
198 .llseek = seq_lseek,
199 .release = seq_release_private,
200};
201
202/* expects */
5d08ad44
PM
203struct ct_expect_iter_state {
204 unsigned int bucket;
205};
206
207static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
e4bd8bce 208{
5d08ad44 209 struct ct_expect_iter_state *st = seq->private;
e4bd8bce 210
5d08ad44
PM
211 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
212 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
213 return nf_ct_expect_hash[st->bucket].first;
214 }
215 return NULL;
216}
e4bd8bce 217
5d08ad44
PM
218static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
219 struct hlist_node *head)
220{
221 struct ct_expect_iter_state *st = seq->private;
e4bd8bce 222
5d08ad44
PM
223 head = head->next;
224 while (head == NULL) {
225 if (++st->bucket >= nf_ct_expect_hsize)
e4bd8bce 226 return NULL;
5d08ad44 227 head = nf_ct_expect_hash[st->bucket].first;
e4bd8bce 228 }
5d08ad44 229 return head;
e4bd8bce
PM
230}
231
5d08ad44 232static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
e4bd8bce 233{
5d08ad44 234 struct hlist_node *head = ct_expect_get_first(seq);
e4bd8bce 235
5d08ad44
PM
236 if (head)
237 while (pos && (head = ct_expect_get_next(seq, head)))
238 pos--;
239 return pos ? NULL : head;
240}
e4bd8bce 241
5d08ad44
PM
242static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
243{
244 read_lock_bh(&nf_conntrack_lock);
245 return ct_expect_get_idx(seq, *pos);
246}
e4bd8bce 247
5d08ad44
PM
248static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
249{
250 (*pos)++;
251 return ct_expect_get_next(seq, v);
e4bd8bce
PM
252}
253
5d08ad44 254static void exp_seq_stop(struct seq_file *seq, void *v)
e4bd8bce
PM
255{
256 read_unlock_bh(&nf_conntrack_lock);
257}
258
259static int exp_seq_show(struct seq_file *s, void *v)
260{
5d08ad44
PM
261 struct nf_conntrack_expect *exp;
262 struct hlist_node *n = v;
263
264 exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
e4bd8bce
PM
265
266 if (exp->tuple.src.l3num != AF_INET)
267 return 0;
268
269 if (exp->timeout.function)
270 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
271 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
272 else
273 seq_printf(s, "- ");
274
275 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
276
277 print_tuple(s, &exp->tuple,
278 __nf_ct_l3proto_find(exp->tuple.src.l3num),
279 __nf_ct_l4proto_find(exp->tuple.src.l3num,
e905a9ed 280 exp->tuple.dst.protonum));
e4bd8bce
PM
281 return seq_putc(s, '\n');
282}
283
56b3d975 284static const struct seq_operations exp_seq_ops = {
e4bd8bce
PM
285 .start = exp_seq_start,
286 .next = exp_seq_next,
287 .stop = exp_seq_stop,
288 .show = exp_seq_show
289};
290
291static int exp_open(struct inode *inode, struct file *file)
292{
5d08ad44
PM
293 struct seq_file *seq;
294 struct ct_expect_iter_state *st;
295 int ret;
296
297 st = kmalloc(sizeof(struct ct_expect_iter_state), GFP_KERNEL);
298 if (st == NULL)
299 return -ENOMEM;
300 ret = seq_open(file, &exp_seq_ops);
301 if (ret)
302 goto out_free;
303 seq = file->private_data;
304 seq->private = st;
305 memset(st, 0, sizeof(struct ct_expect_iter_state));
306 return ret;
307out_free:
308 kfree(st);
309 return ret;
e4bd8bce
PM
310}
311
9a32144e 312static const struct file_operations ip_exp_file_ops = {
e4bd8bce
PM
313 .owner = THIS_MODULE,
314 .open = exp_open,
315 .read = seq_read,
316 .llseek = seq_lseek,
5d08ad44 317 .release = seq_release_private,
e4bd8bce
PM
318};
319
320static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
321{
322 int cpu;
323
324 if (*pos == 0)
325 return SEQ_START_TOKEN;
326
327 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
328 if (!cpu_possible(cpu))
329 continue;
330 *pos = cpu+1;
331 return &per_cpu(nf_conntrack_stat, cpu);
332 }
333
334 return NULL;
335}
336
337static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
338{
339 int cpu;
340
341 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
342 if (!cpu_possible(cpu))
343 continue;
344 *pos = cpu+1;
345 return &per_cpu(nf_conntrack_stat, cpu);
346 }
347
348 return NULL;
349}
350
351static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
352{
353}
354
355static int ct_cpu_seq_show(struct seq_file *seq, void *v)
356{
357 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
358 struct ip_conntrack_stat *st = v;
359
360 if (v == SEQ_START_TOKEN) {
361 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");
362 return 0;
363 }
364
365 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
366 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
367 nr_conntracks,
368 st->searched,
369 st->found,
370 st->new,
371 st->invalid,
372 st->ignore,
373 st->delete,
374 st->delete_list,
375 st->insert,
376 st->insert_failed,
377 st->drop,
378 st->early_drop,
379 st->error,
380
381 st->expect_new,
382 st->expect_create,
383 st->expect_delete
384 );
385 return 0;
386}
387
56b3d975 388static const struct seq_operations ct_cpu_seq_ops = {
e4bd8bce
PM
389 .start = ct_cpu_seq_start,
390 .next = ct_cpu_seq_next,
391 .stop = ct_cpu_seq_stop,
392 .show = ct_cpu_seq_show,
393};
394
395static int ct_cpu_seq_open(struct inode *inode, struct file *file)
396{
397 return seq_open(file, &ct_cpu_seq_ops);
398}
399
9a32144e 400static const struct file_operations ct_cpu_seq_fops = {
e4bd8bce
PM
401 .owner = THIS_MODULE,
402 .open = ct_cpu_seq_open,
403 .read = seq_read,
404 .llseek = seq_lseek,
405 .release = seq_release_private,
406};
407
408int __init nf_conntrack_ipv4_compat_init(void)
409{
410 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
411
412 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
413 if (!proc)
414 goto err1;
415
416 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
417 &ip_exp_file_ops);
418 if (!proc_exp)
419 goto err2;
420
421 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
422 if (!proc_stat)
423 goto err3;
424
425 proc_stat->proc_fops = &ct_cpu_seq_fops;
426 proc_stat->owner = THIS_MODULE;
427
428 return 0;
429
430err3:
431 proc_net_remove("ip_conntrack_expect");
432err2:
433 proc_net_remove("ip_conntrack");
434err1:
435 return -ENOMEM;
436}
437
438void __exit nf_conntrack_ipv4_compat_fini(void)
439{
440 remove_proc_entry("ip_conntrack", proc_net_stat);
441 proc_net_remove("ip_conntrack_expect");
442 proc_net_remove("ip_conntrack");
443}