blob: 9b3943252a5e9c0b0dd6d2127606d11ac4e502b9 [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>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009#include <linux/types.h>
10#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/proc_fs.h>
15#include <linux/seq_file.h>
16#include <linux/percpu.h>
17#include <linux/netdevice.h>
Eric Paris1ae4de02010-10-13 16:25:00 -040018#include <linux/security.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020019#include <net/net_namespace.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#ifdef CONFIG_SYSCTL
21#include <linux/sysctl.h>
22#endif
23
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010025#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010027#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010028#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029#include <net/netfilter/nf_conntrack_helper.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070030#include <net/netfilter/nf_conntrack_acct.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010031#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010032#include <net/netfilter/nf_conntrack_timestamp.h>
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010033#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035MODULE_LICENSE("GPL");
36
Jan Engelhardt54b07dc2011-04-21 09:32:45 +020037#ifdef CONFIG_NF_CONNTRACK_PROCFS
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010038int
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
Jan Engelhardt32948582008-01-31 04:53:24 -080040 const struct nf_conntrack_l3proto *l3proto,
41 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042{
Martin Josefsson605dcad2006-11-29 02:35:06 +010043 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010045EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020048 struct seq_net_private p;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049 unsigned int bucket;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010050 u_int64_t time_now;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051};
52
Eric Dumazetea781f12009-03-25 21:05:46 +010053static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020055 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010057 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058
59 for (st->bucket = 0;
Patrick McHardyd696c7b2010-02-08 11:18:07 -080060 st->bucket < net->ct.htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061 st->bucket++) {
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010062 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
Eric Dumazetea781f12009-03-25 21:05:46 +010063 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080064 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080065 }
66 return NULL;
67}
68
Eric Dumazetea781f12009-03-25 21:05:46 +010069static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
70 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020072 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073 struct ct_iter_state *st = seq->private;
74
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010075 head = rcu_dereference(hlist_nulls_next_rcu(head));
Eric Dumazetea781f12009-03-25 21:05:46 +010076 while (is_a_nulls(head)) {
77 if (likely(get_nulls_value(head) == st->bucket)) {
Patrick McHardyd696c7b2010-02-08 11:18:07 -080078 if (++st->bucket >= net->ct.htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010079 return NULL;
80 }
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010081 head = rcu_dereference(
82 hlist_nulls_first_rcu(
83 &net->ct.hash[st->bucket]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080084 }
85 return head;
86}
87
Eric Dumazetea781f12009-03-25 21:05:46 +010088static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089{
Eric Dumazetea781f12009-03-25 21:05:46 +010090 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091
92 if (head)
93 while (pos && (head = ct_get_next(seq, head)))
94 pos--;
95 return pos ? NULL : head;
96}
97
98static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -080099 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800100{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100101 struct ct_iter_state *st = seq->private;
102
103 st->time_now = ktime_to_ns(ktime_get_real());
Patrick McHardy76507f62008-01-31 04:38:38 -0800104 rcu_read_lock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800105 return ct_get_idx(seq, *pos);
106}
107
108static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
109{
110 (*pos)++;
111 return ct_get_next(s, v);
112}
113
114static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800115 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800116{
Patrick McHardy76507f62008-01-31 04:38:38 -0800117 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800118}
119
Eric Paris1ae4de02010-10-13 16:25:00 -0400120#ifdef CONFIG_NF_CONNTRACK_SECMARK
121static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
122{
123 int ret;
124 u32 len;
125 char *secctx;
126
127 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
128 if (ret)
Pablo Neira Ayusocba85b52011-01-06 11:25:00 -0800129 return 0;
Eric Paris1ae4de02010-10-13 16:25:00 -0400130
131 ret = seq_printf(s, "secctx=%s ", secctx);
132
133 security_release_secctx(secctx, len);
134 return ret;
135}
136#else
137static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
138{
139 return 0;
140}
141#endif
142
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100143#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100144static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
145{
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
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100158 return seq_printf(s, "delta-time=%llu ",
159 (unsigned long long)delta_time);
160 }
161 return 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100162}
163#else
164static inline int
165ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
166{
167 return 0;
168}
169#endif
170
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171/* return 0 on success, 1 in case of error */
172static int ct_seq_show(struct seq_file *s, void *v)
173{
Eric Dumazetea781f12009-03-25 21:05:46 +0100174 struct nf_conntrack_tuple_hash *hash = v;
175 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800176 const struct nf_conntrack_l3proto *l3proto;
177 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100178 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179
Patrick McHardyc88130b2008-01-31 04:42:11 -0800180 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100181 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
182 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183
184 /* we only want to print DIR_ORIGINAL */
185 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100186 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200188 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200190 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100191 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192
Eric Dumazetea781f12009-03-25 21:05:46 +0100193 ret = -ENOSPC;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200195 l3proto->name, nf_ct_l3num(ct),
196 l4proto->name, nf_ct_protonum(ct),
Patrick McHardyc88130b2008-01-31 04:42:11 -0800197 timer_pending(&ct->timeout)
198 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
Eric Dumazetea781f12009-03-25 21:05:46 +0100199 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200
Patrick McHardyc88130b2008-01-31 04:42:11 -0800201 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100202 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203
Patrick McHardyc88130b2008-01-31 04:42:11 -0800204 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100205 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100206 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207
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)))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212 if (seq_printf(s, "[UNREPLIED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100213 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214
Patrick McHardyc88130b2008-01-31 04:42:11 -0800215 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100216 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100217 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800218
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700219 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
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_ASSURED_BIT, &ct->status))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 if (seq_printf(s, "[ASSURED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100224 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225
226#if defined(CONFIG_NF_CONNTRACK_MARK)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800227 if (seq_printf(s, "mark=%u ", ct->mark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100228 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800229#endif
230
Eric Paris1ae4de02010-10-13 16:25:00 -0400231 if (ct_show_secctx(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100232 goto release;
James Morris7c9728c2006-06-09 00:31:46 -0700233
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100234#ifdef CONFIG_NF_CONNTRACK_ZONES
235 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
236 goto release;
237#endif
238
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100239 if (ct_show_delta_time(s, ct))
240 goto release;
241
Patrick McHardyc88130b2008-01-31 04:42:11 -0800242 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
Eric Dumazetea781f12009-03-25 21:05:46 +0100243 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900244
Eric Dumazetea781f12009-03-25 21:05:46 +0100245 ret = 0;
246release:
247 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700248 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800249}
250
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700251static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800252 .start = ct_seq_start,
253 .next = ct_seq_next,
254 .stop = ct_seq_stop,
255 .show = ct_seq_show
256};
257
258static int ct_open(struct inode *inode, struct file *file)
259{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200260 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700261 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262}
263
Arjan van de Venda7071d2007-02-12 00:55:36 -0800264static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800265 .owner = THIS_MODULE,
266 .open = ct_open,
267 .read = seq_read,
268 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200269 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270};
271
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800272static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
273{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200274 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800275 int cpu;
276
277 if (*pos == 0)
278 return SEQ_START_TOKEN;
279
Rusty Russell0f23174a2008-12-29 12:23:42 +0000280 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800281 if (!cpu_possible(cpu))
282 continue;
283 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200284 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800285 }
286
287 return NULL;
288}
289
290static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
291{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200292 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800293 int cpu;
294
Rusty Russell0f23174a2008-12-29 12:23:42 +0000295 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800296 if (!cpu_possible(cpu))
297 continue;
298 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200299 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300 }
301
302 return NULL;
303}
304
305static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
306{
307}
308
309static int ct_cpu_seq_show(struct seq_file *seq, void *v)
310{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200311 struct net *net = seq_file_net(seq);
312 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800313 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800314
315 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200316 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 -0800317 return 0;
318 }
319
320 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200321 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322 nr_conntracks,
323 st->searched,
324 st->found,
325 st->new,
326 st->invalid,
327 st->ignore,
328 st->delete,
329 st->delete_list,
330 st->insert,
331 st->insert_failed,
332 st->drop,
333 st->early_drop,
334 st->error,
335
336 st->expect_new,
337 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200338 st->expect_delete,
339 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800340 );
341 return 0;
342}
343
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700344static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345 .start = ct_cpu_seq_start,
346 .next = ct_cpu_seq_next,
347 .stop = ct_cpu_seq_stop,
348 .show = ct_cpu_seq_show,
349};
350
351static int ct_cpu_seq_open(struct inode *inode, struct file *file)
352{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200353 return seq_open_net(inode, file, &ct_cpu_seq_ops,
354 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355}
356
Arjan van de Venda7071d2007-02-12 00:55:36 -0800357static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 .owner = THIS_MODULE,
359 .open = ct_cpu_seq_open,
360 .read = seq_read,
361 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200362 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100364
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200365static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100366{
367 struct proc_dir_entry *pde;
368
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200369 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100370 if (!pde)
371 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700372
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200373 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700374 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100375 if (!pde)
376 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100377 return 0;
378
379out_stat_nf_conntrack:
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200380 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100381out_nf_conntrack:
382 return -ENOMEM;
383}
384
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200385static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100386{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200387 remove_proc_entry("nf_conntrack", net->proc_net_stat);
388 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100389}
390#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200391static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100392{
393 return 0;
394}
395
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200396static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100397{
398}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200399#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400
401/* Sysctl support */
402
403#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404/* Log invalid packets of a given protocol */
405static int log_invalid_proto_min = 0;
406static int log_invalid_proto_max = 255;
407
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700408static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409
410static ctl_table nf_ct_sysctl_table[] = {
411 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800412 .procname = "nf_conntrack_max",
413 .data = &nf_conntrack_max,
414 .maxlen = sizeof(int),
415 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800416 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 },
418 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800419 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200420 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421 .maxlen = sizeof(int),
422 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800423 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800424 },
425 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426 .procname = "nf_conntrack_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800427 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800428 .maxlen = sizeof(unsigned int),
429 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800430 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800431 },
432 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700433 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200434 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700435 .maxlen = sizeof(unsigned int),
436 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800437 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700438 },
439 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200441 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442 .maxlen = sizeof(unsigned int),
443 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800444 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445 .extra1 = &log_invalid_proto_min,
446 .extra2 = &log_invalid_proto_max,
447 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700448 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700449 .procname = "nf_conntrack_expect_max",
450 .data = &nf_ct_expect_max,
451 .maxlen = sizeof(int),
452 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800453 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700454 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800455 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456};
457
458#define NET_NF_CONNTRACK_MAX 2089
459
460static ctl_table nf_ct_netfilter_table[] = {
461 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462 .procname = "nf_conntrack_max",
463 .data = &nf_conntrack_max,
464 .maxlen = sizeof(int),
465 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800466 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800468 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800469};
470
Alexey Dobriyan80250702008-10-08 11:35:08 +0200471static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100472{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200473 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700474
Alexey Dobriyan80250702008-10-08 11:35:08 +0200475 if (net_eq(net, &init_net)) {
476 nf_ct_netfilter_header =
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000477 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200478 if (!nf_ct_netfilter_header)
479 goto out;
480 }
481
482 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
483 GFP_KERNEL);
484 if (!table)
485 goto out_kmemdup;
486
487 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800488 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200489 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200490 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200491
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000492 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200493 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700494 goto out_unregister_netfilter;
495
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100496 return 0;
497
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700498out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200499 kfree(table);
500out_kmemdup:
501 if (net_eq(net, &init_net))
Eric W. Biederman5dd3df12012-04-19 13:24:33 +0000502 unregister_net_sysctl_table(nf_ct_netfilter_header);
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700503out:
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200504 printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700505 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100506}
507
Alexey Dobriyan80250702008-10-08 11:35:08 +0200508static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100509{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200510 struct ctl_table *table;
511
512 if (net_eq(net, &init_net))
Eric W. Biederman5dd3df12012-04-19 13:24:33 +0000513 unregister_net_sysctl_table(nf_ct_netfilter_header);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200514 table = net->ct.sysctl_header->ctl_table_arg;
515 unregister_net_sysctl_table(net->ct.sysctl_header);
516 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100517}
518#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200519static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100520{
521 return 0;
522}
523
Alexey Dobriyan80250702008-10-08 11:35:08 +0200524static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100525{
526}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800527#endif /* CONFIG_SYSCTL */
528
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200529static int nf_conntrack_net_init(struct net *net)
530{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200531 int ret;
532
533 ret = nf_conntrack_init(net);
534 if (ret < 0)
535 goto out_init;
536 ret = nf_conntrack_standalone_init_proc(net);
537 if (ret < 0)
538 goto out_proc;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200539 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200540 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200541 ret = nf_conntrack_standalone_init_sysctl(net);
542 if (ret < 0)
543 goto out_sysctl;
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200544 return 0;
545
Alexey Dobriyan80250702008-10-08 11:35:08 +0200546out_sysctl:
547 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200548out_proc:
549 nf_conntrack_cleanup(net);
550out_init:
551 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200552}
553
554static void nf_conntrack_net_exit(struct net *net)
555{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200556 nf_conntrack_standalone_fini_sysctl(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200557 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200558 nf_conntrack_cleanup(net);
559}
560
561static struct pernet_operations nf_conntrack_net_ops = {
562 .init = nf_conntrack_net_init,
563 .exit = nf_conntrack_net_exit,
564};
565
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800566static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800567{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200568 return register_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569}
570
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800571static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800572{
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200573 unregister_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800574}
575
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800576module_init(nf_conntrack_standalone_init);
577module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578
579/* Some modules need us, but don't depend directly on any symbol.
580 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800581void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800582{
583}
Patrick McHardy13b18332006-12-02 22:11:25 -0800584EXPORT_SYMBOL_GPL(need_conntrack);