Commit | Line | Data |
---|---|---|
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> | |
1ae4de0c | 14 | #include <linux/security.h> |
457c4cbc | 15 | #include <net/net_namespace.h> |
e4bd8bce PM |
16 | |
17 | #include <linux/netfilter.h> | |
18 | #include <net/netfilter/nf_conntrack_core.h> | |
19 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
20 | #include <net/netfilter/nf_conntrack_l4proto.h> | |
21 | #include <net/netfilter/nf_conntrack_expect.h> | |
58401572 | 22 | #include <net/netfilter/nf_conntrack_acct.h> |
eb733162 | 23 | #include <linux/rculist_nulls.h> |
bc3b2d7f | 24 | #include <linux/export.h> |
e4bd8bce PM |
25 | |
26 | struct ct_iter_state { | |
5e6b2997 | 27 | struct seq_net_private p; |
e4bd8bce PM |
28 | unsigned int bucket; |
29 | }; | |
30 | ||
ea781f19 | 31 | static struct hlist_nulls_node *ct_get_first(struct seq_file *seq) |
e4bd8bce | 32 | { |
5e6b2997 | 33 | struct net *net = seq_file_net(seq); |
e4bd8bce | 34 | struct ct_iter_state *st = seq->private; |
ea781f19 | 35 | struct hlist_nulls_node *n; |
e4bd8bce PM |
36 | |
37 | for (st->bucket = 0; | |
d696c7bd | 38 | st->bucket < net->ct.htable_size; |
e4bd8bce | 39 | st->bucket++) { |
eb733162 ED |
40 | n = rcu_dereference( |
41 | hlist_nulls_first_rcu(&net->ct.hash[st->bucket])); | |
ea781f19 | 42 | if (!is_a_nulls(n)) |
76507f69 | 43 | return n; |
e4bd8bce PM |
44 | } |
45 | return NULL; | |
46 | } | |
47 | ||
ea781f19 ED |
48 | static struct hlist_nulls_node *ct_get_next(struct seq_file *seq, |
49 | struct hlist_nulls_node *head) | |
e4bd8bce | 50 | { |
5e6b2997 | 51 | struct net *net = seq_file_net(seq); |
e4bd8bce PM |
52 | struct ct_iter_state *st = seq->private; |
53 | ||
eb733162 | 54 | head = rcu_dereference(hlist_nulls_next_rcu(head)); |
ea781f19 ED |
55 | while (is_a_nulls(head)) { |
56 | if (likely(get_nulls_value(head) == st->bucket)) { | |
d696c7bd | 57 | if (++st->bucket >= net->ct.htable_size) |
ea781f19 ED |
58 | return NULL; |
59 | } | |
eb733162 ED |
60 | head = rcu_dereference( |
61 | hlist_nulls_first_rcu(&net->ct.hash[st->bucket])); | |
e4bd8bce PM |
62 | } |
63 | return head; | |
64 | } | |
65 | ||
ea781f19 | 66 | static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos) |
e4bd8bce | 67 | { |
ea781f19 | 68 | struct hlist_nulls_node *head = ct_get_first(seq); |
e4bd8bce PM |
69 | |
70 | if (head) | |
71 | while (pos && (head = ct_get_next(seq, head))) | |
72 | pos--; | |
73 | return pos ? NULL : head; | |
74 | } | |
75 | ||
76 | static void *ct_seq_start(struct seq_file *seq, loff_t *pos) | |
76507f69 | 77 | __acquires(RCU) |
e4bd8bce | 78 | { |
76507f69 | 79 | rcu_read_lock(); |
e4bd8bce PM |
80 | return ct_get_idx(seq, *pos); |
81 | } | |
82 | ||
83 | static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
84 | { | |
85 | (*pos)++; | |
86 | return ct_get_next(s, v); | |
87 | } | |
88 | ||
89 | static void ct_seq_stop(struct seq_file *s, void *v) | |
76507f69 | 90 | __releases(RCU) |
e4bd8bce | 91 | { |
76507f69 | 92 | rcu_read_unlock(); |
e4bd8bce PM |
93 | } |
94 | ||
1ae4de0c EP |
95 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
96 | static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) | |
97 | { | |
98 | int ret; | |
99 | u32 len; | |
100 | char *secctx; | |
101 | ||
102 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
103 | if (ret) | |
cba85b53 | 104 | return 0; |
1ae4de0c EP |
105 | |
106 | ret = seq_printf(s, "secctx=%s ", secctx); | |
107 | ||
108 | security_release_secctx(secctx, len); | |
109 | return ret; | |
110 | } | |
111 | #else | |
112 | static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) | |
113 | { | |
114 | return 0; | |
115 | } | |
116 | #endif | |
117 | ||
e4bd8bce PM |
118 | static int ct_seq_show(struct seq_file *s, void *v) |
119 | { | |
ea781f19 ED |
120 | struct nf_conntrack_tuple_hash *hash = v; |
121 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash); | |
32948588 JE |
122 | const struct nf_conntrack_l3proto *l3proto; |
123 | const struct nf_conntrack_l4proto *l4proto; | |
ea781f19 | 124 | int ret = 0; |
e4bd8bce PM |
125 | |
126 | NF_CT_ASSERT(ct); | |
ea781f19 ED |
127 | if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) |
128 | return 0; | |
129 | ||
e4bd8bce PM |
130 | |
131 | /* we only want to print DIR_ORIGINAL */ | |
132 | if (NF_CT_DIRECTION(hash)) | |
ea781f19 | 133 | goto release; |
5e8fbe2a | 134 | if (nf_ct_l3num(ct) != AF_INET) |
ea781f19 | 135 | goto release; |
e4bd8bce | 136 | |
5e8fbe2a | 137 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); |
e4bd8bce | 138 | NF_CT_ASSERT(l3proto); |
5e8fbe2a | 139 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
e4bd8bce PM |
140 | NF_CT_ASSERT(l4proto); |
141 | ||
ea781f19 | 142 | ret = -ENOSPC; |
e4bd8bce | 143 | if (seq_printf(s, "%-8s %u %ld ", |
5e8fbe2a | 144 | l4proto->name, nf_ct_protonum(ct), |
e4bd8bce PM |
145 | timer_pending(&ct->timeout) |
146 | ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0) | |
ea781f19 | 147 | goto release; |
e4bd8bce | 148 | |
c71e9167 | 149 | if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct)) |
ea781f19 | 150 | goto release; |
e4bd8bce PM |
151 | |
152 | if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
153 | l3proto, l4proto)) | |
ea781f19 | 154 | goto release; |
e4bd8bce | 155 | |
58401572 | 156 | if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL)) |
ea781f19 | 157 | goto release; |
e4bd8bce PM |
158 | |
159 | if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) | |
160 | if (seq_printf(s, "[UNREPLIED] ")) | |
ea781f19 | 161 | goto release; |
e4bd8bce PM |
162 | |
163 | if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, | |
164 | l3proto, l4proto)) | |
ea781f19 | 165 | goto release; |
e4bd8bce | 166 | |
58401572 | 167 | if (seq_print_acct(s, ct, IP_CT_DIR_REPLY)) |
ea781f19 | 168 | goto release; |
e4bd8bce PM |
169 | |
170 | if (test_bit(IPS_ASSURED_BIT, &ct->status)) | |
171 | if (seq_printf(s, "[ASSURED] ")) | |
ea781f19 | 172 | goto release; |
e4bd8bce PM |
173 | |
174 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
175 | if (seq_printf(s, "mark=%u ", ct->mark)) | |
ea781f19 | 176 | goto release; |
e4bd8bce PM |
177 | #endif |
178 | ||
1ae4de0c | 179 | if (ct_show_secctx(s, ct)) |
ea781f19 | 180 | goto release; |
e4bd8bce PM |
181 | |
182 | if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use))) | |
ea781f19 ED |
183 | goto release; |
184 | ret = 0; | |
185 | release: | |
186 | nf_ct_put(ct); | |
187 | return ret; | |
e4bd8bce PM |
188 | } |
189 | ||
56b3d975 | 190 | static const struct seq_operations ct_seq_ops = { |
e4bd8bce PM |
191 | .start = ct_seq_start, |
192 | .next = ct_seq_next, | |
193 | .stop = ct_seq_stop, | |
194 | .show = ct_seq_show | |
195 | }; | |
196 | ||
197 | static int ct_open(struct inode *inode, struct file *file) | |
198 | { | |
5e6b2997 AD |
199 | return seq_open_net(inode, file, &ct_seq_ops, |
200 | sizeof(struct ct_iter_state)); | |
e4bd8bce PM |
201 | } |
202 | ||
9a32144e | 203 | static const struct file_operations ct_file_ops = { |
e4bd8bce PM |
204 | .owner = THIS_MODULE, |
205 | .open = ct_open, | |
206 | .read = seq_read, | |
207 | .llseek = seq_lseek, | |
5e6b2997 | 208 | .release = seq_release_net, |
e4bd8bce PM |
209 | }; |
210 | ||
211 | /* expects */ | |
5d08ad44 | 212 | struct ct_expect_iter_state { |
5e6b2997 | 213 | struct seq_net_private p; |
5d08ad44 PM |
214 | unsigned int bucket; |
215 | }; | |
216 | ||
217 | static struct hlist_node *ct_expect_get_first(struct seq_file *seq) | |
e4bd8bce | 218 | { |
5e6b2997 | 219 | struct net *net = seq_file_net(seq); |
5d08ad44 | 220 | struct ct_expect_iter_state *st = seq->private; |
7d0742da | 221 | struct hlist_node *n; |
e4bd8bce | 222 | |
5d08ad44 | 223 | for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) { |
eb733162 ED |
224 | n = rcu_dereference( |
225 | hlist_first_rcu(&net->ct.expect_hash[st->bucket])); | |
7d0742da PM |
226 | if (n) |
227 | return n; | |
5d08ad44 PM |
228 | } |
229 | return NULL; | |
230 | } | |
e4bd8bce | 231 | |
5d08ad44 PM |
232 | static struct hlist_node *ct_expect_get_next(struct seq_file *seq, |
233 | struct hlist_node *head) | |
234 | { | |
5e6b2997 | 235 | struct net *net = seq_file_net(seq); |
5d08ad44 | 236 | struct ct_expect_iter_state *st = seq->private; |
e4bd8bce | 237 | |
eb733162 | 238 | head = rcu_dereference(hlist_next_rcu(head)); |
5d08ad44 PM |
239 | while (head == NULL) { |
240 | if (++st->bucket >= nf_ct_expect_hsize) | |
e4bd8bce | 241 | return NULL; |
eb733162 ED |
242 | head = rcu_dereference( |
243 | hlist_first_rcu(&net->ct.expect_hash[st->bucket])); | |
e4bd8bce | 244 | } |
5d08ad44 | 245 | return head; |
e4bd8bce PM |
246 | } |
247 | ||
5d08ad44 | 248 | static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos) |
e4bd8bce | 249 | { |
5d08ad44 | 250 | struct hlist_node *head = ct_expect_get_first(seq); |
e4bd8bce | 251 | |
5d08ad44 PM |
252 | if (head) |
253 | while (pos && (head = ct_expect_get_next(seq, head))) | |
254 | pos--; | |
255 | return pos ? NULL : head; | |
256 | } | |
e4bd8bce | 257 | |
5d08ad44 | 258 | static void *exp_seq_start(struct seq_file *seq, loff_t *pos) |
76507f69 | 259 | __acquires(RCU) |
5d08ad44 | 260 | { |
7d0742da | 261 | rcu_read_lock(); |
5d08ad44 PM |
262 | return ct_expect_get_idx(seq, *pos); |
263 | } | |
e4bd8bce | 264 | |
5d08ad44 PM |
265 | static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
266 | { | |
267 | (*pos)++; | |
268 | return ct_expect_get_next(seq, v); | |
e4bd8bce PM |
269 | } |
270 | ||
5d08ad44 | 271 | static void exp_seq_stop(struct seq_file *seq, void *v) |
76507f69 | 272 | __releases(RCU) |
e4bd8bce | 273 | { |
7d0742da | 274 | rcu_read_unlock(); |
e4bd8bce PM |
275 | } |
276 | ||
277 | static int exp_seq_show(struct seq_file *s, void *v) | |
278 | { | |
5d08ad44 | 279 | struct nf_conntrack_expect *exp; |
32948588 | 280 | const struct hlist_node *n = v; |
5d08ad44 PM |
281 | |
282 | exp = hlist_entry(n, struct nf_conntrack_expect, hnode); | |
e4bd8bce PM |
283 | |
284 | if (exp->tuple.src.l3num != AF_INET) | |
285 | return 0; | |
286 | ||
287 | if (exp->timeout.function) | |
288 | seq_printf(s, "%ld ", timer_pending(&exp->timeout) | |
289 | ? (long)(exp->timeout.expires - jiffies)/HZ : 0); | |
290 | else | |
291 | seq_printf(s, "- "); | |
292 | ||
293 | seq_printf(s, "proto=%u ", exp->tuple.dst.protonum); | |
294 | ||
295 | print_tuple(s, &exp->tuple, | |
296 | __nf_ct_l3proto_find(exp->tuple.src.l3num), | |
297 | __nf_ct_l4proto_find(exp->tuple.src.l3num, | |
e905a9ed | 298 | exp->tuple.dst.protonum)); |
e4bd8bce PM |
299 | return seq_putc(s, '\n'); |
300 | } | |
301 | ||
56b3d975 | 302 | static const struct seq_operations exp_seq_ops = { |
e4bd8bce PM |
303 | .start = exp_seq_start, |
304 | .next = exp_seq_next, | |
305 | .stop = exp_seq_stop, | |
306 | .show = exp_seq_show | |
307 | }; | |
308 | ||
309 | static int exp_open(struct inode *inode, struct file *file) | |
310 | { | |
5e6b2997 AD |
311 | return seq_open_net(inode, file, &exp_seq_ops, |
312 | sizeof(struct ct_expect_iter_state)); | |
e4bd8bce PM |
313 | } |
314 | ||
9a32144e | 315 | static const struct file_operations ip_exp_file_ops = { |
e4bd8bce PM |
316 | .owner = THIS_MODULE, |
317 | .open = exp_open, | |
318 | .read = seq_read, | |
319 | .llseek = seq_lseek, | |
5e6b2997 | 320 | .release = seq_release_net, |
e4bd8bce PM |
321 | }; |
322 | ||
323 | static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) | |
324 | { | |
8e9df801 | 325 | struct net *net = seq_file_net(seq); |
e4bd8bce PM |
326 | int cpu; |
327 | ||
328 | if (*pos == 0) | |
329 | return SEQ_START_TOKEN; | |
330 | ||
0f23174a | 331 | for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { |
e4bd8bce PM |
332 | if (!cpu_possible(cpu)) |
333 | continue; | |
334 | *pos = cpu+1; | |
8e9df801 | 335 | return per_cpu_ptr(net->ct.stat, cpu); |
e4bd8bce PM |
336 | } |
337 | ||
338 | return NULL; | |
339 | } | |
340 | ||
341 | static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
342 | { | |
8e9df801 | 343 | struct net *net = seq_file_net(seq); |
e4bd8bce PM |
344 | int cpu; |
345 | ||
0f23174a | 346 | for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { |
e4bd8bce PM |
347 | if (!cpu_possible(cpu)) |
348 | continue; | |
349 | *pos = cpu+1; | |
8e9df801 | 350 | return per_cpu_ptr(net->ct.stat, cpu); |
e4bd8bce PM |
351 | } |
352 | ||
353 | return NULL; | |
354 | } | |
355 | ||
356 | static void ct_cpu_seq_stop(struct seq_file *seq, void *v) | |
357 | { | |
358 | } | |
359 | ||
360 | static int ct_cpu_seq_show(struct seq_file *seq, void *v) | |
361 | { | |
8e9df801 AD |
362 | struct net *net = seq_file_net(seq); |
363 | unsigned int nr_conntracks = atomic_read(&net->ct.count); | |
32948588 | 364 | const struct ip_conntrack_stat *st = v; |
e4bd8bce PM |
365 | |
366 | if (v == SEQ_START_TOKEN) { | |
af740b2c | 367 | 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 search_restart\n"); |
e4bd8bce PM |
368 | return 0; |
369 | } | |
370 | ||
371 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " | |
af740b2c | 372 | "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", |
e4bd8bce PM |
373 | nr_conntracks, |
374 | st->searched, | |
375 | st->found, | |
376 | st->new, | |
377 | st->invalid, | |
378 | st->ignore, | |
379 | st->delete, | |
380 | st->delete_list, | |
381 | st->insert, | |
382 | st->insert_failed, | |
383 | st->drop, | |
384 | st->early_drop, | |
385 | st->error, | |
386 | ||
387 | st->expect_new, | |
388 | st->expect_create, | |
af740b2c JDB |
389 | st->expect_delete, |
390 | st->search_restart | |
e4bd8bce PM |
391 | ); |
392 | return 0; | |
393 | } | |
394 | ||
56b3d975 | 395 | static const struct seq_operations ct_cpu_seq_ops = { |
e4bd8bce PM |
396 | .start = ct_cpu_seq_start, |
397 | .next = ct_cpu_seq_next, | |
398 | .stop = ct_cpu_seq_stop, | |
399 | .show = ct_cpu_seq_show, | |
400 | }; | |
401 | ||
402 | static int ct_cpu_seq_open(struct inode *inode, struct file *file) | |
403 | { | |
8e9df801 AD |
404 | return seq_open_net(inode, file, &ct_cpu_seq_ops, |
405 | sizeof(struct seq_net_private)); | |
e4bd8bce PM |
406 | } |
407 | ||
9a32144e | 408 | static const struct file_operations ct_cpu_seq_fops = { |
e4bd8bce PM |
409 | .owner = THIS_MODULE, |
410 | .open = ct_cpu_seq_open, | |
411 | .read = seq_read, | |
412 | .llseek = seq_lseek, | |
8e9df801 | 413 | .release = seq_release_net, |
e4bd8bce PM |
414 | }; |
415 | ||
5e6b2997 | 416 | static int __net_init ip_conntrack_net_init(struct net *net) |
e4bd8bce PM |
417 | { |
418 | struct proc_dir_entry *proc, *proc_exp, *proc_stat; | |
419 | ||
d4beaa66 | 420 | proc = proc_create("ip_conntrack", 0440, net->proc_net, &ct_file_ops); |
e4bd8bce PM |
421 | if (!proc) |
422 | goto err1; | |
423 | ||
d4beaa66 G |
424 | proc_exp = proc_create("ip_conntrack_expect", 0440, net->proc_net, |
425 | &ip_exp_file_ops); | |
e4bd8bce PM |
426 | if (!proc_exp) |
427 | goto err2; | |
428 | ||
8eeee8b1 | 429 | proc_stat = proc_create("ip_conntrack", S_IRUGO, |
5e6b2997 | 430 | net->proc_net_stat, &ct_cpu_seq_fops); |
e4bd8bce PM |
431 | if (!proc_stat) |
432 | goto err3; | |
e4bd8bce PM |
433 | return 0; |
434 | ||
435 | err3: | |
5e6b2997 | 436 | proc_net_remove(net, "ip_conntrack_expect"); |
e4bd8bce | 437 | err2: |
5e6b2997 | 438 | proc_net_remove(net, "ip_conntrack"); |
e4bd8bce PM |
439 | err1: |
440 | return -ENOMEM; | |
441 | } | |
442 | ||
5e6b2997 AD |
443 | static void __net_exit ip_conntrack_net_exit(struct net *net) |
444 | { | |
445 | remove_proc_entry("ip_conntrack", net->proc_net_stat); | |
446 | proc_net_remove(net, "ip_conntrack_expect"); | |
447 | proc_net_remove(net, "ip_conntrack"); | |
448 | } | |
449 | ||
450 | static struct pernet_operations ip_conntrack_net_ops = { | |
451 | .init = ip_conntrack_net_init, | |
452 | .exit = ip_conntrack_net_exit, | |
453 | }; | |
454 | ||
455 | int __init nf_conntrack_ipv4_compat_init(void) | |
456 | { | |
457 | return register_pernet_subsys(&ip_conntrack_net_ops); | |
458 | } | |
459 | ||
e4bd8bce PM |
460 | void __exit nf_conntrack_ipv4_compat_fini(void) |
461 | { | |
5e6b2997 | 462 | unregister_pernet_subsys(&ip_conntrack_net_ops); |
e4bd8bce | 463 | } |