blob: b0f11e5de1e2af0629af6748c9f74b86469f5b4a [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)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200858 NF_CT_STAT_INC_ATOMIC(net, found);
859 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800860 return 1;
861 }
Patrick McHardyba419af2008-01-31 04:39:23 -0800862 }
Florian Westphal95a8d192016-08-25 15:33:29 +0200863
864 if (get_nulls_value(n) != hash) {
865 NF_CT_STAT_INC_ATOMIC(net, search_restart);
866 goto begin;
867 }
868
Florian Westphal2cf12342016-04-28 19:13:40 +0200869 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800870
Patrick McHardyba419af2008-01-31 04:39:23 -0800871 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800872}
Patrick McHardy13b18332006-12-02 22:11:25 -0800873EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800874
Patrick McHardy7ae77302007-07-07 22:37:38 -0700875#define NF_CT_EVICTION_RANGE 8
876
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800877/* There's a small race here where we may free a just-assured
878 connection. Too bad: we're in trouble anyway. */
Florian Westphal242922a2016-07-03 20:44:01 +0200879static unsigned int early_drop_list(struct net *net,
880 struct hlist_nulls_head *head)
881{
882 struct nf_conntrack_tuple_hash *h;
883 struct hlist_nulls_node *n;
884 unsigned int drops = 0;
885 struct nf_conn *tmp;
886
887 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
888 tmp = nf_ct_tuplehash_to_ctrack(h);
889
Florian Westphalf330a7f2016-08-25 15:33:31 +0200890 if (nf_ct_is_expired(tmp)) {
891 nf_ct_gc_expired(tmp);
892 continue;
893 }
894
Florian Westphal242922a2016-07-03 20:44:01 +0200895 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
896 !net_eq(nf_ct_net(tmp), net) ||
897 nf_ct_is_dying(tmp))
898 continue;
899
900 if (!atomic_inc_not_zero(&tmp->ct_general.use))
901 continue;
902
903 /* kill only if still in same netns -- might have moved due to
904 * SLAB_DESTROY_BY_RCU rules.
905 *
906 * We steal the timer reference. If that fails timer has
907 * already fired or someone else deleted it. Just drop ref
908 * and move to next entry.
909 */
910 if (net_eq(nf_ct_net(tmp), net) &&
911 nf_ct_is_confirmed(tmp) &&
Florian Westphal242922a2016-07-03 20:44:01 +0200912 nf_ct_delete(tmp, 0, 0))
913 drops++;
914
915 nf_ct_put(tmp);
916 }
917
918 return drops;
919}
920
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100921static noinline int early_drop(struct net *net, unsigned int _hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800922{
Florian Westphal242922a2016-07-03 20:44:01 +0200923 unsigned int i;
Florian Westphal3e866382016-05-02 18:40:14 +0200924
Florian Westphal242922a2016-07-03 20:44:01 +0200925 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
926 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800927 unsigned int hash, hsize, drops;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800928
Liping Zhang3101e0f2016-07-12 19:45:00 +0800929 rcu_read_lock();
Liping Zhang92e47ba2016-08-13 22:35:36 +0800930 nf_conntrack_get_ht(&ct_hash, &hsize);
931 hash = reciprocal_scale(_hash++, hsize);
Florian Westphal3e866382016-05-02 18:40:14 +0200932
Florian Westphal242922a2016-07-03 20:44:01 +0200933 drops = early_drop_list(net, &ct_hash[hash]);
Liping Zhang3101e0f2016-07-12 19:45:00 +0800934 rcu_read_unlock();
Florian Westphal3e866382016-05-02 18:40:14 +0200935
Florian Westphal242922a2016-07-03 20:44:01 +0200936 if (drops) {
937 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
938 return true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000939 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800940 }
Florian Westphal3e866382016-05-02 18:40:14 +0200941
Florian Westphal242922a2016-07-03 20:44:01 +0200942 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800943}
944
Florian Westphalb87a2f92016-08-25 15:33:33 +0200945static void gc_worker(struct work_struct *work)
946{
Florian Westphal371d0342017-01-18 02:01:22 +0100947 unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
Florian Westphalb87a2f92016-08-25 15:33:33 +0200948 unsigned int i, goal, buckets = 0, expired_count = 0;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200949 struct conntrack_gc_work *gc_work;
Florian Westphale0df8ca2016-11-04 16:54:58 +0100950 unsigned int ratio, scanned = 0;
951 unsigned long next_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200952
953 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
954
Florian Westphale0df8ca2016-11-04 16:54:58 +0100955 goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200956 i = gc_work->last_bucket;
957
958 do {
959 struct nf_conntrack_tuple_hash *h;
960 struct hlist_nulls_head *ct_hash;
961 struct hlist_nulls_node *n;
962 unsigned int hashsz;
963 struct nf_conn *tmp;
964
965 i++;
966 rcu_read_lock();
967
968 nf_conntrack_get_ht(&ct_hash, &hashsz);
969 if (i >= hashsz)
970 i = 0;
971
972 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
973 tmp = nf_ct_tuplehash_to_ctrack(h);
974
Florian Westphalc023c0e2016-08-25 15:33:34 +0200975 scanned++;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200976 if (nf_ct_is_expired(tmp)) {
977 nf_ct_gc_expired(tmp);
978 expired_count++;
979 continue;
980 }
981 }
982
983 /* could check get_nulls_value() here and restart if ct
984 * was moved to another chain. But given gc is best-effort
985 * we will just continue with next hash slot.
986 */
987 rcu_read_unlock();
988 cond_resched_rcu_qs();
Florian Westphal5f7ff592017-01-16 18:24:56 +0100989 } while (++buckets < goal);
Florian Westphalb87a2f92016-08-25 15:33:33 +0200990
991 if (gc_work->exiting)
992 return;
993
Florian Westphale0df8ca2016-11-04 16:54:58 +0100994 /*
995 * Eviction will normally happen from the packet path, and not
996 * from this gc worker.
997 *
998 * This worker is only here to reap expired entries when system went
999 * idle after a busy period.
1000 *
1001 * The heuristics below are supposed to balance conflicting goals:
1002 *
1003 * 1. Minimize time until we notice a stale entry
1004 * 2. Maximize scan intervals to not waste cycles
1005 *
Florian Westphal371d0342017-01-18 02:01:22 +01001006 * Normally, expire ratio will be close to 0.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001007 *
Florian Westphal371d0342017-01-18 02:01:22 +01001008 * As soon as a sizeable fraction of the entries have expired
1009 * increase scan frequency.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001010 */
Florian Westphalc023c0e2016-08-25 15:33:34 +02001011 ratio = scanned ? expired_count * 100 / scanned : 0;
Florian Westphal371d0342017-01-18 02:01:22 +01001012 if (ratio > GC_EVICT_RATIO) {
1013 gc_work->next_gc_run = min_interval;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001014 } else {
Florian Westphal371d0342017-01-18 02:01:22 +01001015 unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001016
Florian Westphal371d0342017-01-18 02:01:22 +01001017 BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1018
1019 gc_work->next_gc_run += min_interval;
1020 if (gc_work->next_gc_run > max)
1021 gc_work->next_gc_run = max;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001022 }
Florian Westphalc023c0e2016-08-25 15:33:34 +02001023
Florian Westphal371d0342017-01-18 02:01:22 +01001024 next_run = gc_work->next_gc_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001025 gc_work->last_bucket = i;
Vincent Guittot44066712017-11-02 16:16:07 +01001026 queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001027}
1028
1029static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1030{
1031 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
Florian Westphal371d0342017-01-18 02:01:22 +01001032 gc_work->next_gc_run = HZ;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001033 gc_work->exiting = false;
1034}
1035
Changli Gao99f07e92010-09-21 17:49:20 +02001036static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +02001037__nf_conntrack_alloc(struct net *net,
1038 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001039 const struct nf_conntrack_tuple *orig,
1040 const struct nf_conntrack_tuple *repl,
1041 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001042{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001043 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001044
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001045 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001046 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001047
Patrick McHardy76eb9462008-01-31 04:41:44 -08001048 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001049 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001050 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001051 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +00001052 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001053 return ERR_PTR(-ENOMEM);
1054 }
1055 }
1056
Eric Dumazet941297f2009-07-16 14:03:40 +02001057 /*
1058 * Do not use kmem_cache_zalloc(), as this cache uses
1059 * SLAB_DESTROY_BY_RCU.
1060 */
Florian Westphal0c5366b2016-05-09 16:24:32 +02001061 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001062 if (ct == NULL)
1063 goto out;
1064
Patrick McHardy440f0d52009-06-10 14:32:47 +02001065 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001066 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +02001067 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -08001068 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +02001069 /* save hash for reusing when confirming */
1070 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +01001071 ct->status = 0;
Eric Dumazetc2d9ba92010-06-01 06:51:19 +00001072 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +01001073 memset(&ct->__nfct_init_offset[0], 0,
1074 offsetof(struct nf_conn, proto) -
1075 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001076
Florian Westphal6c8dee92016-06-11 21:57:35 +02001077 nf_ct_zone_add(ct, zone);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001078
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001079 /* Because we use RCU lookups, we set ct_general.use to zero before
1080 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +02001081 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001082 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001083 return ct;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001084out:
1085 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001086 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001087}
Changli Gao99f07e92010-09-21 17:49:20 +02001088
Daniel Borkmann308ac912015-08-08 21:40:01 +02001089struct nf_conn *nf_conntrack_alloc(struct net *net,
1090 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001091 const struct nf_conntrack_tuple *orig,
1092 const struct nf_conntrack_tuple *repl,
1093 gfp_t gfp)
1094{
1095 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1096}
Patrick McHardy13b18332006-12-02 22:11:25 -08001097EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001098
Patrick McHardyc88130b2008-01-31 04:42:11 -08001099void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001100{
Eric Dumazet1d452092009-03-24 14:26:50 +01001101 struct net *net = nf_ct_net(ct);
1102
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001103 /* A freed object has refcnt == 0, that's
1104 * the golden rule for SLAB_DESTROY_BY_RCU
1105 */
1106 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1107
Patrick McHardyceeff752008-06-11 17:51:10 -07001108 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +01001109 nf_ct_ext_free(ct);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001110 kmem_cache_free(nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001111 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +01001112 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001113}
Patrick McHardy13b18332006-12-02 22:11:25 -08001114EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001115
Florian Westphalc539f012013-01-11 06:30:44 +00001116
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001117/* Allocate a new conntrack: we return -ENOMEM if classification
1118 failed due to stress. Otherwise it really is unclassifiable. */
1119static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001120init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001121 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001122 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001123 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001124 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001125 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001126{
Patrick McHardyc88130b2008-01-31 04:42:11 -08001127 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001128 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001129 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001130 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001131 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +02001132 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001133 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001134 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001135 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001136
Martin Josefsson605dcad2006-11-29 02:35:06 +01001137 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001138 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001139 return NULL;
1140 }
1141
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001142 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +02001143 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1144 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -07001145 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -08001146 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001147
Gao Feng4440a2a2016-09-13 08:49:18 +08001148 if (!nf_ct_add_synproxy(ct, tmpl)) {
1149 nf_conntrack_free(ct);
1150 return ERR_PTR(-ENOMEM);
Patrick McHardy48b1de42013-08-27 08:50:14 +02001151 }
1152
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001153 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001154 if (timeout_ext) {
1155 timeouts = nf_ct_timeout_data(timeout_ext);
1156 if (unlikely(!timeouts))
1157 timeouts = l4proto->get_timeouts(net);
1158 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001159 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001160 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001161
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001162 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -08001163 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001164 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001165 return NULL;
1166 }
1167
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001168 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001169 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1170 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001171
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001172 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001173 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +00001174 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001175
1176 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1177 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1178 ecache ? ecache->expmask : 0,
1179 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001180
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001181 local_bh_disable();
1182 if (net->ct.expect_count) {
1183 spin_lock(&nf_conntrack_expect_lock);
1184 exp = nf_ct_find_expectation(net, zone, tuple);
1185 if (exp) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001186 pr_debug("expectation arrives ct=%p exp=%p\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001187 ct, exp);
1188 /* Welcome, Mr. Bond. We've been expecting you... */
1189 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1190 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1191 ct->master = exp->master;
1192 if (exp->helper) {
1193 help = nf_ct_helper_ext_add(ct, exp->helper,
1194 GFP_ATOMIC);
1195 if (help)
1196 rcu_assign_pointer(help->helper, exp->helper);
1197 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001198
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001199#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001200 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001201#endif
James Morris7c9728c2006-06-09 00:31:46 -07001202#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001203 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001204#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001205 NF_CT_STAT_INC(net, expect_new);
1206 }
1207 spin_unlock(&nf_conntrack_expect_lock);
1208 }
Florian Westphal8e8118f2016-09-11 22:55:53 +02001209 if (!exp)
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001210 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001211
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001212 /* Now it is inserted into the unconfirmed list, bump refcount */
1213 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001214 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001215
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001216 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001217
1218 if (exp) {
1219 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001220 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001221 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001222 }
1223
Patrick McHardyc88130b2008-01-31 04:42:11 -08001224 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001225}
1226
1227/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1228static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001229resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001230 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001231 unsigned int dataoff,
1232 u_int16_t l3num,
1233 u_int8_t protonum,
1234 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001235 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001236 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001237 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001238{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001239 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001240 struct nf_conntrack_tuple tuple;
1241 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001242 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001243 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001244 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001245
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001246 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001247 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001248 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001249 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001250 return NULL;
1251 }
1252
1253 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001254 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001255 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001256 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001257 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001258 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001259 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001260 if (!h)
1261 return NULL;
1262 if (IS_ERR(h))
1263 return (void *)h;
1264 }
1265 ct = nf_ct_tuplehash_to_ctrack(h);
1266
1267 /* It exists; we have (non-exclusive) reference. */
1268 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001269 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001270 /* Please set reply bit if this packet OK */
1271 *set_reply = 1;
1272 } else {
1273 /* Once we've had two way comms, always ESTABLISHED. */
1274 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001275 pr_debug("normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001276 *ctinfo = IP_CT_ESTABLISHED;
1277 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001278 pr_debug("related packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001279 *ctinfo = IP_CT_RELATED;
1280 } else {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001281 pr_debug("new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001282 *ctinfo = IP_CT_NEW;
1283 }
1284 *set_reply = 0;
1285 }
1286 skb->nfct = &ct->ct_general;
1287 skb->nfctinfo = *ctinfo;
1288 return ct;
1289}
1290
1291unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001292nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1293 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001294{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001295 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001296 enum ip_conntrack_info ctinfo;
1297 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001298 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001299 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001300 unsigned int dataoff;
1301 u_int8_t protonum;
1302 int set_reply = 0;
1303 int ret;
1304
Herbert Xu3db05fe2007-10-15 00:53:15 -07001305 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001306 /* Previously seen (loopback or untracked)? Ignore. */
1307 tmpl = (struct nf_conn *)skb->nfct;
1308 if (!nf_ct_is_template(tmpl)) {
1309 NF_CT_STAT_INC_ATOMIC(net, ignore);
1310 return NF_ACCEPT;
1311 }
1312 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001313 }
1314
Aaron Conolee2361cb2016-09-21 11:35:04 -04001315 /* rcu_read_lock()ed by nf_hook_thresh */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001316 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001317 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001318 &dataoff, &protonum);
1319 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001320 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001321 NF_CT_STAT_INC_ATOMIC(net, error);
1322 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001323 ret = -ret;
1324 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001325 }
1326
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001327 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001328
1329 /* It may be an special packet, error, unclean...
1330 * inverse of the return code tells to the netfilter
1331 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001332 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001333 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1334 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001335 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001336 NF_CT_STAT_INC_ATOMIC(net, error);
1337 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001338 ret = -ret;
1339 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001340 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001341 /* ICMP[v6] protocol trackers may assign one conntrack. */
1342 if (skb->nfct)
1343 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001344 }
1345
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001346 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001347 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001348 if (!ct) {
1349 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001350 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001351 ret = NF_ACCEPT;
1352 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001353 }
1354
1355 if (IS_ERR(ct)) {
1356 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001357 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001358 ret = NF_DROP;
1359 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001360 }
1361
Herbert Xu3db05fe2007-10-15 00:53:15 -07001362 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001363
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001364 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001365 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001366
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001367 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001368 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001369 /* Invalid: inverse of the return code tells
1370 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001371 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001372 nf_conntrack_put(skb->nfct);
1373 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001374 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001375 if (ret == -NF_DROP)
1376 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001377 ret = -ret;
1378 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001379 }
1380
1381 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001382 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001383out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001384 if (tmpl) {
1385 /* Special case: we have to repeat this hook, assign the
1386 * template again to this packet. We assume that this packet
1387 * has no conntrack assigned. This is used by nf_ct_tcp. */
1388 if (ret == NF_REPEAT)
1389 skb->nfct = (struct nf_conntrack *)tmpl;
1390 else
1391 nf_ct_put(tmpl);
1392 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001393
1394 return ret;
1395}
Patrick McHardy13b18332006-12-02 22:11:25 -08001396EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001397
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001398bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1399 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001400{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001401 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001402
1403 rcu_read_lock();
1404 ret = nf_ct_invert_tuple(inverse, orig,
1405 __nf_ct_l3proto_find(orig->src.l3num),
1406 __nf_ct_l4proto_find(orig->src.l3num,
1407 orig->dst.protonum));
1408 rcu_read_unlock();
1409 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001410}
Patrick McHardy13b18332006-12-02 22:11:25 -08001411EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001412
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001413/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1414 implicitly racy: see __nf_conntrack_confirm */
1415void nf_conntrack_alter_reply(struct nf_conn *ct,
1416 const struct nf_conntrack_tuple *newreply)
1417{
1418 struct nf_conn_help *help = nfct_help(ct);
1419
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001420 /* Should be unconfirmed, so not in hash table yet */
1421 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1422
Patrick McHardy0d537782007-07-07 22:39:38 -07001423 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001424 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001425
1426 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001427 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001428 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001429
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001430 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001431 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001432 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001433}
Patrick McHardy13b18332006-12-02 22:11:25 -08001434EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001435
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001436/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1437void __nf_ct_refresh_acct(struct nf_conn *ct,
1438 enum ip_conntrack_info ctinfo,
1439 const struct sk_buff *skb,
1440 unsigned long extra_jiffies,
1441 int do_acct)
1442{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001443 NF_CT_ASSERT(skb);
1444
Eric Leblond997ae832006-05-29 18:24:20 -07001445 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001446 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1447 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001448
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001449 /* If not in hash table, timer will not be active yet */
Florian Westphalf330a7f2016-08-25 15:33:31 +02001450 if (nf_ct_is_confirmed(ct))
1451 extra_jiffies += nfct_time_stamp;
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001452
Florian Westphalf330a7f2016-08-25 15:33:31 +02001453 ct->timeout = extra_jiffies;
Patrick McHardy47d95042008-01-31 04:36:31 -08001454acct:
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001455 if (do_acct)
1456 nf_ct_acct_update(ct, ctinfo, skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001457}
Patrick McHardy13b18332006-12-02 22:11:25 -08001458EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001459
Florian Westphalad667132016-08-25 15:33:35 +02001460bool nf_ct_kill_acct(struct nf_conn *ct,
1461 enum ip_conntrack_info ctinfo,
1462 const struct sk_buff *skb)
Patrick McHardy51091762008-06-09 15:59:06 -07001463{
Florian Westphalad667132016-08-25 15:33:35 +02001464 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001465
Florian Westphalf330a7f2016-08-25 15:33:31 +02001466 return nf_ct_delete(ct, 0, 0);
Patrick McHardy51091762008-06-09 15:59:06 -07001467}
Florian Westphalad667132016-08-25 15:33:35 +02001468EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001469
Igor Maravićc0cd1152011-12-12 02:58:24 +00001470#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001471
1472#include <linux/netfilter/nfnetlink.h>
1473#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001474#include <linux/mutex.h>
1475
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001476/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1477 * in ip_conntrack_core, since we don't want the protocols to autoload
1478 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001479int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001480 const struct nf_conntrack_tuple *tuple)
1481{
David S. Millerbae65be2012-04-01 18:58:28 -04001482 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1483 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1484 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001485 return 0;
1486
Patrick McHardydf6fb862007-09-28 14:37:03 -07001487nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001488 return -1;
1489}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001490EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001491
Patrick McHardyf73e9242007-09-28 14:39:55 -07001492const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1493 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1494 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001495};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001496EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001497
Patrick McHardyfdf70832007-09-28 14:37:41 -07001498int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001499 struct nf_conntrack_tuple *t)
1500{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001501 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001502 return -EINVAL;
1503
Patrick McHardy77236b62007-12-17 22:29:45 -08001504 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1505 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001506
1507 return 0;
1508}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001509EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001510
1511int nf_ct_port_nlattr_tuple_size(void)
1512{
1513 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1514}
1515EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001516#endif
1517
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001518/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001519static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001520{
1521 struct nf_conn *ct;
1522 enum ip_conntrack_info ctinfo;
1523
1524 /* This ICMP is in reverse direction to the packet which caused it */
1525 ct = nf_ct_get(skb, &ctinfo);
1526 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001527 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001528 else
1529 ctinfo = IP_CT_RELATED;
1530
1531 /* Attach to new skbuff, and increment count */
1532 nskb->nfct = &ct->ct_general;
1533 nskb->nfctinfo = ctinfo;
1534 nf_conntrack_get(nskb->nfct);
1535}
1536
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001537/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001538static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001539get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001540 void *data, unsigned int *bucket)
1541{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001542 struct nf_conntrack_tuple_hash *h;
1543 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001544 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001545 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001546
Florian Westphal56d52d42016-05-02 18:39:55 +02001547 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001548 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1549 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001550 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001551 if (*bucket < nf_conntrack_htable_size) {
1552 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001553 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1554 continue;
1555 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001556 if (net_eq(nf_ct_net(ct), net) &&
1557 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001558 goto found;
1559 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001560 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001561 spin_unlock(lockp);
1562 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001563 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001564 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001565
Patrick McHardydf0933d2006-09-20 11:57:53 -07001566 return NULL;
1567found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001568 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001569 spin_unlock(lockp);
1570 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001571 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001572}
1573
Florian Westphalf51c4542017-05-21 12:52:56 +02001574static void
1575__nf_ct_unconfirmed_destroy(struct net *net)
1576{
1577 int cpu;
1578
1579 for_each_possible_cpu(cpu) {
1580 struct nf_conntrack_tuple_hash *h;
1581 struct hlist_nulls_node *n;
1582 struct ct_pcpu *pcpu;
1583
1584 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1585
1586 spin_lock_bh(&pcpu->lock);
1587 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1588 struct nf_conn *ct;
1589
1590 ct = nf_ct_tuplehash_to_ctrack(h);
1591
1592 /* we cannot call iter() on unconfirmed list, the
1593 * owning cpu can reallocate ct->ext at any time.
1594 */
1595 set_bit(IPS_DYING_BIT, &ct->status);
1596 }
1597 spin_unlock_bh(&pcpu->lock);
1598 cond_resched();
1599 }
1600}
1601
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001602void nf_ct_iterate_cleanup(struct net *net,
1603 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001604 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001605{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001606 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001607 unsigned int bucket = 0;
1608
Florian Westphald93c6252016-01-20 11:16:43 +01001609 might_sleep();
1610
Florian Westphal88b68bc2016-04-28 19:13:42 +02001611 if (atomic_read(&net->ct.count) == 0)
1612 return;
1613
Florian Westphalf51c4542017-05-21 12:52:56 +02001614 __nf_ct_unconfirmed_destroy(net);
1615
1616 synchronize_net();
1617
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001618 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001619 /* Time to push up daises... */
Florian Westphal02982c22013-07-29 15:41:54 +02001620
Florian Westphalf330a7f2016-08-25 15:33:31 +02001621 nf_ct_delete(ct, portid, report);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001622 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001623 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001624 }
1625}
Patrick McHardy13b18332006-12-02 22:11:25 -08001626EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001627
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001628static int kill_all(struct nf_conn *i, void *data)
1629{
1630 return 1;
1631}
1632
Patrick McHardyd862a662011-01-14 15:45:56 +01001633void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001634{
Patrick McHardyd862a662011-01-14 15:45:56 +01001635 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001636 vfree(hash);
1637 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001638 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001639 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001640}
Patrick McHardyac565e52007-07-07 22:30:08 -07001641EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001642
Eric Dumazetb3c51632010-06-09 14:43:38 +02001643static int untrack_refs(void)
1644{
1645 int cnt = 0, cpu;
1646
1647 for_each_possible_cpu(cpu) {
1648 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1649
1650 cnt += atomic_read(&ct->ct_general.use) - 1;
1651 }
1652 return cnt;
1653}
1654
Gao fengf94161c2013-01-21 22:10:24 +00001655void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001656{
Florian Westphalb87a2f92016-08-25 15:33:33 +02001657 conntrack_gc_work.exiting = true;
Gao fengf94161c2013-01-21 22:10:24 +00001658 RCU_INIT_POINTER(ip_ct_attach, NULL);
1659}
1660
1661void nf_conntrack_cleanup_end(void)
1662{
1663 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001664 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001665 schedule();
1666
Florian Westphalb87a2f92016-08-25 15:33:33 +02001667 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
Florian Westphal56d52d42016-05-02 18:39:55 +02001668 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1669
Gao feng04d87002013-01-21 22:10:32 +00001670 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001671 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001672 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001673 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001674 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001675 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001676 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001677 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001678 nf_conntrack_expect_fini();
Florian Westphal77571142016-06-10 17:25:19 +02001679
1680 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001681}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001682
Gao fengf94161c2013-01-21 22:10:24 +00001683/*
1684 * Mishearing the voices in his head, our hero wonders how he's
1685 * supposed to kill the mall.
1686 */
1687void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001688{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001689 LIST_HEAD(single);
1690
1691 list_add(&net->exit_list, &single);
1692 nf_conntrack_cleanup_net_list(&single);
1693}
1694
1695void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1696{
1697 int busy;
1698 struct net *net;
1699
Gao fengf94161c2013-01-21 22:10:24 +00001700 /*
1701 * This makes sure all current packets have passed through
1702 * netfilter framework. Roll on, two-stage module
1703 * delete...
1704 */
1705 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001706i_see_dead_people:
1707 busy = 0;
1708 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001709 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001710 if (atomic_read(&net->ct.count) != 0)
1711 busy = 1;
1712 }
1713 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001714 schedule();
1715 goto i_see_dead_people;
1716 }
1717
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001718 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001719 nf_conntrack_proto_pernet_fini(net);
1720 nf_conntrack_helper_pernet_fini(net);
1721 nf_conntrack_ecache_pernet_fini(net);
1722 nf_conntrack_tstamp_pernet_fini(net);
1723 nf_conntrack_acct_pernet_fini(net);
1724 nf_conntrack_expect_pernet_fini(net);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001725 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001726 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001727 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001728}
1729
Patrick McHardyd862a662011-01-14 15:45:56 +01001730void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001731{
Eric Dumazetea781f12009-03-25 21:05:46 +01001732 struct hlist_nulls_head *hash;
1733 unsigned int nr_slots, i;
1734 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001735
Florian Westphal9cc1c732016-04-24 01:18:21 +02001736 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1737 return NULL;
1738
Eric Dumazetea781f12009-03-25 21:05:46 +01001739 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1740 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
Florian Westphal9cc1c732016-04-24 01:18:21 +02001741
1742 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1743 return NULL;
1744
Eric Dumazetea781f12009-03-25 21:05:46 +01001745 sz = nr_slots * sizeof(struct hlist_nulls_head);
1746 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1747 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001748 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001749 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001750
Eric Dumazetea781f12009-03-25 21:05:46 +01001751 if (hash && nulls)
1752 for (i = 0; i < nr_slots; i++)
1753 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001754
1755 return hash;
1756}
Patrick McHardyac565e52007-07-07 22:30:08 -07001757EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001758
Florian Westphal3183ab82016-06-22 13:26:10 +02001759int nf_conntrack_hash_resize(unsigned int hashsize)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001760{
Florian Westphal3183ab82016-06-22 13:26:10 +02001761 int i, bucket;
1762 unsigned int old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001763 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001764 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001765 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001766
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001767 if (!hashsize)
1768 return -EINVAL;
1769
Patrick McHardyd862a662011-01-14 15:45:56 +01001770 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001771 if (!hash)
1772 return -ENOMEM;
1773
Florian Westphal3183ab82016-06-22 13:26:10 +02001774 old_size = nf_conntrack_htable_size;
1775 if (old_size == hashsize) {
1776 nf_ct_free_hashtable(hash, hashsize);
1777 return 0;
1778 }
1779
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001780 local_bh_disable();
1781 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001782 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001783
Patrick McHardy76507f62008-01-31 04:38:38 -08001784 /* Lookups in the old hash might happen in parallel, which means we
1785 * might get false negatives during connection lookup. New connections
1786 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001787 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001788 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001789
Florian Westphal56d52d42016-05-02 18:39:55 +02001790 for (i = 0; i < nf_conntrack_htable_size; i++) {
1791 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1792 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1793 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001794 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001795 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001796 bucket = __hash_conntrack(nf_ct_net(ct),
1797 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001798 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001799 }
1800 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001801 old_size = nf_conntrack_htable_size;
1802 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001803
Florian Westphal56d52d42016-05-02 18:39:55 +02001804 nf_conntrack_hash = hash;
1805 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001806
Florian Westphala3efd812016-04-18 16:16:59 +02001807 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001808 nf_conntrack_all_unlock();
1809 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001810
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001811 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001812 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001813 return 0;
1814}
Florian Westphal3183ab82016-06-22 13:26:10 +02001815
Kees Cook24da2c82017-10-17 19:04:42 -07001816int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp)
Florian Westphal3183ab82016-06-22 13:26:10 +02001817{
1818 unsigned int hashsize;
1819 int rc;
1820
1821 if (current->nsproxy->net_ns != &init_net)
1822 return -EOPNOTSUPP;
1823
1824 /* On boot, we can set this without any fancy locking. */
1825 if (!nf_conntrack_htable_size)
1826 return param_set_uint(val, kp);
1827
1828 rc = kstrtouint(val, 0, &hashsize);
1829 if (rc)
1830 return rc;
1831
1832 return nf_conntrack_hash_resize(hashsize);
1833}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001834EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001835
Patrick McHardyfae718d2007-12-24 21:09:10 -08001836module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001837 &nf_conntrack_htable_size, 0600);
1838
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001839void nf_ct_untracked_status_or(unsigned long bits)
1840{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001841 int cpu;
1842
1843 for_each_possible_cpu(cpu)
1844 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001845}
1846EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1847
Gao fengf94161c2013-01-21 22:10:24 +00001848int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001849{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001850 int max_factor = 8;
Florian Westphal0c5366b2016-05-09 16:24:32 +02001851 int ret = -ENOMEM;
1852 int i, cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001853
Florian Westphala3efd812016-04-18 16:16:59 +02001854 seqcount_init(&nf_conntrack_generation);
1855
Eric Dumazetd5d20912014-03-17 13:37:53 -07001856 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001857 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001858
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001859 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001860 /* Idea from tcp.c: use 1/16384 of memory.
1861 * On i386: 32MB machine has 512 buckets.
1862 * >= 1GB machines have 16384 buckets.
1863 * >= 4GB machines have 65536 buckets.
1864 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001865 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07001866 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001867 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02001868 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1869 nf_conntrack_htable_size = 65536;
1870 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001871 nf_conntrack_htable_size = 16384;
1872 if (nf_conntrack_htable_size < 32)
1873 nf_conntrack_htable_size = 32;
1874
1875 /* Use a max. factor of four by default to get the same max as
1876 * with the old struct list_heads. When a table size is given
1877 * we use the old value of 8 to avoid reducing the max.
1878 * entries. */
1879 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001880 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001881
1882 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1883 if (!nf_conntrack_hash)
1884 return -ENOMEM;
1885
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001886 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001887
Florian Westphal0c5366b2016-05-09 16:24:32 +02001888 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1889 sizeof(struct nf_conn), 0,
Florian Westphal5a75cde2016-06-10 22:25:22 +02001890 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001891 if (!nf_conntrack_cachep)
1892 goto err_cachep;
1893
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001894 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001895 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1896 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00001897
1898 ret = nf_conntrack_expect_init();
1899 if (ret < 0)
1900 goto err_expect;
1901
Gao fengb7ff3a12013-01-21 22:10:26 +00001902 ret = nf_conntrack_acct_init();
1903 if (ret < 0)
1904 goto err_acct;
1905
Gao feng73f40012013-01-21 22:10:27 +00001906 ret = nf_conntrack_tstamp_init();
1907 if (ret < 0)
1908 goto err_tstamp;
1909
Gao feng3fe0f942013-01-21 22:10:28 +00001910 ret = nf_conntrack_ecache_init();
1911 if (ret < 0)
1912 goto err_ecache;
1913
Gao feng86840942013-01-21 22:10:29 +00001914 ret = nf_conntrack_timeout_init();
1915 if (ret < 0)
1916 goto err_timeout;
1917
Gao feng5e615b22013-01-21 22:10:30 +00001918 ret = nf_conntrack_helper_init();
1919 if (ret < 0)
1920 goto err_helper;
1921
Gao feng5f69b8f2013-01-21 22:10:31 +00001922 ret = nf_conntrack_labels_init();
1923 if (ret < 0)
1924 goto err_labels;
1925
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001926 ret = nf_conntrack_seqadj_init();
1927 if (ret < 0)
1928 goto err_seqadj;
1929
Gao feng04d87002013-01-21 22:10:32 +00001930 ret = nf_conntrack_proto_init();
1931 if (ret < 0)
1932 goto err_proto;
1933
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001934 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02001935 for_each_possible_cpu(cpu) {
1936 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001937 write_pnet(&ct->ct_net, &init_net);
1938 atomic_set(&ct->ct_general.use, 1);
1939 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001940 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001941 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001942
1943 conntrack_gc_work_init(&conntrack_gc_work);
Vincent Guittot44066712017-11-02 16:16:07 +01001944 queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001945
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001946 return 0;
1947
Gao feng04d87002013-01-21 22:10:32 +00001948err_proto:
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001949 nf_conntrack_seqadj_fini();
1950err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00001951 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001952err_labels:
1953 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001954err_helper:
1955 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00001956err_timeout:
1957 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001958err_ecache:
1959 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00001960err_tstamp:
1961 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001962err_acct:
1963 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001964err_expect:
Florian Westphal0c5366b2016-05-09 16:24:32 +02001965 kmem_cache_destroy(nf_conntrack_cachep);
1966err_cachep:
Florian Westphal56d52d42016-05-02 18:39:55 +02001967 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001968 return ret;
1969}
1970
Gao fengf94161c2013-01-21 22:10:24 +00001971void nf_conntrack_init_end(void)
1972{
1973 /* For use by REJECT target */
1974 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1975 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00001976}
1977
Eric Dumazet8cc20192009-06-22 14:13:55 +02001978/*
1979 * We need to use special "null" values, not used in hash table
1980 */
1981#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1982#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00001983#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02001984
Gao fengf94161c2013-01-21 22:10:24 +00001985int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001986{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001987 int ret = -ENOMEM;
1988 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001989
1990 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001991
1992 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1993 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001994 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001995
1996 for_each_possible_cpu(cpu) {
1997 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1998
1999 spin_lock_init(&pcpu->lock);
2000 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
2001 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002002 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08002003
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002004 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2005 if (!net->ct.stat)
2006 goto err_pcpu_lists;
2007
Gao feng83b4dbe2013-01-21 22:10:25 +00002008 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002009 if (ret < 0)
2010 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00002011 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07002012 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002013 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00002014 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002015 if (ret < 0)
2016 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00002017 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002018 if (ret < 0)
2019 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00002020 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002021 if (ret < 0)
2022 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00002023 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00002024 if (ret < 0)
2025 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002026 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00002027
Gao fengf94161c2013-01-21 22:10:24 +00002028err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00002029 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002030err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00002031 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002032err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00002033 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002034err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00002035 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002036err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00002037 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002038err_expect:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002039 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002040err_pcpu_lists:
2041 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002042err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002043 return ret;
2044}