blob: 28c8b2b982ecdecea9238de0a2196d66eacac048 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02003 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08008 */
9
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#include <linux/types.h>
11#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/proc_fs.h>
16#include <linux/seq_file.h>
17#include <linux/percpu.h>
18#include <linux/netdevice.h>
Eric Paris1ae4de02010-10-13 16:25:00 -040019#include <linux/security.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020020#include <net/net_namespace.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021#ifdef CONFIG_SYSCTL
22#include <linux/sysctl.h>
23#endif
24
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010026#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010028#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010029#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030#include <net/netfilter/nf_conntrack_helper.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070031#include <net/netfilter/nf_conntrack_acct.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010032#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010033#include <net/netfilter/nf_conntrack_timestamp.h>
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010034#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036MODULE_LICENSE("GPL");
37
Jan Engelhardt54b07dc2011-04-21 09:32:45 +020038#ifdef CONFIG_NF_CONNTRACK_PROCFS
Joe Perches824f1fb2014-09-29 16:08:22 -070039void
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
Jan Engelhardt32948582008-01-31 04:53:24 -080041 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043{
Joe Perches824f1fb2014-09-29 16:08:22 -070044 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010047EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020050 struct seq_net_private p;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051 unsigned int bucket;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010052 u_int64_t time_now;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053};
54
Eric Dumazetea781f12009-03-25 21:05:46 +010055static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020057 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010059 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060
61 for (st->bucket = 0;
Patrick McHardyd696c7b2010-02-08 11:18:07 -080062 st->bucket < net->ct.htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063 st->bucket++) {
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010064 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
Eric Dumazetea781f12009-03-25 21:05:46 +010065 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080066 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 }
68 return NULL;
69}
70
Eric Dumazetea781f12009-03-25 21:05:46 +010071static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
72 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020074 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075 struct ct_iter_state *st = seq->private;
76
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010077 head = rcu_dereference(hlist_nulls_next_rcu(head));
Eric Dumazetea781f12009-03-25 21:05:46 +010078 while (is_a_nulls(head)) {
79 if (likely(get_nulls_value(head) == st->bucket)) {
Patrick McHardyd696c7b2010-02-08 11:18:07 -080080 if (++st->bucket >= net->ct.htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010081 return NULL;
82 }
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010083 head = rcu_dereference(
84 hlist_nulls_first_rcu(
85 &net->ct.hash[st->bucket]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 }
87 return head;
88}
89
Eric Dumazetea781f12009-03-25 21:05:46 +010090static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091{
Eric Dumazetea781f12009-03-25 21:05:46 +010092 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080093
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
98}
99
100static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -0800101 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100103 struct ct_iter_state *st = seq->private;
104
Eric Dumazetd2de8752014-08-22 18:32:09 -0700105 st->time_now = ktime_get_real_ns();
Patrick McHardy76507f62008-01-31 04:38:38 -0800106 rcu_read_lock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107 return ct_get_idx(seq, *pos);
108}
109
110static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
111{
112 (*pos)++;
113 return ct_get_next(s, v);
114}
115
116static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800117 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800118{
Patrick McHardy76507f62008-01-31 04:38:38 -0800119 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800120}
121
Eric Paris1ae4de02010-10-13 16:25:00 -0400122#ifdef CONFIG_NF_CONNTRACK_SECMARK
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400123static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
Eric Paris1ae4de02010-10-13 16:25:00 -0400124{
125 int ret;
126 u32 len;
127 char *secctx;
128
129 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
130 if (ret)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400131 return;
Eric Paris1ae4de02010-10-13 16:25:00 -0400132
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400133 seq_printf(s, "secctx=%s ", secctx);
Eric Paris1ae4de02010-10-13 16:25:00 -0400134
135 security_release_secctx(secctx, len);
Eric Paris1ae4de02010-10-13 16:25:00 -0400136}
137#else
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400138static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
Eric Paris1ae4de02010-10-13 16:25:00 -0400139{
Eric Paris1ae4de02010-10-13 16:25:00 -0400140}
141#endif
142
Daniel Borkmann308ac912015-08-08 21:40:01 +0200143#ifdef CONFIG_NF_CONNTRACK_ZONES
144static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct)
145{
146 seq_printf(s, "zone=%u ", nf_ct_zone(ct)->id);
147}
148#else
149static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct)
150{
151}
152#endif
153
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100154#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400155static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100156{
157 struct ct_iter_state *st = s->private;
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100158 struct nf_conn_tstamp *tstamp;
159 s64 delta_time;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100160
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100161 tstamp = nf_conn_tstamp_find(ct);
162 if (tstamp) {
163 delta_time = st->time_now - tstamp->start;
164 if (delta_time > 0)
165 delta_time = div_s64(delta_time, NSEC_PER_SEC);
166 else
167 delta_time = 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100168
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400169 seq_printf(s, "delta-time=%llu ",
170 (unsigned long long)delta_time);
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100171 }
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400172 return;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100173}
174#else
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400175static inline void
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100176ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
177{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100178}
179#endif
180
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800181/* return 0 on success, 1 in case of error */
182static int ct_seq_show(struct seq_file *s, void *v)
183{
Eric Dumazetea781f12009-03-25 21:05:46 +0100184 struct nf_conntrack_tuple_hash *hash = v;
185 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800186 const struct nf_conntrack_l3proto *l3proto;
187 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100188 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189
Patrick McHardyc88130b2008-01-31 04:42:11 -0800190 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100191 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
192 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193
194 /* we only want to print DIR_ORIGINAL */
195 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100196 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200198 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200200 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100201 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800202
Eric Dumazetea781f12009-03-25 21:05:46 +0100203 ret = -ENOSPC;
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400204 seq_printf(s, "%-8s %u %-8s %u %ld ",
205 l3proto->name, nf_ct_l3num(ct),
206 l4proto->name, nf_ct_protonum(ct),
207 timer_pending(&ct->timeout)
208 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400210 if (l4proto->print_conntrack)
211 l4proto->print_conntrack(s, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212
Joe Perches824f1fb2014-09-29 16:08:22 -0700213 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
214 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400216 if (seq_has_overflowed(s))
217 goto release;
218
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700219 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100220 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221
Patrick McHardyc88130b2008-01-31 04:42:11 -0800222 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400223 seq_printf(s, "[UNREPLIED] ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224
Joe Perches824f1fb2014-09-29 16:08:22 -0700225 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
226 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800227
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700228 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100229 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230
Patrick McHardyc88130b2008-01-31 04:42:11 -0800231 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400232 seq_printf(s, "[ASSURED] ");
233
234 if (seq_has_overflowed(s))
235 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236
237#if defined(CONFIG_NF_CONNTRACK_MARK)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400238 seq_printf(s, "mark=%u ", ct->mark);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239#endif
240
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400241 ct_show_secctx(s, ct);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200242 ct_show_zone(s, ct);
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400243 ct_show_delta_time(s, ct);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100244
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400245 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
246
247 if (seq_has_overflowed(s))
Eric Dumazetea781f12009-03-25 21:05:46 +0100248 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900249
Eric Dumazetea781f12009-03-25 21:05:46 +0100250 ret = 0;
251release:
252 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700253 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254}
255
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700256static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 .start = ct_seq_start,
258 .next = ct_seq_next,
259 .stop = ct_seq_stop,
260 .show = ct_seq_show
261};
262
263static int ct_open(struct inode *inode, struct file *file)
264{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200265 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700266 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267}
268
Arjan van de Venda7071d2007-02-12 00:55:36 -0800269static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270 .owner = THIS_MODULE,
271 .open = ct_open,
272 .read = seq_read,
273 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200274 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800275};
276
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800277static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
278{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200279 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800280 int cpu;
281
282 if (*pos == 0)
283 return SEQ_START_TOKEN;
284
Rusty Russell0f23174a2008-12-29 12:23:42 +0000285 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286 if (!cpu_possible(cpu))
287 continue;
288 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200289 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290 }
291
292 return NULL;
293}
294
295static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
296{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200297 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800298 int cpu;
299
Rusty Russell0f23174a2008-12-29 12:23:42 +0000300 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301 if (!cpu_possible(cpu))
302 continue;
303 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200304 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800305 }
306
307 return NULL;
308}
309
310static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
311{
312}
313
314static int ct_cpu_seq_show(struct seq_file *seq, void *v)
315{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200316 struct net *net = seq_file_net(seq);
317 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800318 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319
320 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200321 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");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322 return 0;
323 }
324
325 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200326 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800327 nr_conntracks,
328 st->searched,
329 st->found,
330 st->new,
331 st->invalid,
332 st->ignore,
333 st->delete,
334 st->delete_list,
335 st->insert,
336 st->insert_failed,
337 st->drop,
338 st->early_drop,
339 st->error,
340
341 st->expect_new,
342 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200343 st->expect_delete,
344 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345 );
346 return 0;
347}
348
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700349static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800350 .start = ct_cpu_seq_start,
351 .next = ct_cpu_seq_next,
352 .stop = ct_cpu_seq_stop,
353 .show = ct_cpu_seq_show,
354};
355
356static int ct_cpu_seq_open(struct inode *inode, struct file *file)
357{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200358 return seq_open_net(inode, file, &ct_cpu_seq_ops,
359 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800360}
361
Arjan van de Venda7071d2007-02-12 00:55:36 -0800362static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 .owner = THIS_MODULE,
364 .open = ct_cpu_seq_open,
365 .read = seq_read,
366 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200367 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100369
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200370static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100371{
372 struct proc_dir_entry *pde;
373
Gao fengd4beaa62013-02-18 01:34:54 +0000374 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100375 if (!pde)
376 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700377
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200378 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700379 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100380 if (!pde)
381 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100382 return 0;
383
384out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000385 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100386out_nf_conntrack:
387 return -ENOMEM;
388}
389
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200390static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100391{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200392 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000393 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100394}
395#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200396static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100397{
398 return 0;
399}
400
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200401static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100402{
403}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200404#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800405
406/* Sysctl support */
407
408#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409/* Log invalid packets of a given protocol */
410static int log_invalid_proto_min = 0;
411static int log_invalid_proto_max = 255;
412
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700413static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414
Joe Perchesfe2c6332013-06-11 23:04:25 -0700415static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 .procname = "nf_conntrack_max",
418 .data = &nf_conntrack_max,
419 .maxlen = sizeof(int),
420 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800421 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800422 },
423 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800424 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200425 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426 .maxlen = sizeof(int),
427 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800428 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800429 },
430 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800431 .procname = "nf_conntrack_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800432 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800433 .maxlen = sizeof(unsigned int),
434 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800435 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800436 },
437 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700438 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200439 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700440 .maxlen = sizeof(unsigned int),
441 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800442 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700443 },
444 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200446 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 .maxlen = sizeof(unsigned int),
448 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800449 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800450 .extra1 = &log_invalid_proto_min,
451 .extra2 = &log_invalid_proto_max,
452 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700453 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700454 .procname = "nf_conntrack_expect_max",
455 .data = &nf_ct_expect_max,
456 .maxlen = sizeof(int),
457 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800458 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700459 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800460 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461};
462
463#define NET_NF_CONNTRACK_MAX 2089
464
Joe Perchesfe2c6332013-06-11 23:04:25 -0700465static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 .procname = "nf_conntrack_max",
468 .data = &nf_conntrack_max,
469 .maxlen = sizeof(int),
470 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800471 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800473 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800474};
475
Alexey Dobriyan80250702008-10-08 11:35:08 +0200476static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100477{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200478 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700479
Alexey Dobriyan80250702008-10-08 11:35:08 +0200480 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
481 GFP_KERNEL);
482 if (!table)
483 goto out_kmemdup;
484
485 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800486 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200487 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200488 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200489
Eric W. Biederman464dc802012-11-16 03:02:59 +0000490 /* Don't export sysctls to unprivileged users */
491 if (net->user_ns != &init_user_ns)
492 table[0].procname = NULL;
493
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000494 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200495 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700496 goto out_unregister_netfilter;
497
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100498 return 0;
499
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700500out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200501 kfree(table);
502out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700503 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100504}
505
Alexey Dobriyan80250702008-10-08 11:35:08 +0200506static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100507{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200508 struct ctl_table *table;
509
Alexey Dobriyan80250702008-10-08 11:35:08 +0200510 table = net->ct.sysctl_header->ctl_table_arg;
511 unregister_net_sysctl_table(net->ct.sysctl_header);
512 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100513}
514#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200515static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100516{
517 return 0;
518}
519
Alexey Dobriyan80250702008-10-08 11:35:08 +0200520static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100521{
522}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800523#endif /* CONFIG_SYSCTL */
524
Gao fengf94161c2013-01-21 22:10:24 +0000525static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200526{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200527 int ret;
528
Gao fengf94161c2013-01-21 22:10:24 +0000529 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200530 if (ret < 0)
531 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000532
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200533 ret = nf_conntrack_standalone_init_proc(net);
534 if (ret < 0)
535 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000536
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200537 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200538 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200539 ret = nf_conntrack_standalone_init_sysctl(net);
540 if (ret < 0)
541 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000542
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200543 return 0;
544
Alexey Dobriyan80250702008-10-08 11:35:08 +0200545out_sysctl:
546 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200547out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000548 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200549out_init:
550 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200551}
552
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000553static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200554{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000555 struct net *net;
556
557 list_for_each_entry(net, net_exit_list, exit_list) {
558 nf_conntrack_standalone_fini_sysctl(net);
559 nf_conntrack_standalone_fini_proc(net);
560 }
561 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200562}
563
564static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000565 .init = nf_conntrack_pernet_init,
566 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200567};
568
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800569static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800570{
Gao fengf94161c2013-01-21 22:10:24 +0000571 int ret = nf_conntrack_init_start();
572 if (ret < 0)
573 goto out_start;
574
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100575#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000576 nf_ct_netfilter_header =
577 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100578 if (!nf_ct_netfilter_header) {
579 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000580 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000581 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100582 }
583#endif
Gao fengf94161c2013-01-21 22:10:24 +0000584
585 ret = register_pernet_subsys(&nf_conntrack_net_ops);
586 if (ret < 0)
587 goto out_pernet;
588
589 nf_conntrack_init_end();
590 return 0;
591
592out_pernet:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100593#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000594 unregister_net_sysctl_table(nf_ct_netfilter_header);
595out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100596#endif
Gao fengf94161c2013-01-21 22:10:24 +0000597 nf_conntrack_cleanup_end();
598out_start:
599 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800600}
601
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800602static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800603{
Gao fengf94161c2013-01-21 22:10:24 +0000604 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200605 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100606#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000607 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100608#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100609 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800610}
611
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800612module_init(nf_conntrack_standalone_init);
613module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800614
615/* Some modules need us, but don't depend directly on any symbol.
616 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800617void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800618{
619}
Patrick McHardy13b18332006-12-02 22:11:25 -0800620EXPORT_SYMBOL_GPL(need_conntrack);