blob: 4e7becde4357bc934bc5d8aa6aa8159f5e1bb6c6 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02003 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08008 */
9
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#include <linux/types.h>
11#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/proc_fs.h>
16#include <linux/seq_file.h>
17#include <linux/percpu.h>
18#include <linux/netdevice.h>
Eric Paris1ae4de02010-10-13 16:25:00 -040019#include <linux/security.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020020#include <net/net_namespace.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021#ifdef CONFIG_SYSCTL
22#include <linux/sysctl.h>
23#endif
24
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010026#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010028#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010029#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030#include <net/netfilter/nf_conntrack_helper.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070031#include <net/netfilter/nf_conntrack_acct.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010032#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010033#include <net/netfilter/nf_conntrack_timestamp.h>
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010034#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036MODULE_LICENSE("GPL");
37
Jan Engelhardt54b07dc2011-04-21 09:32:45 +020038#ifdef CONFIG_NF_CONNTRACK_PROCFS
Joe Perches824f1fb2014-09-29 16:08:22 -070039void
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
Jan Engelhardt32948582008-01-31 04:53:24 -080041 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043{
Joe Perches824f1fb2014-09-29 16:08:22 -070044 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046}
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010047EXPORT_SYMBOL_GPL(print_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049struct ct_iter_state {
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +020050 struct seq_net_private p;
Liping Zhang64b87632016-07-03 13:18:43 +080051 struct hlist_nulls_head *hash;
52 unsigned int htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053 unsigned int bucket;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010054 u_int64_t time_now;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055};
56
Eric Dumazetea781f12009-03-25 21:05:46 +010057static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058{
59 struct ct_iter_state *st = seq->private;
Eric Dumazetea781f12009-03-25 21:05:46 +010060 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061
62 for (st->bucket = 0;
Liping Zhang64b87632016-07-03 13:18:43 +080063 st->bucket < st->htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064 st->bucket++) {
Liping Zhang64b87632016-07-03 13:18:43 +080065 n = rcu_dereference(
66 hlist_nulls_first_rcu(&st->hash[st->bucket]));
Eric Dumazetea781f12009-03-25 21:05:46 +010067 if (!is_a_nulls(n))
Patrick McHardy76507f62008-01-31 04:38:38 -080068 return n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069 }
70 return NULL;
71}
72
Eric Dumazetea781f12009-03-25 21:05:46 +010073static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
74 struct hlist_nulls_node *head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075{
76 struct ct_iter_state *st = seq->private;
77
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010078 head = rcu_dereference(hlist_nulls_next_rcu(head));
Eric Dumazetea781f12009-03-25 21:05:46 +010079 while (is_a_nulls(head)) {
80 if (likely(get_nulls_value(head) == st->bucket)) {
Liping Zhang64b87632016-07-03 13:18:43 +080081 if (++st->bucket >= st->htable_size)
Eric Dumazetea781f12009-03-25 21:05:46 +010082 return NULL;
83 }
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010084 head = rcu_dereference(
Liping Zhang64b87632016-07-03 13:18:43 +080085 hlist_nulls_first_rcu(&st->hash[st->bucket]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 }
87 return head;
88}
89
Eric Dumazetea781f12009-03-25 21:05:46 +010090static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091{
Eric Dumazetea781f12009-03-25 21:05:46 +010092 struct hlist_nulls_node *head = ct_get_first(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080093
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
98}
99
100static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
Patrick McHardy76507f62008-01-31 04:38:38 -0800101 __acquires(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100103 struct ct_iter_state *st = seq->private;
104
Eric Dumazetd2de8752014-08-22 18:32:09 -0700105 st->time_now = ktime_get_real_ns();
Patrick McHardy76507f62008-01-31 04:38:38 -0800106 rcu_read_lock();
Liping Zhang64b87632016-07-03 13:18:43 +0800107
108 nf_conntrack_get_ht(&st->hash, &st->htable_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109 return ct_get_idx(seq, *pos);
110}
111
112static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
113{
114 (*pos)++;
115 return ct_get_next(s, v);
116}
117
118static void ct_seq_stop(struct seq_file *s, void *v)
Patrick McHardy76507f62008-01-31 04:38:38 -0800119 __releases(RCU)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800120{
Patrick McHardy76507f62008-01-31 04:38:38 -0800121 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122}
123
Eric Paris1ae4de02010-10-13 16:25:00 -0400124#ifdef CONFIG_NF_CONNTRACK_SECMARK
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400125static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
Eric Paris1ae4de02010-10-13 16:25:00 -0400126{
127 int ret;
128 u32 len;
129 char *secctx;
130
131 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
132 if (ret)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400133 return;
Eric Paris1ae4de02010-10-13 16:25:00 -0400134
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400135 seq_printf(s, "secctx=%s ", secctx);
Eric Paris1ae4de02010-10-13 16:25:00 -0400136
137 security_release_secctx(secctx, len);
Eric Paris1ae4de02010-10-13 16:25:00 -0400138}
139#else
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400140static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
Eric Paris1ae4de02010-10-13 16:25:00 -0400141{
Eric Paris1ae4de02010-10-13 16:25:00 -0400142}
143#endif
144
Daniel Borkmann308ac912015-08-08 21:40:01 +0200145#ifdef CONFIG_NF_CONNTRACK_ZONES
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200146static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
147 int dir)
Daniel Borkmann308ac912015-08-08 21:40:01 +0200148{
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200149 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
150
151 if (zone->dir != dir)
152 return;
153 switch (zone->dir) {
154 case NF_CT_DEFAULT_ZONE_DIR:
155 seq_printf(s, "zone=%u ", zone->id);
156 break;
157 case NF_CT_ZONE_DIR_ORIG:
158 seq_printf(s, "zone-orig=%u ", zone->id);
159 break;
160 case NF_CT_ZONE_DIR_REPL:
161 seq_printf(s, "zone-reply=%u ", zone->id);
162 break;
163 default:
164 break;
165 }
Daniel Borkmann308ac912015-08-08 21:40:01 +0200166}
167#else
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200168static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
169 int dir)
Daniel Borkmann308ac912015-08-08 21:40:01 +0200170{
171}
172#endif
173
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100174#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400175static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100176{
177 struct ct_iter_state *st = s->private;
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100178 struct nf_conn_tstamp *tstamp;
179 s64 delta_time;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100180
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100181 tstamp = nf_conn_tstamp_find(ct);
182 if (tstamp) {
183 delta_time = st->time_now - tstamp->start;
184 if (delta_time > 0)
185 delta_time = div_s64(delta_time, NSEC_PER_SEC);
186 else
187 delta_time = 0;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100188
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400189 seq_printf(s, "delta-time=%llu ",
190 (unsigned long long)delta_time);
Patrick McHardyf5c88f52011-01-19 19:10:49 +0100191 }
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400192 return;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100193}
194#else
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400195static inline void
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100196ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
197{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100198}
199#endif
200
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201/* return 0 on success, 1 in case of error */
202static int ct_seq_show(struct seq_file *s, void *v)
203{
Eric Dumazetea781f12009-03-25 21:05:46 +0100204 struct nf_conntrack_tuple_hash *hash = v;
205 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
Jan Engelhardt32948582008-01-31 04:53:24 -0800206 const struct nf_conntrack_l3proto *l3proto;
207 const struct nf_conntrack_l4proto *l4proto;
Eric Dumazetea781f12009-03-25 21:05:46 +0100208 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
Patrick McHardyc88130b2008-01-31 04:42:11 -0800210 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100211 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
212 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213
214 /* we only want to print DIR_ORIGINAL */
215 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100216 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800217
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200218 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200220 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100221 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222
Eric Dumazetea781f12009-03-25 21:05:46 +0100223 ret = -ENOSPC;
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400224 seq_printf(s, "%-8s %u %-8s %u %ld ",
225 l3proto->name, nf_ct_l3num(ct),
226 l4proto->name, nf_ct_protonum(ct),
Florian Westphald0b35b92016-08-03 02:45:07 +0200227 nf_ct_expires(ct) / HZ);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400229 if (l4proto->print_conntrack)
230 l4proto->print_conntrack(s, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800231
Joe Perches824f1fb2014-09-29 16:08:22 -0700232 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
233 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800234
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200235 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
236
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400237 if (seq_has_overflowed(s))
238 goto release;
239
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700240 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100241 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242
Patrick McHardyc88130b2008-01-31 04:42:11 -0800243 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400244 seq_printf(s, "[UNREPLIED] ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245
Joe Perches824f1fb2014-09-29 16:08:22 -0700246 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
247 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200249 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
250
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700251 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100252 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253
Patrick McHardyc88130b2008-01-31 04:42:11 -0800254 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400255 seq_printf(s, "[ASSURED] ");
256
257 if (seq_has_overflowed(s))
258 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800259
260#if defined(CONFIG_NF_CONNTRACK_MARK)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400261 seq_printf(s, "mark=%u ", ct->mark);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262#endif
263
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400264 ct_show_secctx(s, ct);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200265 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400266 ct_show_delta_time(s, ct);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100267
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400268 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
269
270 if (seq_has_overflowed(s))
Eric Dumazetea781f12009-03-25 21:05:46 +0100271 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900272
Eric Dumazetea781f12009-03-25 21:05:46 +0100273 ret = 0;
274release:
275 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700276 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800277}
278
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700279static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800280 .start = ct_seq_start,
281 .next = ct_seq_next,
282 .stop = ct_seq_stop,
283 .show = ct_seq_show
284};
285
286static int ct_open(struct inode *inode, struct file *file)
287{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200288 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700289 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290}
291
Arjan van de Venda7071d2007-02-12 00:55:36 -0800292static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800293 .owner = THIS_MODULE,
294 .open = ct_open,
295 .read = seq_read,
296 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200297 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800298};
299
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
301{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200302 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303 int cpu;
304
305 if (*pos == 0)
306 return SEQ_START_TOKEN;
307
Rusty Russell0f23174a2008-12-29 12:23:42 +0000308 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800309 if (!cpu_possible(cpu))
310 continue;
311 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200312 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313 }
314
315 return NULL;
316}
317
318static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
319{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200320 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800321 int cpu;
322
Rusty Russell0f23174a2008-12-29 12:23:42 +0000323 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800324 if (!cpu_possible(cpu))
325 continue;
326 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200327 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328 }
329
330 return NULL;
331}
332
333static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
334{
335}
336
337static int ct_cpu_seq_show(struct seq_file *seq, void *v)
338{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200339 struct net *net = seq_file_net(seq);
340 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800341 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800342
343 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200344 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 -0800345 return 0;
346 }
347
348 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200349 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800350 nr_conntracks,
351 st->searched,
352 st->found,
353 st->new,
354 st->invalid,
355 st->ignore,
356 st->delete,
357 st->delete_list,
358 st->insert,
359 st->insert_failed,
360 st->drop,
361 st->early_drop,
362 st->error,
363
364 st->expect_new,
365 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200366 st->expect_delete,
367 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368 );
369 return 0;
370}
371
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700372static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800373 .start = ct_cpu_seq_start,
374 .next = ct_cpu_seq_next,
375 .stop = ct_cpu_seq_stop,
376 .show = ct_cpu_seq_show,
377};
378
379static int ct_cpu_seq_open(struct inode *inode, struct file *file)
380{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200381 return seq_open_net(inode, file, &ct_cpu_seq_ops,
382 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383}
384
Arjan van de Venda7071d2007-02-12 00:55:36 -0800385static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386 .owner = THIS_MODULE,
387 .open = ct_cpu_seq_open,
388 .read = seq_read,
389 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200390 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100392
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200393static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100394{
395 struct proc_dir_entry *pde;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000396 kuid_t root_uid;
397 kgid_t root_gid;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100398
Gao fengd4beaa62013-02-18 01:34:54 +0000399 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100400 if (!pde)
401 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700402
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000403 root_uid = make_kuid(net->user_ns, 0);
404 root_gid = make_kgid(net->user_ns, 0);
405 if (uid_valid(root_uid) && gid_valid(root_gid))
406 proc_set_user(pde, root_uid, root_gid);
407
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200408 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700409 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100410 if (!pde)
411 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100412 return 0;
413
414out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000415 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100416out_nf_conntrack:
417 return -ENOMEM;
418}
419
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200420static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100421{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200422 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000423 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100424}
425#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200426static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100427{
428 return 0;
429}
430
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200431static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100432{
433}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200434#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435
436/* Sysctl support */
437
438#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800439/* Log invalid packets of a given protocol */
Florian Westphal3183ab82016-06-22 13:26:10 +0200440static int log_invalid_proto_min __read_mostly;
441static int log_invalid_proto_max __read_mostly = 255;
442
443/* size the user *wants to set */
444static unsigned int nf_conntrack_htable_size_user __read_mostly;
445
446static int
447nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
448 void __user *buffer, size_t *lenp, loff_t *ppos)
449{
450 int ret;
451
452 ret = proc_dointvec(table, write, buffer, lenp, ppos);
453 if (ret < 0 || !write)
454 return ret;
455
456 /* update ret, we might not be able to satisfy request */
457 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
458
459 /* update it to the actual value used by conntrack */
460 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
461 return ret;
462}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800463
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700464static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800465
Joe Perchesfe2c6332013-06-11 23:04:25 -0700466static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 .procname = "nf_conntrack_max",
469 .data = &nf_conntrack_max,
470 .maxlen = sizeof(int),
471 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800472 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800473 },
474 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800475 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200476 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 .maxlen = sizeof(int),
478 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800479 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 },
481 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482 .procname = "nf_conntrack_buckets",
Florian Westphal3183ab82016-06-22 13:26:10 +0200483 .data = &nf_conntrack_htable_size_user,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 .maxlen = sizeof(unsigned int),
Florian Westphal3183ab82016-06-22 13:26:10 +0200485 .mode = 0644,
486 .proc_handler = nf_conntrack_hash_sysctl,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800487 },
488 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700489 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200490 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700491 .maxlen = sizeof(unsigned int),
492 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800493 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700494 },
495 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800496 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200497 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498 .maxlen = sizeof(unsigned int),
499 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800500 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800501 .extra1 = &log_invalid_proto_min,
502 .extra2 = &log_invalid_proto_max,
503 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700504 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700505 .procname = "nf_conntrack_expect_max",
506 .data = &nf_ct_expect_max,
507 .maxlen = sizeof(int),
508 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800509 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700510 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800511 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512};
513
Joe Perchesfe2c6332013-06-11 23:04:25 -0700514static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516 .procname = "nf_conntrack_max",
517 .data = &nf_conntrack_max,
518 .maxlen = sizeof(int),
519 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800520 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800521 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800522 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800523};
524
Alexey Dobriyan80250702008-10-08 11:35:08 +0200525static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100526{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200527 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700528
Alexey Dobriyan80250702008-10-08 11:35:08 +0200529 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
530 GFP_KERNEL);
531 if (!table)
532 goto out_kmemdup;
533
534 table[1].data = &net->ct.count;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200535 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200536 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200537
Eric W. Biederman464dc802012-11-16 03:02:59 +0000538 /* Don't export sysctls to unprivileged users */
539 if (net->user_ns != &init_user_ns)
540 table[0].procname = NULL;
541
Florian Westphal3183ab82016-06-22 13:26:10 +0200542 if (!net_eq(&init_net, net))
543 table[2].mode = 0444;
544
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000545 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200546 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700547 goto out_unregister_netfilter;
548
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100549 return 0;
550
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700551out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200552 kfree(table);
553out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700554 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100555}
556
Alexey Dobriyan80250702008-10-08 11:35:08 +0200557static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100558{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200559 struct ctl_table *table;
560
Alexey Dobriyan80250702008-10-08 11:35:08 +0200561 table = net->ct.sysctl_header->ctl_table_arg;
562 unregister_net_sysctl_table(net->ct.sysctl_header);
563 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100564}
565#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200566static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100567{
568 return 0;
569}
570
Alexey Dobriyan80250702008-10-08 11:35:08 +0200571static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100572{
573}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800574#endif /* CONFIG_SYSCTL */
575
Gao fengf94161c2013-01-21 22:10:24 +0000576static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200577{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200578 int ret;
579
Gao fengf94161c2013-01-21 22:10:24 +0000580 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200581 if (ret < 0)
582 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000583
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200584 ret = nf_conntrack_standalone_init_proc(net);
585 if (ret < 0)
586 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000587
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200588 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200589 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200590 ret = nf_conntrack_standalone_init_sysctl(net);
591 if (ret < 0)
592 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000593
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200594 return 0;
595
Alexey Dobriyan80250702008-10-08 11:35:08 +0200596out_sysctl:
597 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200598out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000599 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200600out_init:
601 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200602}
603
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000604static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200605{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000606 struct net *net;
607
608 list_for_each_entry(net, net_exit_list, exit_list) {
609 nf_conntrack_standalone_fini_sysctl(net);
610 nf_conntrack_standalone_fini_proc(net);
611 }
612 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200613}
614
615static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000616 .init = nf_conntrack_pernet_init,
617 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200618};
619
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800620static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800621{
Gao fengf94161c2013-01-21 22:10:24 +0000622 int ret = nf_conntrack_init_start();
623 if (ret < 0)
624 goto out_start;
625
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100626#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000627 nf_ct_netfilter_header =
628 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100629 if (!nf_ct_netfilter_header) {
630 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000631 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000632 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100633 }
Florian Westphal3183ab82016-06-22 13:26:10 +0200634
635 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100636#endif
Gao fengf94161c2013-01-21 22:10:24 +0000637
638 ret = register_pernet_subsys(&nf_conntrack_net_ops);
639 if (ret < 0)
640 goto out_pernet;
641
642 nf_conntrack_init_end();
643 return 0;
644
645out_pernet:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100646#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000647 unregister_net_sysctl_table(nf_ct_netfilter_header);
648out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100649#endif
Gao fengf94161c2013-01-21 22:10:24 +0000650 nf_conntrack_cleanup_end();
651out_start:
652 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653}
654
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800655static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800656{
Gao fengf94161c2013-01-21 22:10:24 +0000657 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200658 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100659#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000660 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100661#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100662 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800663}
664
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800665module_init(nf_conntrack_standalone_init);
666module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800667
668/* Some modules need us, but don't depend directly on any symbol.
669 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800670void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671{
672}
Patrick McHardy13b18332006-12-02 22:11:25 -0800673EXPORT_SYMBOL_GPL(need_conntrack);