blob: eb973fcd67ab4273a3cdee566a5f4a4fb44a24f2 [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 W. Biederman457c4cb2007-09-12 12:01:34 +020018#include <net/net_namespace.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080019#ifdef CONFIG_SYSCTL
20#include <linux/sysctl.h>
21#endif
22
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010024#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010026#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010027#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028#include <net/netfilter/nf_conntrack_helper.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070029#include <net/netfilter/nf_conntrack_acct.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010030#include <net/netfilter/nf_conntrack_zones.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032MODULE_LICENSE("GPL");
33
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034#ifdef CONFIG_PROC_FS
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010035int
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
Jan Engelhardt32948582008-01-31 04:53:24 -080037 const struct nf_conntrack_l3proto *l3proto,
38 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039{
Martin Josefsson605dcad2006-11-29 02:35:06 +010040 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010042EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020045 struct seq_net_private p;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046 unsigned int bucket;
47};
48
Eric Dumazetea781f12009-03-25 21:05:46 +010049static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020051 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010053 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054
55 for (st->bucket = 0;
Patrick McHardyd696c7b2010-02-08 11:18:07 -080056 st->bucket < net->ct.htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 st->bucket++) {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020058 n = rcu_dereference(net->ct.hash[st->bucket].first);
Eric Dumazetea781f12009-03-25 21:05:46 +010059 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080060 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061 }
62 return NULL;
63}
64
Eric Dumazetea781f12009-03-25 21:05:46 +010065static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
66 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020068 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069 struct ct_iter_state *st = seq->private;
70
Patrick McHardy76507f62008-01-31 04:38:38 -080071 head = rcu_dereference(head->next);
Eric Dumazetea781f12009-03-25 21:05:46 +010072 while (is_a_nulls(head)) {
73 if (likely(get_nulls_value(head) == st->bucket)) {
Patrick McHardyd696c7b2010-02-08 11:18:07 -080074 if (++st->bucket >= net->ct.htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010075 return NULL;
76 }
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020077 head = rcu_dereference(net->ct.hash[st->bucket].first);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078 }
79 return head;
80}
81
Eric Dumazetea781f12009-03-25 21:05:46 +010082static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083{
Eric Dumazetea781f12009-03-25 21:05:46 +010084 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080085
86 if (head)
87 while (pos && (head = ct_get_next(seq, head)))
88 pos--;
89 return pos ? NULL : head;
90}
91
92static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -080093 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094{
Patrick McHardy76507f62008-01-31 04:38:38 -080095 rcu_read_lock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080096 return ct_get_idx(seq, *pos);
97}
98
99static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
100{
101 (*pos)++;
102 return ct_get_next(s, v);
103}
104
105static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800106 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107{
Patrick McHardy76507f62008-01-31 04:38:38 -0800108 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109}
110
111/* return 0 on success, 1 in case of error */
112static int ct_seq_show(struct seq_file *s, void *v)
113{
Eric Dumazetea781f12009-03-25 21:05:46 +0100114 struct nf_conntrack_tuple_hash *hash = v;
115 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800116 const struct nf_conntrack_l3proto *l3proto;
117 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100118 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119
Patrick McHardyc88130b2008-01-31 04:42:11 -0800120 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100121 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
122 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123
124 /* we only want to print DIR_ORIGINAL */
125 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100126 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200128 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800129 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200130 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100131 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132
Eric Dumazetea781f12009-03-25 21:05:46 +0100133 ret = -ENOSPC;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800134 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200135 l3proto->name, nf_ct_l3num(ct),
136 l4proto->name, nf_ct_protonum(ct),
Patrick McHardyc88130b2008-01-31 04:42:11 -0800137 timer_pending(&ct->timeout)
138 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
Eric Dumazetea781f12009-03-25 21:05:46 +0100139 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800140
Patrick McHardyc88130b2008-01-31 04:42:11 -0800141 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100142 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800143
Patrick McHardyc88130b2008-01-31 04:42:11 -0800144 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100145 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100146 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700148 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100149 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150
Patrick McHardyc88130b2008-01-31 04:42:11 -0800151 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800152 if (seq_printf(s, "[UNREPLIED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100153 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154
Patrick McHardyc88130b2008-01-31 04:42:11 -0800155 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100157 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700159 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100160 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161
Patrick McHardyc88130b2008-01-31 04:42:11 -0800162 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163 if (seq_printf(s, "[ASSURED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100164 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800165
166#if defined(CONFIG_NF_CONNTRACK_MARK)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800167 if (seq_printf(s, "mark=%u ", ct->mark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100168 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169#endif
170
James Morris7c9728c2006-06-09 00:31:46 -0700171#ifdef CONFIG_NF_CONNTRACK_SECMARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800172 if (seq_printf(s, "secmark=%u ", ct->secmark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100173 goto release;
James Morris7c9728c2006-06-09 00:31:46 -0700174#endif
175
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100176#ifdef CONFIG_NF_CONNTRACK_ZONES
177 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
178 goto release;
179#endif
180
Patrick McHardyc88130b2008-01-31 04:42:11 -0800181 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
Eric Dumazetea781f12009-03-25 21:05:46 +0100182 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900183
Eric Dumazetea781f12009-03-25 21:05:46 +0100184 ret = 0;
185release:
186 nf_ct_put(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187 return 0;
188}
189
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700190static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191 .start = ct_seq_start,
192 .next = ct_seq_next,
193 .stop = ct_seq_stop,
194 .show = ct_seq_show
195};
196
197static int ct_open(struct inode *inode, struct file *file)
198{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200199 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700200 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201}
202
Arjan van de Venda7071d2007-02-12 00:55:36 -0800203static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204 .owner = THIS_MODULE,
205 .open = ct_open,
206 .read = seq_read,
207 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200208 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209};
210
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
212{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200213 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 int cpu;
215
216 if (*pos == 0)
217 return SEQ_START_TOKEN;
218
Rusty Russell0f23174a2008-12-29 12:23:42 +0000219 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 if (!cpu_possible(cpu))
221 continue;
222 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200223 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224 }
225
226 return NULL;
227}
228
229static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
230{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200231 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232 int cpu;
233
Rusty Russell0f23174a2008-12-29 12:23:42 +0000234 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235 if (!cpu_possible(cpu))
236 continue;
237 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200238 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239 }
240
241 return NULL;
242}
243
244static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
245{
246}
247
248static int ct_cpu_seq_show(struct seq_file *seq, void *v)
249{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200250 struct net *net = seq_file_net(seq);
251 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800252 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253
254 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200255 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 -0800256 return 0;
257 }
258
259 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200260 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800261 nr_conntracks,
262 st->searched,
263 st->found,
264 st->new,
265 st->invalid,
266 st->ignore,
267 st->delete,
268 st->delete_list,
269 st->insert,
270 st->insert_failed,
271 st->drop,
272 st->early_drop,
273 st->error,
274
275 st->expect_new,
276 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200277 st->expect_delete,
278 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800279 );
280 return 0;
281}
282
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700283static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800284 .start = ct_cpu_seq_start,
285 .next = ct_cpu_seq_next,
286 .stop = ct_cpu_seq_stop,
287 .show = ct_cpu_seq_show,
288};
289
290static int ct_cpu_seq_open(struct inode *inode, struct file *file)
291{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200292 return seq_open_net(inode, file, &ct_cpu_seq_ops,
293 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294}
295
Arjan van de Venda7071d2007-02-12 00:55:36 -0800296static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800297 .owner = THIS_MODULE,
298 .open = ct_cpu_seq_open,
299 .read = seq_read,
300 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200301 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100303
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200304static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100305{
306 struct proc_dir_entry *pde;
307
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200308 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100309 if (!pde)
310 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700311
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200312 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700313 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100314 if (!pde)
315 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100316 return 0;
317
318out_stat_nf_conntrack:
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200319 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100320out_nf_conntrack:
321 return -ENOMEM;
322}
323
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200324static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100325{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200326 remove_proc_entry("nf_conntrack", net->proc_net_stat);
327 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100328}
329#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200330static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100331{
332 return 0;
333}
334
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200335static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100336{
337}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800338#endif /* CONFIG_PROC_FS */
339
340/* Sysctl support */
341
342#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343/* Log invalid packets of a given protocol */
344static int log_invalid_proto_min = 0;
345static int log_invalid_proto_max = 255;
346
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700347static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800348
349static ctl_table nf_ct_sysctl_table[] = {
350 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800351 .procname = "nf_conntrack_max",
352 .data = &nf_conntrack_max,
353 .maxlen = sizeof(int),
354 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800355 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 },
357 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200359 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800360 .maxlen = sizeof(int),
361 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800362 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 },
364 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 .procname = "nf_conntrack_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800366 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800367 .maxlen = sizeof(unsigned int),
368 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800369 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800370 },
371 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700372 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200373 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700374 .maxlen = sizeof(unsigned int),
375 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800376 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700377 },
378 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200380 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800381 .maxlen = sizeof(unsigned int),
382 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800383 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800384 .extra1 = &log_invalid_proto_min,
385 .extra2 = &log_invalid_proto_max,
386 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700387 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700388 .procname = "nf_conntrack_expect_max",
389 .data = &nf_ct_expect_max,
390 .maxlen = sizeof(int),
391 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800392 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700393 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800394 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395};
396
397#define NET_NF_CONNTRACK_MAX 2089
398
399static ctl_table nf_ct_netfilter_table[] = {
400 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401 .procname = "nf_conntrack_max",
402 .data = &nf_conntrack_max,
403 .maxlen = sizeof(int),
404 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800405 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800407 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800408};
409
Patrick McHardy9e232492008-01-31 04:54:45 -0800410static struct ctl_path nf_ct_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800411 { .procname = "net", },
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -0800412 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800413};
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -0800414
Alexey Dobriyan80250702008-10-08 11:35:08 +0200415static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100416{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200417 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700418
Alexey Dobriyan80250702008-10-08 11:35:08 +0200419 if (net_eq(net, &init_net)) {
420 nf_ct_netfilter_header =
421 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
422 if (!nf_ct_netfilter_header)
423 goto out;
424 }
425
426 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
427 GFP_KERNEL);
428 if (!table)
429 goto out_kmemdup;
430
431 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800432 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200433 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200434 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200435
436 net->ct.sysctl_header = register_net_sysctl_table(net,
437 nf_net_netfilter_sysctl_path, table);
438 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700439 goto out_unregister_netfilter;
440
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100441 return 0;
442
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700443out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200444 kfree(table);
445out_kmemdup:
446 if (net_eq(net, &init_net))
447 unregister_sysctl_table(nf_ct_netfilter_header);
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700448out:
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200449 printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700450 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100451}
452
Alexey Dobriyan80250702008-10-08 11:35:08 +0200453static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100454{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200455 struct ctl_table *table;
456
457 if (net_eq(net, &init_net))
458 unregister_sysctl_table(nf_ct_netfilter_header);
459 table = net->ct.sysctl_header->ctl_table_arg;
460 unregister_net_sysctl_table(net->ct.sysctl_header);
461 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100462}
463#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200464static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100465{
466 return 0;
467}
468
Alexey Dobriyan80250702008-10-08 11:35:08 +0200469static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100470{
471}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472#endif /* CONFIG_SYSCTL */
473
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200474static int nf_conntrack_net_init(struct net *net)
475{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200476 int ret;
477
478 ret = nf_conntrack_init(net);
479 if (ret < 0)
480 goto out_init;
481 ret = nf_conntrack_standalone_init_proc(net);
482 if (ret < 0)
483 goto out_proc;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200484 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200485 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200486 ret = nf_conntrack_standalone_init_sysctl(net);
487 if (ret < 0)
488 goto out_sysctl;
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200489 return 0;
490
Alexey Dobriyan80250702008-10-08 11:35:08 +0200491out_sysctl:
492 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200493out_proc:
494 nf_conntrack_cleanup(net);
495out_init:
496 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200497}
498
499static void nf_conntrack_net_exit(struct net *net)
500{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200501 nf_conntrack_standalone_fini_sysctl(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200502 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200503 nf_conntrack_cleanup(net);
504}
505
506static struct pernet_operations nf_conntrack_net_ops = {
507 .init = nf_conntrack_net_init,
508 .exit = nf_conntrack_net_exit,
509};
510
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800511static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200513 return register_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800514}
515
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800516static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800517{
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200518 unregister_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519}
520
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800521module_init(nf_conntrack_standalone_init);
522module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800523
524/* Some modules need us, but don't depend directly on any symbol.
525 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800526void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800527{
528}
Patrick McHardy13b18332006-12-02 22:11:25 -0800529EXPORT_SYMBOL_GPL(need_conntrack);