blob: 2f1014eb3cc553035107c0819a844c6917c0f129 [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
Florian Westphal58e207e2016-09-22 23:49:17 +0200215 if (nf_ct_should_gc(ct)) {
216 nf_ct_kill(ct);
217 goto release;
218 }
219
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 /* we only want to print DIR_ORIGINAL */
221 if (NF_CT_DIRECTION(hash))
Eric Dumazetea781f12009-03-25 21:05:46 +0100222 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223
Liping Zhange77e6ff2016-08-15 21:50:35 +0800224 if (!net_eq(nf_ct_net(ct), net))
225 goto release;
226
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200227 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228 NF_CT_ASSERT(l3proto);
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200229 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100230 NF_CT_ASSERT(l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800231
Eric Dumazetea781f12009-03-25 21:05:46 +0100232 ret = -ENOSPC;
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400233 seq_printf(s, "%-8s %u %-8s %u %ld ",
234 l3proto->name, nf_ct_l3num(ct),
235 l4proto->name, nf_ct_protonum(ct),
Florian Westphald0b35b92016-08-03 02:45:07 +0200236 nf_ct_expires(ct) / HZ);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800237
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400238 if (l4proto->print_conntrack)
239 l4proto->print_conntrack(s, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800240
Joe Perches824f1fb2014-09-29 16:08:22 -0700241 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
242 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800243
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200244 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
245
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400246 if (seq_has_overflowed(s))
247 goto release;
248
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700249 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
Eric Dumazetea781f12009-03-25 21:05:46 +0100250 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800251
Patrick McHardyc88130b2008-01-31 04:42:11 -0800252 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400253 seq_printf(s, "[UNREPLIED] ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254
Joe Perches824f1fb2014-09-29 16:08:22 -0700255 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
256 l3proto, l4proto);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200258 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
259
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700260 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
Eric Dumazetea781f12009-03-25 21:05:46 +0100261 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262
Patrick McHardyc88130b2008-01-31 04:42:11 -0800263 if (test_bit(IPS_ASSURED_BIT, &ct->status))
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400264 seq_printf(s, "[ASSURED] ");
265
266 if (seq_has_overflowed(s))
267 goto release;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268
269#if defined(CONFIG_NF_CONNTRACK_MARK)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400270 seq_printf(s, "mark=%u ", ct->mark);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271#endif
272
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400273 ct_show_secctx(s, ct);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200274 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400275 ct_show_delta_time(s, ct);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100276
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400277 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
278
279 if (seq_has_overflowed(s))
Eric Dumazetea781f12009-03-25 21:05:46 +0100280 goto release;
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +0900281
Eric Dumazetea781f12009-03-25 21:05:46 +0100282 ret = 0;
283release:
284 nf_ct_put(ct);
David S. Millerd88d7de2011-04-17 17:03:33 -0700285 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286}
287
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700288static const struct seq_operations ct_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800289 .start = ct_seq_start,
290 .next = ct_seq_next,
291 .stop = ct_seq_stop,
292 .show = ct_seq_show
293};
294
295static int ct_open(struct inode *inode, struct file *file)
296{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200297 return seq_open_net(inode, file, &ct_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700298 sizeof(struct ct_iter_state));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800299}
300
Arjan van de Venda7071d2007-02-12 00:55:36 -0800301static const struct file_operations ct_file_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302 .owner = THIS_MODULE,
303 .open = ct_open,
304 .read = seq_read,
305 .llseek = seq_lseek,
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200306 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800307};
308
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800309static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
310{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200311 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312 int cpu;
313
314 if (*pos == 0)
315 return SEQ_START_TOKEN;
316
Rusty Russell0f23174a2008-12-29 12:23:42 +0000317 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800318 if (!cpu_possible(cpu))
319 continue;
320 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200321 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322 }
323
324 return NULL;
325}
326
327static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
328{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200329 struct net *net = seq_file_net(seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800330 int cpu;
331
Rusty Russell0f23174a2008-12-29 12:23:42 +0000332 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800333 if (!cpu_possible(cpu))
334 continue;
335 *pos = cpu + 1;
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200336 return per_cpu_ptr(net->ct.stat, cpu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800337 }
338
339 return NULL;
340}
341
342static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
343{
344}
345
346static int ct_cpu_seq_show(struct seq_file *seq, void *v)
347{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200348 struct net *net = seq_file_net(seq);
349 unsigned int nr_conntracks = atomic_read(&net->ct.count);
Jan Engelhardt32948582008-01-31 04:53:24 -0800350 const struct ip_conntrack_stat *st = v;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800351
352 if (v == SEQ_START_TOKEN) {
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200353 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 -0800354 return 0;
355 }
356
357 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200358 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800359 nr_conntracks,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200360 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800361 st->found,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200362 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 st->invalid,
364 st->ignore,
Florian Westphal8e8118f2016-09-11 22:55:53 +0200365 0,
366 0,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800367 st->insert,
368 st->insert_failed,
369 st->drop,
370 st->early_drop,
371 st->error,
372
373 st->expect_new,
374 st->expect_create,
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200375 st->expect_delete,
376 st->search_restart
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377 );
378 return 0;
379}
380
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700381static const struct seq_operations ct_cpu_seq_ops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 .start = ct_cpu_seq_start,
383 .next = ct_cpu_seq_next,
384 .stop = ct_cpu_seq_stop,
385 .show = ct_cpu_seq_show,
386};
387
388static int ct_cpu_seq_open(struct inode *inode, struct file *file)
389{
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200390 return seq_open_net(inode, file, &ct_cpu_seq_ops,
391 sizeof(struct seq_net_private));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392}
393
Arjan van de Venda7071d2007-02-12 00:55:36 -0800394static const struct file_operations ct_cpu_seq_fops = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395 .owner = THIS_MODULE,
396 .open = ct_cpu_seq_open,
397 .read = seq_read,
398 .llseek = seq_lseek,
Alexey Dobriyan8e9df802008-10-08 11:35:08 +0200399 .release = seq_release_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400};
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100401
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200402static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100403{
404 struct proc_dir_entry *pde;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000405 kuid_t root_uid;
406 kgid_t root_gid;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100407
Gao fengd4beaa62013-02-18 01:34:54 +0000408 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100409 if (!pde)
410 goto out_nf_conntrack;
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700411
Philip Whinerayf13f2ae2015-11-22 11:35:07 +0000412 root_uid = make_kuid(net->user_ns, 0);
413 root_gid = make_kgid(net->user_ns, 0);
414 if (uid_valid(root_uid) && gid_valid(root_gid))
415 proc_set_user(pde, root_uid, root_gid);
416
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200417 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
Denis V. Lunev52c0e112008-05-02 04:10:57 -0700418 &ct_cpu_seq_fops);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100419 if (!pde)
420 goto out_stat_nf_conntrack;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100421 return 0;
422
423out_stat_nf_conntrack:
Gao fengece31ff2013-02-18 01:34:56 +0000424 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100425out_nf_conntrack:
426 return -ENOMEM;
427}
428
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200429static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100430{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200431 remove_proc_entry("nf_conntrack", net->proc_net_stat);
Gao fengece31ff2013-02-18 01:34:56 +0000432 remove_proc_entry("nf_conntrack", net->proc_net);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100433}
434#else
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200435static int nf_conntrack_standalone_init_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100436{
437 return 0;
438}
439
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200440static void nf_conntrack_standalone_fini_proc(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100441{
442}
Jan Engelhardt54b07dc2011-04-21 09:32:45 +0200443#endif /* CONFIG_NF_CONNTRACK_PROCFS */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800444
445/* Sysctl support */
446
447#ifdef CONFIG_SYSCTL
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800448/* Log invalid packets of a given protocol */
Florian Westphal3183ab82016-06-22 13:26:10 +0200449static int log_invalid_proto_min __read_mostly;
450static int log_invalid_proto_max __read_mostly = 255;
451
452/* size the user *wants to set */
453static unsigned int nf_conntrack_htable_size_user __read_mostly;
454
455static int
456nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
457 void __user *buffer, size_t *lenp, loff_t *ppos)
458{
459 int ret;
460
461 ret = proc_dointvec(table, write, buffer, lenp, ppos);
462 if (ret < 0 || !write)
463 return ret;
464
465 /* update ret, we might not be able to satisfy request */
466 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
467
468 /* update it to the actual value used by conntrack */
469 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
470 return ret;
471}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700473static struct ctl_table_header *nf_ct_netfilter_header;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800474
Joe Perchesfe2c6332013-06-11 23:04:25 -0700475static struct ctl_table nf_ct_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 .procname = "nf_conntrack_max",
478 .data = &nf_conntrack_max,
479 .maxlen = sizeof(int),
480 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800481 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482 },
483 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 .procname = "nf_conntrack_count",
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200485 .data = &init_net.ct.count,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486 .maxlen = sizeof(int),
487 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800488 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800489 },
490 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800491 .procname = "nf_conntrack_buckets",
Florian Westphal3183ab82016-06-22 13:26:10 +0200492 .data = &nf_conntrack_htable_size_user,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800493 .maxlen = sizeof(unsigned int),
Florian Westphal3183ab82016-06-22 13:26:10 +0200494 .mode = 0644,
495 .proc_handler = nf_conntrack_hash_sysctl,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800496 },
497 {
Patrick McHardy39a27a32006-05-29 18:23:54 -0700498 .procname = "nf_conntrack_checksum",
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200499 .data = &init_net.ct.sysctl_checksum,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700500 .maxlen = sizeof(unsigned int),
501 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800502 .proc_handler = proc_dointvec,
Patrick McHardy39a27a32006-05-29 18:23:54 -0700503 },
504 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505 .procname = "nf_conntrack_log_invalid",
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200506 .data = &init_net.ct.sysctl_log_invalid,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800507 .maxlen = sizeof(unsigned int),
508 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800509 .proc_handler = proc_dointvec_minmax,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800510 .extra1 = &log_invalid_proto_min,
511 .extra2 = &log_invalid_proto_max,
512 },
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700513 {
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700514 .procname = "nf_conntrack_expect_max",
515 .data = &nf_ct_expect_max,
516 .maxlen = sizeof(int),
517 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800518 .proc_handler = proc_dointvec,
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700519 },
Mohammed Javid53d20ee2016-02-10 17:29:57 +0530520 {
521 .procname = "nf_conntrack_pkt_threshold",
522 .data = &nf_conntrack_pkt_threshold,
523 .maxlen = sizeof(int),
524 .mode = 0644,
525 .proc_handler = proc_dointvec,
526 },
527
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800528 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800529};
530
Joe Perchesfe2c6332013-06-11 23:04:25 -0700531static struct ctl_table nf_ct_netfilter_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800532 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800533 .procname = "nf_conntrack_max",
534 .data = &nf_conntrack_max,
535 .maxlen = sizeof(int),
536 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800537 .proc_handler = proc_dointvec,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800538 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800539 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800540};
541
Alexey Dobriyan80250702008-10-08 11:35:08 +0200542static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100543{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200544 struct ctl_table *table;
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700545
Alexey Dobriyan80250702008-10-08 11:35:08 +0200546 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
547 GFP_KERNEL);
548 if (!table)
549 goto out_kmemdup;
550
551 table[1].data = &net->ct.count;
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200552 table[3].data = &net->ct.sysctl_checksum;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200553 table[4].data = &net->ct.sysctl_log_invalid;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200554
Eric W. Biederman464dc802012-11-16 03:02:59 +0000555 /* Don't export sysctls to unprivileged users */
556 if (net->user_ns != &init_user_ns)
557 table[0].procname = NULL;
558
Florian Westphal3183ab82016-06-22 13:26:10 +0200559 if (!net_eq(&init_net, net))
560 table[2].mode = 0444;
561
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000562 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
Alexey Dobriyan80250702008-10-08 11:35:08 +0200563 if (!net->ct.sysctl_header)
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700564 goto out_unregister_netfilter;
565
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100566 return 0;
567
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700568out_unregister_netfilter:
Alexey Dobriyan80250702008-10-08 11:35:08 +0200569 kfree(table);
570out_kmemdup:
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -0700571 return -ENOMEM;
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100572}
573
Alexey Dobriyan80250702008-10-08 11:35:08 +0200574static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100575{
Alexey Dobriyan80250702008-10-08 11:35:08 +0200576 struct ctl_table *table;
577
Alexey Dobriyan80250702008-10-08 11:35:08 +0200578 table = net->ct.sysctl_header->ctl_table_arg;
579 unregister_net_sysctl_table(net->ct.sysctl_header);
580 kfree(table);
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100581}
582#else
Alexey Dobriyan80250702008-10-08 11:35:08 +0200583static int nf_conntrack_standalone_init_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100584{
585 return 0;
586}
587
Alexey Dobriyan80250702008-10-08 11:35:08 +0200588static void nf_conntrack_standalone_fini_sysctl(struct net *net)
Alexey Dobriyanb916f7d2008-03-20 15:15:43 +0100589{
590}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800591#endif /* CONFIG_SYSCTL */
592
Gao fengf94161c2013-01-21 22:10:24 +0000593static int nf_conntrack_pernet_init(struct net *net)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200594{
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200595 int ret;
596
Gao fengf94161c2013-01-21 22:10:24 +0000597 ret = nf_conntrack_init_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200598 if (ret < 0)
599 goto out_init;
Gao fengf94161c2013-01-21 22:10:24 +0000600
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200601 ret = nf_conntrack_standalone_init_proc(net);
602 if (ret < 0)
603 goto out_proc;
Gao fengf94161c2013-01-21 22:10:24 +0000604
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200605 net->ct.sysctl_checksum = 1;
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200606 net->ct.sysctl_log_invalid = 0;
Alexey Dobriyan80250702008-10-08 11:35:08 +0200607 ret = nf_conntrack_standalone_init_sysctl(net);
608 if (ret < 0)
609 goto out_sysctl;
Gao fengf94161c2013-01-21 22:10:24 +0000610
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200611 return 0;
612
Alexey Dobriyan80250702008-10-08 11:35:08 +0200613out_sysctl:
614 nf_conntrack_standalone_fini_proc(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200615out_proc:
Gao fengf94161c2013-01-21 22:10:24 +0000616 nf_conntrack_cleanup_net(net);
Alexey Dobriyanb2ce2c72008-10-08 11:35:05 +0200617out_init:
618 return ret;
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200619}
620
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000621static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200622{
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000623 struct net *net;
624
625 list_for_each_entry(net, net_exit_list, exit_list) {
626 nf_conntrack_standalone_fini_sysctl(net);
627 nf_conntrack_standalone_fini_proc(net);
628 }
629 nf_conntrack_cleanup_net_list(net_exit_list);
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200630}
631
632static struct pernet_operations nf_conntrack_net_ops = {
Vladimir Davydovdece40e2013-03-13 23:40:14 +0000633 .init = nf_conntrack_pernet_init,
634 .exit_batch = nf_conntrack_pernet_exit,
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200635};
636
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800637static int __init nf_conntrack_standalone_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800638{
Gao fengf94161c2013-01-21 22:10:24 +0000639 int ret = nf_conntrack_init_start();
640 if (ret < 0)
641 goto out_start;
642
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100643#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000644 nf_ct_netfilter_header =
645 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100646 if (!nf_ct_netfilter_header) {
647 pr_err("nf_conntrack: can't register to sysctl.\n");
Wei Yongjun53890902013-03-27 03:57:10 +0000648 ret = -ENOMEM;
Gao fengf94161c2013-01-21 22:10:24 +0000649 goto out_sysctl;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100650 }
Florian Westphal3183ab82016-06-22 13:26:10 +0200651
652 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100653#endif
Gao fengf94161c2013-01-21 22:10:24 +0000654
655 ret = register_pernet_subsys(&nf_conntrack_net_ops);
656 if (ret < 0)
657 goto out_pernet;
658
659 nf_conntrack_init_end();
660 return 0;
661
662out_pernet:
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);
665out_sysctl:
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100666#endif
Gao fengf94161c2013-01-21 22:10:24 +0000667 nf_conntrack_cleanup_end();
668out_start:
669 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800670}
671
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800672static void __exit nf_conntrack_standalone_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800673{
Gao fengf94161c2013-01-21 22:10:24 +0000674 nf_conntrack_cleanup_start();
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +0200675 unregister_pernet_subsys(&nf_conntrack_net_ops);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100676#ifdef CONFIG_SYSCTL
Gao fengf94161c2013-01-21 22:10:24 +0000677 unregister_net_sysctl_table(nf_ct_netfilter_header);
Pablo Neira Ayuso5f9f9462013-01-23 15:12:25 +0100678#endif
Pablo Neira Ayuso1e47ee82013-01-10 16:12:01 +0100679 nf_conntrack_cleanup_end();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800680}
681
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800682module_init(nf_conntrack_standalone_init);
683module_exit(nf_conntrack_standalone_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800684
685/* Some modules need us, but don't depend directly on any symbol.
686 They should call this. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800687void need_conntrack(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800688{
689}
Patrick McHardy13b18332006-12-02 22:11:25 -0800690EXPORT_SYMBOL_GPL(need_conntrack);