blob: f95a9e999f09a39cf860592bf36ce76b08643786 [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;
79};
80
Florian Westphal0c5366b2016-05-09 16:24:32 +020081static __read_mostly struct kmem_cache *nf_conntrack_cachep;
Sasha Levinb16c2912016-01-18 19:23:51 -050082static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
Florian Westphal70d72b72016-04-24 01:17:14 +020083static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -050084static __read_mostly bool nf_conntrack_locks_all;
85
Florian Westphalb87a2f92016-08-25 15:33:33 +020086#define GC_MAX_BUCKETS_DIV 64u
87#define GC_MAX_BUCKETS 8192u
88#define GC_INTERVAL (5 * HZ)
89#define GC_MAX_EVICTS 256u
90
91static struct conntrack_gc_work conntrack_gc_work;
92
Sasha Levinb16c2912016-01-18 19:23:51 -050093void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
94{
95 spin_lock(lock);
96 while (unlikely(nf_conntrack_locks_all)) {
97 spin_unlock(lock);
Peter Zijlstrab316ff72016-05-24 15:00:38 +020098
99 /*
100 * Order the 'nf_conntrack_locks_all' load vs. the
101 * spin_unlock_wait() loads below, to ensure
102 * that 'nf_conntrack_locks_all_lock' is indeed held:
103 */
104 smp_rmb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
Nicholas Mc Guiree39365b2016-03-14 12:39:02 +0100105 spin_unlock_wait(&nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -0500106 spin_lock(lock);
107 }
108}
109EXPORT_SYMBOL_GPL(nf_conntrack_lock);
110
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100111static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
112{
113 h1 %= CONNTRACK_LOCKS;
114 h2 %= CONNTRACK_LOCKS;
115 spin_unlock(&nf_conntrack_locks[h1]);
116 if (h1 != h2)
117 spin_unlock(&nf_conntrack_locks[h2]);
118}
119
120/* return true if we need to recompute hashes (in case hash table was resized) */
121static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
122 unsigned int h2, unsigned int sequence)
123{
124 h1 %= CONNTRACK_LOCKS;
125 h2 %= CONNTRACK_LOCKS;
126 if (h1 <= h2) {
Sasha Levinb16c2912016-01-18 19:23:51 -0500127 nf_conntrack_lock(&nf_conntrack_locks[h1]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100128 if (h1 != h2)
129 spin_lock_nested(&nf_conntrack_locks[h2],
130 SINGLE_DEPTH_NESTING);
131 } else {
Sasha Levinb16c2912016-01-18 19:23:51 -0500132 nf_conntrack_lock(&nf_conntrack_locks[h2]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100133 spin_lock_nested(&nf_conntrack_locks[h1],
134 SINGLE_DEPTH_NESTING);
135 }
Florian Westphala3efd812016-04-18 16:16:59 +0200136 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100137 nf_conntrack_double_unlock(h1, h2);
138 return true;
139 }
140 return false;
141}
142
143static void nf_conntrack_all_lock(void)
144{
145 int i;
146
Sasha Levinb16c2912016-01-18 19:23:51 -0500147 spin_lock(&nf_conntrack_locks_all_lock);
148 nf_conntrack_locks_all = true;
149
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200150 /*
151 * Order the above store of 'nf_conntrack_locks_all' against
152 * the spin_unlock_wait() loads below, such that if
153 * nf_conntrack_lock() observes 'nf_conntrack_locks_all'
154 * we must observe nf_conntrack_locks[] held:
155 */
156 smp_mb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
157
Sasha Levinb16c2912016-01-18 19:23:51 -0500158 for (i = 0; i < CONNTRACK_LOCKS; i++) {
Nicholas Mc Guiree39365b2016-03-14 12:39:02 +0100159 spin_unlock_wait(&nf_conntrack_locks[i]);
Sasha Levinb16c2912016-01-18 19:23:51 -0500160 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100161}
162
163static void nf_conntrack_all_unlock(void)
164{
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200165 /*
166 * All prior stores must be complete before we clear
167 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
168 * might observe the false value but not the entire
169 * critical section:
170 */
171 smp_store_release(&nf_conntrack_locks_all, false);
Sasha Levinb16c2912016-01-18 19:23:51 -0500172 spin_unlock(&nf_conntrack_locks_all_lock);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100173}
174
Martin Josefssone2b76062006-11-29 02:35:04 +0100175unsigned int nf_conntrack_htable_size __read_mostly;
Pablo Neira Ayuso2567c4e2016-08-18 11:15:12 +0200176EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
177
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100178unsigned int nf_conntrack_max __read_mostly;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800179seqcount_t nf_conntrack_generation __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -0800180
Eric Dumazetb3c51632010-06-09 14:43:38 +0200181DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
182EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
Patrick McHardy13b18332006-12-02 22:11:25 -0800183
Florian Westphal141658f2016-04-18 16:17:01 +0200184static unsigned int nf_conntrack_hash_rnd __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200186static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
187 const struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188{
Patrick McHardy07949352008-01-31 04:40:52 -0800189 unsigned int n;
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200190 u32 seed;
Sami Farin9b887902007-03-14 16:43:00 -0700191
Florian Westphal141658f2016-04-18 16:17:01 +0200192 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
193
Patrick McHardy07949352008-01-31 04:40:52 -0800194 /* The direction must be ignored, so we hash everything up to the
195 * destination ports (which is a multiple of 4) and treat the last
196 * three bytes manually.
197 */
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200198 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
Patrick McHardy07949352008-01-31 04:40:52 -0800199 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200200 return jhash2((u32 *)tuple, n, seed ^
Changli Gao99f07e92010-09-21 17:49:20 +0200201 (((__force __u16)tuple->dst.u.all << 16) |
202 tuple->dst.protonum));
203}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204
Florian Westphal56d52d42016-05-02 18:39:55 +0200205static u32 scale_hash(u32 hash)
Changli Gao99f07e92010-09-21 17:49:20 +0200206{
Florian Westphal56d52d42016-05-02 18:39:55 +0200207 return reciprocal_scale(hash, nf_conntrack_htable_size);
Changli Gao99f07e92010-09-21 17:49:20 +0200208}
209
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200210static u32 __hash_conntrack(const struct net *net,
211 const struct nf_conntrack_tuple *tuple,
212 unsigned int size)
Changli Gao99f07e92010-09-21 17:49:20 +0200213{
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200214 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215}
216
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200217static u32 hash_conntrack(const struct net *net,
218 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219{
Florian Westphal56d52d42016-05-02 18:39:55 +0200220 return scale_hash(hash_conntrack_raw(tuple, net));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221}
222
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200223bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224nf_ct_get_tuple(const struct sk_buff *skb,
225 unsigned int nhoff,
226 unsigned int dataoff,
227 u_int16_t l3num,
228 u_int8_t protonum,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500229 struct net *net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230 struct nf_conntrack_tuple *tuple,
231 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100232 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800233{
Philip Craig443a70d2008-04-29 03:35:10 -0700234 memset(tuple, 0, sizeof(*tuple));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235
236 tuple->src.l3num = l3num;
237 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200238 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239
240 tuple->dst.protonum = protonum;
241 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
242
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500243 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244}
Patrick McHardy13b18332006-12-02 22:11:25 -0800245EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200247bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500248 u_int16_t l3num,
249 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700250{
251 struct nf_conntrack_l3proto *l3proto;
252 struct nf_conntrack_l4proto *l4proto;
253 unsigned int protoff;
254 u_int8_t protonum;
255 int ret;
256
257 rcu_read_lock();
258
259 l3proto = __nf_ct_l3proto_find(l3num);
260 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
261 if (ret != NF_ACCEPT) {
262 rcu_read_unlock();
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200263 return false;
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700264 }
265
266 l4proto = __nf_ct_l4proto_find(l3num, protonum);
267
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500268 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700269 l3proto, l4proto);
270
271 rcu_read_unlock();
272 return ret;
273}
274EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
275
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200276bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800277nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
278 const struct nf_conntrack_tuple *orig,
279 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100280 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800281{
Philip Craig443a70d2008-04-29 03:35:10 -0700282 memset(inverse, 0, sizeof(*inverse));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800283
284 inverse->src.l3num = orig->src.l3num;
285 if (l3proto->invert_tuple(inverse, orig) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200286 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800287
288 inverse->dst.dir = !orig->dst.dir;
289
290 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100291 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292}
Patrick McHardy13b18332006-12-02 22:11:25 -0800293EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295static void
296clean_from_lists(struct nf_conn *ct)
297{
Patrick McHardy0d537782007-07-07 22:39:38 -0700298 pr_debug("clean_from_lists(%p)\n", ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100299 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
300 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301
302 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800303 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304}
305
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100306/* must be called with local_bh_disable */
307static void nf_ct_add_to_dying_list(struct nf_conn *ct)
308{
309 struct ct_pcpu *pcpu;
310
311 /* add this conntrack to the (per cpu) dying list */
312 ct->cpu = smp_processor_id();
313 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
314
315 spin_lock(&pcpu->lock);
316 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
317 &pcpu->dying);
318 spin_unlock(&pcpu->lock);
319}
320
321/* must be called with local_bh_disable */
322static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
323{
324 struct ct_pcpu *pcpu;
325
326 /* add this conntrack to the (per cpu) unconfirmed list */
327 ct->cpu = smp_processor_id();
328 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
329
330 spin_lock(&pcpu->lock);
331 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
332 &pcpu->unconfirmed);
333 spin_unlock(&pcpu->lock);
334}
335
336/* must be called with local_bh_disable */
337static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
338{
339 struct ct_pcpu *pcpu;
340
341 /* We overload first tuple to link into unconfirmed or dying list.*/
342 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
343
344 spin_lock(&pcpu->lock);
345 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
346 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
347 spin_unlock(&pcpu->lock);
348}
349
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200350/* Released via destroy_conntrack() */
Daniel Borkmann308ac912015-08-08 21:40:01 +0200351struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
352 const struct nf_conntrack_zone *zone,
353 gfp_t flags)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200354{
355 struct nf_conn *tmpl;
356
Joe Stringerf58e5aa2015-08-04 18:34:00 -0700357 tmpl = kzalloc(sizeof(*tmpl), flags);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200358 if (tmpl == NULL)
359 return NULL;
360
361 tmpl->status = IPS_TEMPLATE;
362 write_pnet(&tmpl->ct_net, net);
Florian Westphal6c8dee92016-06-11 21:57:35 +0200363 nf_ct_zone_add(tmpl, zone);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200364 atomic_set(&tmpl->ct_general.use, 0);
365
366 return tmpl;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200367}
368EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
369
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200370void nf_ct_tmpl_free(struct nf_conn *tmpl)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200371{
372 nf_ct_ext_destroy(tmpl);
373 nf_ct_ext_free(tmpl);
374 kfree(tmpl);
375}
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200376EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200377
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800378static void
379destroy_conntrack(struct nf_conntrack *nfct)
380{
381 struct nf_conn *ct = (struct nf_conn *)nfct;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200382 struct net *net = nf_ct_net(ct);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100383 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800384
Patrick McHardy0d537782007-07-07 22:39:38 -0700385 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200388 if (unlikely(nf_ct_is_template(ct))) {
389 nf_ct_tmpl_free(ct);
390 return;
391 }
Patrick McHardy923f4902007-02-12 11:12:57 -0800392 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200393 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Pablo Neira Ayuso4b4ceb92016-05-01 00:34:37 +0200394 if (l4proto->destroy)
Martin Josefsson605dcad2006-11-29 02:35:06 +0100395 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800396
Patrick McHardy982d9a92007-02-12 11:14:11 -0800397 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100399 local_bh_disable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400 /* Expectations will have been removed in clean_from_lists,
401 * except TFTP can create an expectation on the first packet,
402 * before connection is in the list, so we need to clean here,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100403 * too.
404 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800405 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100407 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800408
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200409 NF_CT_STAT_INC(net, delete);
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100410 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411
412 if (ct->master)
413 nf_ct_put(ct->master);
414
Patrick McHardy0d537782007-07-07 22:39:38 -0700415 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416 nf_conntrack_free(ct);
417}
418
Florian Westphal02982c22013-07-29 15:41:54 +0200419static void nf_ct_delete_from_lists(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420{
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200421 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100422 unsigned int hash, reply_hash;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100423 unsigned int sequence;
Patrick McHardy5397e972007-05-19 14:23:52 -0700424
Pablo Neira Ayuso9858a3a2009-06-13 12:28:22 +0200425 nf_ct_helper_destroy(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100426
427 local_bh_disable();
428 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200429 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200430 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100431 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200432 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100433 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
434 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
435
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800436 clean_from_lists(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100437 nf_conntrack_double_unlock(hash, reply_hash);
438
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100439 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100440
441 NF_CT_STAT_INC(net, delete_list);
442 local_bh_enable();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200443}
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200444
Florian Westphal02982c22013-07-29 15:41:54 +0200445bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200446{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100447 struct nf_conn_tstamp *tstamp;
448
Florian Westphalf330a7f2016-08-25 15:33:31 +0200449 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
450 return false;
451
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100452 tstamp = nf_conn_tstamp_find(ct);
453 if (tstamp && tstamp->stop == 0)
Eric Dumazetd2de8752014-08-22 18:32:09 -0700454 tstamp->stop = ktime_get_real_ns();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200455
Florian Westphal95005072014-06-10 23:12:56 +0200456 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
457 portid, report) < 0) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200458 /* destroy event was not delivered. nf_ct_put will
459 * be done by event cache worker on redelivery.
460 */
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200461 nf_ct_delete_from_lists(ct);
Florian Westphal95005072014-06-10 23:12:56 +0200462 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
Florian Westphal02982c22013-07-29 15:41:54 +0200463 return false;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200464 }
Florian Westphal95005072014-06-10 23:12:56 +0200465
466 nf_conntrack_ecache_work(nf_ct_net(ct));
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200467 nf_ct_delete_from_lists(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 nf_ct_put(ct);
Florian Westphal02982c22013-07-29 15:41:54 +0200469 return true;
470}
471EXPORT_SYMBOL_GPL(nf_ct_delete);
472
Andrey Vaginc6825c02014-01-29 19:34:14 +0100473static inline bool
474nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
Daniel Borkmann308ac912015-08-08 21:40:01 +0200475 const struct nf_conntrack_tuple *tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200476 const struct nf_conntrack_zone *zone,
477 const struct net *net)
Andrey Vaginc6825c02014-01-29 19:34:14 +0100478{
479 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
480
481 /* A conntrack can be recreated with the equal tuple,
482 * so we need to check that the conntrack is confirmed
483 */
484 return nf_ct_tuple_equal(tuple, &h->tuple) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200485 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200486 nf_ct_is_confirmed(ct) &&
487 net_eq(net, nf_ct_net(ct));
Andrey Vaginc6825c02014-01-29 19:34:14 +0100488}
489
Florian Westphalf330a7f2016-08-25 15:33:31 +0200490/* caller must hold rcu readlock and none of the nf_conntrack_locks */
491static void nf_ct_gc_expired(struct nf_conn *ct)
492{
493 if (!atomic_inc_not_zero(&ct->ct_general.use))
494 return;
495
496 if (nf_ct_should_gc(ct))
497 nf_ct_kill(ct);
498
499 nf_ct_put(ct);
500}
501
Eric Dumazetea781f12009-03-25 21:05:46 +0100502/*
503 * Warning :
504 * - Caller must take a reference on returned object
505 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
Eric Dumazetea781f12009-03-25 21:05:46 +0100506 */
Changli Gao99f07e92010-09-21 17:49:20 +0200507static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200508____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200509 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800510{
511 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200512 struct hlist_nulls_head *ct_hash;
Eric Dumazetea781f12009-03-25 21:05:46 +0100513 struct hlist_nulls_node *n;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800514 unsigned int bucket, hsize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515
Eric Dumazetea781f12009-03-25 21:05:46 +0100516begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800517 nf_conntrack_get_ht(&ct_hash, &hsize);
518 bucket = reciprocal_scale(hash, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200519
520 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200521 struct nf_conn *ct;
522
523 ct = nf_ct_tuplehash_to_ctrack(h);
524 if (nf_ct_is_expired(ct)) {
525 nf_ct_gc_expired(ct);
526 continue;
527 }
528
529 if (nf_ct_is_dying(ct))
530 continue;
531
Florian Westphale0c7d472016-04-28 19:13:45 +0200532 if (nf_ct_key_equal(h, tuple, zone, net)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200533 NF_CT_STAT_INC_ATOMIC(net, found);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800534 return h;
535 }
Florian Westphal2cf12342016-04-28 19:13:40 +0200536 NF_CT_STAT_INC_ATOMIC(net, searched);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800537 }
Eric Dumazetea781f12009-03-25 21:05:46 +0100538 /*
539 * if the nulls value we got at the end of this lookup is
540 * not the expected one, we must restart lookup.
541 * We probably met an item that was moved to another chain.
542 */
Changli Gao99f07e92010-09-21 17:49:20 +0200543 if (get_nulls_value(n) != bucket) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200544 NF_CT_STAT_INC_ATOMIC(net, search_restart);
Eric Dumazetea781f12009-03-25 21:05:46 +0100545 goto begin;
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200546 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800547
548 return NULL;
549}
Changli Gao99f07e92010-09-21 17:49:20 +0200550
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800551/* Find a connection corresponding to a tuple. */
Changli Gao99f07e92010-09-21 17:49:20 +0200552static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200553__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200554 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800555{
556 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800557 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800558
Patrick McHardy76507f62008-01-31 04:38:38 -0800559 rcu_read_lock();
Eric Dumazetea781f12009-03-25 21:05:46 +0100560begin:
Changli Gao99f07e92010-09-21 17:49:20 +0200561 h = ____nf_conntrack_find(net, zone, tuple, hash);
Patrick McHardy76507f62008-01-31 04:38:38 -0800562 if (h) {
563 ct = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy8d8890b72009-06-22 14:14:41 +0200564 if (unlikely(nf_ct_is_dying(ct) ||
565 !atomic_inc_not_zero(&ct->ct_general.use)))
Patrick McHardy76507f62008-01-31 04:38:38 -0800566 h = NULL;
Eric Dumazetea781f12009-03-25 21:05:46 +0100567 else {
Florian Westphale0c7d472016-04-28 19:13:45 +0200568 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
Eric Dumazetea781f12009-03-25 21:05:46 +0100569 nf_ct_put(ct);
570 goto begin;
571 }
572 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800573 }
574 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800575
576 return h;
577}
Changli Gao99f07e92010-09-21 17:49:20 +0200578
579struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200580nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200581 const struct nf_conntrack_tuple *tuple)
582{
583 return __nf_conntrack_find_get(net, zone, tuple,
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200584 hash_conntrack_raw(tuple, net));
Changli Gao99f07e92010-09-21 17:49:20 +0200585}
Patrick McHardy13b18332006-12-02 22:11:25 -0800586EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800587
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800588static void __nf_conntrack_hash_insert(struct nf_conn *ct,
589 unsigned int hash,
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100590 unsigned int reply_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800591{
Eric Dumazetea781f12009-03-25 21:05:46 +0100592 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200593 &nf_conntrack_hash[hash]);
Eric Dumazetea781f12009-03-25 21:05:46 +0100594 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200595 &nf_conntrack_hash[reply_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800596}
597
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100598int
599nf_conntrack_hash_check_insert(struct nf_conn *ct)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800600{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200601 const struct nf_conntrack_zone *zone;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800602 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100603 unsigned int hash, reply_hash;
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100604 struct nf_conntrack_tuple_hash *h;
605 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100606 unsigned int sequence;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800607
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100608 zone = nf_ct_zone(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800609
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100610 local_bh_disable();
611 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200612 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200613 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100614 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200615 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100616 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
617 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100618
619 /* See if there's one in the list already, including reverse */
Florian Westphal56d52d42016-05-02 18:39:55 +0200620 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200621 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200622 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100623 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200624
Florian Westphal56d52d42016-05-02 18:39:55 +0200625 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200626 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200627 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100628 goto out;
629
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100630 smp_wmb();
631 /* The caller holds a reference to this object */
632 atomic_set(&ct->ct_general.use, 2);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100633 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100634 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100635 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100636 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100637 return 0;
638
639out:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100640 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100641 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100642 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100643 return -EEXIST;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800644}
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100645EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800646
Pablo Neira Ayusoba767382016-05-02 21:28:57 +0200647static inline void nf_ct_acct_update(struct nf_conn *ct,
648 enum ip_conntrack_info ctinfo,
649 unsigned int len)
650{
651 struct nf_conn_acct *acct;
652
653 acct = nf_conn_acct_find(ct);
654 if (acct) {
655 struct nf_conn_counter *counter = acct->counter;
656
657 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
658 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
659 }
660}
661
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200662static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
663 const struct nf_conn *loser_ct)
664{
665 struct nf_conn_acct *acct;
666
667 acct = nf_conn_acct_find(loser_ct);
668 if (acct) {
669 struct nf_conn_counter *counter = acct->counter;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200670 unsigned int bytes;
671
672 /* u32 should be fine since we must have seen one packet. */
673 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
674 nf_ct_acct_update(ct, ctinfo, bytes);
675 }
676}
677
678/* Resolve race on insertion if this protocol allows this. */
679static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
680 enum ip_conntrack_info ctinfo,
681 struct nf_conntrack_tuple_hash *h)
682{
683 /* This is the conntrack entry already in hashes that won race. */
684 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
685 struct nf_conntrack_l4proto *l4proto;
686
687 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
688 if (l4proto->allow_clash &&
Pablo Neira Ayuso590b52e2016-07-11 17:28:54 +0200689 !nfct_nat(ct) &&
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200690 !nf_ct_is_dying(ct) &&
691 atomic_inc_not_zero(&ct->ct_general.use)) {
692 nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct);
693 nf_conntrack_put(skb->nfct);
694 /* Assign conntrack already in hashes to this skbuff. Don't
695 * modify skb->nfctinfo to ensure consistent stateful filtering.
696 */
697 skb->nfct = &ct->ct_general;
698 return NF_ACCEPT;
699 }
700 NF_CT_STAT_INC(net, drop);
701 return NF_DROP;
702}
703
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800704/* Confirm a connection given skb; places it in hash table */
705int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700706__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800707{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200708 const struct nf_conntrack_zone *zone;
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100709 unsigned int hash, reply_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700710 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800711 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700712 struct nf_conn_help *help;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100713 struct nf_conn_tstamp *tstamp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100714 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800715 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200716 struct net *net;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100717 unsigned int sequence;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200718 int ret = NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800719
Herbert Xu3db05fe2007-10-15 00:53:15 -0700720 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200721 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800722
723 /* ipt_REJECT uses nf_conntrack_attach to attach related
724 ICMP/TCP RST packets in other direction. Actual packet
725 which created connection will be IP_CT_NEW or for an
726 expected connection, IP_CT_RELATED. */
727 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
728 return NF_ACCEPT;
729
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100730 zone = nf_ct_zone(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100731 local_bh_disable();
732
733 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200734 sequence = read_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100735 /* reuse the hash saved before */
736 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
Florian Westphal56d52d42016-05-02 18:39:55 +0200737 hash = scale_hash(hash);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200738 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100739 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
740
741 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800742
743 /* We're not in hash table, and we refuse to set up related
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100744 * connections for unconfirmed conns. But packet copies and
745 * REJECT will give spurious warnings here.
746 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800747 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
748
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300749 /* No external references means no one else could have
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100750 * confirmed us.
751 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800752 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700753 pr_debug("Confirming conntrack %p\n", ct);
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100754 /* We have to check the DYING flag after unlink to prevent
755 * a race against nf_ct_get_next_corpse() possibly called from
756 * user context, else we insert an already 'dead' hash, blocking
757 * further use of that particular connection -JM.
758 */
759 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Joerg Marxfc350772010-05-20 15:55:30 +0200760
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200761 if (unlikely(nf_ct_is_dying(ct))) {
762 nf_ct_add_to_dying_list(ct);
763 goto dying;
764 }
Joerg Marxfc350772010-05-20 15:55:30 +0200765
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800766 /* See if there's one in the list already, including reverse:
767 NAT could have grabbed it without realizing, since we're
768 not in the hash. If there is, we lost race. */
Florian Westphal56d52d42016-05-02 18:39:55 +0200769 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200770 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200771 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700772 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200773
Florian Westphal56d52d42016-05-02 18:39:55 +0200774 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200775 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200776 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700777 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800778
Patrick McHardydf0933d2006-09-20 11:57:53 -0700779 /* Timer relative to confirmation time, not original
780 setting time, otherwise we'd get timer wrap in
781 weird delay cases. */
Florian Westphalf330a7f2016-08-25 15:33:31 +0200782 ct->timeout += nfct_time_stamp;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700783 atomic_inc(&ct->ct_general.use);
Changli Gao45eec342011-01-18 15:08:13 +0100784 ct->status |= IPS_CONFIRMED;
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200785
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100786 /* set conntrack timestamp, if enabled. */
787 tstamp = nf_conn_tstamp_find(ct);
788 if (tstamp) {
789 if (skb->tstamp.tv64 == 0)
Joe Perchese3192692012-06-03 17:41:40 +0000790 __net_timestamp(skb);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100791
792 tstamp->start = ktime_to_ns(skb->tstamp);
793 }
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200794 /* Since the lookup is lockless, hash insertion must be done after
795 * starting the timer and setting the CONFIRMED bit. The RCU barriers
796 * guarantee that no other CPU can find the conntrack before the above
797 * stores are visible.
798 */
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100799 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100800 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200801 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100802 local_bh_enable();
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200803
Patrick McHardydf0933d2006-09-20 11:57:53 -0700804 help = nfct_help(ct);
805 if (help && help->helper)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200806 nf_conntrack_event_cache(IPCT_HELPER, ct);
Pablo Neira Ayuso17e6e4e2009-06-02 20:08:46 +0200807
Patrick McHardydf0933d2006-09-20 11:57:53 -0700808 nf_conntrack_event_cache(master_ct(ct) ?
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200809 IPCT_RELATED : IPCT_NEW, ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700810 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800811
Patrick McHardydf0933d2006-09-20 11:57:53 -0700812out:
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100813 nf_ct_add_to_dying_list(ct);
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200814 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
815dying:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100816 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200817 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100818 local_bh_enable();
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200819 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800820}
Patrick McHardy13b18332006-12-02 22:11:25 -0800821EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800822
823/* Returns true if a connection correspondings to the tuple (required
824 for NAT). */
825int
826nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
827 const struct nf_conn *ignored_conntrack)
828{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200829 struct net *net = nf_ct_net(ignored_conntrack);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200830 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800831 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200832 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800833 unsigned int hash, hsize;
Eric Dumazetea781f12009-03-25 21:05:46 +0100834 struct hlist_nulls_node *n;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100835 struct nf_conn *ct;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200836
837 zone = nf_ct_zone(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800838
Florian Westphal2cf12342016-04-28 19:13:40 +0200839 rcu_read_lock();
Florian Westphal95a8d192016-08-25 15:33:29 +0200840 begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800841 nf_conntrack_get_ht(&ct_hash, &hsize);
842 hash = __hash_conntrack(net, tuple, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200843
844 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100845 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphalf330a7f2016-08-25 15:33:31 +0200846
847 if (ct == ignored_conntrack)
848 continue;
849
850 if (nf_ct_is_expired(ct)) {
851 nf_ct_gc_expired(ct);
852 continue;
853 }
854
855 if (nf_ct_key_equal(h, tuple, zone, net)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200856 NF_CT_STAT_INC_ATOMIC(net, found);
857 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800858 return 1;
859 }
Florian Westphal2cf12342016-04-28 19:13:40 +0200860 NF_CT_STAT_INC_ATOMIC(net, searched);
Patrick McHardyba419af2008-01-31 04:39:23 -0800861 }
Florian Westphal95a8d192016-08-25 15:33:29 +0200862
863 if (get_nulls_value(n) != hash) {
864 NF_CT_STAT_INC_ATOMIC(net, search_restart);
865 goto begin;
866 }
867
Florian Westphal2cf12342016-04-28 19:13:40 +0200868 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800869
Patrick McHardyba419af2008-01-31 04:39:23 -0800870 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800871}
Patrick McHardy13b18332006-12-02 22:11:25 -0800872EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800873
Patrick McHardy7ae77302007-07-07 22:37:38 -0700874#define NF_CT_EVICTION_RANGE 8
875
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800876/* There's a small race here where we may free a just-assured
877 connection. Too bad: we're in trouble anyway. */
Florian Westphal242922a2016-07-03 20:44:01 +0200878static unsigned int early_drop_list(struct net *net,
879 struct hlist_nulls_head *head)
880{
881 struct nf_conntrack_tuple_hash *h;
882 struct hlist_nulls_node *n;
883 unsigned int drops = 0;
884 struct nf_conn *tmp;
885
886 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
887 tmp = nf_ct_tuplehash_to_ctrack(h);
888
Florian Westphalf330a7f2016-08-25 15:33:31 +0200889 if (nf_ct_is_expired(tmp)) {
890 nf_ct_gc_expired(tmp);
891 continue;
892 }
893
Florian Westphal242922a2016-07-03 20:44:01 +0200894 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
895 !net_eq(nf_ct_net(tmp), net) ||
896 nf_ct_is_dying(tmp))
897 continue;
898
899 if (!atomic_inc_not_zero(&tmp->ct_general.use))
900 continue;
901
902 /* kill only if still in same netns -- might have moved due to
903 * SLAB_DESTROY_BY_RCU rules.
904 *
905 * We steal the timer reference. If that fails timer has
906 * already fired or someone else deleted it. Just drop ref
907 * and move to next entry.
908 */
909 if (net_eq(nf_ct_net(tmp), net) &&
910 nf_ct_is_confirmed(tmp) &&
Florian Westphal242922a2016-07-03 20:44:01 +0200911 nf_ct_delete(tmp, 0, 0))
912 drops++;
913
914 nf_ct_put(tmp);
915 }
916
917 return drops;
918}
919
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100920static noinline int early_drop(struct net *net, unsigned int _hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800921{
Florian Westphal242922a2016-07-03 20:44:01 +0200922 unsigned int i;
Florian Westphal3e866382016-05-02 18:40:14 +0200923
Florian Westphal242922a2016-07-03 20:44:01 +0200924 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
925 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800926 unsigned int hash, hsize, drops;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800927
Liping Zhang3101e0f2016-07-12 19:45:00 +0800928 rcu_read_lock();
Liping Zhang92e47ba2016-08-13 22:35:36 +0800929 nf_conntrack_get_ht(&ct_hash, &hsize);
930 hash = reciprocal_scale(_hash++, hsize);
Florian Westphal3e866382016-05-02 18:40:14 +0200931
Florian Westphal242922a2016-07-03 20:44:01 +0200932 drops = early_drop_list(net, &ct_hash[hash]);
Liping Zhang3101e0f2016-07-12 19:45:00 +0800933 rcu_read_unlock();
Florian Westphal3e866382016-05-02 18:40:14 +0200934
Florian Westphal242922a2016-07-03 20:44:01 +0200935 if (drops) {
936 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
937 return true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000938 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800939 }
Florian Westphal3e866382016-05-02 18:40:14 +0200940
Florian Westphal242922a2016-07-03 20:44:01 +0200941 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800942}
943
Florian Westphalb87a2f92016-08-25 15:33:33 +0200944static void gc_worker(struct work_struct *work)
945{
946 unsigned int i, goal, buckets = 0, expired_count = 0;
947 unsigned long next_run = GC_INTERVAL;
948 struct conntrack_gc_work *gc_work;
949
950 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
951
952 goal = min(nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV, GC_MAX_BUCKETS);
953 i = gc_work->last_bucket;
954
955 do {
956 struct nf_conntrack_tuple_hash *h;
957 struct hlist_nulls_head *ct_hash;
958 struct hlist_nulls_node *n;
959 unsigned int hashsz;
960 struct nf_conn *tmp;
961
962 i++;
963 rcu_read_lock();
964
965 nf_conntrack_get_ht(&ct_hash, &hashsz);
966 if (i >= hashsz)
967 i = 0;
968
969 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
970 tmp = nf_ct_tuplehash_to_ctrack(h);
971
972 if (nf_ct_is_expired(tmp)) {
973 nf_ct_gc_expired(tmp);
974 expired_count++;
975 continue;
976 }
977 }
978
979 /* could check get_nulls_value() here and restart if ct
980 * was moved to another chain. But given gc is best-effort
981 * we will just continue with next hash slot.
982 */
983 rcu_read_unlock();
984 cond_resched_rcu_qs();
985 } while (++buckets < goal &&
986 expired_count < GC_MAX_EVICTS);
987
988 if (gc_work->exiting)
989 return;
990
991 gc_work->last_bucket = i;
992 schedule_delayed_work(&gc_work->dwork, next_run);
993}
994
995static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
996{
997 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
998 gc_work->exiting = false;
999}
1000
Changli Gao99f07e92010-09-21 17:49:20 +02001001static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +02001002__nf_conntrack_alloc(struct net *net,
1003 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001004 const struct nf_conntrack_tuple *orig,
1005 const struct nf_conntrack_tuple *repl,
1006 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001007{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001008 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001009
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001010 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001011 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001012
Patrick McHardy76eb9462008-01-31 04:41:44 -08001013 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001014 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001015 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001016 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +00001017 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001018 return ERR_PTR(-ENOMEM);
1019 }
1020 }
1021
Eric Dumazet941297f2009-07-16 14:03:40 +02001022 /*
1023 * Do not use kmem_cache_zalloc(), as this cache uses
1024 * SLAB_DESTROY_BY_RCU.
1025 */
Florian Westphal0c5366b2016-05-09 16:24:32 +02001026 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001027 if (ct == NULL)
1028 goto out;
1029
Patrick McHardy440f0d52009-06-10 14:32:47 +02001030 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001031 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +02001032 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -08001033 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +02001034 /* save hash for reusing when confirming */
1035 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +01001036 ct->status = 0;
Eric Dumazetc2d9ba92010-06-01 06:51:19 +00001037 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +01001038 memset(&ct->__nfct_init_offset[0], 0,
1039 offsetof(struct nf_conn, proto) -
1040 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001041
Florian Westphal6c8dee92016-06-11 21:57:35 +02001042 nf_ct_zone_add(ct, zone);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001043
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001044 /* Because we use RCU lookups, we set ct_general.use to zero before
1045 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +02001046 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001047 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001048 return ct;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001049out:
1050 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001051 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001052}
Changli Gao99f07e92010-09-21 17:49:20 +02001053
Daniel Borkmann308ac912015-08-08 21:40:01 +02001054struct nf_conn *nf_conntrack_alloc(struct net *net,
1055 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001056 const struct nf_conntrack_tuple *orig,
1057 const struct nf_conntrack_tuple *repl,
1058 gfp_t gfp)
1059{
1060 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1061}
Patrick McHardy13b18332006-12-02 22:11:25 -08001062EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063
Patrick McHardyc88130b2008-01-31 04:42:11 -08001064void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001065{
Eric Dumazet1d452092009-03-24 14:26:50 +01001066 struct net *net = nf_ct_net(ct);
1067
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001068 /* A freed object has refcnt == 0, that's
1069 * the golden rule for SLAB_DESTROY_BY_RCU
1070 */
1071 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1072
Patrick McHardyceeff752008-06-11 17:51:10 -07001073 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +01001074 nf_ct_ext_free(ct);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001075 kmem_cache_free(nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001076 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +01001077 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001078}
Patrick McHardy13b18332006-12-02 22:11:25 -08001079EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001080
Florian Westphalc539f012013-01-11 06:30:44 +00001081
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001082/* Allocate a new conntrack: we return -ENOMEM if classification
1083 failed due to stress. Otherwise it really is unclassifiable. */
1084static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001085init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001086 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001087 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001088 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001089 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001090 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001091{
Patrick McHardyc88130b2008-01-31 04:42:11 -08001092 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001093 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001094 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001095 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001096 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +02001097 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001098 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001099 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001100 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001101
Martin Josefsson605dcad2006-11-29 02:35:06 +01001102 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001103 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001104 return NULL;
1105 }
1106
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001107 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +02001108 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1109 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -07001110 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -08001111 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001112
Patrick McHardy48b1de42013-08-27 08:50:14 +02001113 if (tmpl && nfct_synproxy(tmpl)) {
1114 nfct_seqadj_ext_add(ct);
1115 nfct_synproxy_ext_add(ct);
1116 }
1117
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001118 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001119 if (timeout_ext) {
1120 timeouts = nf_ct_timeout_data(timeout_ext);
1121 if (unlikely(!timeouts))
1122 timeouts = l4proto->get_timeouts(net);
1123 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001124 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001125 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001126
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001127 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -08001128 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001129 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001130 return NULL;
1131 }
1132
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001133 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001134 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1135 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001136
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001137 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001138 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +00001139 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001140
1141 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1142 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1143 ecache ? ecache->expmask : 0,
1144 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001145
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001146 local_bh_disable();
1147 if (net->ct.expect_count) {
1148 spin_lock(&nf_conntrack_expect_lock);
1149 exp = nf_ct_find_expectation(net, zone, tuple);
1150 if (exp) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001151 pr_debug("expectation arrives ct=%p exp=%p\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001152 ct, exp);
1153 /* Welcome, Mr. Bond. We've been expecting you... */
1154 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1155 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1156 ct->master = exp->master;
1157 if (exp->helper) {
1158 help = nf_ct_helper_ext_add(ct, exp->helper,
1159 GFP_ATOMIC);
1160 if (help)
1161 rcu_assign_pointer(help->helper, exp->helper);
1162 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001163
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001164#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001165 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001166#endif
James Morris7c9728c2006-06-09 00:31:46 -07001167#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001168 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001169#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001170 NF_CT_STAT_INC(net, expect_new);
1171 }
1172 spin_unlock(&nf_conntrack_expect_lock);
1173 }
1174 if (!exp) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001175 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001176 NF_CT_STAT_INC(net, new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -08001177 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001178
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001179 /* Now it is inserted into the unconfirmed list, bump refcount */
1180 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001181 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001182
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001183 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001184
1185 if (exp) {
1186 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001187 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001188 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001189 }
1190
Patrick McHardyc88130b2008-01-31 04:42:11 -08001191 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001192}
1193
1194/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1195static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001196resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001197 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001198 unsigned int dataoff,
1199 u_int16_t l3num,
1200 u_int8_t protonum,
1201 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001202 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001203 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001204 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001205{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001206 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001207 struct nf_conntrack_tuple tuple;
1208 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001209 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001210 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001211 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001212
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001213 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001214 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001215 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001216 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001217 return NULL;
1218 }
1219
1220 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001221 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001222 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001223 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001224 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001225 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001226 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001227 if (!h)
1228 return NULL;
1229 if (IS_ERR(h))
1230 return (void *)h;
1231 }
1232 ct = nf_ct_tuplehash_to_ctrack(h);
1233
1234 /* It exists; we have (non-exclusive) reference. */
1235 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001236 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001237 /* Please set reply bit if this packet OK */
1238 *set_reply = 1;
1239 } else {
1240 /* Once we've had two way comms, always ESTABLISHED. */
1241 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001242 pr_debug("normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001243 *ctinfo = IP_CT_ESTABLISHED;
1244 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001245 pr_debug("related packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001246 *ctinfo = IP_CT_RELATED;
1247 } else {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001248 pr_debug("new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001249 *ctinfo = IP_CT_NEW;
1250 }
1251 *set_reply = 0;
1252 }
1253 skb->nfct = &ct->ct_general;
1254 skb->nfctinfo = *ctinfo;
1255 return ct;
1256}
1257
1258unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001259nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1260 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001261{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001262 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001263 enum ip_conntrack_info ctinfo;
1264 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001265 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001266 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001267 unsigned int dataoff;
1268 u_int8_t protonum;
1269 int set_reply = 0;
1270 int ret;
1271
Herbert Xu3db05fe2007-10-15 00:53:15 -07001272 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001273 /* Previously seen (loopback or untracked)? Ignore. */
1274 tmpl = (struct nf_conn *)skb->nfct;
1275 if (!nf_ct_is_template(tmpl)) {
1276 NF_CT_STAT_INC_ATOMIC(net, ignore);
1277 return NF_ACCEPT;
1278 }
1279 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001280 }
1281
Patrick McHardy923f4902007-02-12 11:12:57 -08001282 /* rcu_read_lock()ed by nf_hook_slow */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001283 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001284 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001285 &dataoff, &protonum);
1286 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001287 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001288 NF_CT_STAT_INC_ATOMIC(net, error);
1289 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001290 ret = -ret;
1291 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001292 }
1293
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001294 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001295
1296 /* It may be an special packet, error, unclean...
1297 * inverse of the return code tells to the netfilter
1298 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001299 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001300 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1301 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001302 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001303 NF_CT_STAT_INC_ATOMIC(net, error);
1304 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001305 ret = -ret;
1306 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001307 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001308 /* ICMP[v6] protocol trackers may assign one conntrack. */
1309 if (skb->nfct)
1310 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001311 }
1312
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001313 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001314 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001315 if (!ct) {
1316 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001317 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001318 ret = NF_ACCEPT;
1319 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001320 }
1321
1322 if (IS_ERR(ct)) {
1323 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001324 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001325 ret = NF_DROP;
1326 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001327 }
1328
Herbert Xu3db05fe2007-10-15 00:53:15 -07001329 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001330
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001331 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001332 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001333
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001334 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001335 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001336 /* Invalid: inverse of the return code tells
1337 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001338 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001339 nf_conntrack_put(skb->nfct);
1340 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001341 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001342 if (ret == -NF_DROP)
1343 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001344 ret = -ret;
1345 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001346 }
1347
1348 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001349 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001350out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001351 if (tmpl) {
1352 /* Special case: we have to repeat this hook, assign the
1353 * template again to this packet. We assume that this packet
1354 * has no conntrack assigned. This is used by nf_ct_tcp. */
1355 if (ret == NF_REPEAT)
1356 skb->nfct = (struct nf_conntrack *)tmpl;
1357 else
1358 nf_ct_put(tmpl);
1359 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001360
1361 return ret;
1362}
Patrick McHardy13b18332006-12-02 22:11:25 -08001363EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001364
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001365bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1366 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001367{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001368 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001369
1370 rcu_read_lock();
1371 ret = nf_ct_invert_tuple(inverse, orig,
1372 __nf_ct_l3proto_find(orig->src.l3num),
1373 __nf_ct_l4proto_find(orig->src.l3num,
1374 orig->dst.protonum));
1375 rcu_read_unlock();
1376 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001377}
Patrick McHardy13b18332006-12-02 22:11:25 -08001378EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001379
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001380/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1381 implicitly racy: see __nf_conntrack_confirm */
1382void nf_conntrack_alter_reply(struct nf_conn *ct,
1383 const struct nf_conntrack_tuple *newreply)
1384{
1385 struct nf_conn_help *help = nfct_help(ct);
1386
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001387 /* Should be unconfirmed, so not in hash table yet */
1388 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1389
Patrick McHardy0d537782007-07-07 22:39:38 -07001390 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001391 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001392
1393 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001394 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001395 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001396
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001397 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001398 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001399 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001400}
Patrick McHardy13b18332006-12-02 22:11:25 -08001401EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001402
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001403/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1404void __nf_ct_refresh_acct(struct nf_conn *ct,
1405 enum ip_conntrack_info ctinfo,
1406 const struct sk_buff *skb,
1407 unsigned long extra_jiffies,
1408 int do_acct)
1409{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001410 NF_CT_ASSERT(skb);
1411
Eric Leblond997ae832006-05-29 18:24:20 -07001412 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001413 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1414 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001415
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001416 /* If not in hash table, timer will not be active yet */
Florian Westphalf330a7f2016-08-25 15:33:31 +02001417 if (nf_ct_is_confirmed(ct))
1418 extra_jiffies += nfct_time_stamp;
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001419
Florian Westphalf330a7f2016-08-25 15:33:31 +02001420 ct->timeout = extra_jiffies;
Patrick McHardy47d95042008-01-31 04:36:31 -08001421acct:
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001422 if (do_acct)
1423 nf_ct_acct_update(ct, ctinfo, skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001424}
Patrick McHardy13b18332006-12-02 22:11:25 -08001425EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001426
David S. Miller4c889492008-07-14 20:22:38 -07001427bool __nf_ct_kill_acct(struct nf_conn *ct,
1428 enum ip_conntrack_info ctinfo,
1429 const struct sk_buff *skb,
1430 int do_acct)
Patrick McHardy51091762008-06-09 15:59:06 -07001431{
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001432 if (do_acct)
1433 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001434
Florian Westphalf330a7f2016-08-25 15:33:31 +02001435 return nf_ct_delete(ct, 0, 0);
Patrick McHardy51091762008-06-09 15:59:06 -07001436}
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -07001437EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001438
Igor Maravićc0cd1152011-12-12 02:58:24 +00001439#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001440
1441#include <linux/netfilter/nfnetlink.h>
1442#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001443#include <linux/mutex.h>
1444
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001445/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1446 * in ip_conntrack_core, since we don't want the protocols to autoload
1447 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001448int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001449 const struct nf_conntrack_tuple *tuple)
1450{
David S. Millerbae65be2012-04-01 18:58:28 -04001451 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1452 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1453 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001454 return 0;
1455
Patrick McHardydf6fb862007-09-28 14:37:03 -07001456nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001457 return -1;
1458}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001459EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001460
Patrick McHardyf73e9242007-09-28 14:39:55 -07001461const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1462 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1463 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001464};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001465EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001466
Patrick McHardyfdf70832007-09-28 14:37:41 -07001467int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001468 struct nf_conntrack_tuple *t)
1469{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001470 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001471 return -EINVAL;
1472
Patrick McHardy77236b62007-12-17 22:29:45 -08001473 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1474 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001475
1476 return 0;
1477}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001478EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001479
1480int nf_ct_port_nlattr_tuple_size(void)
1481{
1482 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1483}
1484EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001485#endif
1486
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001487/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001488static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001489{
1490 struct nf_conn *ct;
1491 enum ip_conntrack_info ctinfo;
1492
1493 /* This ICMP is in reverse direction to the packet which caused it */
1494 ct = nf_ct_get(skb, &ctinfo);
1495 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001496 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001497 else
1498 ctinfo = IP_CT_RELATED;
1499
1500 /* Attach to new skbuff, and increment count */
1501 nskb->nfct = &ct->ct_general;
1502 nskb->nfctinfo = ctinfo;
1503 nf_conntrack_get(nskb->nfct);
1504}
1505
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001506/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001507static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001508get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001509 void *data, unsigned int *bucket)
1510{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001511 struct nf_conntrack_tuple_hash *h;
1512 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001513 struct hlist_nulls_node *n;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001514 int cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001515 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001516
Florian Westphal56d52d42016-05-02 18:39:55 +02001517 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001518 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1519 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001520 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001521 if (*bucket < nf_conntrack_htable_size) {
1522 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001523 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1524 continue;
1525 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001526 if (net_eq(nf_ct_net(ct), net) &&
1527 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001528 goto found;
1529 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001530 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001531 spin_unlock(lockp);
1532 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001533 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001534 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001535
1536 for_each_possible_cpu(cpu) {
1537 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1538
1539 spin_lock_bh(&pcpu->lock);
1540 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1541 ct = nf_ct_tuplehash_to_ctrack(h);
1542 if (iter(ct, data))
1543 set_bit(IPS_DYING_BIT, &ct->status);
1544 }
1545 spin_unlock_bh(&pcpu->lock);
Florian Westphald93c6252016-01-20 11:16:43 +01001546 cond_resched();
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001547 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001548 return NULL;
1549found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001550 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001551 spin_unlock(lockp);
1552 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001553 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001554}
1555
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001556void nf_ct_iterate_cleanup(struct net *net,
1557 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001558 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001559{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001560 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001561 unsigned int bucket = 0;
1562
Florian Westphald93c6252016-01-20 11:16:43 +01001563 might_sleep();
1564
Florian Westphal88b68bc2016-04-28 19:13:42 +02001565 if (atomic_read(&net->ct.count) == 0)
1566 return;
1567
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001568 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001569 /* Time to push up daises... */
Florian Westphal02982c22013-07-29 15:41:54 +02001570
Florian Westphalf330a7f2016-08-25 15:33:31 +02001571 nf_ct_delete(ct, portid, report);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001572 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001573 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001574 }
1575}
Patrick McHardy13b18332006-12-02 22:11:25 -08001576EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001577
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001578static int kill_all(struct nf_conn *i, void *data)
1579{
1580 return 1;
1581}
1582
Patrick McHardyd862a662011-01-14 15:45:56 +01001583void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001584{
Patrick McHardyd862a662011-01-14 15:45:56 +01001585 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001586 vfree(hash);
1587 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001588 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001589 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001590}
Patrick McHardyac565e52007-07-07 22:30:08 -07001591EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001592
Eric Dumazetb3c51632010-06-09 14:43:38 +02001593static int untrack_refs(void)
1594{
1595 int cnt = 0, cpu;
1596
1597 for_each_possible_cpu(cpu) {
1598 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1599
1600 cnt += atomic_read(&ct->ct_general.use) - 1;
1601 }
1602 return cnt;
1603}
1604
Gao fengf94161c2013-01-21 22:10:24 +00001605void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001606{
Florian Westphalb87a2f92016-08-25 15:33:33 +02001607 conntrack_gc_work.exiting = true;
Gao fengf94161c2013-01-21 22:10:24 +00001608 RCU_INIT_POINTER(ip_ct_attach, NULL);
1609}
1610
1611void nf_conntrack_cleanup_end(void)
1612{
1613 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001614 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001615 schedule();
1616
Florian Westphalb87a2f92016-08-25 15:33:33 +02001617 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
Florian Westphal56d52d42016-05-02 18:39:55 +02001618 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1619
Gao feng04d87002013-01-21 22:10:32 +00001620 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001621 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001622 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001623 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001624 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001625 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001626 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001627 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001628 nf_conntrack_expect_fini();
Florian Westphal77571142016-06-10 17:25:19 +02001629
1630 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001631}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001632
Gao fengf94161c2013-01-21 22:10:24 +00001633/*
1634 * Mishearing the voices in his head, our hero wonders how he's
1635 * supposed to kill the mall.
1636 */
1637void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001638{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001639 LIST_HEAD(single);
1640
1641 list_add(&net->exit_list, &single);
1642 nf_conntrack_cleanup_net_list(&single);
1643}
1644
1645void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1646{
1647 int busy;
1648 struct net *net;
1649
Gao fengf94161c2013-01-21 22:10:24 +00001650 /*
1651 * This makes sure all current packets have passed through
1652 * netfilter framework. Roll on, two-stage module
1653 * delete...
1654 */
1655 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001656i_see_dead_people:
1657 busy = 0;
1658 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001659 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001660 if (atomic_read(&net->ct.count) != 0)
1661 busy = 1;
1662 }
1663 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001664 schedule();
1665 goto i_see_dead_people;
1666 }
1667
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001668 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001669 nf_conntrack_proto_pernet_fini(net);
1670 nf_conntrack_helper_pernet_fini(net);
1671 nf_conntrack_ecache_pernet_fini(net);
1672 nf_conntrack_tstamp_pernet_fini(net);
1673 nf_conntrack_acct_pernet_fini(net);
1674 nf_conntrack_expect_pernet_fini(net);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001675 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001676 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001677 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001678}
1679
Patrick McHardyd862a662011-01-14 15:45:56 +01001680void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001681{
Eric Dumazetea781f12009-03-25 21:05:46 +01001682 struct hlist_nulls_head *hash;
1683 unsigned int nr_slots, i;
1684 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001685
Florian Westphal9cc1c732016-04-24 01:18:21 +02001686 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1687 return NULL;
1688
Eric Dumazetea781f12009-03-25 21:05:46 +01001689 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1690 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
Florian Westphal9cc1c732016-04-24 01:18:21 +02001691
1692 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1693 return NULL;
1694
Eric Dumazetea781f12009-03-25 21:05:46 +01001695 sz = nr_slots * sizeof(struct hlist_nulls_head);
1696 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1697 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001698 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001699 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001700
Eric Dumazetea781f12009-03-25 21:05:46 +01001701 if (hash && nulls)
1702 for (i = 0; i < nr_slots; i++)
1703 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001704
1705 return hash;
1706}
Patrick McHardyac565e52007-07-07 22:30:08 -07001707EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001708
Florian Westphal3183ab82016-06-22 13:26:10 +02001709int nf_conntrack_hash_resize(unsigned int hashsize)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001710{
Florian Westphal3183ab82016-06-22 13:26:10 +02001711 int i, bucket;
1712 unsigned int old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001713 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001714 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001715 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001716
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001717 if (!hashsize)
1718 return -EINVAL;
1719
Patrick McHardyd862a662011-01-14 15:45:56 +01001720 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001721 if (!hash)
1722 return -ENOMEM;
1723
Florian Westphal3183ab82016-06-22 13:26:10 +02001724 old_size = nf_conntrack_htable_size;
1725 if (old_size == hashsize) {
1726 nf_ct_free_hashtable(hash, hashsize);
1727 return 0;
1728 }
1729
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001730 local_bh_disable();
1731 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001732 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001733
Patrick McHardy76507f62008-01-31 04:38:38 -08001734 /* Lookups in the old hash might happen in parallel, which means we
1735 * might get false negatives during connection lookup. New connections
1736 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001737 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001738 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001739
Florian Westphal56d52d42016-05-02 18:39:55 +02001740 for (i = 0; i < nf_conntrack_htable_size; i++) {
1741 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1742 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1743 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001744 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001745 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001746 bucket = __hash_conntrack(nf_ct_net(ct),
1747 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001748 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001749 }
1750 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001751 old_size = nf_conntrack_htable_size;
1752 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001753
Florian Westphal56d52d42016-05-02 18:39:55 +02001754 nf_conntrack_hash = hash;
1755 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001756
Florian Westphala3efd812016-04-18 16:16:59 +02001757 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001758 nf_conntrack_all_unlock();
1759 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001760
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001761 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001762 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001763 return 0;
1764}
Florian Westphal3183ab82016-06-22 13:26:10 +02001765
1766int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1767{
1768 unsigned int hashsize;
1769 int rc;
1770
1771 if (current->nsproxy->net_ns != &init_net)
1772 return -EOPNOTSUPP;
1773
1774 /* On boot, we can set this without any fancy locking. */
1775 if (!nf_conntrack_htable_size)
1776 return param_set_uint(val, kp);
1777
1778 rc = kstrtouint(val, 0, &hashsize);
1779 if (rc)
1780 return rc;
1781
1782 return nf_conntrack_hash_resize(hashsize);
1783}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001784EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001785
Patrick McHardyfae718d2007-12-24 21:09:10 -08001786module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001787 &nf_conntrack_htable_size, 0600);
1788
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001789void nf_ct_untracked_status_or(unsigned long bits)
1790{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001791 int cpu;
1792
1793 for_each_possible_cpu(cpu)
1794 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001795}
1796EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1797
Gao fengf94161c2013-01-21 22:10:24 +00001798int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001799{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001800 int max_factor = 8;
Florian Westphal0c5366b2016-05-09 16:24:32 +02001801 int ret = -ENOMEM;
1802 int i, cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001803
Florian Westphala3efd812016-04-18 16:16:59 +02001804 seqcount_init(&nf_conntrack_generation);
1805
Eric Dumazetd5d20912014-03-17 13:37:53 -07001806 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001807 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001808
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001809 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001810 /* Idea from tcp.c: use 1/16384 of memory.
1811 * On i386: 32MB machine has 512 buckets.
1812 * >= 1GB machines have 16384 buckets.
1813 * >= 4GB machines have 65536 buckets.
1814 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001815 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07001816 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001817 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02001818 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1819 nf_conntrack_htable_size = 65536;
1820 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001821 nf_conntrack_htable_size = 16384;
1822 if (nf_conntrack_htable_size < 32)
1823 nf_conntrack_htable_size = 32;
1824
1825 /* Use a max. factor of four by default to get the same max as
1826 * with the old struct list_heads. When a table size is given
1827 * we use the old value of 8 to avoid reducing the max.
1828 * entries. */
1829 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001830 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001831
1832 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1833 if (!nf_conntrack_hash)
1834 return -ENOMEM;
1835
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001836 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001837
Florian Westphal0c5366b2016-05-09 16:24:32 +02001838 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1839 sizeof(struct nf_conn), 0,
Florian Westphal5a75cde2016-06-10 22:25:22 +02001840 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001841 if (!nf_conntrack_cachep)
1842 goto err_cachep;
1843
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001844 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001845 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1846 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00001847
1848 ret = nf_conntrack_expect_init();
1849 if (ret < 0)
1850 goto err_expect;
1851
Gao fengb7ff3a12013-01-21 22:10:26 +00001852 ret = nf_conntrack_acct_init();
1853 if (ret < 0)
1854 goto err_acct;
1855
Gao feng73f40012013-01-21 22:10:27 +00001856 ret = nf_conntrack_tstamp_init();
1857 if (ret < 0)
1858 goto err_tstamp;
1859
Gao feng3fe0f942013-01-21 22:10:28 +00001860 ret = nf_conntrack_ecache_init();
1861 if (ret < 0)
1862 goto err_ecache;
1863
Gao feng86840942013-01-21 22:10:29 +00001864 ret = nf_conntrack_timeout_init();
1865 if (ret < 0)
1866 goto err_timeout;
1867
Gao feng5e615b22013-01-21 22:10:30 +00001868 ret = nf_conntrack_helper_init();
1869 if (ret < 0)
1870 goto err_helper;
1871
Gao feng5f69b8f2013-01-21 22:10:31 +00001872 ret = nf_conntrack_labels_init();
1873 if (ret < 0)
1874 goto err_labels;
1875
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001876 ret = nf_conntrack_seqadj_init();
1877 if (ret < 0)
1878 goto err_seqadj;
1879
Gao feng04d87002013-01-21 22:10:32 +00001880 ret = nf_conntrack_proto_init();
1881 if (ret < 0)
1882 goto err_proto;
1883
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001884 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02001885 for_each_possible_cpu(cpu) {
1886 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001887 write_pnet(&ct->ct_net, &init_net);
1888 atomic_set(&ct->ct_general.use, 1);
1889 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001890 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001891 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001892
1893 conntrack_gc_work_init(&conntrack_gc_work);
1894 schedule_delayed_work(&conntrack_gc_work.dwork, GC_INTERVAL);
1895
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001896 return 0;
1897
Gao feng04d87002013-01-21 22:10:32 +00001898err_proto:
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001899 nf_conntrack_seqadj_fini();
1900err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00001901 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001902err_labels:
1903 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001904err_helper:
1905 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00001906err_timeout:
1907 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001908err_ecache:
1909 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00001910err_tstamp:
1911 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001912err_acct:
1913 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001914err_expect:
Florian Westphal0c5366b2016-05-09 16:24:32 +02001915 kmem_cache_destroy(nf_conntrack_cachep);
1916err_cachep:
Florian Westphal56d52d42016-05-02 18:39:55 +02001917 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001918 return ret;
1919}
1920
Gao fengf94161c2013-01-21 22:10:24 +00001921void nf_conntrack_init_end(void)
1922{
1923 /* For use by REJECT target */
1924 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1925 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00001926}
1927
Eric Dumazet8cc20192009-06-22 14:13:55 +02001928/*
1929 * We need to use special "null" values, not used in hash table
1930 */
1931#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1932#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00001933#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02001934
Gao fengf94161c2013-01-21 22:10:24 +00001935int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001936{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001937 int ret = -ENOMEM;
1938 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001939
1940 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001941
1942 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1943 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001944 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001945
1946 for_each_possible_cpu(cpu) {
1947 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1948
1949 spin_lock_init(&pcpu->lock);
1950 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1951 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001952 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001953
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001954 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1955 if (!net->ct.stat)
1956 goto err_pcpu_lists;
1957
Gao feng83b4dbe2013-01-21 22:10:25 +00001958 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001959 if (ret < 0)
1960 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00001961 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001962 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001963 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00001964 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001965 if (ret < 0)
1966 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00001967 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001968 if (ret < 0)
1969 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00001970 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001971 if (ret < 0)
1972 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00001973 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00001974 if (ret < 0)
1975 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001976 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00001977
Gao fengf94161c2013-01-21 22:10:24 +00001978err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00001979 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001980err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00001981 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001982err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00001983 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001984err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00001985 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001986err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00001987 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001988err_expect:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001989 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001990err_pcpu_lists:
1991 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001992err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001993 return ret;
1994}