blob: 24a42efe62ef630ba48496b20a3a2bdfdb482d9e [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>
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/proc_fs.h>
14#include <linux/seq_file.h>
15#include <linux/percpu.h>
16#include <linux/netdevice.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020017#include <net/net_namespace.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080018#ifdef CONFIG_SYSCTL
19#include <linux/sysctl.h>
20#endif
21
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080022#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010023#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010025#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010026#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027#include <net/netfilter/nf_conntrack_helper.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070028#include <net/netfilter/nf_conntrack_acct.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010029#include <net/netfilter/nf_conntrack_zones.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031MODULE_LICENSE("GPL");
32
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033#ifdef CONFIG_PROC_FS
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010034int
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
Jan Engelhardt32948582008-01-31 04:53:24 -080036 const struct nf_conntrack_l3proto *l3proto,
37 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038{
Martin Josefsson605dcad2006-11-29 02:35:06 +010039 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010041EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020044 struct seq_net_private p;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045 unsigned int bucket;
46};
47
Eric Dumazetea781f12009-03-25 21:05:46 +010048static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020050 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010052 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053
54 for (st->bucket = 0;
Patrick McHardyd696c7b2010-02-08 11:18:07 -080055 st->bucket < net->ct.htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 st->bucket++) {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020057 n = rcu_dereference(net->ct.hash[st->bucket].first);
Eric Dumazetea781f12009-03-25 21:05:46 +010058 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080059 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060 }
61 return NULL;
62}
63
Eric Dumazetea781f12009-03-25 21:05:46 +010064static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
65 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020067 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068 struct ct_iter_state *st = seq->private;
69
Patrick McHardy76507f62008-01-31 04:38:38 -080070 head = rcu_dereference(head->next);
Eric Dumazetea781f12009-03-25 21:05:46 +010071 while (is_a_nulls(head)) {
72 if (likely(get_nulls_value(head) == st->bucket)) {
Patrick McHardyd696c7b2010-02-08 11:18:07 -080073 if (++st->bucket >= net->ct.htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010074 return NULL;
75 }
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020076 head = rcu_dereference(net->ct.hash[st->bucket].first);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077 }
78 return head;
79}
80
Eric Dumazetea781f12009-03-25 21:05:46 +010081static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082{
Eric Dumazetea781f12009-03-25 21:05:46 +010083 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080084
85 if (head)
86 while (pos && (head = ct_get_next(seq, head)))
87 pos--;
88 return pos ? NULL : head;
89}
90
91static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -080092 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080093{
Patrick McHardy76507f62008-01-31 04:38:38 -080094 rcu_read_lock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 return ct_get_idx(seq, *pos);
96}
97
98static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
99{
100 (*pos)++;
101 return ct_get_next(s, v);
102}
103
104static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800105 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106{
Patrick McHardy76507f62008-01-31 04:38:38 -0800107 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108}
109
110/* return 0 on success, 1 in case of error */
111static int ct_seq_show(struct seq_file *s, void *v)
112{
Eric Dumazetea781f12009-03-25 21:05:46 +0100113 struct nf_conntrack_tuple_hash *hash = v;
114 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800115 const struct nf_conntrack_l3proto *l3proto;
116 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100117 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800118
Patrick McHardyc88130b2008-01-31 04:42:11 -0800119 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100120 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
121 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122
123 /* we only want to print DIR_ORIGINAL */
124 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100125 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800126
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200127 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800128 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200129 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100130 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800131
Eric Dumazetea781f12009-03-25 21:05:46 +0100132 ret = -ENOSPC;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200134 l3proto->name, nf_ct_l3num(ct),
135 l4proto->name, nf_ct_protonum(ct),
Patrick McHardyc88130b2008-01-31 04:42:11 -0800136 timer_pending(&ct->timeout)
137 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
Eric Dumazetea781f12009-03-25 21:05:46 +0100138 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139
Patrick McHardyc88130b2008-01-31 04:42:11 -0800140 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
Eric Dumazetea781f12009-03-25 21:05:46 +0100141 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800142
Patrick McHardyc88130b2008-01-31 04:42:11 -0800143 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100144 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100145 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700147 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100148 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149
Patrick McHardyc88130b2008-01-31 04:42:11 -0800150 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 if (seq_printf(s, "[UNREPLIED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100152 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153
Patrick McHardyc88130b2008-01-31 04:42:11 -0800154 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100155 l3proto, l4proto))
Eric Dumazetea781f12009-03-25 21:05:46 +0100156 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700158 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100159 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160
Patrick McHardyc88130b2008-01-31 04:42:11 -0800161 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 if (seq_printf(s, "[ASSURED] "))
Eric Dumazetea781f12009-03-25 21:05:46 +0100163 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164
165#if defined(CONFIG_NF_CONNTRACK_MARK)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800166 if (seq_printf(s, "mark=%u ", ct->mark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100167 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168#endif
169
James Morris7c9728c2006-06-09 00:31:46 -0700170#ifdef CONFIG_NF_CONNTRACK_SECMARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800171 if (seq_printf(s, "secmark=%u ", ct->secmark))
Eric Dumazetea781f12009-03-25 21:05:46 +0100172 goto release;
James Morris7c9728c2006-06-09 00:31:46 -0700173#endif
174
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100175#ifdef CONFIG_NF_CONNTRACK_ZONES
176 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
177 goto release;
178#endif
179
Patrick McHardyc88130b2008-01-31 04:42:11 -0800180 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
Eric Dumazetea781f12009-03-25 21:05:46 +0100181 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900182
Eric Dumazetea781f12009-03-25 21:05:46 +0100183 ret = 0;
184release:
185 nf_ct_put(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186 return 0;
187}
188
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700189static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800190 .start = ct_seq_start,
191 .next = ct_seq_next,
192 .stop = ct_seq_stop,
193 .show = ct_seq_show
194};
195
196static int ct_open(struct inode *inode, struct file *file)
197{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200198 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700199 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200}
201
Arjan van de Venda7071d2007-02-12 00:55:36 -0800202static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 .owner = THIS_MODULE,
204 .open = ct_open,
205 .read = seq_read,
206 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200207 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800208};
209
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
211{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200212 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213 int cpu;
214
215 if (*pos == 0)
216 return SEQ_START_TOKEN;
217
Rusty Russell0f23174a2008-12-29 12:23:42 +0000218 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 if (!cpu_possible(cpu))
220 continue;
221 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200222 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 }
224
225 return NULL;
226}
227
228static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
229{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200230 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800231 int cpu;
232
Rusty Russell0f23174a2008-12-29 12:23:42 +0000233 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800234 if (!cpu_possible(cpu))
235 continue;
236 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200237 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800238 }
239
240 return NULL;
241}
242
243static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
244{
245}
246
247static int ct_cpu_seq_show(struct seq_file *seq, void *v)
248{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200249 struct net *net = seq_file_net(seq);
250 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800251 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800252
253 if (v == SEQ_START_TOKEN) {
254 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\n");
255 return 0;
256 }
257
258 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
259 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
260 nr_conntracks,
261 st->searched,
262 st->found,
263 st->new,
264 st->invalid,
265 st->ignore,
266 st->delete,
267 st->delete_list,
268 st->insert,
269 st->insert_failed,
270 st->drop,
271 st->early_drop,
272 st->error,
273
274 st->expect_new,
275 st->expect_create,
276 st->expect_delete
277 );
278 return 0;
279}
280
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700281static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800282 .start = ct_cpu_seq_start,
283 .next = ct_cpu_seq_next,
284 .stop = ct_cpu_seq_stop,
285 .show = ct_cpu_seq_show,
286};
287
288static int ct_cpu_seq_open(struct inode *inode, struct file *file)
289{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200290 return seq_open_net(inode, file, &ct_cpu_seq_ops,
291 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292}
293
Arjan van de Venda7071d2007-02-12 00:55:36 -0800294static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295 .owner = THIS_MODULE,
296 .open = ct_cpu_seq_open,
297 .read = seq_read,
298 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200299 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100301
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200302static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100303{
304 struct proc_dir_entry *pde;
305
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200306 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100307 if (!pde)
308 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700309
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200310 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700311 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100312 if (!pde)
313 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100314 return 0;
315
316out_stat_nf_conntrack:
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200317 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100318out_nf_conntrack:
319 return -ENOMEM;
320}
321
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200322static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100323{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200324 remove_proc_entry("nf_conntrack", net->proc_net_stat);
325 proc_net_remove(net, "nf_conntrack");
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100326}
327#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200328static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100329{
330 return 0;
331}
332
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200333static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100334{
335}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336#endif /* CONFIG_PROC_FS */
337
338/* Sysctl support */
339
340#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800341/* Log invalid packets of a given protocol */
342static int log_invalid_proto_min = 0;
343static int log_invalid_proto_max = 255;
344
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700345static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346
347static ctl_table nf_ct_sysctl_table[] = {
348 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349 .procname = "nf_conntrack_max",
350 .data = &nf_conntrack_max,
351 .maxlen = sizeof(int),
352 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800353 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800354 },
355 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200357 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 .maxlen = sizeof(int),
359 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800360 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800361 },
362 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 .procname = "nf_conntrack_buckets",
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800364 .data = &init_net.ct.htable_size,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 .maxlen = sizeof(unsigned int),
366 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800367 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368 },
369 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700370 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200371 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700372 .maxlen = sizeof(unsigned int),
373 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800374 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700375 },
376 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200378 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379 .maxlen = sizeof(unsigned int),
380 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800381 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 .extra1 = &log_invalid_proto_min,
383 .extra2 = &log_invalid_proto_max,
384 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700385 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700386 .procname = "nf_conntrack_expect_max",
387 .data = &nf_ct_expect_max,
388 .maxlen = sizeof(int),
389 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800390 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700391 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800392 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800393};
394
395#define NET_NF_CONNTRACK_MAX 2089
396
397static ctl_table nf_ct_netfilter_table[] = {
398 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800399 .procname = "nf_conntrack_max",
400 .data = &nf_conntrack_max,
401 .maxlen = sizeof(int),
402 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800403 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800405 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406};
407
Patrick McHardy9e232492008-01-31 04:54:45 -0800408static struct ctl_path nf_ct_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800409 { .procname = "net", },
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -0800410 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411};
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -0800412
Alexey Dobriyan80250702008-10-08 11:35:08 +0200413static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100414{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200415 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700416
Alexey Dobriyan80250702008-10-08 11:35:08 +0200417 if (net_eq(net, &init_net)) {
418 nf_ct_netfilter_header =
419 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
420 if (!nf_ct_netfilter_header)
421 goto out;
422 }
423
424 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
425 GFP_KERNEL);
426 if (!table)
427 goto out_kmemdup;
428
429 table[1].data = &net->ct.count;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800430 table[2].data = &net->ct.htable_size;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200431 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200432 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200433
434 net->ct.sysctl_header = register_net_sysctl_table(net,
435 nf_net_netfilter_sysctl_path, table);
436 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700437 goto out_unregister_netfilter;
438
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100439 return 0;
440
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700441out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200442 kfree(table);
443out_kmemdup:
444 if (net_eq(net, &init_net))
445 unregister_sysctl_table(nf_ct_netfilter_header);
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700446out:
447 printk("nf_conntrack: can't register to sysctl.\n");
448 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100449}
450
Alexey Dobriyan80250702008-10-08 11:35:08 +0200451static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100452{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200453 struct ctl_table *table;
454
455 if (net_eq(net, &init_net))
456 unregister_sysctl_table(nf_ct_netfilter_header);
457 table = net->ct.sysctl_header->ctl_table_arg;
458 unregister_net_sysctl_table(net->ct.sysctl_header);
459 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100460}
461#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200462static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100463{
464 return 0;
465}
466
Alexey Dobriyan80250702008-10-08 11:35:08 +0200467static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100468{
469}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470#endif /* CONFIG_SYSCTL */
471
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200472static int nf_conntrack_net_init(struct net *net)
473{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200474 int ret;
475
476 ret = nf_conntrack_init(net);
477 if (ret < 0)
478 goto out_init;
479 ret = nf_conntrack_standalone_init_proc(net);
480 if (ret < 0)
481 goto out_proc;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200482 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200483 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200484 ret = nf_conntrack_standalone_init_sysctl(net);
485 if (ret < 0)
486 goto out_sysctl;
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200487 return 0;
488
Alexey Dobriyan80250702008-10-08 11:35:08 +0200489out_sysctl:
490 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200491out_proc:
492 nf_conntrack_cleanup(net);
493out_init:
494 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200495}
496
497static void nf_conntrack_net_exit(struct net *net)
498{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200499 nf_conntrack_standalone_fini_sysctl(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200500 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200501 nf_conntrack_cleanup(net);
502}
503
504static struct pernet_operations nf_conntrack_net_ops = {
505 .init = nf_conntrack_net_init,
506 .exit = nf_conntrack_net_exit,
507};
508
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800509static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800510{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200511 return register_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512}
513
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800514static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515{
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200516 unregister_pernet_subsys(&nf_conntrack_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800517}
518
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800519module_init(nf_conntrack_standalone_init);
520module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800521
522/* Some modules need us, but don't depend directly on any symbol.
523 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800524void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800525{
526}
Patrick McHardy13b18332006-12-02 22:11:25 -0800527EXPORT_SYMBOL_GPL(need_conntrack);