blob: fc823fa5dcf53794bc8977cb5502d1dac92938e2 [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
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100143#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400144static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100145{
146 struct ct_iter_state *st = s->private;
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100147 struct nf_conn_tstamp *tstamp;
148 s64 delta_time;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100149
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100150 tstamp = nf_conn_tstamp_find(ct);
151 if (tstamp) {
152 delta_time = st->time_now - tstamp->start;
153 if (delta_time > 0)
154 delta_time = div_s64(delta_time, NSEC_PER_SEC);
155 else
156 delta_time = 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100157
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400158 seq_printf(s, "delta-time=%llu ",
159 (unsigned long long)delta_time);
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100160 }
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400161 return;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100162}
163#else
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400164static inline void
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100165ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
166{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100167}
168#endif
169
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170/* return 0 on success, 1 in case of error */
171static int ct_seq_show(struct seq_file *s, void *v)
172{
Eric Dumazetea781f12009-03-25 21:05:46 +0100173 struct nf_conntrack_tuple_hash *hash = v;
174 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800175 const struct nf_conntrack_l3proto *l3proto;
176 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100177 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178
Patrick McHardyc88130b2008-01-31 04:42:11 -0800179 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100180 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
181 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182
183 /* we only want to print DIR_ORIGINAL */
184 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100185 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200187 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200189 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100190 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191
Eric Dumazetea781f12009-03-25 21:05:46 +0100192 ret = -ENOSPC;
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400193 seq_printf(s, "%-8s %u %-8s %u %ld ",
194 l3proto->name, nf_ct_l3num(ct),
195 l4proto->name, nf_ct_protonum(ct),
196 timer_pending(&ct->timeout)
197 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400199 if (l4proto->print_conntrack)
200 l4proto->print_conntrack(s, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201
Joe Perches824f1fb2014-09-29 16:08:22 -0700202 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
203 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400205 if (seq_has_overflowed(s))
206 goto release;
207
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700208 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100209 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210
Patrick McHardyc88130b2008-01-31 04:42:11 -0800211 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400212 seq_printf(s, "[UNREPLIED] ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213
Joe Perches824f1fb2014-09-29 16:08:22 -0700214 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
215 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800216
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700217 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100218 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219
Patrick McHardyc88130b2008-01-31 04:42:11 -0800220 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400221 seq_printf(s, "[ASSURED] ");
222
223 if (seq_has_overflowed(s))
224 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225
226#if defined(CONFIG_NF_CONNTRACK_MARK)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400227 seq_printf(s, "mark=%u ", ct->mark);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228#endif
229
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400230 ct_show_secctx(s, ct);
James Morris7c9728c2006-06-09 00:31:46 -0700231
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100232#ifdef CONFIG_NF_CONNTRACK_ZONES
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400233 seq_printf(s, "zone=%u ", nf_ct_zone(ct));
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100234#endif
235
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400236 ct_show_delta_time(s, ct);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100237
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400238 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
239
240 if (seq_has_overflowed(s))
Eric Dumazetea781f12009-03-25 21:05:46 +0100241 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900242
Eric Dumazetea781f12009-03-25 21:05:46 +0100243 ret = 0;
244release:
245 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700246 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800247}
248
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700249static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800250 .start = ct_seq_start,
251 .next = ct_seq_next,
252 .stop = ct_seq_stop,
253 .show = ct_seq_show
254};
255
256static int ct_open(struct inode *inode, struct file *file)
257{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200258 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700259 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260}
261
Arjan van de Venda7071d2007-02-12 00:55:36 -0800262static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800263 .owner = THIS_MODULE,
264 .open = ct_open,
265 .read = seq_read,
266 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200267 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268};
269
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
271{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200272 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800273 int cpu;
274
275 if (*pos == 0)
276 return SEQ_START_TOKEN;
277
Rusty Russell0f23174a2008-12-29 12:23:42 +0000278 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800279 if (!cpu_possible(cpu))
280 continue;
281 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200282 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800283 }
284
285 return NULL;
286}
287
288static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
289{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200290 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291 int cpu;
292
Rusty Russell0f23174a2008-12-29 12:23:42 +0000293 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294 if (!cpu_possible(cpu))
295 continue;
296 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200297 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800298 }
299
300 return NULL;
301}
302
303static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
304{
305}
306
307static int ct_cpu_seq_show(struct seq_file *seq, void *v)
308{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200309 struct net *net = seq_file_net(seq);
310 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800311 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312
313 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200314 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 -0800315 return 0;
316 }
317
318 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200319 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320 nr_conntracks,
321 st->searched,
322 st->found,
323 st->new,
324 st->invalid,
325 st->ignore,
326 st->delete,
327 st->delete_list,
328 st->insert,
329 st->insert_failed,
330 st->drop,
331 st->early_drop,
332 st->error,
333
334 st->expect_new,
335 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200336 st->expect_delete,
337 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800338 );
339 return 0;
340}
341
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700342static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343 .start = ct_cpu_seq_start,
344 .next = ct_cpu_seq_next,
345 .stop = ct_cpu_seq_stop,
346 .show = ct_cpu_seq_show,
347};
348
349static int ct_cpu_seq_open(struct inode *inode, struct file *file)
350{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200351 return seq_open_net(inode, file, &ct_cpu_seq_ops,
352 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800353}
354
Arjan van de Venda7071d2007-02-12 00:55:36 -0800355static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 .owner = THIS_MODULE,
357 .open = ct_cpu_seq_open,
358 .read = seq_read,
359 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200360 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800361};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100362
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200363static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100364{
365 struct proc_dir_entry *pde;
366
Gao fengd4beaa62013-02-18 01:34:54 +0000367 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100368 if (!pde)
369 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700370
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200371 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700372 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100373 if (!pde)
374 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100375 return 0;
376
377out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000378 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100379out_nf_conntrack:
380 return -ENOMEM;
381}
382
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200383static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100384{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200385 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000386 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100387}
388#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200389static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100390{
391 return 0;
392}
393
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200394static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100395{
396}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200397#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398
399/* Sysctl support */
400
401#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800402/* Log invalid packets of a given protocol */
403static int log_invalid_proto_min = 0;
404static int log_invalid_proto_max = 255;
405
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700406static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407
Joe Perchesfe2c6332013-06-11 23:04:25 -0700408static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800410 .procname = "nf_conntrack_max",
411 .data = &nf_conntrack_max,
412 .maxlen = sizeof(int),
413 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800414 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800415 },
416 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200418 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800419 .maxlen = sizeof(int),
420 .mode = 0444,
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_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800425 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426 .maxlen = sizeof(unsigned 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 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700431 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200432 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700433 .maxlen = sizeof(unsigned int),
434 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800435 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700436 },
437 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800438 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200439 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440 .maxlen = sizeof(unsigned int),
441 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800442 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443 .extra1 = &log_invalid_proto_min,
444 .extra2 = &log_invalid_proto_max,
445 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700446 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700447 .procname = "nf_conntrack_expect_max",
448 .data = &nf_ct_expect_max,
449 .maxlen = sizeof(int),
450 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800451 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700452 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800453 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800454};
455
456#define NET_NF_CONNTRACK_MAX 2089
457
Joe Perchesfe2c6332013-06-11 23:04:25 -0700458static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800460 .procname = "nf_conntrack_max",
461 .data = &nf_conntrack_max,
462 .maxlen = sizeof(int),
463 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800464 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800465 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800466 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467};
468
Alexey Dobriyan80250702008-10-08 11:35:08 +0200469static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100470{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200471 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700472
Alexey Dobriyan80250702008-10-08 11:35:08 +0200473 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
474 GFP_KERNEL);
475 if (!table)
476 goto out_kmemdup;
477
478 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800479 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200480 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200481 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200482
Eric W. Biederman464dc802012-11-16 03:02:59 +0000483 /* Don't export sysctls to unprivileged users */
484 if (net->user_ns != &init_user_ns)
485 table[0].procname = NULL;
486
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000487 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200488 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700489 goto out_unregister_netfilter;
490
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100491 return 0;
492
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700493out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200494 kfree(table);
495out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700496 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100497}
498
Alexey Dobriyan80250702008-10-08 11:35:08 +0200499static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100500{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200501 struct ctl_table *table;
502
Alexey Dobriyan80250702008-10-08 11:35:08 +0200503 table = net->ct.sysctl_header->ctl_table_arg;
504 unregister_net_sysctl_table(net->ct.sysctl_header);
505 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100506}
507#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200508static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100509{
510 return 0;
511}
512
Alexey Dobriyan80250702008-10-08 11:35:08 +0200513static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100514{
515}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516#endif /* CONFIG_SYSCTL */
517
Gao fengf94161c2013-01-21 22:10:24 +0000518static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200519{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200520 int ret;
521
Gao fengf94161c2013-01-21 22:10:24 +0000522 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200523 if (ret < 0)
524 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000525
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200526 ret = nf_conntrack_standalone_init_proc(net);
527 if (ret < 0)
528 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000529
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200530 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200531 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200532 ret = nf_conntrack_standalone_init_sysctl(net);
533 if (ret < 0)
534 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000535
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200536 return 0;
537
Alexey Dobriyan80250702008-10-08 11:35:08 +0200538out_sysctl:
539 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200540out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000541 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200542out_init:
543 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200544}
545
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000546static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200547{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000548 struct net *net;
549
550 list_for_each_entry(net, net_exit_list, exit_list) {
551 nf_conntrack_standalone_fini_sysctl(net);
552 nf_conntrack_standalone_fini_proc(net);
553 }
554 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200555}
556
557static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000558 .init = nf_conntrack_pernet_init,
559 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200560};
561
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800562static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800563{
Gao fengf94161c2013-01-21 22:10:24 +0000564 int ret = nf_conntrack_init_start();
565 if (ret < 0)
566 goto out_start;
567
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100568#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000569 nf_ct_netfilter_header =
570 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100571 if (!nf_ct_netfilter_header) {
572 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000573 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000574 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100575 }
576#endif
Gao fengf94161c2013-01-21 22:10:24 +0000577
578 ret = register_pernet_subsys(&nf_conntrack_net_ops);
579 if (ret < 0)
580 goto out_pernet;
581
582 nf_conntrack_init_end();
583 return 0;
584
585out_pernet:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100586#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000587 unregister_net_sysctl_table(nf_ct_netfilter_header);
588out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100589#endif
Gao fengf94161c2013-01-21 22:10:24 +0000590 nf_conntrack_cleanup_end();
591out_start:
592 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800593}
594
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800595static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800596{
Gao fengf94161c2013-01-21 22:10:24 +0000597 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200598 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100599#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000600 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100601#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100602 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800603}
604
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800605module_init(nf_conntrack_standalone_init);
606module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800607
608/* Some modules need us, but don't depend directly on any symbol.
609 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800610void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800611{
612}
Patrick McHardy13b18332006-12-02 22:11:25 -0800613EXPORT_SYMBOL_GPL(need_conntrack);