blob: f641751dba9dc467b4207716bdb0ac69208747f8 [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
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010039int
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{
Martin Josefsson605dcad2006-11-29 02:35:06 +010044 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010046EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020049 struct seq_net_private p;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050 unsigned int bucket;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010051 u_int64_t time_now;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052};
53
Eric Dumazetea781f12009-03-25 21:05:46 +010054static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020056 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010058 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059
60 for (st->bucket = 0;
Patrick McHardyd696c7b2010-02-08 11:18:07 -080061 st->bucket < net->ct.htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080062 st->bucket++) {
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010063 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
Eric Dumazetea781f12009-03-25 21:05:46 +010064 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080065 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066 }
67 return NULL;
68}
69
Eric Dumazetea781f12009-03-25 21:05:46 +010070static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
71 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020073 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074 struct ct_iter_state *st = seq->private;
75
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010076 head = rcu_dereference(hlist_nulls_next_rcu(head));
Eric Dumazetea781f12009-03-25 21:05:46 +010077 while (is_a_nulls(head)) {
78 if (likely(get_nulls_value(head) == st->bucket)) {
Patrick McHardyd696c7b2010-02-08 11:18:07 -080079 if (++st->bucket >= net->ct.htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010080 return NULL;
81 }
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010082 head = rcu_dereference(
83 hlist_nulls_first_rcu(
84 &net->ct.hash[st->bucket]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080085 }
86 return head;
87}
88
Eric Dumazetea781f12009-03-25 21:05:46 +010089static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080090{
Eric Dumazetea781f12009-03-25 21:05:46 +010091 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092
93 if (head)
94 while (pos && (head = ct_get_next(seq, head)))
95 pos--;
96 return pos ? NULL : head;
97}
98
99static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -0800100 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100102 struct ct_iter_state *st = seq->private;
103
104 st->time_now = ktime_to_ns(ktime_get_real());
Patrick McHardy76507f62008-01-31 04:38:38 -0800105 rcu_read_lock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106 return ct_get_idx(seq, *pos);
107}
108
109static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
110{
111 (*pos)++;
112 return ct_get_next(s, v);
113}
114
115static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800116 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117{
Patrick McHardy76507f62008-01-31 04:38:38 -0800118 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119}
120
Eric Paris1ae4de02010-10-13 16:25:00 -0400121#ifdef CONFIG_NF_CONNTRACK_SECMARK
122static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
123{
124 int ret;
125 u32 len;
126 char *secctx;
127
128 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
129 if (ret)
Pablo Neira Ayusocba85b52011-01-06 11:25:00 -0800130 return 0;
Eric Paris1ae4de02010-10-13 16:25:00 -0400131
132 ret = seq_printf(s, "secctx=%s ", secctx);
133
134 security_release_secctx(secctx, len);
135 return ret;
136}
137#else
138static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
139{
140 return 0;
141}
142#endif
143
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100144#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100145static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
146{
147 struct ct_iter_state *st = s->private;
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100148 struct nf_conn_tstamp *tstamp;
149 s64 delta_time;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100150
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100151 tstamp = nf_conn_tstamp_find(ct);
152 if (tstamp) {
153 delta_time = st->time_now - tstamp->start;
154 if (delta_time > 0)
155 delta_time = div_s64(delta_time, NSEC_PER_SEC);
156 else
157 delta_time = 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100158
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100159 return seq_printf(s, "delta-time=%llu ",
160 (unsigned long long)delta_time);
161 }
162 return 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100163}
164#else
165static inline int
166ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
167{
168 return 0;
169}
170#endif
171
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800172/* return 0 on success, 1 in case of error */
173static int ct_seq_show(struct seq_file *s, void *v)
174{
Eric Dumazetea781f12009-03-25 21:05:46 +0100175 struct nf_conntrack_tuple_hash *hash = v;
176 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800177 const struct nf_conntrack_l3proto *l3proto;
178 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100179 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180
Patrick McHardyc88130b2008-01-31 04:42:11 -0800181 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100182 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
183 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184
185 /* we only want to print DIR_ORIGINAL */
186 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100187 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200189 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800190 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200191 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100192 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193
Eric Dumazetea781f12009-03-25 21:05:46 +0100194 ret = -ENOSPC;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200196 l3proto->name, nf_ct_l3num(ct),
197 l4proto->name, nf_ct_protonum(ct),
Patrick McHardyc88130b2008-01-31 04:42:11 -0800198 timer_pending(&ct->timeout)
199 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
Eric Dumazetea781f12009-03-25 21:05:46 +0100200 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201
Patrick McHardyc88130b2008-01-31 04:42:11 -0800202 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100203 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204
Patrick McHardyc88130b2008-01-31 04:42:11 -0800205 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100206 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100207 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800208
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700209 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100210 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211
Patrick McHardyc88130b2008-01-31 04:42:11 -0800212 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213 if (seq_printf(s, "[UNREPLIED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100214 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215
Patrick McHardyc88130b2008-01-31 04:42:11 -0800216 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100217 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100218 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700220 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100221 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222
Patrick McHardyc88130b2008-01-31 04:42:11 -0800223 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224 if (seq_printf(s, "[ASSURED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100225 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226
227#if defined(CONFIG_NF_CONNTRACK_MARK)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800228 if (seq_printf(s, "mark=%u ", ct->mark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100229 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230#endif
231
Eric Paris1ae4de02010-10-13 16:25:00 -0400232 if (ct_show_secctx(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100233 goto release;
James Morris7c9728c2006-06-09 00:31:46 -0700234
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100235#ifdef CONFIG_NF_CONNTRACK_ZONES
236 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
237 goto release;
238#endif
239
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100240 if (ct_show_delta_time(s, ct))
241 goto release;
242
Patrick McHardyc88130b2008-01-31 04:42:11 -0800243 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
Eric Dumazetea781f12009-03-25 21:05:46 +0100244 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900245
Eric Dumazetea781f12009-03-25 21:05:46 +0100246 ret = 0;
247release:
248 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700249 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800250}
251
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700252static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253 .start = ct_seq_start,
254 .next = ct_seq_next,
255 .stop = ct_seq_stop,
256 .show = ct_seq_show
257};
258
259static int ct_open(struct inode *inode, struct file *file)
260{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200261 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700262 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800263}
264
Arjan van de Venda7071d2007-02-12 00:55:36 -0800265static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800266 .owner = THIS_MODULE,
267 .open = ct_open,
268 .read = seq_read,
269 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200270 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271};
272
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800273static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
274{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200275 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800276 int cpu;
277
278 if (*pos == 0)
279 return SEQ_START_TOKEN;
280
Rusty Russell0f23174a2008-12-29 12:23:42 +0000281 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800282 if (!cpu_possible(cpu))
283 continue;
284 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200285 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286 }
287
288 return NULL;
289}
290
291static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
292{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200293 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294 int cpu;
295
Rusty Russell0f23174a2008-12-29 12:23:42 +0000296 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800297 if (!cpu_possible(cpu))
298 continue;
299 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200300 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301 }
302
303 return NULL;
304}
305
306static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
307{
308}
309
310static int ct_cpu_seq_show(struct seq_file *seq, void *v)
311{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200312 struct net *net = seq_file_net(seq);
313 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800314 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800315
316 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200317 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 -0800318 return 0;
319 }
320
321 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200322 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323 nr_conntracks,
324 st->searched,
325 st->found,
326 st->new,
327 st->invalid,
328 st->ignore,
329 st->delete,
330 st->delete_list,
331 st->insert,
332 st->insert_failed,
333 st->drop,
334 st->early_drop,
335 st->error,
336
337 st->expect_new,
338 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200339 st->expect_delete,
340 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800341 );
342 return 0;
343}
344
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700345static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346 .start = ct_cpu_seq_start,
347 .next = ct_cpu_seq_next,
348 .stop = ct_cpu_seq_stop,
349 .show = ct_cpu_seq_show,
350};
351
352static int ct_cpu_seq_open(struct inode *inode, struct file *file)
353{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200354 return seq_open_net(inode, file, &ct_cpu_seq_ops,
355 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356}
357
Arjan van de Venda7071d2007-02-12 00:55:36 -0800358static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800359 .owner = THIS_MODULE,
360 .open = ct_cpu_seq_open,
361 .read = seq_read,
362 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200363 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800364};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100365
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200366static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100367{
368 struct proc_dir_entry *pde;
369
Gao fengd4beaa62013-02-18 01:34:54 +0000370 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100371 if (!pde)
372 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700373
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200374 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700375 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100376 if (!pde)
377 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100378 return 0;
379
380out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000381 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100382out_nf_conntrack:
383 return -ENOMEM;
384}
385
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200386static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100387{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200388 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000389 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100390}
391#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200392static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100393{
394 return 0;
395}
396
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200397static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100398{
399}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200400#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401
402/* Sysctl support */
403
404#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800405/* Log invalid packets of a given protocol */
406static int log_invalid_proto_min = 0;
407static int log_invalid_proto_max = 255;
408
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700409static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800410
Joe Perchesfe2c6332013-06-11 23:04:25 -0700411static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800412 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800413 .procname = "nf_conntrack_max",
414 .data = &nf_conntrack_max,
415 .maxlen = sizeof(int),
416 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800417 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418 },
419 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200421 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800422 .maxlen = sizeof(int),
423 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800424 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425 },
426 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800427 .procname = "nf_conntrack_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800428 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800429 .maxlen = sizeof(unsigned int),
430 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800431 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 },
433 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700434 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200435 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700436 .maxlen = sizeof(unsigned int),
437 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800438 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700439 },
440 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800441 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200442 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443 .maxlen = sizeof(unsigned int),
444 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800445 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800446 .extra1 = &log_invalid_proto_min,
447 .extra2 = &log_invalid_proto_max,
448 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700449 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700450 .procname = "nf_conntrack_expect_max",
451 .data = &nf_ct_expect_max,
452 .maxlen = sizeof(int),
453 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800454 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700455 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800456 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800457};
458
459#define NET_NF_CONNTRACK_MAX 2089
460
Joe Perchesfe2c6332013-06-11 23:04:25 -0700461static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800463 .procname = "nf_conntrack_max",
464 .data = &nf_conntrack_max,
465 .maxlen = sizeof(int),
466 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800467 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800469 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470};
471
Alexey Dobriyan80250702008-10-08 11:35:08 +0200472static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100473{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200474 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700475
Alexey Dobriyan80250702008-10-08 11:35:08 +0200476 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
477 GFP_KERNEL);
478 if (!table)
479 goto out_kmemdup;
480
481 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800482 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200483 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200484 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200485
Eric W. Biederman464dc802012-11-16 03:02:59 +0000486 /* Don't export sysctls to unprivileged users */
487 if (net->user_ns != &init_user_ns)
488 table[0].procname = NULL;
489
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000490 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200491 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700492 goto out_unregister_netfilter;
493
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100494 return 0;
495
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700496out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200497 kfree(table);
498out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700499 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100500}
501
Alexey Dobriyan80250702008-10-08 11:35:08 +0200502static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100503{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200504 struct ctl_table *table;
505
Alexey Dobriyan80250702008-10-08 11:35:08 +0200506 table = net->ct.sysctl_header->ctl_table_arg;
507 unregister_net_sysctl_table(net->ct.sysctl_header);
508 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100509}
510#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200511static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100512{
513 return 0;
514}
515
Alexey Dobriyan80250702008-10-08 11:35:08 +0200516static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100517{
518}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519#endif /* CONFIG_SYSCTL */
520
Gao fengf94161c2013-01-21 22:10:24 +0000521static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200522{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200523 int ret;
524
Gao fengf94161c2013-01-21 22:10:24 +0000525 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200526 if (ret < 0)
527 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000528
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200529 ret = nf_conntrack_standalone_init_proc(net);
530 if (ret < 0)
531 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000532
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200533 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200534 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200535 ret = nf_conntrack_standalone_init_sysctl(net);
536 if (ret < 0)
537 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000538
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200539 return 0;
540
Alexey Dobriyan80250702008-10-08 11:35:08 +0200541out_sysctl:
542 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200543out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000544 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200545out_init:
546 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200547}
548
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000549static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200550{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000551 struct net *net;
552
553 list_for_each_entry(net, net_exit_list, exit_list) {
554 nf_conntrack_standalone_fini_sysctl(net);
555 nf_conntrack_standalone_fini_proc(net);
556 }
557 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200558}
559
560static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000561 .init = nf_conntrack_pernet_init,
562 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200563};
564
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800565static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800566{
Gao fengf94161c2013-01-21 22:10:24 +0000567 int ret = nf_conntrack_init_start();
568 if (ret < 0)
569 goto out_start;
570
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100571#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000572 nf_ct_netfilter_header =
573 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100574 if (!nf_ct_netfilter_header) {
575 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000576 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000577 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100578 }
579#endif
Gao fengf94161c2013-01-21 22:10:24 +0000580
581 ret = register_pernet_subsys(&nf_conntrack_net_ops);
582 if (ret < 0)
583 goto out_pernet;
584
585 nf_conntrack_init_end();
586 return 0;
587
588out_pernet:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100589#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000590 unregister_net_sysctl_table(nf_ct_netfilter_header);
591out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100592#endif
Gao fengf94161c2013-01-21 22:10:24 +0000593 nf_conntrack_cleanup_end();
594out_start:
595 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800596}
597
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800598static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800599{
Gao fengf94161c2013-01-21 22:10:24 +0000600 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200601 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100602#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000603 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100604#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100605 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800606}
607
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800608module_init(nf_conntrack_standalone_init);
609module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800610
611/* Some modules need us, but don't depend directly on any symbol.
612 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800613void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800614{
615}
Patrick McHardy13b18332006-12-02 22:11:25 -0800616EXPORT_SYMBOL_GPL(need_conntrack);