blob: 7d52f8401afd1a4d681320f6823af3a46695a2f7 [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;
Liping Zhange77e6ff2016-08-15 21:50:35 +0800208 struct net *net = seq_file_net(s);
Eric Dumazetea781f12009-03-25 21:05:46 +0100209 int ret = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210
Patrick McHardyc88130b2008-01-31 04:42:11 -0800211 NF_CT_ASSERT(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100212 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
213 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214
215 /* we only want to print DIR_ORIGINAL */
216 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100217 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800218
Liping Zhange77e6ff2016-08-15 21:50:35 +0800219 if (!net_eq(nf_ct_net(ct), net))
220 goto release;
221
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200222 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200224 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100225 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226
Eric Dumazetea781f12009-03-25 21:05:46 +0100227 ret = -ENOSPC;
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400228 seq_printf(s, "%-8s %u %-8s %u %ld ",
229 l3proto->name, nf_ct_l3num(ct),
230 l4proto->name, nf_ct_protonum(ct),
Florian Westphald0b35b92016-08-03 02:45:07 +0200231 nf_ct_expires(ct) / HZ);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400233 if (l4proto->print_conntrack)
234 l4proto->print_conntrack(s, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235
Joe Perches824f1fb2014-09-29 16:08:22 -0700236 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
237 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800238
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200239 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
240
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400241 if (seq_has_overflowed(s))
242 goto release;
243
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700244 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100245 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246
Patrick McHardyc88130b2008-01-31 04:42:11 -0800247 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400248 seq_printf(s, "[UNREPLIED] ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800249
Joe Perches824f1fb2014-09-29 16:08:22 -0700250 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
251 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800252
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200253 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
254
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700255 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100256 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257
Patrick McHardyc88130b2008-01-31 04:42:11 -0800258 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400259 seq_printf(s, "[ASSURED] ");
260
261 if (seq_has_overflowed(s))
262 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800263
264#if defined(CONFIG_NF_CONNTRACK_MARK)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400265 seq_printf(s, "mark=%u ", ct->mark);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800266#endif
267
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400268 ct_show_secctx(s, ct);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200269 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400270 ct_show_delta_time(s, ct);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100271
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400272 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
273
274 if (seq_has_overflowed(s))
Eric Dumazetea781f12009-03-25 21:05:46 +0100275 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900276
Eric Dumazetea781f12009-03-25 21:05:46 +0100277 ret = 0;
278release:
279 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700280 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800281}
282
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700283static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800284 .start = ct_seq_start,
285 .next = ct_seq_next,
286 .stop = ct_seq_stop,
287 .show = ct_seq_show
288};
289
290static int ct_open(struct inode *inode, struct file *file)
291{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200292 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700293 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294}
295
Arjan van de Venda7071d2007-02-12 00:55:36 -0800296static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800297 .owner = THIS_MODULE,
298 .open = ct_open,
299 .read = seq_read,
300 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200301 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302};
303
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
305{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200306 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800307 int cpu;
308
309 if (*pos == 0)
310 return SEQ_START_TOKEN;
311
Rusty Russell0f23174a2008-12-29 12:23:42 +0000312 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313 if (!cpu_possible(cpu))
314 continue;
315 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200316 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800317 }
318
319 return NULL;
320}
321
322static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
323{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200324 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800325 int cpu;
326
Rusty Russell0f23174a2008-12-29 12:23:42 +0000327 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328 if (!cpu_possible(cpu))
329 continue;
330 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200331 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800332 }
333
334 return NULL;
335}
336
337static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
338{
339}
340
341static int ct_cpu_seq_show(struct seq_file *seq, void *v)
342{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200343 struct net *net = seq_file_net(seq);
344 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800345 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346
347 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200348 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 -0800349 return 0;
350 }
351
352 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200353 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800354 nr_conntracks,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200355 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 st->found,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200357 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 st->invalid,
359 st->ignore,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200360 0,
361 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 st->insert,
363 st->insert_failed,
364 st->drop,
365 st->early_drop,
366 st->error,
367
368 st->expect_new,
369 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200370 st->expect_delete,
371 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800372 );
373 return 0;
374}
375
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700376static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377 .start = ct_cpu_seq_start,
378 .next = ct_cpu_seq_next,
379 .stop = ct_cpu_seq_stop,
380 .show = ct_cpu_seq_show,
381};
382
383static int ct_cpu_seq_open(struct inode *inode, struct file *file)
384{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200385 return seq_open_net(inode, file, &ct_cpu_seq_ops,
386 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387}
388
Arjan van de Venda7071d2007-02-12 00:55:36 -0800389static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390 .owner = THIS_MODULE,
391 .open = ct_cpu_seq_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200394 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100396
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200397static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100398{
399 struct proc_dir_entry *pde;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000400 kuid_t root_uid;
401 kgid_t root_gid;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100402
Gao fengd4beaa62013-02-18 01:34:54 +0000403 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100404 if (!pde)
405 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700406
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000407 root_uid = make_kuid(net->user_ns, 0);
408 root_gid = make_kgid(net->user_ns, 0);
409 if (uid_valid(root_uid) && gid_valid(root_gid))
410 proc_set_user(pde, root_uid, root_gid);
411
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200412 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700413 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100414 if (!pde)
415 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100416 return 0;
417
418out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000419 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100420out_nf_conntrack:
421 return -ENOMEM;
422}
423
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200424static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100425{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200426 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000427 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100428}
429#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200430static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100431{
432 return 0;
433}
434
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200435static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100436{
437}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200438#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800439
440/* Sysctl support */
441
442#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443/* Log invalid packets of a given protocol */
Florian Westphal3183ab82016-06-22 13:26:10 +0200444static int log_invalid_proto_min __read_mostly;
445static int log_invalid_proto_max __read_mostly = 255;
446
447/* size the user *wants to set */
448static unsigned int nf_conntrack_htable_size_user __read_mostly;
449
450static int
451nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
452 void __user *buffer, size_t *lenp, loff_t *ppos)
453{
454 int ret;
455
456 ret = proc_dointvec(table, write, buffer, lenp, ppos);
457 if (ret < 0 || !write)
458 return ret;
459
460 /* update ret, we might not be able to satisfy request */
461 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
462
463 /* update it to the actual value used by conntrack */
464 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
465 return ret;
466}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700468static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800469
Joe Perchesfe2c6332013-06-11 23:04:25 -0700470static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800471 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472 .procname = "nf_conntrack_max",
473 .data = &nf_conntrack_max,
474 .maxlen = sizeof(int),
475 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800476 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 },
478 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800479 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200480 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481 .maxlen = sizeof(int),
482 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800483 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 },
485 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486 .procname = "nf_conntrack_buckets",
Florian Westphal3183ab82016-06-22 13:26:10 +0200487 .data = &nf_conntrack_htable_size_user,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488 .maxlen = sizeof(unsigned int),
Florian Westphal3183ab82016-06-22 13:26:10 +0200489 .mode = 0644,
490 .proc_handler = nf_conntrack_hash_sysctl,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800491 },
492 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700493 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200494 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700495 .maxlen = sizeof(unsigned int),
496 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800497 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700498 },
499 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800500 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200501 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800502 .maxlen = sizeof(unsigned int),
503 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800504 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505 .extra1 = &log_invalid_proto_min,
506 .extra2 = &log_invalid_proto_max,
507 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700508 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700509 .procname = "nf_conntrack_expect_max",
510 .data = &nf_ct_expect_max,
511 .maxlen = sizeof(int),
512 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800513 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700514 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800515 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516};
517
Joe Perchesfe2c6332013-06-11 23:04:25 -0700518static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520 .procname = "nf_conntrack_max",
521 .data = &nf_conntrack_max,
522 .maxlen = sizeof(int),
523 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800524 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800525 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800526 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800527};
528
Alexey Dobriyan80250702008-10-08 11:35:08 +0200529static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100530{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200531 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700532
Alexey Dobriyan80250702008-10-08 11:35:08 +0200533 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
534 GFP_KERNEL);
535 if (!table)
536 goto out_kmemdup;
537
538 table[1].data = &net->ct.count;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200539 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200540 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200541
Eric W. Biederman464dc802012-11-16 03:02:59 +0000542 /* Don't export sysctls to unprivileged users */
543 if (net->user_ns != &init_user_ns)
544 table[0].procname = NULL;
545
Florian Westphal3183ab82016-06-22 13:26:10 +0200546 if (!net_eq(&init_net, net))
547 table[2].mode = 0444;
548
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000549 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200550 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700551 goto out_unregister_netfilter;
552
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100553 return 0;
554
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700555out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200556 kfree(table);
557out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700558 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100559}
560
Alexey Dobriyan80250702008-10-08 11:35:08 +0200561static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100562{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200563 struct ctl_table *table;
564
Alexey Dobriyan80250702008-10-08 11:35:08 +0200565 table = net->ct.sysctl_header->ctl_table_arg;
566 unregister_net_sysctl_table(net->ct.sysctl_header);
567 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100568}
569#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200570static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100571{
572 return 0;
573}
574
Alexey Dobriyan80250702008-10-08 11:35:08 +0200575static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100576{
577}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578#endif /* CONFIG_SYSCTL */
579
Gao fengf94161c2013-01-21 22:10:24 +0000580static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200581{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200582 int ret;
583
Gao fengf94161c2013-01-21 22:10:24 +0000584 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200585 if (ret < 0)
586 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000587
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200588 ret = nf_conntrack_standalone_init_proc(net);
589 if (ret < 0)
590 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000591
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200592 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200593 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200594 ret = nf_conntrack_standalone_init_sysctl(net);
595 if (ret < 0)
596 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000597
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200598 return 0;
599
Alexey Dobriyan80250702008-10-08 11:35:08 +0200600out_sysctl:
601 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200602out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000603 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200604out_init:
605 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200606}
607
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000608static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200609{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000610 struct net *net;
611
612 list_for_each_entry(net, net_exit_list, exit_list) {
613 nf_conntrack_standalone_fini_sysctl(net);
614 nf_conntrack_standalone_fini_proc(net);
615 }
616 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200617}
618
619static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000620 .init = nf_conntrack_pernet_init,
621 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200622};
623
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800624static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625{
Gao fengf94161c2013-01-21 22:10:24 +0000626 int ret = nf_conntrack_init_start();
627 if (ret < 0)
628 goto out_start;
629
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100630#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000631 nf_ct_netfilter_header =
632 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100633 if (!nf_ct_netfilter_header) {
634 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000635 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000636 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100637 }
Florian Westphal3183ab82016-06-22 13:26:10 +0200638
639 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100640#endif
Gao fengf94161c2013-01-21 22:10:24 +0000641
642 ret = register_pernet_subsys(&nf_conntrack_net_ops);
643 if (ret < 0)
644 goto out_pernet;
645
646 nf_conntrack_init_end();
647 return 0;
648
649out_pernet:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100650#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000651 unregister_net_sysctl_table(nf_ct_netfilter_header);
652out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100653#endif
Gao fengf94161c2013-01-21 22:10:24 +0000654 nf_conntrack_cleanup_end();
655out_start:
656 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800657}
658
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800659static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800660{
Gao fengf94161c2013-01-21 22:10:24 +0000661 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200662 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100663#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000664 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100665#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100666 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800667}
668
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800669module_init(nf_conntrack_standalone_init);
670module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671
672/* Some modules need us, but don't depend directly on any symbol.
673 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800674void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800675{
676}
Patrick McHardy13b18332006-12-02 22:11:25 -0800677EXPORT_SYMBOL_GPL(need_conntrack);