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