blob: df1d5618b008fc38571d807778b1f8fa7c72d3ab [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* Connection state tracking for netfilter. This is separated from,
2 but required by, the NAT layer; it can also be used by an iptables
3 extension. */
4
5/* (C) 1999-2001 Paul `Rusty' Russell
Harald Weltedc808fe2006-03-20 17:56:32 -08006 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02008 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013 */
14
Weongyo Jeongccd63c22016-03-15 10:57:44 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <linux/types.h>
18#include <linux/netfilter.h>
19#include <linux/module.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021#include <linux/skbuff.h>
22#include <linux/proc_fs.h>
23#include <linux/vmalloc.h>
24#include <linux/stddef.h>
25#include <linux/slab.h>
26#include <linux/random.h>
27#include <linux/jhash.h>
28#include <linux/err.h>
29#include <linux/percpu.h>
30#include <linux/moduleparam.h>
31#include <linux/notifier.h>
32#include <linux/kernel.h>
33#include <linux/netdevice.h>
34#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050035#include <linux/mm.h>
Patrick McHardyd696c7b2010-02-08 11:18:07 -080036#include <linux/nsproxy.h>
Eric Dumazetea781f12009-03-25 21:05:46 +010037#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039#include <net/netfilter/nf_conntrack.h>
40#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010041#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010042#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy41d73ec2013-08-27 08:50:12 +020044#include <net/netfilter/nf_conntrack_seqadj.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070046#include <net/netfilter/nf_conntrack_extend.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070047#include <net/netfilter/nf_conntrack_acct.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020048#include <net/netfilter/nf_conntrack_ecache.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010049#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010050#include <net/netfilter/nf_conntrack_timestamp.h>
Pablo Neira Ayusodd705072012-02-28 23:36:48 +010051#include <net/netfilter/nf_conntrack_timeout.h>
Florian Westphalc539f012013-01-11 06:30:44 +000052#include <net/netfilter/nf_conntrack_labels.h>
Patrick McHardy48b1de42013-08-27 08:50:14 +020053#include <net/netfilter/nf_conntrack_synproxy.h>
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070054#include <net/netfilter/nf_nat.h>
Patrick McHardye17b6662008-11-18 12:24:17 +010055#include <net/netfilter/nf_nat_core.h>
stephen hemminger49376362013-03-16 07:00:28 +000056#include <net/netfilter/nf_nat_helper.h>
Florian Westphal1b8c8a92016-05-03 00:25:58 +020057#include <net/netns/hash.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058
Harald Weltedc808fe2006-03-20 17:56:32 -080059#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060
Patrick McHardye17b6662008-11-18 12:24:17 +010061int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
62 enum nf_nat_manip_type manip,
Patrick McHardy39938322009-08-25 16:07:58 +020063 const struct nlattr *attr) __read_mostly;
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070064EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
65
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +010066__cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
67EXPORT_SYMBOL_GPL(nf_conntrack_locks);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +010069__cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
70EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
71
Florian Westphal56d52d42016-05-02 18:39:55 +020072struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
73EXPORT_SYMBOL_GPL(nf_conntrack_hash);
74
Florian Westphalb87a2f92016-08-25 15:33:33 +020075struct conntrack_gc_work {
76 struct delayed_work dwork;
77 u32 last_bucket;
78 bool exiting;
Florian Westphale0df8ca2016-11-04 16:54:58 +010079 long next_gc_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +020080};
81
Florian Westphal0c5366b2016-05-09 16:24:32 +020082static __read_mostly struct kmem_cache *nf_conntrack_cachep;
Sasha Levinb16c2912016-01-18 19:23:51 -050083static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
Florian Westphal70d72b72016-04-24 01:17:14 +020084static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -050085static __read_mostly bool nf_conntrack_locks_all;
86
Florian Westphale0df8ca2016-11-04 16:54:58 +010087/* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */
Florian Westphal371d0342017-01-18 02:01:22 +010088#define GC_MAX_BUCKETS_DIV 128u
89/* upper bound of full table scan */
90#define GC_MAX_SCAN_JIFFIES (16u * HZ)
91/* desired ratio of entries found to be expired */
92#define GC_EVICT_RATIO 50u
Florian Westphalb87a2f92016-08-25 15:33:33 +020093
94static struct conntrack_gc_work conntrack_gc_work;
95
Sasha Levinb16c2912016-01-18 19:23:51 -050096void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
97{
Manfred Spraul4c7f54a2017-07-06 20:45:59 +020098 /* 1) Acquire the lock */
Sasha Levinb16c2912016-01-18 19:23:51 -050099 spin_lock(lock);
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200100
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200101 /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
102 * It pairs with the smp_store_release() in nf_conntrack_all_unlock()
103 */
104 if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
105 return;
106
107 /* fast path failed, unlock */
108 spin_unlock(lock);
109
110 /* Slow path 1) get global lock */
111 spin_lock(&nf_conntrack_locks_all_lock);
112
113 /* Slow path 2) get the lock we want */
114 spin_lock(lock);
115
116 /* Slow path 3) release the global lock */
117 spin_unlock(&nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -0500118}
119EXPORT_SYMBOL_GPL(nf_conntrack_lock);
120
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100121static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
122{
123 h1 %= CONNTRACK_LOCKS;
124 h2 %= CONNTRACK_LOCKS;
125 spin_unlock(&nf_conntrack_locks[h1]);
126 if (h1 != h2)
127 spin_unlock(&nf_conntrack_locks[h2]);
128}
129
130/* return true if we need to recompute hashes (in case hash table was resized) */
131static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
132 unsigned int h2, unsigned int sequence)
133{
134 h1 %= CONNTRACK_LOCKS;
135 h2 %= CONNTRACK_LOCKS;
136 if (h1 <= h2) {
Sasha Levinb16c2912016-01-18 19:23:51 -0500137 nf_conntrack_lock(&nf_conntrack_locks[h1]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100138 if (h1 != h2)
139 spin_lock_nested(&nf_conntrack_locks[h2],
140 SINGLE_DEPTH_NESTING);
141 } else {
Sasha Levinb16c2912016-01-18 19:23:51 -0500142 nf_conntrack_lock(&nf_conntrack_locks[h2]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100143 spin_lock_nested(&nf_conntrack_locks[h1],
144 SINGLE_DEPTH_NESTING);
145 }
Florian Westphala3efd812016-04-18 16:16:59 +0200146 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100147 nf_conntrack_double_unlock(h1, h2);
148 return true;
149 }
150 return false;
151}
152
153static void nf_conntrack_all_lock(void)
154{
155 int i;
156
Sasha Levinb16c2912016-01-18 19:23:51 -0500157 spin_lock(&nf_conntrack_locks_all_lock);
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200158
Sasha Levinb16c2912016-01-18 19:23:51 -0500159 nf_conntrack_locks_all = true;
160
161 for (i = 0; i < CONNTRACK_LOCKS; i++) {
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200162 spin_lock(&nf_conntrack_locks[i]);
163
164 /* This spin_unlock provides the "release" to ensure that
165 * nf_conntrack_locks_all==true is visible to everyone that
166 * acquired spin_lock(&nf_conntrack_locks[]).
167 */
168 spin_unlock(&nf_conntrack_locks[i]);
Sasha Levinb16c2912016-01-18 19:23:51 -0500169 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100170}
171
172static void nf_conntrack_all_unlock(void)
173{
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200174 /* All prior stores must be complete before we clear
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200175 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
176 * might observe the false value but not the entire
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200177 * critical section.
178 * It pairs with the smp_load_acquire() in nf_conntrack_lock()
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200179 */
180 smp_store_release(&nf_conntrack_locks_all, false);
Sasha Levinb16c2912016-01-18 19:23:51 -0500181 spin_unlock(&nf_conntrack_locks_all_lock);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100182}
183
Martin Josefssone2b76062006-11-29 02:35:04 +0100184unsigned int nf_conntrack_htable_size __read_mostly;
Pablo Neira Ayuso2567c4e2016-08-18 11:15:12 +0200185EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
186
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100187unsigned int nf_conntrack_max __read_mostly;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800188seqcount_t nf_conntrack_generation __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -0800189
Eric Dumazetb3c51632010-06-09 14:43:38 +0200190DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
191EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
Patrick McHardy13b18332006-12-02 22:11:25 -0800192
Florian Westphal141658f2016-04-18 16:17:01 +0200193static unsigned int nf_conntrack_hash_rnd __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200195static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
196 const struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197{
Patrick McHardy07949352008-01-31 04:40:52 -0800198 unsigned int n;
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200199 u32 seed;
Sami Farin9b887902007-03-14 16:43:00 -0700200
Florian Westphal141658f2016-04-18 16:17:01 +0200201 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
202
Patrick McHardy07949352008-01-31 04:40:52 -0800203 /* The direction must be ignored, so we hash everything up to the
204 * destination ports (which is a multiple of 4) and treat the last
205 * three bytes manually.
206 */
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200207 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
Patrick McHardy07949352008-01-31 04:40:52 -0800208 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200209 return jhash2((u32 *)tuple, n, seed ^
Changli Gao99f07e92010-09-21 17:49:20 +0200210 (((__force __u16)tuple->dst.u.all << 16) |
211 tuple->dst.protonum));
212}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213
Florian Westphal56d52d42016-05-02 18:39:55 +0200214static u32 scale_hash(u32 hash)
Changli Gao99f07e92010-09-21 17:49:20 +0200215{
Florian Westphal56d52d42016-05-02 18:39:55 +0200216 return reciprocal_scale(hash, nf_conntrack_htable_size);
Changli Gao99f07e92010-09-21 17:49:20 +0200217}
218
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200219static u32 __hash_conntrack(const struct net *net,
220 const struct nf_conntrack_tuple *tuple,
221 unsigned int size)
Changli Gao99f07e92010-09-21 17:49:20 +0200222{
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200223 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224}
225
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200226static u32 hash_conntrack(const struct net *net,
227 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228{
Florian Westphal56d52d42016-05-02 18:39:55 +0200229 return scale_hash(hash_conntrack_raw(tuple, net));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230}
231
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200232bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800233nf_ct_get_tuple(const struct sk_buff *skb,
234 unsigned int nhoff,
235 unsigned int dataoff,
236 u_int16_t l3num,
237 u_int8_t protonum,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500238 struct net *net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239 struct nf_conntrack_tuple *tuple,
240 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100241 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242{
Philip Craig443a70d2008-04-29 03:35:10 -0700243 memset(tuple, 0, sizeof(*tuple));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244
245 tuple->src.l3num = l3num;
246 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200247 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248
249 tuple->dst.protonum = protonum;
250 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
251
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500252 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253}
Patrick McHardy13b18332006-12-02 22:11:25 -0800254EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200256bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500257 u_int16_t l3num,
258 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700259{
260 struct nf_conntrack_l3proto *l3proto;
261 struct nf_conntrack_l4proto *l4proto;
262 unsigned int protoff;
263 u_int8_t protonum;
264 int ret;
265
266 rcu_read_lock();
267
268 l3proto = __nf_ct_l3proto_find(l3num);
269 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
270 if (ret != NF_ACCEPT) {
271 rcu_read_unlock();
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200272 return false;
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700273 }
274
275 l4proto = __nf_ct_l4proto_find(l3num, protonum);
276
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500277 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700278 l3proto, l4proto);
279
280 rcu_read_unlock();
281 return ret;
282}
283EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
284
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200285bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
287 const struct nf_conntrack_tuple *orig,
288 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100289 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290{
Philip Craig443a70d2008-04-29 03:35:10 -0700291 memset(inverse, 0, sizeof(*inverse));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292
293 inverse->src.l3num = orig->src.l3num;
294 if (l3proto->invert_tuple(inverse, orig) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200295 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800296
297 inverse->dst.dir = !orig->dst.dir;
298
299 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100300 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301}
Patrick McHardy13b18332006-12-02 22:11:25 -0800302EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304static void
305clean_from_lists(struct nf_conn *ct)
306{
Patrick McHardy0d537782007-07-07 22:39:38 -0700307 pr_debug("clean_from_lists(%p)\n", ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100308 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
309 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800310
311 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800312 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313}
314
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100315/* must be called with local_bh_disable */
316static void nf_ct_add_to_dying_list(struct nf_conn *ct)
317{
318 struct ct_pcpu *pcpu;
319
320 /* add this conntrack to the (per cpu) dying list */
321 ct->cpu = smp_processor_id();
322 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
323
324 spin_lock(&pcpu->lock);
325 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
326 &pcpu->dying);
327 spin_unlock(&pcpu->lock);
328}
329
330/* must be called with local_bh_disable */
331static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
332{
333 struct ct_pcpu *pcpu;
334
335 /* add this conntrack to the (per cpu) unconfirmed list */
336 ct->cpu = smp_processor_id();
337 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
338
339 spin_lock(&pcpu->lock);
340 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
341 &pcpu->unconfirmed);
342 spin_unlock(&pcpu->lock);
343}
344
345/* must be called with local_bh_disable */
346static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
347{
348 struct ct_pcpu *pcpu;
349
350 /* We overload first tuple to link into unconfirmed or dying list.*/
351 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
352
353 spin_lock(&pcpu->lock);
354 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
355 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
356 spin_unlock(&pcpu->lock);
357}
358
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200359/* Released via destroy_conntrack() */
Daniel Borkmann308ac912015-08-08 21:40:01 +0200360struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
361 const struct nf_conntrack_zone *zone,
362 gfp_t flags)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200363{
364 struct nf_conn *tmpl;
365
Joe Stringerf58e5aa2015-08-04 18:34:00 -0700366 tmpl = kzalloc(sizeof(*tmpl), flags);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200367 if (tmpl == NULL)
368 return NULL;
369
370 tmpl->status = IPS_TEMPLATE;
371 write_pnet(&tmpl->ct_net, net);
Florian Westphal6c8dee92016-06-11 21:57:35 +0200372 nf_ct_zone_add(tmpl, zone);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200373 atomic_set(&tmpl->ct_general.use, 0);
374
375 return tmpl;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200376}
377EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
378
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200379void nf_ct_tmpl_free(struct nf_conn *tmpl)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200380{
381 nf_ct_ext_destroy(tmpl);
382 nf_ct_ext_free(tmpl);
383 kfree(tmpl);
384}
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200385EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200386
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387static void
388destroy_conntrack(struct nf_conntrack *nfct)
389{
390 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100391 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392
Patrick McHardy0d537782007-07-07 22:39:38 -0700393 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200396 if (unlikely(nf_ct_is_template(ct))) {
397 nf_ct_tmpl_free(ct);
398 return;
399 }
Patrick McHardy923f4902007-02-12 11:12:57 -0800400 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200401 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Pablo Neira Ayuso4b4ceb92016-05-01 00:34:37 +0200402 if (l4proto->destroy)
Martin Josefsson605dcad2006-11-29 02:35:06 +0100403 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404
Patrick McHardy982d9a92007-02-12 11:14:11 -0800405 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100407 local_bh_disable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800408 /* Expectations will have been removed in clean_from_lists,
409 * except TFTP can create an expectation on the first packet,
410 * before connection is in the list, so we need to clean here,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100411 * too.
412 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800413 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100415 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100417 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418
419 if (ct->master)
420 nf_ct_put(ct->master);
421
Patrick McHardy0d537782007-07-07 22:39:38 -0700422 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 nf_conntrack_free(ct);
424}
425
Florian Westphal02982c22013-07-29 15:41:54 +0200426static void nf_ct_delete_from_lists(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800427{
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200428 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100429 unsigned int hash, reply_hash;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100430 unsigned int sequence;
Patrick McHardy5397e972007-05-19 14:23:52 -0700431
Pablo Neira Ayuso9858a3a2009-06-13 12:28:22 +0200432 nf_ct_helper_destroy(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100433
434 local_bh_disable();
435 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200436 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200437 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100438 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200439 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100440 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
441 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
442
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443 clean_from_lists(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100444 nf_conntrack_double_unlock(hash, reply_hash);
445
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100446 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100447
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100448 local_bh_enable();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200449}
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200450
Florian Westphal02982c22013-07-29 15:41:54 +0200451bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200452{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100453 struct nf_conn_tstamp *tstamp;
454
Florian Westphalf330a7f2016-08-25 15:33:31 +0200455 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
456 return false;
457
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100458 tstamp = nf_conn_tstamp_find(ct);
459 if (tstamp && tstamp->stop == 0)
Eric Dumazetd2de8752014-08-22 18:32:09 -0700460 tstamp->stop = ktime_get_real_ns();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200461
Florian Westphal95005072014-06-10 23:12:56 +0200462 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
463 portid, report) < 0) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200464 /* destroy event was not delivered. nf_ct_put will
465 * be done by event cache worker on redelivery.
466 */
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200467 nf_ct_delete_from_lists(ct);
Florian Westphal95005072014-06-10 23:12:56 +0200468 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
Florian Westphal02982c22013-07-29 15:41:54 +0200469 return false;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200470 }
Florian Westphal95005072014-06-10 23:12:56 +0200471
472 nf_conntrack_ecache_work(nf_ct_net(ct));
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200473 nf_ct_delete_from_lists(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800474 nf_ct_put(ct);
Florian Westphal02982c22013-07-29 15:41:54 +0200475 return true;
476}
477EXPORT_SYMBOL_GPL(nf_ct_delete);
478
Andrey Vaginc6825c02014-01-29 19:34:14 +0100479static inline bool
480nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
Daniel Borkmann308ac912015-08-08 21:40:01 +0200481 const struct nf_conntrack_tuple *tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200482 const struct nf_conntrack_zone *zone,
483 const struct net *net)
Andrey Vaginc6825c02014-01-29 19:34:14 +0100484{
485 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
486
487 /* A conntrack can be recreated with the equal tuple,
488 * so we need to check that the conntrack is confirmed
489 */
490 return nf_ct_tuple_equal(tuple, &h->tuple) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200491 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200492 nf_ct_is_confirmed(ct) &&
493 net_eq(net, nf_ct_net(ct));
Andrey Vaginc6825c02014-01-29 19:34:14 +0100494}
495
Florian Westphalf330a7f2016-08-25 15:33:31 +0200496/* caller must hold rcu readlock and none of the nf_conntrack_locks */
497static void nf_ct_gc_expired(struct nf_conn *ct)
498{
499 if (!atomic_inc_not_zero(&ct->ct_general.use))
500 return;
501
502 if (nf_ct_should_gc(ct))
503 nf_ct_kill(ct);
504
505 nf_ct_put(ct);
506}
507
Eric Dumazetea781f12009-03-25 21:05:46 +0100508/*
509 * Warning :
510 * - Caller must take a reference on returned object
511 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
Eric Dumazetea781f12009-03-25 21:05:46 +0100512 */
Changli Gao99f07e92010-09-21 17:49:20 +0200513static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200514____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200515 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516{
517 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200518 struct hlist_nulls_head *ct_hash;
Eric Dumazetea781f12009-03-25 21:05:46 +0100519 struct hlist_nulls_node *n;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800520 unsigned int bucket, hsize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800521
Eric Dumazetea781f12009-03-25 21:05:46 +0100522begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800523 nf_conntrack_get_ht(&ct_hash, &hsize);
524 bucket = reciprocal_scale(hash, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200525
526 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200527 struct nf_conn *ct;
528
529 ct = nf_ct_tuplehash_to_ctrack(h);
530 if (nf_ct_is_expired(ct)) {
531 nf_ct_gc_expired(ct);
532 continue;
533 }
534
535 if (nf_ct_is_dying(ct))
536 continue;
537
Florian Westphal8e8118f2016-09-11 22:55:53 +0200538 if (nf_ct_key_equal(h, tuple, zone, net))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800539 return h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800540 }
Eric Dumazetea781f12009-03-25 21:05:46 +0100541 /*
542 * if the nulls value we got at the end of this lookup is
543 * not the expected one, we must restart lookup.
544 * We probably met an item that was moved to another chain.
545 */
Changli Gao99f07e92010-09-21 17:49:20 +0200546 if (get_nulls_value(n) != bucket) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200547 NF_CT_STAT_INC_ATOMIC(net, search_restart);
Eric Dumazetea781f12009-03-25 21:05:46 +0100548 goto begin;
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200549 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800550
551 return NULL;
552}
Changli Gao99f07e92010-09-21 17:49:20 +0200553
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800554/* Find a connection corresponding to a tuple. */
Changli Gao99f07e92010-09-21 17:49:20 +0200555static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200556__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200557 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800558{
559 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800560 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800561
Patrick McHardy76507f62008-01-31 04:38:38 -0800562 rcu_read_lock();
Eric Dumazetea781f12009-03-25 21:05:46 +0100563begin:
Changli Gao99f07e92010-09-21 17:49:20 +0200564 h = ____nf_conntrack_find(net, zone, tuple, hash);
Patrick McHardy76507f62008-01-31 04:38:38 -0800565 if (h) {
566 ct = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy8d8890b72009-06-22 14:14:41 +0200567 if (unlikely(nf_ct_is_dying(ct) ||
568 !atomic_inc_not_zero(&ct->ct_general.use)))
Patrick McHardy76507f62008-01-31 04:38:38 -0800569 h = NULL;
Eric Dumazetea781f12009-03-25 21:05:46 +0100570 else {
Florian Westphale0c7d472016-04-28 19:13:45 +0200571 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
Eric Dumazetea781f12009-03-25 21:05:46 +0100572 nf_ct_put(ct);
573 goto begin;
574 }
575 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800576 }
577 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578
579 return h;
580}
Changli Gao99f07e92010-09-21 17:49:20 +0200581
582struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200583nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200584 const struct nf_conntrack_tuple *tuple)
585{
586 return __nf_conntrack_find_get(net, zone, tuple,
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200587 hash_conntrack_raw(tuple, net));
Changli Gao99f07e92010-09-21 17:49:20 +0200588}
Patrick McHardy13b18332006-12-02 22:11:25 -0800589EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800590
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800591static void __nf_conntrack_hash_insert(struct nf_conn *ct,
592 unsigned int hash,
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100593 unsigned int reply_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800594{
Eric Dumazetea781f12009-03-25 21:05:46 +0100595 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200596 &nf_conntrack_hash[hash]);
Eric Dumazetea781f12009-03-25 21:05:46 +0100597 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200598 &nf_conntrack_hash[reply_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800599}
600
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100601int
602nf_conntrack_hash_check_insert(struct nf_conn *ct)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800603{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200604 const struct nf_conntrack_zone *zone;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800605 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100606 unsigned int hash, reply_hash;
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100607 struct nf_conntrack_tuple_hash *h;
608 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100609 unsigned int sequence;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800610
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100611 zone = nf_ct_zone(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800612
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100613 local_bh_disable();
614 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200615 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200616 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100617 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200618 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100619 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
620 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100621
622 /* See if there's one in the list already, including reverse */
Florian Westphal56d52d42016-05-02 18:39:55 +0200623 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200624 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200625 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100626 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200627
Florian Westphal56d52d42016-05-02 18:39:55 +0200628 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200629 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200630 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100631 goto out;
632
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100633 smp_wmb();
634 /* The caller holds a reference to this object */
635 atomic_set(&ct->ct_general.use, 2);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100636 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100637 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100638 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100639 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100640 return 0;
641
642out:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100643 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100644 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100645 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100646 return -EEXIST;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800647}
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100648EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800649
Pablo Neira Ayusoba767382016-05-02 21:28:57 +0200650static inline void nf_ct_acct_update(struct nf_conn *ct,
651 enum ip_conntrack_info ctinfo,
652 unsigned int len)
653{
654 struct nf_conn_acct *acct;
655
656 acct = nf_conn_acct_find(ct);
657 if (acct) {
658 struct nf_conn_counter *counter = acct->counter;
659
660 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
661 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
662 }
663}
664
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200665static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
666 const struct nf_conn *loser_ct)
667{
668 struct nf_conn_acct *acct;
669
670 acct = nf_conn_acct_find(loser_ct);
671 if (acct) {
672 struct nf_conn_counter *counter = acct->counter;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200673 unsigned int bytes;
674
675 /* u32 should be fine since we must have seen one packet. */
676 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
677 nf_ct_acct_update(ct, ctinfo, bytes);
678 }
679}
680
681/* Resolve race on insertion if this protocol allows this. */
682static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
683 enum ip_conntrack_info ctinfo,
684 struct nf_conntrack_tuple_hash *h)
685{
686 /* This is the conntrack entry already in hashes that won race. */
687 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
688 struct nf_conntrack_l4proto *l4proto;
689
690 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
691 if (l4proto->allow_clash &&
Florian Westphal25db12f2017-03-28 10:31:03 +0200692 ((ct->status & IPS_NAT_DONE_MASK) == 0) &&
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200693 !nf_ct_is_dying(ct) &&
694 atomic_inc_not_zero(&ct->ct_general.use)) {
695 nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct);
696 nf_conntrack_put(skb->nfct);
697 /* Assign conntrack already in hashes to this skbuff. Don't
698 * modify skb->nfctinfo to ensure consistent stateful filtering.
699 */
700 skb->nfct = &ct->ct_general;
701 return NF_ACCEPT;
702 }
703 NF_CT_STAT_INC(net, drop);
704 return NF_DROP;
705}
706
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800707/* Confirm a connection given skb; places it in hash table */
708int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700709__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800710{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200711 const struct nf_conntrack_zone *zone;
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100712 unsigned int hash, reply_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700713 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800714 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700715 struct nf_conn_help *help;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100716 struct nf_conn_tstamp *tstamp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100717 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800718 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200719 struct net *net;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100720 unsigned int sequence;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200721 int ret = NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800722
Herbert Xu3db05fe2007-10-15 00:53:15 -0700723 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200724 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800725
726 /* ipt_REJECT uses nf_conntrack_attach to attach related
727 ICMP/TCP RST packets in other direction. Actual packet
728 which created connection will be IP_CT_NEW or for an
729 expected connection, IP_CT_RELATED. */
730 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
731 return NF_ACCEPT;
732
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100733 zone = nf_ct_zone(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100734 local_bh_disable();
735
736 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200737 sequence = read_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100738 /* reuse the hash saved before */
739 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
Florian Westphal56d52d42016-05-02 18:39:55 +0200740 hash = scale_hash(hash);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200741 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100742 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
743
744 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800745
746 /* We're not in hash table, and we refuse to set up related
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100747 * connections for unconfirmed conns. But packet copies and
748 * REJECT will give spurious warnings here.
749 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800750 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
751
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300752 /* No external references means no one else could have
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100753 * confirmed us.
754 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800755 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700756 pr_debug("Confirming conntrack %p\n", ct);
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100757 /* We have to check the DYING flag after unlink to prevent
758 * a race against nf_ct_get_next_corpse() possibly called from
759 * user context, else we insert an already 'dead' hash, blocking
760 * further use of that particular connection -JM.
761 */
762 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Joerg Marxfc350772010-05-20 15:55:30 +0200763
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200764 if (unlikely(nf_ct_is_dying(ct))) {
765 nf_ct_add_to_dying_list(ct);
766 goto dying;
767 }
Joerg Marxfc350772010-05-20 15:55:30 +0200768
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800769 /* See if there's one in the list already, including reverse:
770 NAT could have grabbed it without realizing, since we're
771 not in the hash. If there is, we lost race. */
Florian Westphal56d52d42016-05-02 18:39:55 +0200772 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200773 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200774 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700775 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200776
Florian Westphal56d52d42016-05-02 18:39:55 +0200777 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200778 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200779 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700780 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800781
Patrick McHardydf0933d2006-09-20 11:57:53 -0700782 /* Timer relative to confirmation time, not original
783 setting time, otherwise we'd get timer wrap in
784 weird delay cases. */
Florian Westphalf330a7f2016-08-25 15:33:31 +0200785 ct->timeout += nfct_time_stamp;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700786 atomic_inc(&ct->ct_general.use);
Changli Gao45eec342011-01-18 15:08:13 +0100787 ct->status |= IPS_CONFIRMED;
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200788
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100789 /* set conntrack timestamp, if enabled. */
790 tstamp = nf_conn_tstamp_find(ct);
791 if (tstamp) {
792 if (skb->tstamp.tv64 == 0)
Joe Perchese3192692012-06-03 17:41:40 +0000793 __net_timestamp(skb);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100794
795 tstamp->start = ktime_to_ns(skb->tstamp);
796 }
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200797 /* Since the lookup is lockless, hash insertion must be done after
798 * starting the timer and setting the CONFIRMED bit. The RCU barriers
799 * guarantee that no other CPU can find the conntrack before the above
800 * stores are visible.
801 */
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100802 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100803 nf_conntrack_double_unlock(hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100804 local_bh_enable();
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200805
Patrick McHardydf0933d2006-09-20 11:57:53 -0700806 help = nfct_help(ct);
807 if (help && help->helper)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200808 nf_conntrack_event_cache(IPCT_HELPER, ct);
Pablo Neira Ayuso17e6e4e2009-06-02 20:08:46 +0200809
Patrick McHardydf0933d2006-09-20 11:57:53 -0700810 nf_conntrack_event_cache(master_ct(ct) ?
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200811 IPCT_RELATED : IPCT_NEW, ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700812 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800813
Patrick McHardydf0933d2006-09-20 11:57:53 -0700814out:
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100815 nf_ct_add_to_dying_list(ct);
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200816 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
817dying:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100818 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200819 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100820 local_bh_enable();
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200821 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800822}
Patrick McHardy13b18332006-12-02 22:11:25 -0800823EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800824
825/* Returns true if a connection correspondings to the tuple (required
826 for NAT). */
827int
828nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
829 const struct nf_conn *ignored_conntrack)
830{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200831 struct net *net = nf_ct_net(ignored_conntrack);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200832 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800833 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200834 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800835 unsigned int hash, hsize;
Eric Dumazetea781f12009-03-25 21:05:46 +0100836 struct hlist_nulls_node *n;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100837 struct nf_conn *ct;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200838
839 zone = nf_ct_zone(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800840
Florian Westphal2cf12342016-04-28 19:13:40 +0200841 rcu_read_lock();
Florian Westphal95a8d192016-08-25 15:33:29 +0200842 begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800843 nf_conntrack_get_ht(&ct_hash, &hsize);
844 hash = __hash_conntrack(net, tuple, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200845
846 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100847 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphalf330a7f2016-08-25 15:33:31 +0200848
849 if (ct == ignored_conntrack)
850 continue;
851
852 if (nf_ct_is_expired(ct)) {
853 nf_ct_gc_expired(ct);
854 continue;
855 }
856
857 if (nf_ct_key_equal(h, tuple, zone, net)) {
Martynas Pumputis92044922019-01-29 15:51:42 +0100858 /* Tuple is taken already, so caller will need to find
859 * a new source port to use.
860 *
861 * Only exception:
862 * If the *original tuples* are identical, then both
863 * conntracks refer to the same flow.
864 * This is a rare situation, it can occur e.g. when
865 * more than one UDP packet is sent from same socket
866 * in different threads.
867 *
868 * Let nf_ct_resolve_clash() deal with this later.
869 */
870 if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
871 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple))
872 continue;
873
Florian Westphal2cf12342016-04-28 19:13:40 +0200874 NF_CT_STAT_INC_ATOMIC(net, found);
875 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800876 return 1;
877 }
Patrick McHardyba419af2008-01-31 04:39:23 -0800878 }
Florian Westphal95a8d192016-08-25 15:33:29 +0200879
880 if (get_nulls_value(n) != hash) {
881 NF_CT_STAT_INC_ATOMIC(net, search_restart);
882 goto begin;
883 }
884
Florian Westphal2cf12342016-04-28 19:13:40 +0200885 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800886
Patrick McHardyba419af2008-01-31 04:39:23 -0800887 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800888}
Patrick McHardy13b18332006-12-02 22:11:25 -0800889EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800890
Patrick McHardy7ae77302007-07-07 22:37:38 -0700891#define NF_CT_EVICTION_RANGE 8
892
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800893/* There's a small race here where we may free a just-assured
894 connection. Too bad: we're in trouble anyway. */
Florian Westphal242922a2016-07-03 20:44:01 +0200895static unsigned int early_drop_list(struct net *net,
896 struct hlist_nulls_head *head)
897{
898 struct nf_conntrack_tuple_hash *h;
899 struct hlist_nulls_node *n;
900 unsigned int drops = 0;
901 struct nf_conn *tmp;
902
903 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
904 tmp = nf_ct_tuplehash_to_ctrack(h);
905
Florian Westphalf330a7f2016-08-25 15:33:31 +0200906 if (nf_ct_is_expired(tmp)) {
907 nf_ct_gc_expired(tmp);
908 continue;
909 }
910
Florian Westphal242922a2016-07-03 20:44:01 +0200911 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
912 !net_eq(nf_ct_net(tmp), net) ||
913 nf_ct_is_dying(tmp))
914 continue;
915
916 if (!atomic_inc_not_zero(&tmp->ct_general.use))
917 continue;
918
919 /* kill only if still in same netns -- might have moved due to
920 * SLAB_DESTROY_BY_RCU rules.
921 *
922 * We steal the timer reference. If that fails timer has
923 * already fired or someone else deleted it. Just drop ref
924 * and move to next entry.
925 */
926 if (net_eq(nf_ct_net(tmp), net) &&
927 nf_ct_is_confirmed(tmp) &&
Florian Westphal242922a2016-07-03 20:44:01 +0200928 nf_ct_delete(tmp, 0, 0))
929 drops++;
930
931 nf_ct_put(tmp);
932 }
933
934 return drops;
935}
936
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -0700937static noinline int early_drop(struct net *net, unsigned int hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800938{
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -0700939 unsigned int i, bucket;
Florian Westphal3e866382016-05-02 18:40:14 +0200940
Florian Westphal242922a2016-07-03 20:44:01 +0200941 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
942 struct hlist_nulls_head *ct_hash;
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -0700943 unsigned int hsize, drops;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800944
Liping Zhang3101e0f2016-07-12 19:45:00 +0800945 rcu_read_lock();
Liping Zhang92e47ba2016-08-13 22:35:36 +0800946 nf_conntrack_get_ht(&ct_hash, &hsize);
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -0700947 if (!i)
948 bucket = reciprocal_scale(hash, hsize);
949 else
950 bucket = (bucket + 1) % hsize;
Florian Westphal3e866382016-05-02 18:40:14 +0200951
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -0700952 drops = early_drop_list(net, &ct_hash[bucket]);
Liping Zhang3101e0f2016-07-12 19:45:00 +0800953 rcu_read_unlock();
Florian Westphal3e866382016-05-02 18:40:14 +0200954
Florian Westphal242922a2016-07-03 20:44:01 +0200955 if (drops) {
956 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
957 return true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000958 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800959 }
Florian Westphal3e866382016-05-02 18:40:14 +0200960
Florian Westphal242922a2016-07-03 20:44:01 +0200961 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800962}
963
Florian Westphalb87a2f92016-08-25 15:33:33 +0200964static void gc_worker(struct work_struct *work)
965{
Florian Westphal371d0342017-01-18 02:01:22 +0100966 unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
Florian Westphalb87a2f92016-08-25 15:33:33 +0200967 unsigned int i, goal, buckets = 0, expired_count = 0;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200968 struct conntrack_gc_work *gc_work;
Florian Westphale0df8ca2016-11-04 16:54:58 +0100969 unsigned int ratio, scanned = 0;
970 unsigned long next_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200971
972 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
973
Florian Westphale0df8ca2016-11-04 16:54:58 +0100974 goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200975 i = gc_work->last_bucket;
976
977 do {
978 struct nf_conntrack_tuple_hash *h;
979 struct hlist_nulls_head *ct_hash;
980 struct hlist_nulls_node *n;
981 unsigned int hashsz;
982 struct nf_conn *tmp;
983
984 i++;
985 rcu_read_lock();
986
987 nf_conntrack_get_ht(&ct_hash, &hashsz);
988 if (i >= hashsz)
989 i = 0;
990
991 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
992 tmp = nf_ct_tuplehash_to_ctrack(h);
993
Florian Westphalc023c0e2016-08-25 15:33:34 +0200994 scanned++;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200995 if (nf_ct_is_expired(tmp)) {
996 nf_ct_gc_expired(tmp);
997 expired_count++;
998 continue;
999 }
1000 }
1001
1002 /* could check get_nulls_value() here and restart if ct
1003 * was moved to another chain. But given gc is best-effort
1004 * we will just continue with next hash slot.
1005 */
1006 rcu_read_unlock();
1007 cond_resched_rcu_qs();
Florian Westphal5f7ff592017-01-16 18:24:56 +01001008 } while (++buckets < goal);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001009
1010 if (gc_work->exiting)
1011 return;
1012
Florian Westphale0df8ca2016-11-04 16:54:58 +01001013 /*
1014 * Eviction will normally happen from the packet path, and not
1015 * from this gc worker.
1016 *
1017 * This worker is only here to reap expired entries when system went
1018 * idle after a busy period.
1019 *
1020 * The heuristics below are supposed to balance conflicting goals:
1021 *
1022 * 1. Minimize time until we notice a stale entry
1023 * 2. Maximize scan intervals to not waste cycles
1024 *
Florian Westphal371d0342017-01-18 02:01:22 +01001025 * Normally, expire ratio will be close to 0.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001026 *
Florian Westphal371d0342017-01-18 02:01:22 +01001027 * As soon as a sizeable fraction of the entries have expired
1028 * increase scan frequency.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001029 */
Florian Westphalc023c0e2016-08-25 15:33:34 +02001030 ratio = scanned ? expired_count * 100 / scanned : 0;
Florian Westphal371d0342017-01-18 02:01:22 +01001031 if (ratio > GC_EVICT_RATIO) {
1032 gc_work->next_gc_run = min_interval;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001033 } else {
Florian Westphal371d0342017-01-18 02:01:22 +01001034 unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001035
Florian Westphal371d0342017-01-18 02:01:22 +01001036 BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1037
1038 gc_work->next_gc_run += min_interval;
1039 if (gc_work->next_gc_run > max)
1040 gc_work->next_gc_run = max;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001041 }
Florian Westphalc023c0e2016-08-25 15:33:34 +02001042
Florian Westphal371d0342017-01-18 02:01:22 +01001043 next_run = gc_work->next_gc_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001044 gc_work->last_bucket = i;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001045 queue_delayed_work(system_long_wq, &gc_work->dwork, next_run);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001046}
1047
1048static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1049{
1050 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
Florian Westphal371d0342017-01-18 02:01:22 +01001051 gc_work->next_gc_run = HZ;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001052 gc_work->exiting = false;
1053}
1054
Changli Gao99f07e92010-09-21 17:49:20 +02001055static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +02001056__nf_conntrack_alloc(struct net *net,
1057 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001058 const struct nf_conntrack_tuple *orig,
1059 const struct nf_conntrack_tuple *repl,
1060 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001061{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001062 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001064 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001065 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001066
Patrick McHardy76eb9462008-01-31 04:41:44 -08001067 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001068 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001069 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001070 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +00001071 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001072 return ERR_PTR(-ENOMEM);
1073 }
1074 }
1075
Eric Dumazet941297f2009-07-16 14:03:40 +02001076 /*
1077 * Do not use kmem_cache_zalloc(), as this cache uses
1078 * SLAB_DESTROY_BY_RCU.
1079 */
Florian Westphal0c5366b2016-05-09 16:24:32 +02001080 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001081 if (ct == NULL)
1082 goto out;
1083
Patrick McHardy440f0d52009-06-10 14:32:47 +02001084 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001085 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +02001086 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -08001087 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +02001088 /* save hash for reusing when confirming */
1089 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +01001090 ct->status = 0;
Eric Dumazetc2d9ba92010-06-01 06:51:19 +00001091 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +01001092 memset(&ct->__nfct_init_offset[0], 0,
1093 offsetof(struct nf_conn, proto) -
1094 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001095
Florian Westphal6c8dee92016-06-11 21:57:35 +02001096 nf_ct_zone_add(ct, zone);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001097
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001098 /* Because we use RCU lookups, we set ct_general.use to zero before
1099 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +02001100 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001101 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001102 return ct;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001103out:
1104 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001105 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001106}
Changli Gao99f07e92010-09-21 17:49:20 +02001107
Daniel Borkmann308ac912015-08-08 21:40:01 +02001108struct nf_conn *nf_conntrack_alloc(struct net *net,
1109 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001110 const struct nf_conntrack_tuple *orig,
1111 const struct nf_conntrack_tuple *repl,
1112 gfp_t gfp)
1113{
1114 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1115}
Patrick McHardy13b18332006-12-02 22:11:25 -08001116EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001117
Patrick McHardyc88130b2008-01-31 04:42:11 -08001118void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001119{
Eric Dumazet1d452092009-03-24 14:26:50 +01001120 struct net *net = nf_ct_net(ct);
1121
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001122 /* A freed object has refcnt == 0, that's
1123 * the golden rule for SLAB_DESTROY_BY_RCU
1124 */
1125 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1126
Patrick McHardyceeff752008-06-11 17:51:10 -07001127 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +01001128 nf_ct_ext_free(ct);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001129 kmem_cache_free(nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001130 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +01001131 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001132}
Patrick McHardy13b18332006-12-02 22:11:25 -08001133EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001134
Florian Westphalc539f012013-01-11 06:30:44 +00001135
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001136/* Allocate a new conntrack: we return -ENOMEM if classification
1137 failed due to stress. Otherwise it really is unclassifiable. */
1138static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001139init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001140 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001141 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001142 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001143 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001144 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001145{
Patrick McHardyc88130b2008-01-31 04:42:11 -08001146 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001147 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001148 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001149 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001150 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +02001151 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001152 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001153 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001154 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001155
Martin Josefsson605dcad2006-11-29 02:35:06 +01001156 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001157 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001158 return NULL;
1159 }
1160
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001161 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +02001162 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1163 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -07001164 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -08001165 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001166
Gao Feng4440a2a2016-09-13 08:49:18 +08001167 if (!nf_ct_add_synproxy(ct, tmpl)) {
1168 nf_conntrack_free(ct);
1169 return ERR_PTR(-ENOMEM);
Patrick McHardy48b1de42013-08-27 08:50:14 +02001170 }
1171
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001172 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001173 if (timeout_ext) {
1174 timeouts = nf_ct_timeout_data(timeout_ext);
1175 if (unlikely(!timeouts))
1176 timeouts = l4proto->get_timeouts(net);
1177 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001178 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001179 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001180
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001181 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -08001182 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001183 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001184 return NULL;
1185 }
1186
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001187 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001188 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1189 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001190
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001191 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001192 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +00001193 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001194
1195 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1196 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1197 ecache ? ecache->expmask : 0,
1198 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001199
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001200 local_bh_disable();
1201 if (net->ct.expect_count) {
1202 spin_lock(&nf_conntrack_expect_lock);
1203 exp = nf_ct_find_expectation(net, zone, tuple);
1204 if (exp) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001205 pr_debug("expectation arrives ct=%p exp=%p\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001206 ct, exp);
1207 /* Welcome, Mr. Bond. We've been expecting you... */
1208 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1209 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1210 ct->master = exp->master;
1211 if (exp->helper) {
1212 help = nf_ct_helper_ext_add(ct, exp->helper,
1213 GFP_ATOMIC);
1214 if (help)
1215 rcu_assign_pointer(help->helper, exp->helper);
1216 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001217
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001218#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001219 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001220#endif
James Morris7c9728c2006-06-09 00:31:46 -07001221#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001222 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001223#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001224 NF_CT_STAT_INC(net, expect_new);
1225 }
1226 spin_unlock(&nf_conntrack_expect_lock);
1227 }
Florian Westphal8e8118f2016-09-11 22:55:53 +02001228 if (!exp)
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001229 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001230
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001231 /* Now it is inserted into the unconfirmed list, bump refcount */
1232 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001233 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001234
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001235 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001236
1237 if (exp) {
1238 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001239 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001240 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001241 }
1242
Patrick McHardyc88130b2008-01-31 04:42:11 -08001243 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001244}
1245
1246/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1247static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001248resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001249 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001250 unsigned int dataoff,
1251 u_int16_t l3num,
1252 u_int8_t protonum,
1253 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001254 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001255 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001256 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001257{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001258 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001259 struct nf_conntrack_tuple tuple;
1260 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001261 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001262 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001263 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001264
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001265 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001266 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001267 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001268 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001269 return NULL;
1270 }
1271
1272 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001273 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001274 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001275 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001276 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001277 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001278 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001279 if (!h)
1280 return NULL;
1281 if (IS_ERR(h))
1282 return (void *)h;
1283 }
1284 ct = nf_ct_tuplehash_to_ctrack(h);
1285
1286 /* It exists; we have (non-exclusive) reference. */
1287 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001288 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001289 /* Please set reply bit if this packet OK */
1290 *set_reply = 1;
1291 } else {
1292 /* Once we've had two way comms, always ESTABLISHED. */
1293 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001294 pr_debug("normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001295 *ctinfo = IP_CT_ESTABLISHED;
1296 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001297 pr_debug("related packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001298 *ctinfo = IP_CT_RELATED;
1299 } else {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001300 pr_debug("new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001301 *ctinfo = IP_CT_NEW;
1302 }
1303 *set_reply = 0;
1304 }
1305 skb->nfct = &ct->ct_general;
1306 skb->nfctinfo = *ctinfo;
1307 return ct;
1308}
1309
1310unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001311nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1312 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001313{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001314 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001315 enum ip_conntrack_info ctinfo;
1316 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001317 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001318 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001319 unsigned int dataoff;
1320 u_int8_t protonum;
1321 int set_reply = 0;
1322 int ret;
1323
Herbert Xu3db05fe2007-10-15 00:53:15 -07001324 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001325 /* Previously seen (loopback or untracked)? Ignore. */
1326 tmpl = (struct nf_conn *)skb->nfct;
1327 if (!nf_ct_is_template(tmpl)) {
1328 NF_CT_STAT_INC_ATOMIC(net, ignore);
1329 return NF_ACCEPT;
1330 }
1331 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001332 }
1333
Aaron Conolee2361cb2016-09-21 11:35:04 -04001334 /* rcu_read_lock()ed by nf_hook_thresh */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001335 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001336 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001337 &dataoff, &protonum);
1338 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001339 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001340 NF_CT_STAT_INC_ATOMIC(net, error);
1341 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001342 ret = -ret;
1343 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001344 }
1345
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001346 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001347
1348 /* It may be an special packet, error, unclean...
1349 * inverse of the return code tells to the netfilter
1350 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001351 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001352 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1353 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001354 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001355 NF_CT_STAT_INC_ATOMIC(net, error);
1356 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001357 ret = -ret;
1358 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001359 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001360 /* ICMP[v6] protocol trackers may assign one conntrack. */
1361 if (skb->nfct)
1362 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001363 }
1364
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001365 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001366 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001367 if (!ct) {
1368 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001369 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001370 ret = NF_ACCEPT;
1371 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001372 }
1373
1374 if (IS_ERR(ct)) {
1375 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001376 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001377 ret = NF_DROP;
1378 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001379 }
1380
Herbert Xu3db05fe2007-10-15 00:53:15 -07001381 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001382
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001383 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001384 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001385
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001386 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001387 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001388 /* Invalid: inverse of the return code tells
1389 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001390 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001391 nf_conntrack_put(skb->nfct);
1392 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001393 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001394 if (ret == -NF_DROP)
1395 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001396 ret = -ret;
1397 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001398 }
1399
1400 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001401 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001402out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001403 if (tmpl) {
1404 /* Special case: we have to repeat this hook, assign the
1405 * template again to this packet. We assume that this packet
1406 * has no conntrack assigned. This is used by nf_ct_tcp. */
1407 if (ret == NF_REPEAT)
1408 skb->nfct = (struct nf_conntrack *)tmpl;
1409 else
1410 nf_ct_put(tmpl);
1411 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001412
1413 return ret;
1414}
Patrick McHardy13b18332006-12-02 22:11:25 -08001415EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001416
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001417bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1418 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001419{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001420 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001421
1422 rcu_read_lock();
1423 ret = nf_ct_invert_tuple(inverse, orig,
1424 __nf_ct_l3proto_find(orig->src.l3num),
1425 __nf_ct_l4proto_find(orig->src.l3num,
1426 orig->dst.protonum));
1427 rcu_read_unlock();
1428 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001429}
Patrick McHardy13b18332006-12-02 22:11:25 -08001430EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001431
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001432/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1433 implicitly racy: see __nf_conntrack_confirm */
1434void nf_conntrack_alter_reply(struct nf_conn *ct,
1435 const struct nf_conntrack_tuple *newreply)
1436{
1437 struct nf_conn_help *help = nfct_help(ct);
1438
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001439 /* Should be unconfirmed, so not in hash table yet */
1440 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1441
Patrick McHardy0d537782007-07-07 22:39:38 -07001442 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001443 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001444
1445 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001446 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001447 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001448
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001449 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001450 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001451 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001452}
Patrick McHardy13b18332006-12-02 22:11:25 -08001453EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001454
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001455/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1456void __nf_ct_refresh_acct(struct nf_conn *ct,
1457 enum ip_conntrack_info ctinfo,
1458 const struct sk_buff *skb,
1459 unsigned long extra_jiffies,
1460 int do_acct)
1461{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001462 NF_CT_ASSERT(skb);
1463
Eric Leblond997ae832006-05-29 18:24:20 -07001464 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001465 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1466 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001467
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001468 /* If not in hash table, timer will not be active yet */
Florian Westphalf330a7f2016-08-25 15:33:31 +02001469 if (nf_ct_is_confirmed(ct))
1470 extra_jiffies += nfct_time_stamp;
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001471
Florian Westphalf330a7f2016-08-25 15:33:31 +02001472 ct->timeout = extra_jiffies;
Patrick McHardy47d95042008-01-31 04:36:31 -08001473acct:
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001474 if (do_acct)
1475 nf_ct_acct_update(ct, ctinfo, skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001476}
Patrick McHardy13b18332006-12-02 22:11:25 -08001477EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001478
Florian Westphalad667132016-08-25 15:33:35 +02001479bool nf_ct_kill_acct(struct nf_conn *ct,
1480 enum ip_conntrack_info ctinfo,
1481 const struct sk_buff *skb)
Patrick McHardy51091762008-06-09 15:59:06 -07001482{
Florian Westphalad667132016-08-25 15:33:35 +02001483 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001484
Florian Westphalf330a7f2016-08-25 15:33:31 +02001485 return nf_ct_delete(ct, 0, 0);
Patrick McHardy51091762008-06-09 15:59:06 -07001486}
Florian Westphalad667132016-08-25 15:33:35 +02001487EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001488
Igor Maravićc0cd1152011-12-12 02:58:24 +00001489#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001490
1491#include <linux/netfilter/nfnetlink.h>
1492#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001493#include <linux/mutex.h>
1494
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001495/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1496 * in ip_conntrack_core, since we don't want the protocols to autoload
1497 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001498int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001499 const struct nf_conntrack_tuple *tuple)
1500{
David S. Millerbae65be2012-04-01 18:58:28 -04001501 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1502 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1503 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001504 return 0;
1505
Patrick McHardydf6fb862007-09-28 14:37:03 -07001506nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001507 return -1;
1508}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001509EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001510
Patrick McHardyf73e9242007-09-28 14:39:55 -07001511const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1512 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1513 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001514};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001515EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001516
Patrick McHardyfdf70832007-09-28 14:37:41 -07001517int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001518 struct nf_conntrack_tuple *t)
1519{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001520 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001521 return -EINVAL;
1522
Patrick McHardy77236b62007-12-17 22:29:45 -08001523 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1524 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001525
1526 return 0;
1527}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001528EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001529
1530int nf_ct_port_nlattr_tuple_size(void)
1531{
1532 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1533}
1534EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001535#endif
1536
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001537/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001538static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001539{
1540 struct nf_conn *ct;
1541 enum ip_conntrack_info ctinfo;
1542
1543 /* This ICMP is in reverse direction to the packet which caused it */
1544 ct = nf_ct_get(skb, &ctinfo);
1545 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001546 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001547 else
1548 ctinfo = IP_CT_RELATED;
1549
1550 /* Attach to new skbuff, and increment count */
1551 nskb->nfct = &ct->ct_general;
1552 nskb->nfctinfo = ctinfo;
1553 nf_conntrack_get(nskb->nfct);
1554}
1555
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001556/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001557static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001558get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001559 void *data, unsigned int *bucket)
1560{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001561 struct nf_conntrack_tuple_hash *h;
1562 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001563 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001564 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001565
Florian Westphal56d52d42016-05-02 18:39:55 +02001566 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001567 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1568 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001569 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001570 if (*bucket < nf_conntrack_htable_size) {
1571 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001572 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1573 continue;
1574 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001575 if (net_eq(nf_ct_net(ct), net) &&
1576 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001577 goto found;
1578 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001579 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001580 spin_unlock(lockp);
1581 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001582 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001583 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001584
Patrick McHardydf0933d2006-09-20 11:57:53 -07001585 return NULL;
1586found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001587 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001588 spin_unlock(lockp);
1589 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001590 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001591}
1592
Florian Westphalf51c4542017-05-21 12:52:56 +02001593static void
1594__nf_ct_unconfirmed_destroy(struct net *net)
1595{
1596 int cpu;
1597
1598 for_each_possible_cpu(cpu) {
1599 struct nf_conntrack_tuple_hash *h;
1600 struct hlist_nulls_node *n;
1601 struct ct_pcpu *pcpu;
1602
1603 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1604
1605 spin_lock_bh(&pcpu->lock);
1606 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1607 struct nf_conn *ct;
1608
1609 ct = nf_ct_tuplehash_to_ctrack(h);
1610
1611 /* we cannot call iter() on unconfirmed list, the
1612 * owning cpu can reallocate ct->ext at any time.
1613 */
1614 set_bit(IPS_DYING_BIT, &ct->status);
1615 }
1616 spin_unlock_bh(&pcpu->lock);
1617 cond_resched();
1618 }
1619}
1620
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001621void nf_ct_iterate_cleanup(struct net *net,
1622 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001623 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001624{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001625 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001626 unsigned int bucket = 0;
1627
Florian Westphald93c6252016-01-20 11:16:43 +01001628 might_sleep();
1629
Florian Westphal88b68bc2016-04-28 19:13:42 +02001630 if (atomic_read(&net->ct.count) == 0)
1631 return;
1632
Florian Westphalf51c4542017-05-21 12:52:56 +02001633 __nf_ct_unconfirmed_destroy(net);
1634
1635 synchronize_net();
1636
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001637 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001638 /* Time to push up daises... */
Florian Westphal02982c22013-07-29 15:41:54 +02001639
Florian Westphalf330a7f2016-08-25 15:33:31 +02001640 nf_ct_delete(ct, portid, report);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001641 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001642 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001643 }
1644}
Patrick McHardy13b18332006-12-02 22:11:25 -08001645EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001646
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001647static int kill_all(struct nf_conn *i, void *data)
1648{
1649 return 1;
1650}
1651
Patrick McHardyd862a662011-01-14 15:45:56 +01001652void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001653{
Patrick McHardyd862a662011-01-14 15:45:56 +01001654 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001655 vfree(hash);
1656 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001657 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001658 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001659}
Patrick McHardyac565e52007-07-07 22:30:08 -07001660EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001661
Eric Dumazetb3c51632010-06-09 14:43:38 +02001662static int untrack_refs(void)
1663{
1664 int cnt = 0, cpu;
1665
1666 for_each_possible_cpu(cpu) {
1667 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1668
1669 cnt += atomic_read(&ct->ct_general.use) - 1;
1670 }
1671 return cnt;
1672}
1673
Gao fengf94161c2013-01-21 22:10:24 +00001674void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001675{
Florian Westphalb87a2f92016-08-25 15:33:33 +02001676 conntrack_gc_work.exiting = true;
Gao fengf94161c2013-01-21 22:10:24 +00001677 RCU_INIT_POINTER(ip_ct_attach, NULL);
1678}
1679
1680void nf_conntrack_cleanup_end(void)
1681{
1682 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001683 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001684 schedule();
1685
Florian Westphalb87a2f92016-08-25 15:33:33 +02001686 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
Florian Westphal56d52d42016-05-02 18:39:55 +02001687 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1688
Gao feng04d87002013-01-21 22:10:32 +00001689 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001690 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001691 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001692 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001693 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001694 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001695 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001696 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001697 nf_conntrack_expect_fini();
Florian Westphal77571142016-06-10 17:25:19 +02001698
1699 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001700}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001701
Gao fengf94161c2013-01-21 22:10:24 +00001702/*
1703 * Mishearing the voices in his head, our hero wonders how he's
1704 * supposed to kill the mall.
1705 */
1706void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001707{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001708 LIST_HEAD(single);
1709
1710 list_add(&net->exit_list, &single);
1711 nf_conntrack_cleanup_net_list(&single);
1712}
1713
1714void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1715{
1716 int busy;
1717 struct net *net;
1718
Gao fengf94161c2013-01-21 22:10:24 +00001719 /*
1720 * This makes sure all current packets have passed through
1721 * netfilter framework. Roll on, two-stage module
1722 * delete...
1723 */
1724 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001725i_see_dead_people:
1726 busy = 0;
1727 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001728 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001729 if (atomic_read(&net->ct.count) != 0)
1730 busy = 1;
1731 }
1732 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001733 schedule();
1734 goto i_see_dead_people;
1735 }
1736
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001737 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001738 nf_conntrack_proto_pernet_fini(net);
1739 nf_conntrack_helper_pernet_fini(net);
1740 nf_conntrack_ecache_pernet_fini(net);
1741 nf_conntrack_tstamp_pernet_fini(net);
1742 nf_conntrack_acct_pernet_fini(net);
1743 nf_conntrack_expect_pernet_fini(net);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001744 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001745 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001746 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001747}
1748
Patrick McHardyd862a662011-01-14 15:45:56 +01001749void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001750{
Eric Dumazetea781f12009-03-25 21:05:46 +01001751 struct hlist_nulls_head *hash;
1752 unsigned int nr_slots, i;
1753 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001754
Florian Westphal9cc1c732016-04-24 01:18:21 +02001755 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1756 return NULL;
1757
Eric Dumazetea781f12009-03-25 21:05:46 +01001758 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1759 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
Florian Westphal9cc1c732016-04-24 01:18:21 +02001760
1761 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1762 return NULL;
1763
Eric Dumazetea781f12009-03-25 21:05:46 +01001764 sz = nr_slots * sizeof(struct hlist_nulls_head);
1765 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1766 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001767 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001768 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001769
Eric Dumazetea781f12009-03-25 21:05:46 +01001770 if (hash && nulls)
1771 for (i = 0; i < nr_slots; i++)
1772 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001773
1774 return hash;
1775}
Patrick McHardyac565e52007-07-07 22:30:08 -07001776EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001777
Florian Westphal3183ab82016-06-22 13:26:10 +02001778int nf_conntrack_hash_resize(unsigned int hashsize)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001779{
Florian Westphal3183ab82016-06-22 13:26:10 +02001780 int i, bucket;
1781 unsigned int old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001782 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001783 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001784 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001785
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001786 if (!hashsize)
1787 return -EINVAL;
1788
Patrick McHardyd862a662011-01-14 15:45:56 +01001789 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001790 if (!hash)
1791 return -ENOMEM;
1792
Florian Westphal3183ab82016-06-22 13:26:10 +02001793 old_size = nf_conntrack_htable_size;
1794 if (old_size == hashsize) {
1795 nf_ct_free_hashtable(hash, hashsize);
1796 return 0;
1797 }
1798
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001799 local_bh_disable();
1800 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001801 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001802
Patrick McHardy76507f62008-01-31 04:38:38 -08001803 /* Lookups in the old hash might happen in parallel, which means we
1804 * might get false negatives during connection lookup. New connections
1805 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001806 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001807 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001808
Florian Westphal56d52d42016-05-02 18:39:55 +02001809 for (i = 0; i < nf_conntrack_htable_size; i++) {
1810 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1811 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1812 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001813 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001814 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001815 bucket = __hash_conntrack(nf_ct_net(ct),
1816 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001817 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001818 }
1819 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001820 old_size = nf_conntrack_htable_size;
1821 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001822
Florian Westphal56d52d42016-05-02 18:39:55 +02001823 nf_conntrack_hash = hash;
1824 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001825
Florian Westphala3efd812016-04-18 16:16:59 +02001826 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001827 nf_conntrack_all_unlock();
1828 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001829
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001830 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001831 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001832 return 0;
1833}
Florian Westphal3183ab82016-06-22 13:26:10 +02001834
1835int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1836{
1837 unsigned int hashsize;
1838 int rc;
1839
1840 if (current->nsproxy->net_ns != &init_net)
1841 return -EOPNOTSUPP;
1842
1843 /* On boot, we can set this without any fancy locking. */
Andrey Ryabinind8a77d12018-07-06 16:38:53 +03001844 if (!nf_conntrack_hash)
Florian Westphal3183ab82016-06-22 13:26:10 +02001845 return param_set_uint(val, kp);
1846
1847 rc = kstrtouint(val, 0, &hashsize);
1848 if (rc)
1849 return rc;
1850
1851 return nf_conntrack_hash_resize(hashsize);
1852}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001853EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001854
Patrick McHardyfae718d2007-12-24 21:09:10 -08001855module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001856 &nf_conntrack_htable_size, 0600);
1857
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001858void nf_ct_untracked_status_or(unsigned long bits)
1859{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001860 int cpu;
1861
1862 for_each_possible_cpu(cpu)
1863 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001864}
1865EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1866
Gao fengf94161c2013-01-21 22:10:24 +00001867int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001868{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001869 int max_factor = 8;
Florian Westphal0c5366b2016-05-09 16:24:32 +02001870 int ret = -ENOMEM;
1871 int i, cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001872
Florian Westphala3efd812016-04-18 16:16:59 +02001873 seqcount_init(&nf_conntrack_generation);
1874
Eric Dumazetd5d20912014-03-17 13:37:53 -07001875 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001876 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001877
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001878 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001879 /* Idea from tcp.c: use 1/16384 of memory.
1880 * On i386: 32MB machine has 512 buckets.
1881 * >= 1GB machines have 16384 buckets.
1882 * >= 4GB machines have 65536 buckets.
1883 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001884 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07001885 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001886 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02001887 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1888 nf_conntrack_htable_size = 65536;
1889 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001890 nf_conntrack_htable_size = 16384;
1891 if (nf_conntrack_htable_size < 32)
1892 nf_conntrack_htable_size = 32;
1893
1894 /* Use a max. factor of four by default to get the same max as
1895 * with the old struct list_heads. When a table size is given
1896 * we use the old value of 8 to avoid reducing the max.
1897 * entries. */
1898 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001899 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001900
1901 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1902 if (!nf_conntrack_hash)
1903 return -ENOMEM;
1904
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001905 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001906
Florian Westphal0c5366b2016-05-09 16:24:32 +02001907 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1908 sizeof(struct nf_conn), 0,
Florian Westphal5a75cde2016-06-10 22:25:22 +02001909 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001910 if (!nf_conntrack_cachep)
1911 goto err_cachep;
1912
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001913 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001914 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1915 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00001916
1917 ret = nf_conntrack_expect_init();
1918 if (ret < 0)
1919 goto err_expect;
1920
Gao fengb7ff3a12013-01-21 22:10:26 +00001921 ret = nf_conntrack_acct_init();
1922 if (ret < 0)
1923 goto err_acct;
1924
Gao feng73f40012013-01-21 22:10:27 +00001925 ret = nf_conntrack_tstamp_init();
1926 if (ret < 0)
1927 goto err_tstamp;
1928
Gao feng3fe0f942013-01-21 22:10:28 +00001929 ret = nf_conntrack_ecache_init();
1930 if (ret < 0)
1931 goto err_ecache;
1932
Gao feng86840942013-01-21 22:10:29 +00001933 ret = nf_conntrack_timeout_init();
1934 if (ret < 0)
1935 goto err_timeout;
1936
Gao feng5e615b22013-01-21 22:10:30 +00001937 ret = nf_conntrack_helper_init();
1938 if (ret < 0)
1939 goto err_helper;
1940
Gao feng5f69b8f2013-01-21 22:10:31 +00001941 ret = nf_conntrack_labels_init();
1942 if (ret < 0)
1943 goto err_labels;
1944
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001945 ret = nf_conntrack_seqadj_init();
1946 if (ret < 0)
1947 goto err_seqadj;
1948
Gao feng04d87002013-01-21 22:10:32 +00001949 ret = nf_conntrack_proto_init();
1950 if (ret < 0)
1951 goto err_proto;
1952
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001953 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02001954 for_each_possible_cpu(cpu) {
1955 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001956 write_pnet(&ct->ct_net, &init_net);
1957 atomic_set(&ct->ct_general.use, 1);
1958 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001959 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001960 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001961
1962 conntrack_gc_work_init(&conntrack_gc_work);
Florian Westphal371d0342017-01-18 02:01:22 +01001963 queue_delayed_work(system_long_wq, &conntrack_gc_work.dwork, HZ);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001964
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001965 return 0;
1966
Gao feng04d87002013-01-21 22:10:32 +00001967err_proto:
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001968 nf_conntrack_seqadj_fini();
1969err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00001970 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001971err_labels:
1972 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001973err_helper:
1974 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00001975err_timeout:
1976 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001977err_ecache:
1978 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00001979err_tstamp:
1980 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001981err_acct:
1982 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001983err_expect:
Florian Westphal0c5366b2016-05-09 16:24:32 +02001984 kmem_cache_destroy(nf_conntrack_cachep);
1985err_cachep:
Florian Westphal56d52d42016-05-02 18:39:55 +02001986 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001987 return ret;
1988}
1989
Gao fengf94161c2013-01-21 22:10:24 +00001990void nf_conntrack_init_end(void)
1991{
1992 /* For use by REJECT target */
1993 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1994 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00001995}
1996
Eric Dumazet8cc20192009-06-22 14:13:55 +02001997/*
1998 * We need to use special "null" values, not used in hash table
1999 */
2000#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
2001#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00002002#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02002003
Gao fengf94161c2013-01-21 22:10:24 +00002004int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002005{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002006 int ret = -ENOMEM;
2007 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002008
2009 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002010
2011 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
2012 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002013 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002014
2015 for_each_possible_cpu(cpu) {
2016 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
2017
2018 spin_lock_init(&pcpu->lock);
2019 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
2020 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002021 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08002022
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002023 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2024 if (!net->ct.stat)
2025 goto err_pcpu_lists;
2026
Gao feng83b4dbe2013-01-21 22:10:25 +00002027 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002028 if (ret < 0)
2029 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00002030 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07002031 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002032 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00002033 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002034 if (ret < 0)
2035 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00002036 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002037 if (ret < 0)
2038 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00002039 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002040 if (ret < 0)
2041 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00002042 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00002043 if (ret < 0)
2044 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002045 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00002046
Gao fengf94161c2013-01-21 22:10:24 +00002047err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00002048 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002049err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00002050 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002051err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00002052 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002053err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00002054 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002055err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00002056 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002057err_expect:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002058 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002059err_pcpu_lists:
2060 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002061err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002062 return ret;
2063}