blob: 8d1ddb9b63edbb3eac91bd5ac2623448aa6aea3d [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;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100382 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383
Patrick McHardy0d537782007-07-07 22:39:38 -0700384 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200387 if (unlikely(nf_ct_is_template(ct))) {
388 nf_ct_tmpl_free(ct);
389 return;
390 }
Patrick McHardy923f4902007-02-12 11:12:57 -0800391 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200392 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Pablo Neira Ayuso4b4ceb92016-05-01 00:34:37 +0200393 if (l4proto->destroy)
Martin Josefsson605dcad2006-11-29 02:35:06 +0100394 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395
Patrick McHardy982d9a92007-02-12 11:14:11 -0800396 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100398 local_bh_disable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800399 /* Expectations will have been removed in clean_from_lists,
400 * except TFTP can create an expectation on the first packet,
401 * before connection is in the list, so we need to clean here,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100402 * too.
403 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800404 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800405
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100406 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100408 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409
410 if (ct->master)
411 nf_ct_put(ct->master);
412
Patrick McHardy0d537782007-07-07 22:39:38 -0700413 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414 nf_conntrack_free(ct);
415}
416
Florian Westphal02982c22013-07-29 15:41:54 +0200417static void nf_ct_delete_from_lists(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418{
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200419 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100420 unsigned int hash, reply_hash;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100421 unsigned int sequence;
Patrick McHardy5397e972007-05-19 14:23:52 -0700422
Pablo Neira Ayuso9858a3a2009-06-13 12:28:22 +0200423 nf_ct_helper_destroy(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100424
425 local_bh_disable();
426 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200427 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200428 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100429 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200430 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100431 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
432 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
433
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800434 clean_from_lists(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100435 nf_conntrack_double_unlock(hash, reply_hash);
436
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100437 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100438
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100439 local_bh_enable();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200440}
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200441
Florian Westphal02982c22013-07-29 15:41:54 +0200442bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200443{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100444 struct nf_conn_tstamp *tstamp;
445
Florian Westphalf330a7f2016-08-25 15:33:31 +0200446 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
447 return false;
448
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100449 tstamp = nf_conn_tstamp_find(ct);
450 if (tstamp && tstamp->stop == 0)
Eric Dumazetd2de8752014-08-22 18:32:09 -0700451 tstamp->stop = ktime_get_real_ns();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200452
Florian Westphal95005072014-06-10 23:12:56 +0200453 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
454 portid, report) < 0) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200455 /* destroy event was not delivered. nf_ct_put will
456 * be done by event cache worker on redelivery.
457 */
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200458 nf_ct_delete_from_lists(ct);
Florian Westphal95005072014-06-10 23:12:56 +0200459 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
Florian Westphal02982c22013-07-29 15:41:54 +0200460 return false;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200461 }
Florian Westphal95005072014-06-10 23:12:56 +0200462
463 nf_conntrack_ecache_work(nf_ct_net(ct));
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200464 nf_ct_delete_from_lists(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800465 nf_ct_put(ct);
Florian Westphal02982c22013-07-29 15:41:54 +0200466 return true;
467}
468EXPORT_SYMBOL_GPL(nf_ct_delete);
469
Andrey Vaginc6825c02014-01-29 19:34:14 +0100470static inline bool
471nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
Daniel Borkmann308ac912015-08-08 21:40:01 +0200472 const struct nf_conntrack_tuple *tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200473 const struct nf_conntrack_zone *zone,
474 const struct net *net)
Andrey Vaginc6825c02014-01-29 19:34:14 +0100475{
476 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
477
478 /* A conntrack can be recreated with the equal tuple,
479 * so we need to check that the conntrack is confirmed
480 */
481 return nf_ct_tuple_equal(tuple, &h->tuple) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200482 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200483 nf_ct_is_confirmed(ct) &&
484 net_eq(net, nf_ct_net(ct));
Andrey Vaginc6825c02014-01-29 19:34:14 +0100485}
486
Florian Westphalf330a7f2016-08-25 15:33:31 +0200487/* caller must hold rcu readlock and none of the nf_conntrack_locks */
488static void nf_ct_gc_expired(struct nf_conn *ct)
489{
490 if (!atomic_inc_not_zero(&ct->ct_general.use))
491 return;
492
493 if (nf_ct_should_gc(ct))
494 nf_ct_kill(ct);
495
496 nf_ct_put(ct);
497}
498
Eric Dumazetea781f12009-03-25 21:05:46 +0100499/*
500 * Warning :
501 * - Caller must take a reference on returned object
502 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
Eric Dumazetea781f12009-03-25 21:05:46 +0100503 */
Changli Gao99f07e92010-09-21 17:49:20 +0200504static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200505____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200506 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800507{
508 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200509 struct hlist_nulls_head *ct_hash;
Eric Dumazetea781f12009-03-25 21:05:46 +0100510 struct hlist_nulls_node *n;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800511 unsigned int bucket, hsize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512
Eric Dumazetea781f12009-03-25 21:05:46 +0100513begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800514 nf_conntrack_get_ht(&ct_hash, &hsize);
515 bucket = reciprocal_scale(hash, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200516
517 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200518 struct nf_conn *ct;
519
520 ct = nf_ct_tuplehash_to_ctrack(h);
521 if (nf_ct_is_expired(ct)) {
522 nf_ct_gc_expired(ct);
523 continue;
524 }
525
526 if (nf_ct_is_dying(ct))
527 continue;
528
Florian Westphal8e8118f2016-09-11 22:55:53 +0200529 if (nf_ct_key_equal(h, tuple, zone, net))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800530 return h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800531 }
Eric Dumazetea781f12009-03-25 21:05:46 +0100532 /*
533 * if the nulls value we got at the end of this lookup is
534 * not the expected one, we must restart lookup.
535 * We probably met an item that was moved to another chain.
536 */
Changli Gao99f07e92010-09-21 17:49:20 +0200537 if (get_nulls_value(n) != bucket) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200538 NF_CT_STAT_INC_ATOMIC(net, search_restart);
Eric Dumazetea781f12009-03-25 21:05:46 +0100539 goto begin;
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200540 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800541
542 return NULL;
543}
Changli Gao99f07e92010-09-21 17:49:20 +0200544
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800545/* Find a connection corresponding to a tuple. */
Changli Gao99f07e92010-09-21 17:49:20 +0200546static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200547__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200548 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800549{
550 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800551 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800552
Patrick McHardy76507f62008-01-31 04:38:38 -0800553 rcu_read_lock();
Eric Dumazetea781f12009-03-25 21:05:46 +0100554begin:
Changli Gao99f07e92010-09-21 17:49:20 +0200555 h = ____nf_conntrack_find(net, zone, tuple, hash);
Patrick McHardy76507f62008-01-31 04:38:38 -0800556 if (h) {
557 ct = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy8d8890b72009-06-22 14:14:41 +0200558 if (unlikely(nf_ct_is_dying(ct) ||
559 !atomic_inc_not_zero(&ct->ct_general.use)))
Patrick McHardy76507f62008-01-31 04:38:38 -0800560 h = NULL;
Eric Dumazetea781f12009-03-25 21:05:46 +0100561 else {
Florian Westphale0c7d472016-04-28 19:13:45 +0200562 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
Eric Dumazetea781f12009-03-25 21:05:46 +0100563 nf_ct_put(ct);
564 goto begin;
565 }
566 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800567 }
568 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569
570 return h;
571}
Changli Gao99f07e92010-09-21 17:49:20 +0200572
573struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200574nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200575 const struct nf_conntrack_tuple *tuple)
576{
577 return __nf_conntrack_find_get(net, zone, tuple,
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200578 hash_conntrack_raw(tuple, net));
Changli Gao99f07e92010-09-21 17:49:20 +0200579}
Patrick McHardy13b18332006-12-02 22:11:25 -0800580EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800581
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800582static void __nf_conntrack_hash_insert(struct nf_conn *ct,
583 unsigned int hash,
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100584 unsigned int reply_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800585{
Eric Dumazetea781f12009-03-25 21:05:46 +0100586 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200587 &nf_conntrack_hash[hash]);
Eric Dumazetea781f12009-03-25 21:05:46 +0100588 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200589 &nf_conntrack_hash[reply_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800590}
591
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100592int
593nf_conntrack_hash_check_insert(struct nf_conn *ct)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800594{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200595 const struct nf_conntrack_zone *zone;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800596 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100597 unsigned int hash, reply_hash;
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100598 struct nf_conntrack_tuple_hash *h;
599 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100600 unsigned int sequence;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800601
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100602 zone = nf_ct_zone(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800603
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100604 local_bh_disable();
605 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200606 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200607 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100608 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200609 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100610 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
611 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100612
613 /* See if there's one in the list already, including reverse */
Florian Westphal56d52d42016-05-02 18:39:55 +0200614 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200615 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200616 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100617 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200618
Florian Westphal56d52d42016-05-02 18:39:55 +0200619 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200620 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200621 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100622 goto out;
623
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100624 smp_wmb();
625 /* The caller holds a reference to this object */
626 atomic_set(&ct->ct_general.use, 2);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100627 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100628 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100629 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100630 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100631 return 0;
632
633out:
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_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100636 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100637 return -EEXIST;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800638}
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100639EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800640
Pablo Neira Ayusoba767382016-05-02 21:28:57 +0200641static inline void nf_ct_acct_update(struct nf_conn *ct,
642 enum ip_conntrack_info ctinfo,
643 unsigned int len)
644{
645 struct nf_conn_acct *acct;
646
647 acct = nf_conn_acct_find(ct);
648 if (acct) {
649 struct nf_conn_counter *counter = acct->counter;
650
651 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
652 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
653 }
654}
655
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200656static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
657 const struct nf_conn *loser_ct)
658{
659 struct nf_conn_acct *acct;
660
661 acct = nf_conn_acct_find(loser_ct);
662 if (acct) {
663 struct nf_conn_counter *counter = acct->counter;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200664 unsigned int bytes;
665
666 /* u32 should be fine since we must have seen one packet. */
667 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
668 nf_ct_acct_update(ct, ctinfo, bytes);
669 }
670}
671
672/* Resolve race on insertion if this protocol allows this. */
673static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
674 enum ip_conntrack_info ctinfo,
675 struct nf_conntrack_tuple_hash *h)
676{
677 /* This is the conntrack entry already in hashes that won race. */
678 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
679 struct nf_conntrack_l4proto *l4proto;
680
681 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
682 if (l4proto->allow_clash &&
Pablo Neira Ayuso590b52e2016-07-11 17:28:54 +0200683 !nfct_nat(ct) &&
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200684 !nf_ct_is_dying(ct) &&
685 atomic_inc_not_zero(&ct->ct_general.use)) {
686 nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct);
687 nf_conntrack_put(skb->nfct);
688 /* Assign conntrack already in hashes to this skbuff. Don't
689 * modify skb->nfctinfo to ensure consistent stateful filtering.
690 */
691 skb->nfct = &ct->ct_general;
692 return NF_ACCEPT;
693 }
694 NF_CT_STAT_INC(net, drop);
695 return NF_DROP;
696}
697
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800698/* Confirm a connection given skb; places it in hash table */
699int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700700__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800701{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200702 const struct nf_conntrack_zone *zone;
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100703 unsigned int hash, reply_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700704 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800705 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700706 struct nf_conn_help *help;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100707 struct nf_conn_tstamp *tstamp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100708 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800709 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200710 struct net *net;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100711 unsigned int sequence;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200712 int ret = NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800713
Herbert Xu3db05fe2007-10-15 00:53:15 -0700714 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200715 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800716
717 /* ipt_REJECT uses nf_conntrack_attach to attach related
718 ICMP/TCP RST packets in other direction. Actual packet
719 which created connection will be IP_CT_NEW or for an
720 expected connection, IP_CT_RELATED. */
721 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
722 return NF_ACCEPT;
723
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100724 zone = nf_ct_zone(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100725 local_bh_disable();
726
727 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200728 sequence = read_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100729 /* reuse the hash saved before */
730 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
Florian Westphal56d52d42016-05-02 18:39:55 +0200731 hash = scale_hash(hash);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200732 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100733 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
734
735 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800736
737 /* We're not in hash table, and we refuse to set up related
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100738 * connections for unconfirmed conns. But packet copies and
739 * REJECT will give spurious warnings here.
740 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800741 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
742
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300743 /* No external references means no one else could have
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100744 * confirmed us.
745 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800746 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700747 pr_debug("Confirming conntrack %p\n", ct);
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100748 /* We have to check the DYING flag after unlink to prevent
749 * a race against nf_ct_get_next_corpse() possibly called from
750 * user context, else we insert an already 'dead' hash, blocking
751 * further use of that particular connection -JM.
752 */
753 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Joerg Marxfc350772010-05-20 15:55:30 +0200754
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200755 if (unlikely(nf_ct_is_dying(ct))) {
756 nf_ct_add_to_dying_list(ct);
757 goto dying;
758 }
Joerg Marxfc350772010-05-20 15:55:30 +0200759
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800760 /* See if there's one in the list already, including reverse:
761 NAT could have grabbed it without realizing, since we're
762 not in the hash. If there is, we lost race. */
Florian Westphal56d52d42016-05-02 18:39:55 +0200763 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200764 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200765 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700766 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200767
Florian Westphal56d52d42016-05-02 18:39:55 +0200768 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200769 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200770 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700771 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800772
Patrick McHardydf0933d2006-09-20 11:57:53 -0700773 /* Timer relative to confirmation time, not original
774 setting time, otherwise we'd get timer wrap in
775 weird delay cases. */
Florian Westphalf330a7f2016-08-25 15:33:31 +0200776 ct->timeout += nfct_time_stamp;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700777 atomic_inc(&ct->ct_general.use);
Changli Gao45eec342011-01-18 15:08:13 +0100778 ct->status |= IPS_CONFIRMED;
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200779
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100780 /* set conntrack timestamp, if enabled. */
781 tstamp = nf_conn_tstamp_find(ct);
782 if (tstamp) {
783 if (skb->tstamp.tv64 == 0)
Joe Perchese3192692012-06-03 17:41:40 +0000784 __net_timestamp(skb);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100785
786 tstamp->start = ktime_to_ns(skb->tstamp);
787 }
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200788 /* Since the lookup is lockless, hash insertion must be done after
789 * starting the timer and setting the CONFIRMED bit. The RCU barriers
790 * guarantee that no other CPU can find the conntrack before the above
791 * stores are visible.
792 */
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100793 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100794 nf_conntrack_double_unlock(hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100795 local_bh_enable();
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200796
Patrick McHardydf0933d2006-09-20 11:57:53 -0700797 help = nfct_help(ct);
798 if (help && help->helper)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200799 nf_conntrack_event_cache(IPCT_HELPER, ct);
Pablo Neira Ayuso17e6e4e2009-06-02 20:08:46 +0200800
Patrick McHardydf0933d2006-09-20 11:57:53 -0700801 nf_conntrack_event_cache(master_ct(ct) ?
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200802 IPCT_RELATED : IPCT_NEW, ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700803 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800804
Patrick McHardydf0933d2006-09-20 11:57:53 -0700805out:
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100806 nf_ct_add_to_dying_list(ct);
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200807 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
808dying:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100809 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200810 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100811 local_bh_enable();
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200812 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800813}
Patrick McHardy13b18332006-12-02 22:11:25 -0800814EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800815
816/* Returns true if a connection correspondings to the tuple (required
817 for NAT). */
818int
819nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
820 const struct nf_conn *ignored_conntrack)
821{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200822 struct net *net = nf_ct_net(ignored_conntrack);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200823 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800824 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200825 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800826 unsigned int hash, hsize;
Eric Dumazetea781f12009-03-25 21:05:46 +0100827 struct hlist_nulls_node *n;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100828 struct nf_conn *ct;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200829
830 zone = nf_ct_zone(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800831
Florian Westphal2cf12342016-04-28 19:13:40 +0200832 rcu_read_lock();
Florian Westphal95a8d192016-08-25 15:33:29 +0200833 begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800834 nf_conntrack_get_ht(&ct_hash, &hsize);
835 hash = __hash_conntrack(net, tuple, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200836
837 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100838 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphalf330a7f2016-08-25 15:33:31 +0200839
840 if (ct == ignored_conntrack)
841 continue;
842
843 if (nf_ct_is_expired(ct)) {
844 nf_ct_gc_expired(ct);
845 continue;
846 }
847
848 if (nf_ct_key_equal(h, tuple, zone, net)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200849 NF_CT_STAT_INC_ATOMIC(net, found);
850 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800851 return 1;
852 }
Patrick McHardyba419af2008-01-31 04:39:23 -0800853 }
Florian Westphal95a8d192016-08-25 15:33:29 +0200854
855 if (get_nulls_value(n) != hash) {
856 NF_CT_STAT_INC_ATOMIC(net, search_restart);
857 goto begin;
858 }
859
Florian Westphal2cf12342016-04-28 19:13:40 +0200860 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800861
Patrick McHardyba419af2008-01-31 04:39:23 -0800862 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800863}
Patrick McHardy13b18332006-12-02 22:11:25 -0800864EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800865
Patrick McHardy7ae77302007-07-07 22:37:38 -0700866#define NF_CT_EVICTION_RANGE 8
867
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800868/* There's a small race here where we may free a just-assured
869 connection. Too bad: we're in trouble anyway. */
Florian Westphal242922a2016-07-03 20:44:01 +0200870static unsigned int early_drop_list(struct net *net,
871 struct hlist_nulls_head *head)
872{
873 struct nf_conntrack_tuple_hash *h;
874 struct hlist_nulls_node *n;
875 unsigned int drops = 0;
876 struct nf_conn *tmp;
877
878 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
879 tmp = nf_ct_tuplehash_to_ctrack(h);
880
Florian Westphalf330a7f2016-08-25 15:33:31 +0200881 if (nf_ct_is_expired(tmp)) {
882 nf_ct_gc_expired(tmp);
883 continue;
884 }
885
Florian Westphal242922a2016-07-03 20:44:01 +0200886 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
887 !net_eq(nf_ct_net(tmp), net) ||
888 nf_ct_is_dying(tmp))
889 continue;
890
891 if (!atomic_inc_not_zero(&tmp->ct_general.use))
892 continue;
893
894 /* kill only if still in same netns -- might have moved due to
895 * SLAB_DESTROY_BY_RCU rules.
896 *
897 * We steal the timer reference. If that fails timer has
898 * already fired or someone else deleted it. Just drop ref
899 * and move to next entry.
900 */
901 if (net_eq(nf_ct_net(tmp), net) &&
902 nf_ct_is_confirmed(tmp) &&
Florian Westphal242922a2016-07-03 20:44:01 +0200903 nf_ct_delete(tmp, 0, 0))
904 drops++;
905
906 nf_ct_put(tmp);
907 }
908
909 return drops;
910}
911
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100912static noinline int early_drop(struct net *net, unsigned int _hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800913{
Florian Westphal242922a2016-07-03 20:44:01 +0200914 unsigned int i;
Florian Westphal3e866382016-05-02 18:40:14 +0200915
Florian Westphal242922a2016-07-03 20:44:01 +0200916 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
917 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800918 unsigned int hash, hsize, drops;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800919
Liping Zhang3101e0f2016-07-12 19:45:00 +0800920 rcu_read_lock();
Liping Zhang92e47ba2016-08-13 22:35:36 +0800921 nf_conntrack_get_ht(&ct_hash, &hsize);
922 hash = reciprocal_scale(_hash++, hsize);
Florian Westphal3e866382016-05-02 18:40:14 +0200923
Florian Westphal242922a2016-07-03 20:44:01 +0200924 drops = early_drop_list(net, &ct_hash[hash]);
Liping Zhang3101e0f2016-07-12 19:45:00 +0800925 rcu_read_unlock();
Florian Westphal3e866382016-05-02 18:40:14 +0200926
Florian Westphal242922a2016-07-03 20:44:01 +0200927 if (drops) {
928 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
929 return true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000930 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800931 }
Florian Westphal3e866382016-05-02 18:40:14 +0200932
Florian Westphal242922a2016-07-03 20:44:01 +0200933 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800934}
935
Florian Westphalb87a2f92016-08-25 15:33:33 +0200936static void gc_worker(struct work_struct *work)
937{
938 unsigned int i, goal, buckets = 0, expired_count = 0;
939 unsigned long next_run = GC_INTERVAL;
Florian Westphalc023c0e2016-08-25 15:33:34 +0200940 unsigned int ratio, scanned = 0;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200941 struct conntrack_gc_work *gc_work;
942
943 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
944
945 goal = min(nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV, GC_MAX_BUCKETS);
946 i = gc_work->last_bucket;
947
948 do {
949 struct nf_conntrack_tuple_hash *h;
950 struct hlist_nulls_head *ct_hash;
951 struct hlist_nulls_node *n;
952 unsigned int hashsz;
953 struct nf_conn *tmp;
954
955 i++;
956 rcu_read_lock();
957
958 nf_conntrack_get_ht(&ct_hash, &hashsz);
959 if (i >= hashsz)
960 i = 0;
961
962 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
963 tmp = nf_ct_tuplehash_to_ctrack(h);
964
Florian Westphalc023c0e2016-08-25 15:33:34 +0200965 scanned++;
Florian Westphalb87a2f92016-08-25 15:33:33 +0200966 if (nf_ct_is_expired(tmp)) {
967 nf_ct_gc_expired(tmp);
968 expired_count++;
969 continue;
970 }
971 }
972
973 /* could check get_nulls_value() here and restart if ct
974 * was moved to another chain. But given gc is best-effort
975 * we will just continue with next hash slot.
976 */
977 rcu_read_unlock();
978 cond_resched_rcu_qs();
979 } while (++buckets < goal &&
980 expired_count < GC_MAX_EVICTS);
981
982 if (gc_work->exiting)
983 return;
984
Florian Westphalc023c0e2016-08-25 15:33:34 +0200985 ratio = scanned ? expired_count * 100 / scanned : 0;
986 if (ratio >= 90)
987 next_run = 0;
988
Florian Westphalb87a2f92016-08-25 15:33:33 +0200989 gc_work->last_bucket = i;
990 schedule_delayed_work(&gc_work->dwork, next_run);
991}
992
993static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
994{
995 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
996 gc_work->exiting = false;
997}
998
Changli Gao99f07e92010-09-21 17:49:20 +0200999static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +02001000__nf_conntrack_alloc(struct net *net,
1001 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001002 const struct nf_conntrack_tuple *orig,
1003 const struct nf_conntrack_tuple *repl,
1004 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001005{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001006 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001007
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001008 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001009 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001010
Patrick McHardy76eb9462008-01-31 04:41:44 -08001011 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001012 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001013 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001014 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +00001015 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001016 return ERR_PTR(-ENOMEM);
1017 }
1018 }
1019
Eric Dumazet941297f2009-07-16 14:03:40 +02001020 /*
1021 * Do not use kmem_cache_zalloc(), as this cache uses
1022 * SLAB_DESTROY_BY_RCU.
1023 */
Florian Westphal0c5366b2016-05-09 16:24:32 +02001024 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001025 if (ct == NULL)
1026 goto out;
1027
Patrick McHardy440f0d52009-06-10 14:32:47 +02001028 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001029 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +02001030 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -08001031 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +02001032 /* save hash for reusing when confirming */
1033 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +01001034 ct->status = 0;
Eric Dumazetc2d9ba92010-06-01 06:51:19 +00001035 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +01001036 memset(&ct->__nfct_init_offset[0], 0,
1037 offsetof(struct nf_conn, proto) -
1038 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001039
Florian Westphal6c8dee92016-06-11 21:57:35 +02001040 nf_ct_zone_add(ct, zone);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001041
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001042 /* Because we use RCU lookups, we set ct_general.use to zero before
1043 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +02001044 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001045 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001046 return ct;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001047out:
1048 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001049 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001050}
Changli Gao99f07e92010-09-21 17:49:20 +02001051
Daniel Borkmann308ac912015-08-08 21:40:01 +02001052struct nf_conn *nf_conntrack_alloc(struct net *net,
1053 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001054 const struct nf_conntrack_tuple *orig,
1055 const struct nf_conntrack_tuple *repl,
1056 gfp_t gfp)
1057{
1058 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1059}
Patrick McHardy13b18332006-12-02 22:11:25 -08001060EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001061
Patrick McHardyc88130b2008-01-31 04:42:11 -08001062void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063{
Eric Dumazet1d452092009-03-24 14:26:50 +01001064 struct net *net = nf_ct_net(ct);
1065
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001066 /* A freed object has refcnt == 0, that's
1067 * the golden rule for SLAB_DESTROY_BY_RCU
1068 */
1069 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1070
Patrick McHardyceeff752008-06-11 17:51:10 -07001071 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +01001072 nf_ct_ext_free(ct);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001073 kmem_cache_free(nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001074 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +01001075 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001076}
Patrick McHardy13b18332006-12-02 22:11:25 -08001077EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001078
Florian Westphalc539f012013-01-11 06:30:44 +00001079
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001080/* Allocate a new conntrack: we return -ENOMEM if classification
1081 failed due to stress. Otherwise it really is unclassifiable. */
1082static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001083init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001084 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001085 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001086 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001087 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001088 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001089{
Patrick McHardyc88130b2008-01-31 04:42:11 -08001090 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001091 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001092 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001093 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001094 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +02001095 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001096 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001097 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001098 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001099
Martin Josefsson605dcad2006-11-29 02:35:06 +01001100 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001101 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001102 return NULL;
1103 }
1104
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001105 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +02001106 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1107 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -07001108 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -08001109 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001110
Patrick McHardy48b1de42013-08-27 08:50:14 +02001111 if (tmpl && nfct_synproxy(tmpl)) {
1112 nfct_seqadj_ext_add(ct);
1113 nfct_synproxy_ext_add(ct);
1114 }
1115
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001116 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001117 if (timeout_ext) {
1118 timeouts = nf_ct_timeout_data(timeout_ext);
1119 if (unlikely(!timeouts))
1120 timeouts = l4proto->get_timeouts(net);
1121 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001122 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001123 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001124
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001125 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -08001126 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001127 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001128 return NULL;
1129 }
1130
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001131 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001132 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1133 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001134
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001135 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001136 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +00001137 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001138
1139 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1140 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1141 ecache ? ecache->expmask : 0,
1142 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001143
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001144 local_bh_disable();
1145 if (net->ct.expect_count) {
1146 spin_lock(&nf_conntrack_expect_lock);
1147 exp = nf_ct_find_expectation(net, zone, tuple);
1148 if (exp) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001149 pr_debug("expectation arrives ct=%p exp=%p\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001150 ct, exp);
1151 /* Welcome, Mr. Bond. We've been expecting you... */
1152 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1153 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1154 ct->master = exp->master;
1155 if (exp->helper) {
1156 help = nf_ct_helper_ext_add(ct, exp->helper,
1157 GFP_ATOMIC);
1158 if (help)
1159 rcu_assign_pointer(help->helper, exp->helper);
1160 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001161
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001162#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001163 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001164#endif
James Morris7c9728c2006-06-09 00:31:46 -07001165#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001166 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001167#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001168 NF_CT_STAT_INC(net, expect_new);
1169 }
1170 spin_unlock(&nf_conntrack_expect_lock);
1171 }
Florian Westphal8e8118f2016-09-11 22:55:53 +02001172 if (!exp)
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001173 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001174
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001175 /* Now it is inserted into the unconfirmed list, bump refcount */
1176 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001177 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001178
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001179 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001180
1181 if (exp) {
1182 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001183 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001184 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001185 }
1186
Patrick McHardyc88130b2008-01-31 04:42:11 -08001187 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001188}
1189
1190/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1191static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001192resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001193 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001194 unsigned int dataoff,
1195 u_int16_t l3num,
1196 u_int8_t protonum,
1197 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001198 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001199 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001200 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001201{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001202 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001203 struct nf_conntrack_tuple tuple;
1204 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001205 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001206 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001207 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001208
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001209 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001210 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001211 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001212 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001213 return NULL;
1214 }
1215
1216 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001217 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001218 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001219 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001220 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001221 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001222 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001223 if (!h)
1224 return NULL;
1225 if (IS_ERR(h))
1226 return (void *)h;
1227 }
1228 ct = nf_ct_tuplehash_to_ctrack(h);
1229
1230 /* It exists; we have (non-exclusive) reference. */
1231 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001232 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001233 /* Please set reply bit if this packet OK */
1234 *set_reply = 1;
1235 } else {
1236 /* Once we've had two way comms, always ESTABLISHED. */
1237 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001238 pr_debug("normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001239 *ctinfo = IP_CT_ESTABLISHED;
1240 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001241 pr_debug("related packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001242 *ctinfo = IP_CT_RELATED;
1243 } else {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001244 pr_debug("new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001245 *ctinfo = IP_CT_NEW;
1246 }
1247 *set_reply = 0;
1248 }
1249 skb->nfct = &ct->ct_general;
1250 skb->nfctinfo = *ctinfo;
1251 return ct;
1252}
1253
1254unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001255nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1256 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001257{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001258 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001259 enum ip_conntrack_info ctinfo;
1260 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001261 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001262 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001263 unsigned int dataoff;
1264 u_int8_t protonum;
1265 int set_reply = 0;
1266 int ret;
1267
Herbert Xu3db05fe2007-10-15 00:53:15 -07001268 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001269 /* Previously seen (loopback or untracked)? Ignore. */
1270 tmpl = (struct nf_conn *)skb->nfct;
1271 if (!nf_ct_is_template(tmpl)) {
1272 NF_CT_STAT_INC_ATOMIC(net, ignore);
1273 return NF_ACCEPT;
1274 }
1275 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001276 }
1277
Patrick McHardy923f4902007-02-12 11:12:57 -08001278 /* rcu_read_lock()ed by nf_hook_slow */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001279 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001280 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001281 &dataoff, &protonum);
1282 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001283 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001284 NF_CT_STAT_INC_ATOMIC(net, error);
1285 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001286 ret = -ret;
1287 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001288 }
1289
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001290 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001291
1292 /* It may be an special packet, error, unclean...
1293 * inverse of the return code tells to the netfilter
1294 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001295 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001296 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1297 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001298 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001299 NF_CT_STAT_INC_ATOMIC(net, error);
1300 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001301 ret = -ret;
1302 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001303 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001304 /* ICMP[v6] protocol trackers may assign one conntrack. */
1305 if (skb->nfct)
1306 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001307 }
1308
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001309 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001310 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001311 if (!ct) {
1312 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001313 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001314 ret = NF_ACCEPT;
1315 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001316 }
1317
1318 if (IS_ERR(ct)) {
1319 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001320 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001321 ret = NF_DROP;
1322 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001323 }
1324
Herbert Xu3db05fe2007-10-15 00:53:15 -07001325 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001326
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001327 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001328 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001329
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001330 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001331 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001332 /* Invalid: inverse of the return code tells
1333 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001334 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001335 nf_conntrack_put(skb->nfct);
1336 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001337 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001338 if (ret == -NF_DROP)
1339 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001340 ret = -ret;
1341 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001342 }
1343
1344 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001345 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001346out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001347 if (tmpl) {
1348 /* Special case: we have to repeat this hook, assign the
1349 * template again to this packet. We assume that this packet
1350 * has no conntrack assigned. This is used by nf_ct_tcp. */
1351 if (ret == NF_REPEAT)
1352 skb->nfct = (struct nf_conntrack *)tmpl;
1353 else
1354 nf_ct_put(tmpl);
1355 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001356
1357 return ret;
1358}
Patrick McHardy13b18332006-12-02 22:11:25 -08001359EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001360
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001361bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1362 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001363{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001364 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001365
1366 rcu_read_lock();
1367 ret = nf_ct_invert_tuple(inverse, orig,
1368 __nf_ct_l3proto_find(orig->src.l3num),
1369 __nf_ct_l4proto_find(orig->src.l3num,
1370 orig->dst.protonum));
1371 rcu_read_unlock();
1372 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001373}
Patrick McHardy13b18332006-12-02 22:11:25 -08001374EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001375
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001376/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1377 implicitly racy: see __nf_conntrack_confirm */
1378void nf_conntrack_alter_reply(struct nf_conn *ct,
1379 const struct nf_conntrack_tuple *newreply)
1380{
1381 struct nf_conn_help *help = nfct_help(ct);
1382
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001383 /* Should be unconfirmed, so not in hash table yet */
1384 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1385
Patrick McHardy0d537782007-07-07 22:39:38 -07001386 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001387 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001388
1389 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001390 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001391 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001392
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001393 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001394 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001395 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001396}
Patrick McHardy13b18332006-12-02 22:11:25 -08001397EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001398
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001399/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1400void __nf_ct_refresh_acct(struct nf_conn *ct,
1401 enum ip_conntrack_info ctinfo,
1402 const struct sk_buff *skb,
1403 unsigned long extra_jiffies,
1404 int do_acct)
1405{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001406 NF_CT_ASSERT(skb);
1407
Eric Leblond997ae832006-05-29 18:24:20 -07001408 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001409 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1410 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001411
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001412 /* If not in hash table, timer will not be active yet */
Florian Westphalf330a7f2016-08-25 15:33:31 +02001413 if (nf_ct_is_confirmed(ct))
1414 extra_jiffies += nfct_time_stamp;
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001415
Florian Westphalf330a7f2016-08-25 15:33:31 +02001416 ct->timeout = extra_jiffies;
Patrick McHardy47d95042008-01-31 04:36:31 -08001417acct:
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001418 if (do_acct)
1419 nf_ct_acct_update(ct, ctinfo, skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001420}
Patrick McHardy13b18332006-12-02 22:11:25 -08001421EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001422
Florian Westphalad667132016-08-25 15:33:35 +02001423bool nf_ct_kill_acct(struct nf_conn *ct,
1424 enum ip_conntrack_info ctinfo,
1425 const struct sk_buff *skb)
Patrick McHardy51091762008-06-09 15:59:06 -07001426{
Florian Westphalad667132016-08-25 15:33:35 +02001427 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001428
Florian Westphalf330a7f2016-08-25 15:33:31 +02001429 return nf_ct_delete(ct, 0, 0);
Patrick McHardy51091762008-06-09 15:59:06 -07001430}
Florian Westphalad667132016-08-25 15:33:35 +02001431EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001432
Igor Maravićc0cd1152011-12-12 02:58:24 +00001433#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001434
1435#include <linux/netfilter/nfnetlink.h>
1436#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001437#include <linux/mutex.h>
1438
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001439/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1440 * in ip_conntrack_core, since we don't want the protocols to autoload
1441 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001442int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001443 const struct nf_conntrack_tuple *tuple)
1444{
David S. Millerbae65be2012-04-01 18:58:28 -04001445 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1446 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1447 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001448 return 0;
1449
Patrick McHardydf6fb862007-09-28 14:37:03 -07001450nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001451 return -1;
1452}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001453EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001454
Patrick McHardyf73e9242007-09-28 14:39:55 -07001455const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1456 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1457 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001458};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001459EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001460
Patrick McHardyfdf70832007-09-28 14:37:41 -07001461int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001462 struct nf_conntrack_tuple *t)
1463{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001464 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001465 return -EINVAL;
1466
Patrick McHardy77236b62007-12-17 22:29:45 -08001467 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1468 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001469
1470 return 0;
1471}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001472EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001473
1474int nf_ct_port_nlattr_tuple_size(void)
1475{
1476 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1477}
1478EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001479#endif
1480
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001481/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001482static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001483{
1484 struct nf_conn *ct;
1485 enum ip_conntrack_info ctinfo;
1486
1487 /* This ICMP is in reverse direction to the packet which caused it */
1488 ct = nf_ct_get(skb, &ctinfo);
1489 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001490 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001491 else
1492 ctinfo = IP_CT_RELATED;
1493
1494 /* Attach to new skbuff, and increment count */
1495 nskb->nfct = &ct->ct_general;
1496 nskb->nfctinfo = ctinfo;
1497 nf_conntrack_get(nskb->nfct);
1498}
1499
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001500/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001501static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001502get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001503 void *data, unsigned int *bucket)
1504{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001505 struct nf_conntrack_tuple_hash *h;
1506 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001507 struct hlist_nulls_node *n;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001508 int cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001509 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001510
Florian Westphal56d52d42016-05-02 18:39:55 +02001511 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001512 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1513 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001514 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001515 if (*bucket < nf_conntrack_htable_size) {
1516 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001517 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1518 continue;
1519 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001520 if (net_eq(nf_ct_net(ct), net) &&
1521 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001522 goto found;
1523 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001524 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001525 spin_unlock(lockp);
1526 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001527 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001528 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001529
1530 for_each_possible_cpu(cpu) {
1531 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1532
1533 spin_lock_bh(&pcpu->lock);
1534 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1535 ct = nf_ct_tuplehash_to_ctrack(h);
1536 if (iter(ct, data))
1537 set_bit(IPS_DYING_BIT, &ct->status);
1538 }
1539 spin_unlock_bh(&pcpu->lock);
Florian Westphald93c6252016-01-20 11:16:43 +01001540 cond_resched();
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001541 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001542 return NULL;
1543found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001544 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001545 spin_unlock(lockp);
1546 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001547 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001548}
1549
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001550void nf_ct_iterate_cleanup(struct net *net,
1551 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001552 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001553{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001554 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001555 unsigned int bucket = 0;
1556
Florian Westphald93c6252016-01-20 11:16:43 +01001557 might_sleep();
1558
Florian Westphal88b68bc2016-04-28 19:13:42 +02001559 if (atomic_read(&net->ct.count) == 0)
1560 return;
1561
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001562 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001563 /* Time to push up daises... */
Florian Westphal02982c22013-07-29 15:41:54 +02001564
Florian Westphalf330a7f2016-08-25 15:33:31 +02001565 nf_ct_delete(ct, portid, report);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001566 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001567 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001568 }
1569}
Patrick McHardy13b18332006-12-02 22:11:25 -08001570EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001571
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001572static int kill_all(struct nf_conn *i, void *data)
1573{
1574 return 1;
1575}
1576
Patrick McHardyd862a662011-01-14 15:45:56 +01001577void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001578{
Patrick McHardyd862a662011-01-14 15:45:56 +01001579 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001580 vfree(hash);
1581 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001582 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001583 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001584}
Patrick McHardyac565e52007-07-07 22:30:08 -07001585EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001586
Eric Dumazetb3c51632010-06-09 14:43:38 +02001587static int untrack_refs(void)
1588{
1589 int cnt = 0, cpu;
1590
1591 for_each_possible_cpu(cpu) {
1592 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1593
1594 cnt += atomic_read(&ct->ct_general.use) - 1;
1595 }
1596 return cnt;
1597}
1598
Gao fengf94161c2013-01-21 22:10:24 +00001599void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001600{
Florian Westphalb87a2f92016-08-25 15:33:33 +02001601 conntrack_gc_work.exiting = true;
Gao fengf94161c2013-01-21 22:10:24 +00001602 RCU_INIT_POINTER(ip_ct_attach, NULL);
1603}
1604
1605void nf_conntrack_cleanup_end(void)
1606{
1607 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001608 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001609 schedule();
1610
Florian Westphalb87a2f92016-08-25 15:33:33 +02001611 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
Florian Westphal56d52d42016-05-02 18:39:55 +02001612 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1613
Gao feng04d87002013-01-21 22:10:32 +00001614 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001615 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001616 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001617 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001618 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001619 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001620 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001621 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001622 nf_conntrack_expect_fini();
Florian Westphal77571142016-06-10 17:25:19 +02001623
1624 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001625}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001626
Gao fengf94161c2013-01-21 22:10:24 +00001627/*
1628 * Mishearing the voices in his head, our hero wonders how he's
1629 * supposed to kill the mall.
1630 */
1631void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001632{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001633 LIST_HEAD(single);
1634
1635 list_add(&net->exit_list, &single);
1636 nf_conntrack_cleanup_net_list(&single);
1637}
1638
1639void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1640{
1641 int busy;
1642 struct net *net;
1643
Gao fengf94161c2013-01-21 22:10:24 +00001644 /*
1645 * This makes sure all current packets have passed through
1646 * netfilter framework. Roll on, two-stage module
1647 * delete...
1648 */
1649 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001650i_see_dead_people:
1651 busy = 0;
1652 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001653 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001654 if (atomic_read(&net->ct.count) != 0)
1655 busy = 1;
1656 }
1657 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001658 schedule();
1659 goto i_see_dead_people;
1660 }
1661
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001662 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001663 nf_conntrack_proto_pernet_fini(net);
1664 nf_conntrack_helper_pernet_fini(net);
1665 nf_conntrack_ecache_pernet_fini(net);
1666 nf_conntrack_tstamp_pernet_fini(net);
1667 nf_conntrack_acct_pernet_fini(net);
1668 nf_conntrack_expect_pernet_fini(net);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001669 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001670 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001671 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001672}
1673
Patrick McHardyd862a662011-01-14 15:45:56 +01001674void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001675{
Eric Dumazetea781f12009-03-25 21:05:46 +01001676 struct hlist_nulls_head *hash;
1677 unsigned int nr_slots, i;
1678 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001679
Florian Westphal9cc1c732016-04-24 01:18:21 +02001680 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1681 return NULL;
1682
Eric Dumazetea781f12009-03-25 21:05:46 +01001683 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1684 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
Florian Westphal9cc1c732016-04-24 01:18:21 +02001685
1686 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1687 return NULL;
1688
Eric Dumazetea781f12009-03-25 21:05:46 +01001689 sz = nr_slots * sizeof(struct hlist_nulls_head);
1690 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1691 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001692 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001693 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001694
Eric Dumazetea781f12009-03-25 21:05:46 +01001695 if (hash && nulls)
1696 for (i = 0; i < nr_slots; i++)
1697 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001698
1699 return hash;
1700}
Patrick McHardyac565e52007-07-07 22:30:08 -07001701EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001702
Florian Westphal3183ab82016-06-22 13:26:10 +02001703int nf_conntrack_hash_resize(unsigned int hashsize)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001704{
Florian Westphal3183ab82016-06-22 13:26:10 +02001705 int i, bucket;
1706 unsigned int old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001707 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001708 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001709 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001710
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001711 if (!hashsize)
1712 return -EINVAL;
1713
Patrick McHardyd862a662011-01-14 15:45:56 +01001714 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001715 if (!hash)
1716 return -ENOMEM;
1717
Florian Westphal3183ab82016-06-22 13:26:10 +02001718 old_size = nf_conntrack_htable_size;
1719 if (old_size == hashsize) {
1720 nf_ct_free_hashtable(hash, hashsize);
1721 return 0;
1722 }
1723
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001724 local_bh_disable();
1725 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001726 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001727
Patrick McHardy76507f62008-01-31 04:38:38 -08001728 /* Lookups in the old hash might happen in parallel, which means we
1729 * might get false negatives during connection lookup. New connections
1730 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001731 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001732 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001733
Florian Westphal56d52d42016-05-02 18:39:55 +02001734 for (i = 0; i < nf_conntrack_htable_size; i++) {
1735 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1736 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1737 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001738 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001739 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001740 bucket = __hash_conntrack(nf_ct_net(ct),
1741 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001742 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001743 }
1744 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001745 old_size = nf_conntrack_htable_size;
1746 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001747
Florian Westphal56d52d42016-05-02 18:39:55 +02001748 nf_conntrack_hash = hash;
1749 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001750
Florian Westphala3efd812016-04-18 16:16:59 +02001751 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001752 nf_conntrack_all_unlock();
1753 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001754
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001755 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001756 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001757 return 0;
1758}
Florian Westphal3183ab82016-06-22 13:26:10 +02001759
1760int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1761{
1762 unsigned int hashsize;
1763 int rc;
1764
1765 if (current->nsproxy->net_ns != &init_net)
1766 return -EOPNOTSUPP;
1767
1768 /* On boot, we can set this without any fancy locking. */
1769 if (!nf_conntrack_htable_size)
1770 return param_set_uint(val, kp);
1771
1772 rc = kstrtouint(val, 0, &hashsize);
1773 if (rc)
1774 return rc;
1775
1776 return nf_conntrack_hash_resize(hashsize);
1777}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001778EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001779
Patrick McHardyfae718d2007-12-24 21:09:10 -08001780module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001781 &nf_conntrack_htable_size, 0600);
1782
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001783void nf_ct_untracked_status_or(unsigned long bits)
1784{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001785 int cpu;
1786
1787 for_each_possible_cpu(cpu)
1788 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001789}
1790EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1791
Gao fengf94161c2013-01-21 22:10:24 +00001792int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001793{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001794 int max_factor = 8;
Florian Westphal0c5366b2016-05-09 16:24:32 +02001795 int ret = -ENOMEM;
1796 int i, cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001797
Florian Westphala3efd812016-04-18 16:16:59 +02001798 seqcount_init(&nf_conntrack_generation);
1799
Eric Dumazetd5d20912014-03-17 13:37:53 -07001800 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001801 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001802
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001803 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001804 /* Idea from tcp.c: use 1/16384 of memory.
1805 * On i386: 32MB machine has 512 buckets.
1806 * >= 1GB machines have 16384 buckets.
1807 * >= 4GB machines have 65536 buckets.
1808 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001809 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07001810 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001811 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02001812 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1813 nf_conntrack_htable_size = 65536;
1814 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001815 nf_conntrack_htable_size = 16384;
1816 if (nf_conntrack_htable_size < 32)
1817 nf_conntrack_htable_size = 32;
1818
1819 /* Use a max. factor of four by default to get the same max as
1820 * with the old struct list_heads. When a table size is given
1821 * we use the old value of 8 to avoid reducing the max.
1822 * entries. */
1823 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001824 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001825
1826 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1827 if (!nf_conntrack_hash)
1828 return -ENOMEM;
1829
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001830 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001831
Florian Westphal0c5366b2016-05-09 16:24:32 +02001832 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1833 sizeof(struct nf_conn), 0,
Florian Westphal5a75cde2016-06-10 22:25:22 +02001834 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001835 if (!nf_conntrack_cachep)
1836 goto err_cachep;
1837
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001838 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001839 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1840 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00001841
1842 ret = nf_conntrack_expect_init();
1843 if (ret < 0)
1844 goto err_expect;
1845
Gao fengb7ff3a12013-01-21 22:10:26 +00001846 ret = nf_conntrack_acct_init();
1847 if (ret < 0)
1848 goto err_acct;
1849
Gao feng73f40012013-01-21 22:10:27 +00001850 ret = nf_conntrack_tstamp_init();
1851 if (ret < 0)
1852 goto err_tstamp;
1853
Gao feng3fe0f942013-01-21 22:10:28 +00001854 ret = nf_conntrack_ecache_init();
1855 if (ret < 0)
1856 goto err_ecache;
1857
Gao feng86840942013-01-21 22:10:29 +00001858 ret = nf_conntrack_timeout_init();
1859 if (ret < 0)
1860 goto err_timeout;
1861
Gao feng5e615b22013-01-21 22:10:30 +00001862 ret = nf_conntrack_helper_init();
1863 if (ret < 0)
1864 goto err_helper;
1865
Gao feng5f69b8f2013-01-21 22:10:31 +00001866 ret = nf_conntrack_labels_init();
1867 if (ret < 0)
1868 goto err_labels;
1869
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001870 ret = nf_conntrack_seqadj_init();
1871 if (ret < 0)
1872 goto err_seqadj;
1873
Gao feng04d87002013-01-21 22:10:32 +00001874 ret = nf_conntrack_proto_init();
1875 if (ret < 0)
1876 goto err_proto;
1877
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001878 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02001879 for_each_possible_cpu(cpu) {
1880 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001881 write_pnet(&ct->ct_net, &init_net);
1882 atomic_set(&ct->ct_general.use, 1);
1883 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001884 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001885 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001886
1887 conntrack_gc_work_init(&conntrack_gc_work);
1888 schedule_delayed_work(&conntrack_gc_work.dwork, GC_INTERVAL);
1889
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001890 return 0;
1891
Gao feng04d87002013-01-21 22:10:32 +00001892err_proto:
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001893 nf_conntrack_seqadj_fini();
1894err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00001895 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001896err_labels:
1897 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001898err_helper:
1899 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00001900err_timeout:
1901 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001902err_ecache:
1903 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00001904err_tstamp:
1905 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001906err_acct:
1907 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001908err_expect:
Florian Westphal0c5366b2016-05-09 16:24:32 +02001909 kmem_cache_destroy(nf_conntrack_cachep);
1910err_cachep:
Florian Westphal56d52d42016-05-02 18:39:55 +02001911 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001912 return ret;
1913}
1914
Gao fengf94161c2013-01-21 22:10:24 +00001915void nf_conntrack_init_end(void)
1916{
1917 /* For use by REJECT target */
1918 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1919 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00001920}
1921
Eric Dumazet8cc20192009-06-22 14:13:55 +02001922/*
1923 * We need to use special "null" values, not used in hash table
1924 */
1925#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1926#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00001927#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02001928
Gao fengf94161c2013-01-21 22:10:24 +00001929int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001930{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001931 int ret = -ENOMEM;
1932 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001933
1934 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001935
1936 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1937 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001938 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001939
1940 for_each_possible_cpu(cpu) {
1941 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1942
1943 spin_lock_init(&pcpu->lock);
1944 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1945 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001946 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001947
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001948 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1949 if (!net->ct.stat)
1950 goto err_pcpu_lists;
1951
Gao feng83b4dbe2013-01-21 22:10:25 +00001952 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001953 if (ret < 0)
1954 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00001955 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001956 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001957 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00001958 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001959 if (ret < 0)
1960 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00001961 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001962 if (ret < 0)
1963 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00001964 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001965 if (ret < 0)
1966 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00001967 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00001968 if (ret < 0)
1969 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001970 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00001971
Gao fengf94161c2013-01-21 22:10:24 +00001972err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00001973 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001974err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00001975 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001976err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00001977 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001978err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00001979 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001980err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00001981 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001982err_expect:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001983 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001984err_pcpu_lists:
1985 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001986err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001987 return ret;
1988}