Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 5 | * (C) 2006-2010 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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> |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 15 | #include <linux/security.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 16 | #include <net/net_namespace.h> |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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> |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_acct.h> |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 24 | #include <linux/rculist_nulls.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 25 | #include <linux/export.h> |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 26 | |
| 27 | struct ct_iter_state { |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 28 | struct seq_net_private p; |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 29 | struct hlist_nulls_head *hash; |
| 30 | unsigned int htable_size; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 31 | unsigned int bucket; |
| 32 | }; |
| 33 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 34 | static struct hlist_nulls_node *ct_get_first(struct seq_file *seq) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 35 | { |
| 36 | struct ct_iter_state *st = seq->private; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 37 | struct hlist_nulls_node *n; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 38 | |
| 39 | for (st->bucket = 0; |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 40 | st->bucket < st->htable_size; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 41 | st->bucket++) { |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 42 | n = rcu_dereference( |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 43 | hlist_nulls_first_rcu(&st->hash[st->bucket])); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 44 | if (!is_a_nulls(n)) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 45 | return n; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 50 | static struct hlist_nulls_node *ct_get_next(struct seq_file *seq, |
| 51 | struct hlist_nulls_node *head) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 52 | { |
| 53 | struct ct_iter_state *st = seq->private; |
| 54 | |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 55 | head = rcu_dereference(hlist_nulls_next_rcu(head)); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 56 | while (is_a_nulls(head)) { |
| 57 | if (likely(get_nulls_value(head) == st->bucket)) { |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 58 | if (++st->bucket >= st->htable_size) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 61 | head = rcu_dereference( |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 62 | hlist_nulls_first_rcu(&st->hash[st->bucket])); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 63 | } |
| 64 | return head; |
| 65 | } |
| 66 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 67 | static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 68 | { |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 69 | struct hlist_nulls_node *head = ct_get_first(seq); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 78 | __acquires(RCU) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 79 | { |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 80 | struct ct_iter_state *st = seq->private; |
| 81 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 82 | rcu_read_lock(); |
Liping Zhang | 64b8763 | 2016-07-03 13:18:43 +0800 | [diff] [blame] | 83 | |
| 84 | nf_conntrack_get_ht(&st->hash, &st->htable_size); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 95 | __releases(RCU) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 96 | { |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 97 | rcu_read_unlock(); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 100 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 101 | static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 102 | { |
| 103 | int ret; |
| 104 | u32 len; |
| 105 | char *secctx; |
| 106 | |
| 107 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); |
| 108 | if (ret) |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 109 | return; |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 110 | |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 111 | seq_printf(s, "secctx=%s ", secctx); |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 112 | |
| 113 | security_release_secctx(secctx, len); |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 114 | } |
| 115 | #else |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 116 | static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 117 | { |
Eric Paris | 1ae4de0 | 2010-10-13 16:25:00 -0400 | [diff] [blame] | 118 | } |
| 119 | #endif |
| 120 | |
Florian Westphal | 245cfdc | 2016-04-28 19:13:44 +0200 | [diff] [blame] | 121 | static bool ct_seq_should_skip(const struct nf_conn *ct, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 122 | const struct net *net, |
Florian Westphal | 245cfdc | 2016-04-28 19:13:44 +0200 | [diff] [blame] | 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 | |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 132 | if (!net_eq(nf_ct_net(ct), net)) |
| 133 | return true; |
| 134 | |
Florian Westphal | 245cfdc | 2016-04-28 19:13:44 +0200 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 138 | static int ct_seq_show(struct seq_file *s, void *v) |
| 139 | { |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 140 | struct nf_conntrack_tuple_hash *hash = v; |
| 141 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash); |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 142 | const struct nf_conntrack_l3proto *l3proto; |
| 143 | const struct nf_conntrack_l4proto *l4proto; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 144 | int ret = 0; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 145 | |
| 146 | NF_CT_ASSERT(ct); |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 147 | if (ct_seq_should_skip(ct, seq_file_net(s), hash)) |
Florian Westphal | 245cfdc | 2016-04-28 19:13:44 +0200 | [diff] [blame] | 148 | return 0; |
| 149 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 150 | if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) |
| 151 | return 0; |
| 152 | |
Florian Westphal | 245cfdc | 2016-04-28 19:13:44 +0200 | [diff] [blame] | 153 | /* check if we raced w. object reuse */ |
| 154 | if (!nf_ct_is_confirmed(ct) || |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 155 | ct_seq_should_skip(ct, seq_file_net(s), hash)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 156 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 157 | |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 158 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 159 | NF_CT_ASSERT(l3proto); |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 160 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 161 | NF_CT_ASSERT(l4proto); |
| 162 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 163 | ret = -ENOSPC; |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 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); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 168 | |
Steven Rostedt (Red Hat) | 37246a5 | 2014-10-27 16:02:47 -0400 | [diff] [blame] | 169 | if (l4proto->print_conntrack) |
| 170 | l4proto->print_conntrack(s, ct); |
| 171 | |
| 172 | if (seq_has_overflowed(s)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 173 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 174 | |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 175 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 176 | l3proto, l4proto); |
| 177 | |
| 178 | if (seq_has_overflowed(s)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 179 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 180 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 181 | if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 182 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 183 | |
| 184 | if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 185 | seq_printf(s, "[UNREPLIED] "); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 186 | |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 187 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
| 188 | l3proto, l4proto); |
| 189 | |
| 190 | if (seq_has_overflowed(s)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 191 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 192 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 193 | if (seq_print_acct(s, ct, IP_CT_DIR_REPLY)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 194 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 195 | |
| 196 | if (test_bit(IPS_ASSURED_BIT, &ct->status)) |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 197 | seq_printf(s, "[ASSURED] "); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 198 | |
| 199 | #ifdef CONFIG_NF_CONNTRACK_MARK |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 200 | seq_printf(s, "mark=%u ", ct->mark); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 201 | #endif |
| 202 | |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 203 | ct_show_secctx(s, ct); |
| 204 | |
| 205 | seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)); |
| 206 | |
| 207 | if (seq_has_overflowed(s)) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 208 | goto release; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 209 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 210 | ret = 0; |
| 211 | release: |
| 212 | nf_ct_put(ct); |
| 213 | return ret; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 216 | static const struct seq_operations ct_seq_ops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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 | { |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 225 | return seq_open_net(inode, file, &ct_seq_ops, |
| 226 | sizeof(struct ct_iter_state)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 229 | static const struct file_operations ct_file_ops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 230 | .owner = THIS_MODULE, |
| 231 | .open = ct_open, |
| 232 | .read = seq_read, |
| 233 | .llseek = seq_lseek, |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 234 | .release = seq_release_net, |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* expects */ |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 238 | struct ct_expect_iter_state { |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 239 | struct seq_net_private p; |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 240 | unsigned int bucket; |
| 241 | }; |
| 242 | |
| 243 | static struct hlist_node *ct_expect_get_first(struct seq_file *seq) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 244 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 245 | struct ct_expect_iter_state *st = seq->private; |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 246 | struct hlist_node *n; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 247 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 248 | for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) { |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 249 | n = rcu_dereference( |
Florian Westphal | 0a93aae | 2016-05-06 00:51:49 +0200 | [diff] [blame] | 250 | hlist_first_rcu(&nf_ct_expect_hash[st->bucket])); |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 251 | if (n) |
| 252 | return n; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 253 | } |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 254 | return NULL; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 257 | static struct hlist_node *ct_expect_get_next(struct seq_file *seq, |
| 258 | struct hlist_node *head) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 259 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 260 | struct ct_expect_iter_state *st = seq->private; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 261 | |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 262 | head = rcu_dereference(hlist_next_rcu(head)); |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 263 | while (head == NULL) { |
| 264 | if (++st->bucket >= nf_ct_expect_hsize) |
| 265 | return NULL; |
Eric Dumazet | eb73316 | 2010-11-15 18:43:59 +0100 | [diff] [blame] | 266 | head = rcu_dereference( |
Florian Westphal | 0a93aae | 2016-05-06 00:51:49 +0200 | [diff] [blame] | 267 | hlist_first_rcu(&nf_ct_expect_hash[st->bucket])); |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 268 | } |
| 269 | return head; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 272 | static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos) |
| 273 | { |
| 274 | struct hlist_node *head = ct_expect_get_first(seq); |
| 275 | |
| 276 | if (head) |
| 277 | while (pos && (head = ct_expect_get_next(seq, head))) |
| 278 | pos--; |
| 279 | return pos ? NULL : head; |
| 280 | } |
| 281 | |
| 282 | static void *exp_seq_start(struct seq_file *seq, loff_t *pos) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 283 | __acquires(RCU) |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 284 | { |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 285 | rcu_read_lock(); |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 286 | return ct_expect_get_idx(seq, *pos); |
| 287 | } |
| 288 | |
| 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); |
| 293 | } |
| 294 | |
| 295 | static void exp_seq_stop(struct seq_file *seq, void *v) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 296 | __releases(RCU) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 297 | { |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 298 | rcu_read_unlock(); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static int exp_seq_show(struct seq_file *s, void *v) |
| 302 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 303 | struct nf_conntrack_expect *exp; |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 304 | const struct hlist_node *n = v; |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame] | 305 | |
| 306 | exp = hlist_entry(n, struct nf_conntrack_expect, hnode); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 307 | |
Florian Westphal | 03d7dc5 | 2016-05-06 00:51:47 +0200 | [diff] [blame] | 308 | if (!net_eq(nf_ct_net(exp->master), seq_file_net(s))) |
| 309 | return 0; |
| 310 | |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 325 | exp->tuple.dst.protonum)); |
Joe Perches | 1ca9e41 | 2015-03-16 11:25:17 -0700 | [diff] [blame] | 326 | seq_putc(s, '\n'); |
| 327 | |
| 328 | return 0; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 331 | static const struct seq_operations exp_seq_ops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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 | { |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 340 | return seq_open_net(inode, file, &exp_seq_ops, |
| 341 | sizeof(struct ct_expect_iter_state)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 344 | static const struct file_operations ip_exp_file_ops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 345 | .owner = THIS_MODULE, |
| 346 | .open = exp_open, |
| 347 | .read = seq_read, |
| 348 | .llseek = seq_lseek, |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 349 | .release = seq_release_net, |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) |
| 353 | { |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 354 | struct net *net = seq_file_net(seq); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 355 | int cpu; |
| 356 | |
| 357 | if (*pos == 0) |
| 358 | return SEQ_START_TOKEN; |
| 359 | |
Rusty Russell | 0f23174a | 2008-12-29 12:23:42 +0000 | [diff] [blame] | 360 | for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 361 | if (!cpu_possible(cpu)) |
| 362 | continue; |
| 363 | *pos = cpu+1; |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 364 | return per_cpu_ptr(net->ct.stat, cpu); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 371 | { |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 372 | struct net *net = seq_file_net(seq); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 373 | int cpu; |
| 374 | |
Rusty Russell | 0f23174a | 2008-12-29 12:23:42 +0000 | [diff] [blame] | 375 | for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 376 | if (!cpu_possible(cpu)) |
| 377 | continue; |
| 378 | *pos = cpu+1; |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 379 | return per_cpu_ptr(net->ct.stat, cpu); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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 | { |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 391 | struct net *net = seq_file_net(seq); |
| 392 | unsigned int nr_conntracks = atomic_read(&net->ct.count); |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 393 | const struct ip_conntrack_stat *st = v; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 394 | |
| 395 | if (v == SEQ_START_TOKEN) { |
Jesper Dangaard Brouer | af740b2 | 2010-04-23 12:34:56 +0200 | [diff] [blame] | 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"); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " |
Jesper Dangaard Brouer | af740b2 | 2010-04-23 12:34:56 +0200 | [diff] [blame] | 401 | "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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, |
Jesper Dangaard Brouer | af740b2 | 2010-04-23 12:34:56 +0200 | [diff] [blame] | 418 | st->expect_delete, |
| 419 | st->search_restart |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 420 | ); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 424 | static const struct seq_operations ct_cpu_seq_ops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 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 | { |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 433 | return seq_open_net(inode, file, &ct_cpu_seq_ops, |
| 434 | sizeof(struct seq_net_private)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 437 | static const struct file_operations ct_cpu_seq_fops = { |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 438 | .owner = THIS_MODULE, |
| 439 | .open = ct_cpu_seq_open, |
| 440 | .read = seq_read, |
| 441 | .llseek = seq_lseek, |
Alexey Dobriyan | 8e9df80 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 442 | .release = seq_release_net, |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 443 | }; |
| 444 | |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 445 | static int __net_init ip_conntrack_net_init(struct net *net) |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 446 | { |
| 447 | struct proc_dir_entry *proc, *proc_exp, *proc_stat; |
| 448 | |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 449 | proc = proc_create("ip_conntrack", 0440, net->proc_net, &ct_file_ops); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 450 | if (!proc) |
| 451 | goto err1; |
| 452 | |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 453 | proc_exp = proc_create("ip_conntrack_expect", 0440, net->proc_net, |
| 454 | &ip_exp_file_ops); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 455 | if (!proc_exp) |
| 456 | goto err2; |
| 457 | |
Denis V. Lunev | 8eeee8b | 2008-03-27 16:55:53 -0700 | [diff] [blame] | 458 | proc_stat = proc_create("ip_conntrack", S_IRUGO, |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 459 | net->proc_net_stat, &ct_cpu_seq_fops); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 460 | if (!proc_stat) |
| 461 | goto err3; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 462 | return 0; |
| 463 | |
| 464 | err3: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 465 | remove_proc_entry("ip_conntrack_expect", net->proc_net); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 466 | err2: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 467 | remove_proc_entry("ip_conntrack", net->proc_net); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 468 | err1: |
| 469 | return -ENOMEM; |
| 470 | } |
| 471 | |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 472 | static void __net_exit ip_conntrack_net_exit(struct net *net) |
| 473 | { |
| 474 | remove_proc_entry("ip_conntrack", net->proc_net_stat); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 475 | remove_proc_entry("ip_conntrack_expect", net->proc_net); |
| 476 | remove_proc_entry("ip_conntrack", net->proc_net); |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 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 | |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 489 | void __exit nf_conntrack_ipv4_compat_fini(void) |
| 490 | { |
Alexey Dobriyan | 5e6b299 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 491 | unregister_pernet_subsys(&ip_conntrack_net_ops); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 492 | } |