]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nf_conntrack_core.c
netfilter: remove last traces of variable-sized extensions
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nf_conntrack_core.c
CommitLineData
9fb9cbb1
YK
1/* 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
dc808fe2 6 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
9fb9cbb1 7 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
f229f6ce 8 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
9fb9cbb1
YK
9 *
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.
9fb9cbb1
YK
13 */
14
ccd63c20
WJ
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
9fb9cbb1
YK
17#include <linux/types.h>
18#include <linux/netfilter.h>
19#include <linux/module.h>
d43c36dc 20#include <linux/sched.h>
9fb9cbb1
YK
21#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>
d7fe0f24 35#include <linux/mm.h>
d696c7bd 36#include <linux/nsproxy.h>
ea781f19 37#include <linux/rculist_nulls.h>
9fb9cbb1 38
9fb9cbb1
YK
39#include <net/netfilter/nf_conntrack.h>
40#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 41#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 42#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 43#include <net/netfilter/nf_conntrack_helper.h>
41d73ec0 44#include <net/netfilter/nf_conntrack_seqadj.h>
9fb9cbb1 45#include <net/netfilter/nf_conntrack_core.h>
ecfab2c9 46#include <net/netfilter/nf_conntrack_extend.h>
58401572 47#include <net/netfilter/nf_conntrack_acct.h>
a0891aa6 48#include <net/netfilter/nf_conntrack_ecache.h>
5d0aa2cc 49#include <net/netfilter/nf_conntrack_zones.h>
a992ca2a 50#include <net/netfilter/nf_conntrack_timestamp.h>
dd705072 51#include <net/netfilter/nf_conntrack_timeout.h>
c539f017 52#include <net/netfilter/nf_conntrack_labels.h>
48b1de4c 53#include <net/netfilter/nf_conntrack_synproxy.h>
e6a7d3c0 54#include <net/netfilter/nf_nat.h>
e17b666a 55#include <net/netfilter/nf_nat_core.h>
49376368 56#include <net/netfilter/nf_nat_helper.h>
1b8c8a9f 57#include <net/netns/hash.h>
9fb9cbb1 58
dc808fe2 59#define NF_CONNTRACK_VERSION "0.5.0"
9fb9cbb1 60
e17b666a
PM
61int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
62 enum nf_nat_manip_type manip,
39938324 63 const struct nlattr *attr) __read_mostly;
e6a7d3c0
PNA
64EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
65
93bb0ceb
JDB
66__cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
67EXPORT_SYMBOL_GPL(nf_conntrack_locks);
9fb9cbb1 68
ca7433df
JDB
69__cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
70EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
71
56d52d48
FW
72struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
73EXPORT_SYMBOL_GPL(nf_conntrack_hash);
74
b87a2f91
FW
75struct conntrack_gc_work {
76 struct delayed_work dwork;
77 u32 last_bucket;
78 bool exiting;
e0df8cae 79 long next_gc_run;
b87a2f91
FW
80};
81
0c5366b3 82static __read_mostly struct kmem_cache *nf_conntrack_cachep;
b16c2919 83static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
70d72b7e 84static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
b16c2919
SL
85static __read_mostly bool nf_conntrack_locks_all;
86
e0df8cae 87/* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */
e5072053
FW
88#define GC_MAX_BUCKETS_DIV 128u
89/* upper bound of full table scan */
90#define GC_MAX_SCAN_JIFFIES (16u * HZ)
91/* desired ratio of entries found to be expired */
92#define GC_EVICT_RATIO 50u
b87a2f91
FW
93
94static struct conntrack_gc_work conntrack_gc_work;
95
b16c2919
SL
96void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
97{
98 spin_lock(lock);
99 while (unlikely(nf_conntrack_locks_all)) {
100 spin_unlock(lock);
b316ff78
PZ
101
102 /*
103 * Order the 'nf_conntrack_locks_all' load vs. the
104 * spin_unlock_wait() loads below, to ensure
105 * that 'nf_conntrack_locks_all_lock' is indeed held:
106 */
107 smp_rmb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
e39365be 108 spin_unlock_wait(&nf_conntrack_locks_all_lock);
b16c2919
SL
109 spin_lock(lock);
110 }
111}
112EXPORT_SYMBOL_GPL(nf_conntrack_lock);
113
93bb0ceb
JDB
114static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
115{
116 h1 %= CONNTRACK_LOCKS;
117 h2 %= CONNTRACK_LOCKS;
118 spin_unlock(&nf_conntrack_locks[h1]);
119 if (h1 != h2)
120 spin_unlock(&nf_conntrack_locks[h2]);
121}
122
123/* return true if we need to recompute hashes (in case hash table was resized) */
124static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
125 unsigned int h2, unsigned int sequence)
126{
127 h1 %= CONNTRACK_LOCKS;
128 h2 %= CONNTRACK_LOCKS;
129 if (h1 <= h2) {
b16c2919 130 nf_conntrack_lock(&nf_conntrack_locks[h1]);
93bb0ceb
JDB
131 if (h1 != h2)
132 spin_lock_nested(&nf_conntrack_locks[h2],
133 SINGLE_DEPTH_NESTING);
134 } else {
b16c2919 135 nf_conntrack_lock(&nf_conntrack_locks[h2]);
93bb0ceb
JDB
136 spin_lock_nested(&nf_conntrack_locks[h1],
137 SINGLE_DEPTH_NESTING);
138 }
a3efd812 139 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
93bb0ceb
JDB
140 nf_conntrack_double_unlock(h1, h2);
141 return true;
142 }
143 return false;
144}
145
146static void nf_conntrack_all_lock(void)
147{
148 int i;
149
b16c2919
SL
150 spin_lock(&nf_conntrack_locks_all_lock);
151 nf_conntrack_locks_all = true;
152
b316ff78
PZ
153 /*
154 * Order the above store of 'nf_conntrack_locks_all' against
155 * the spin_unlock_wait() loads below, such that if
156 * nf_conntrack_lock() observes 'nf_conntrack_locks_all'
157 * we must observe nf_conntrack_locks[] held:
158 */
159 smp_mb(); /* spin_lock(&nf_conntrack_locks_all_lock) */
160
b16c2919 161 for (i = 0; i < CONNTRACK_LOCKS; i++) {
e39365be 162 spin_unlock_wait(&nf_conntrack_locks[i]);
b16c2919 163 }
93bb0ceb
JDB
164}
165
166static void nf_conntrack_all_unlock(void)
167{
b316ff78
PZ
168 /*
169 * All prior stores must be complete before we clear
170 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
171 * might observe the false value but not the entire
172 * critical section:
173 */
174 smp_store_release(&nf_conntrack_locks_all, false);
b16c2919 175 spin_unlock(&nf_conntrack_locks_all_lock);
93bb0ceb
JDB
176}
177
e2b7606c 178unsigned int nf_conntrack_htable_size __read_mostly;
2567c4ea
PNA
179EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
180
e478075c 181unsigned int nf_conntrack_max __read_mostly;
92e47ba8 182seqcount_t nf_conntrack_generation __read_mostly;
141658fb 183static unsigned int nf_conntrack_hash_rnd __read_mostly;
9fb9cbb1 184
1b8c8a9f
FW
185static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
186 const struct net *net)
9fb9cbb1 187{
0794935e 188 unsigned int n;
1b8c8a9f 189 u32 seed;
0794935e 190
141658fb
FW
191 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
192
0794935e
PM
193 /* The direction must be ignored, so we hash everything up to the
194 * destination ports (which is a multiple of 4) and treat the last
195 * three bytes manually.
196 */
1b8c8a9f 197 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
0794935e 198 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
1b8c8a9f 199 return jhash2((u32 *)tuple, n, seed ^
99f07e91
CG
200 (((__force __u16)tuple->dst.u.all << 16) |
201 tuple->dst.protonum));
202}
203
56d52d48 204static u32 scale_hash(u32 hash)
99f07e91 205{
56d52d48 206 return reciprocal_scale(hash, nf_conntrack_htable_size);
99f07e91 207}
0794935e 208
1b8c8a9f
FW
209static u32 __hash_conntrack(const struct net *net,
210 const struct nf_conntrack_tuple *tuple,
211 unsigned int size)
99f07e91 212{
1b8c8a9f 213 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
9fb9cbb1
YK
214}
215
1b8c8a9f
FW
216static u32 hash_conntrack(const struct net *net,
217 const struct nf_conntrack_tuple *tuple)
9fb9cbb1 218{
56d52d48 219 return scale_hash(hash_conntrack_raw(tuple, net));
9fb9cbb1
YK
220}
221
5f2b4c90 222bool
9fb9cbb1
YK
223nf_ct_get_tuple(const struct sk_buff *skb,
224 unsigned int nhoff,
225 unsigned int dataoff,
226 u_int16_t l3num,
227 u_int8_t protonum,
a31f1adc 228 struct net *net,
9fb9cbb1
YK
229 struct nf_conntrack_tuple *tuple,
230 const struct nf_conntrack_l3proto *l3proto,
605dcad6 231 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 232{
443a70d5 233 memset(tuple, 0, sizeof(*tuple));
9fb9cbb1
YK
234
235 tuple->src.l3num = l3num;
236 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
5f2b4c90 237 return false;
9fb9cbb1
YK
238
239 tuple->dst.protonum = protonum;
240 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
241
a31f1adc 242 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
9fb9cbb1 243}
13b18339 244EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
9fb9cbb1 245
5f2b4c90 246bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
a31f1adc
EB
247 u_int16_t l3num,
248 struct net *net, struct nf_conntrack_tuple *tuple)
e2a3123f
YK
249{
250 struct nf_conntrack_l3proto *l3proto;
251 struct nf_conntrack_l4proto *l4proto;
252 unsigned int protoff;
253 u_int8_t protonum;
254 int ret;
255
256 rcu_read_lock();
257
258 l3proto = __nf_ct_l3proto_find(l3num);
259 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
260 if (ret != NF_ACCEPT) {
261 rcu_read_unlock();
5f2b4c90 262 return false;
e2a3123f
YK
263 }
264
265 l4proto = __nf_ct_l4proto_find(l3num, protonum);
266
a31f1adc 267 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
e2a3123f
YK
268 l3proto, l4proto);
269
270 rcu_read_unlock();
271 return ret;
272}
273EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
274
5f2b4c90 275bool
9fb9cbb1
YK
276nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
277 const struct nf_conntrack_tuple *orig,
278 const struct nf_conntrack_l3proto *l3proto,
605dcad6 279 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 280{
443a70d5 281 memset(inverse, 0, sizeof(*inverse));
9fb9cbb1
YK
282
283 inverse->src.l3num = orig->src.l3num;
284 if (l3proto->invert_tuple(inverse, orig) == 0)
5f2b4c90 285 return false;
9fb9cbb1
YK
286
287 inverse->dst.dir = !orig->dst.dir;
288
289 inverse->dst.protonum = orig->dst.protonum;
605dcad6 290 return l4proto->invert_tuple(inverse, orig);
9fb9cbb1 291}
13b18339 292EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
9fb9cbb1 293
9fb9cbb1
YK
294static void
295clean_from_lists(struct nf_conn *ct)
296{
0d53778e 297 pr_debug("clean_from_lists(%p)\n", ct);
ea781f19
ED
298 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
299 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
9fb9cbb1
YK
300
301 /* Destroy all pending expectations */
c1d10adb 302 nf_ct_remove_expectations(ct);
9fb9cbb1
YK
303}
304
b7779d06
JDB
305/* must be called with local_bh_disable */
306static void nf_ct_add_to_dying_list(struct nf_conn *ct)
307{
308 struct ct_pcpu *pcpu;
309
310 /* add this conntrack to the (per cpu) dying list */
311 ct->cpu = smp_processor_id();
312 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
313
314 spin_lock(&pcpu->lock);
315 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
316 &pcpu->dying);
317 spin_unlock(&pcpu->lock);
318}
319
320/* must be called with local_bh_disable */
321static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
322{
323 struct ct_pcpu *pcpu;
324
325 /* add this conntrack to the (per cpu) unconfirmed list */
326 ct->cpu = smp_processor_id();
327 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
328
329 spin_lock(&pcpu->lock);
330 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
331 &pcpu->unconfirmed);
332 spin_unlock(&pcpu->lock);
333}
334
335/* must be called with local_bh_disable */
336static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
337{
338 struct ct_pcpu *pcpu;
339
340 /* We overload first tuple to link into unconfirmed or dying list.*/
341 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
342
343 spin_lock(&pcpu->lock);
344 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
345 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
346 spin_unlock(&pcpu->lock);
347}
348
30322309
FW
349#define NFCT_ALIGN(len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK)
350
0838aa7f 351/* Released via destroy_conntrack() */
308ac914
DB
352struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
353 const struct nf_conntrack_zone *zone,
354 gfp_t flags)
0838aa7f 355{
30322309 356 struct nf_conn *tmpl, *p;
0838aa7f 357
30322309
FW
358 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK) {
359 tmpl = kzalloc(sizeof(*tmpl) + NFCT_INFOMASK, flags);
360 if (!tmpl)
361 return NULL;
362
363 p = tmpl;
364 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
365 if (tmpl != p) {
366 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
367 tmpl->proto.tmpl_padto = (char *)tmpl - (char *)p;
368 }
369 } else {
370 tmpl = kzalloc(sizeof(*tmpl), flags);
371 if (!tmpl)
372 return NULL;
373 }
0838aa7f
PNA
374
375 tmpl->status = IPS_TEMPLATE;
376 write_pnet(&tmpl->ct_net, net);
6c8dee98 377 nf_ct_zone_add(tmpl, zone);
0838aa7f
PNA
378 atomic_set(&tmpl->ct_general.use, 0);
379
380 return tmpl;
0838aa7f
PNA
381}
382EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
383
9cf94eab 384void nf_ct_tmpl_free(struct nf_conn *tmpl)
0838aa7f
PNA
385{
386 nf_ct_ext_destroy(tmpl);
387 nf_ct_ext_free(tmpl);
30322309
FW
388
389 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK)
390 kfree((char *)tmpl - tmpl->proto.tmpl_padto);
391 else
392 kfree(tmpl);
0838aa7f 393}
9cf94eab 394EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
0838aa7f 395
9fb9cbb1
YK
396static void
397destroy_conntrack(struct nf_conntrack *nfct)
398{
399 struct nf_conn *ct = (struct nf_conn *)nfct;
605dcad6 400 struct nf_conntrack_l4proto *l4proto;
9fb9cbb1 401
0d53778e 402 pr_debug("destroy_conntrack(%p)\n", ct);
9fb9cbb1 403 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
9fb9cbb1 404
0838aa7f
PNA
405 if (unlikely(nf_ct_is_template(ct))) {
406 nf_ct_tmpl_free(ct);
407 return;
408 }
923f4902 409 rcu_read_lock();
5e8fbe2a 410 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
4b4ceb9d 411 if (l4proto->destroy)
605dcad6 412 l4proto->destroy(ct);
9fb9cbb1 413
982d9a9c 414 rcu_read_unlock();
9fb9cbb1 415
ca7433df 416 local_bh_disable();
9fb9cbb1
YK
417 /* Expectations will have been removed in clean_from_lists,
418 * except TFTP can create an expectation on the first packet,
419 * before connection is in the list, so we need to clean here,
ca7433df
JDB
420 * too.
421 */
c1d10adb 422 nf_ct_remove_expectations(ct);
9fb9cbb1 423
b7779d06 424 nf_ct_del_from_dying_or_unconfirmed_list(ct);
9fb9cbb1 425
ca7433df 426 local_bh_enable();
9fb9cbb1
YK
427
428 if (ct->master)
429 nf_ct_put(ct->master);
430
0d53778e 431 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
9fb9cbb1
YK
432 nf_conntrack_free(ct);
433}
434
02982c27 435static void nf_ct_delete_from_lists(struct nf_conn *ct)
9fb9cbb1 436{
0d55af87 437 struct net *net = nf_ct_net(ct);
93bb0ceb 438 unsigned int hash, reply_hash;
93bb0ceb 439 unsigned int sequence;
9fb9cbb1 440
9858a3ae 441 nf_ct_helper_destroy(ct);
93bb0ceb
JDB
442
443 local_bh_disable();
444 do {
a3efd812 445 sequence = read_seqcount_begin(&nf_conntrack_generation);
deedb590 446 hash = hash_conntrack(net,
93bb0ceb 447 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
deedb590 448 reply_hash = hash_conntrack(net,
93bb0ceb
JDB
449 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
450 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
451
9fb9cbb1 452 clean_from_lists(ct);
93bb0ceb
JDB
453 nf_conntrack_double_unlock(hash, reply_hash);
454
b7779d06 455 nf_ct_add_to_dying_list(ct);
93bb0ceb 456
93bb0ceb 457 local_bh_enable();
dd7669a9 458}
dd7669a9 459
02982c27 460bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
dd7669a9 461{
a992ca2a
PNA
462 struct nf_conn_tstamp *tstamp;
463
f330a7fd
FW
464 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
465 return false;
466
a992ca2a
PNA
467 tstamp = nf_conn_tstamp_find(ct);
468 if (tstamp && tstamp->stop == 0)
d2de875c 469 tstamp->stop = ktime_get_real_ns();
dd7669a9 470
9500507c
FW
471 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
472 portid, report) < 0) {
f330a7fd
FW
473 /* destroy event was not delivered. nf_ct_put will
474 * be done by event cache worker on redelivery.
475 */
dd7669a9 476 nf_ct_delete_from_lists(ct);
9500507c 477 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
02982c27 478 return false;
dd7669a9 479 }
9500507c
FW
480
481 nf_conntrack_ecache_work(nf_ct_net(ct));
dd7669a9 482 nf_ct_delete_from_lists(ct);
9fb9cbb1 483 nf_ct_put(ct);
02982c27
FW
484 return true;
485}
486EXPORT_SYMBOL_GPL(nf_ct_delete);
487
c6825c09
AV
488static inline bool
489nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
308ac914 490 const struct nf_conntrack_tuple *tuple,
e0c7d472
FW
491 const struct nf_conntrack_zone *zone,
492 const struct net *net)
c6825c09
AV
493{
494 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
495
496 /* A conntrack can be recreated with the equal tuple,
497 * so we need to check that the conntrack is confirmed
498 */
499 return nf_ct_tuple_equal(tuple, &h->tuple) &&
deedb590 500 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
e0c7d472
FW
501 nf_ct_is_confirmed(ct) &&
502 net_eq(net, nf_ct_net(ct));
c6825c09
AV
503}
504
f330a7fd
FW
505/* caller must hold rcu readlock and none of the nf_conntrack_locks */
506static void nf_ct_gc_expired(struct nf_conn *ct)
507{
508 if (!atomic_inc_not_zero(&ct->ct_general.use))
509 return;
510
511 if (nf_ct_should_gc(ct))
512 nf_ct_kill(ct);
513
514 nf_ct_put(ct);
515}
516
ea781f19
ED
517/*
518 * Warning :
519 * - Caller must take a reference on returned object
520 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
ea781f19 521 */
99f07e91 522static struct nf_conntrack_tuple_hash *
308ac914 523____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
99f07e91 524 const struct nf_conntrack_tuple *tuple, u32 hash)
9fb9cbb1
YK
525{
526 struct nf_conntrack_tuple_hash *h;
5e3c61f9 527 struct hlist_nulls_head *ct_hash;
ea781f19 528 struct hlist_nulls_node *n;
92e47ba8 529 unsigned int bucket, hsize;
9fb9cbb1 530
ea781f19 531begin:
92e47ba8
LZ
532 nf_conntrack_get_ht(&ct_hash, &hsize);
533 bucket = reciprocal_scale(hash, hsize);
5e3c61f9
FW
534
535 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
f330a7fd
FW
536 struct nf_conn *ct;
537
538 ct = nf_ct_tuplehash_to_ctrack(h);
539 if (nf_ct_is_expired(ct)) {
540 nf_ct_gc_expired(ct);
541 continue;
542 }
543
544 if (nf_ct_is_dying(ct))
545 continue;
546
8e8118f8 547 if (nf_ct_key_equal(h, tuple, zone, net))
9fb9cbb1 548 return h;
9fb9cbb1 549 }
ea781f19
ED
550 /*
551 * if the nulls value we got at the end of this lookup is
552 * not the expected one, we must restart lookup.
553 * We probably met an item that was moved to another chain.
554 */
99f07e91 555 if (get_nulls_value(n) != bucket) {
2cf12348 556 NF_CT_STAT_INC_ATOMIC(net, search_restart);
ea781f19 557 goto begin;
af740b2c 558 }
9fb9cbb1
YK
559
560 return NULL;
561}
99f07e91 562
9fb9cbb1 563/* Find a connection corresponding to a tuple. */
99f07e91 564static struct nf_conntrack_tuple_hash *
308ac914 565__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
99f07e91 566 const struct nf_conntrack_tuple *tuple, u32 hash)
9fb9cbb1
YK
567{
568 struct nf_conntrack_tuple_hash *h;
76507f69 569 struct nf_conn *ct;
9fb9cbb1 570
76507f69 571 rcu_read_lock();
ea781f19 572begin:
99f07e91 573 h = ____nf_conntrack_find(net, zone, tuple, hash);
76507f69
PM
574 if (h) {
575 ct = nf_ct_tuplehash_to_ctrack(h);
8d8890b7
PM
576 if (unlikely(nf_ct_is_dying(ct) ||
577 !atomic_inc_not_zero(&ct->ct_general.use)))
76507f69 578 h = NULL;
ea781f19 579 else {
e0c7d472 580 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
ea781f19
ED
581 nf_ct_put(ct);
582 goto begin;
583 }
584 }
76507f69
PM
585 }
586 rcu_read_unlock();
9fb9cbb1
YK
587
588 return h;
589}
99f07e91
CG
590
591struct nf_conntrack_tuple_hash *
308ac914 592nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
99f07e91
CG
593 const struct nf_conntrack_tuple *tuple)
594{
595 return __nf_conntrack_find_get(net, zone, tuple,
1b8c8a9f 596 hash_conntrack_raw(tuple, net));
99f07e91 597}
13b18339 598EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
9fb9cbb1 599
c1d10adb
PNA
600static void __nf_conntrack_hash_insert(struct nf_conn *ct,
601 unsigned int hash,
b476b72a 602 unsigned int reply_hash)
c1d10adb 603{
ea781f19 604 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
56d52d48 605 &nf_conntrack_hash[hash]);
ea781f19 606 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
56d52d48 607 &nf_conntrack_hash[reply_hash]);
c1d10adb
PNA
608}
609
7d367e06
JK
610int
611nf_conntrack_hash_check_insert(struct nf_conn *ct)
c1d10adb 612{
308ac914 613 const struct nf_conntrack_zone *zone;
d696c7bd 614 struct net *net = nf_ct_net(ct);
b476b72a 615 unsigned int hash, reply_hash;
7d367e06
JK
616 struct nf_conntrack_tuple_hash *h;
617 struct hlist_nulls_node *n;
93bb0ceb 618 unsigned int sequence;
c1d10adb 619
5d0aa2cc 620 zone = nf_ct_zone(ct);
7d367e06 621
93bb0ceb
JDB
622 local_bh_disable();
623 do {
a3efd812 624 sequence = read_seqcount_begin(&nf_conntrack_generation);
deedb590 625 hash = hash_conntrack(net,
93bb0ceb 626 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
deedb590 627 reply_hash = hash_conntrack(net,
93bb0ceb
JDB
628 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
629 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
7d367e06
JK
630
631 /* See if there's one in the list already, including reverse */
56d52d48 632 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
86804348 633 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
e0c7d472 634 zone, net))
7d367e06 635 goto out;
86804348 636
56d52d48 637 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
86804348 638 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
e0c7d472 639 zone, net))
7d367e06 640 goto out;
c1d10adb 641
e53376be
PNA
642 smp_wmb();
643 /* The caller holds a reference to this object */
644 atomic_set(&ct->ct_general.use, 2);
b476b72a 645 __nf_conntrack_hash_insert(ct, hash, reply_hash);
93bb0ceb 646 nf_conntrack_double_unlock(hash, reply_hash);
7d367e06 647 NF_CT_STAT_INC(net, insert);
93bb0ceb 648 local_bh_enable();
7d367e06
JK
649 return 0;
650
651out:
93bb0ceb 652 nf_conntrack_double_unlock(hash, reply_hash);
7d367e06 653 NF_CT_STAT_INC(net, insert_failed);
93bb0ceb 654 local_bh_enable();
7d367e06 655 return -EEXIST;
c1d10adb 656}
7d367e06 657EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
c1d10adb 658
ba76738c
PNA
659static inline void nf_ct_acct_update(struct nf_conn *ct,
660 enum ip_conntrack_info ctinfo,
661 unsigned int len)
662{
663 struct nf_conn_acct *acct;
664
665 acct = nf_conn_acct_find(ct);
666 if (acct) {
667 struct nf_conn_counter *counter = acct->counter;
668
669 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
670 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
671 }
672}
673
71d8c47f
PNA
674static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
675 const struct nf_conn *loser_ct)
676{
677 struct nf_conn_acct *acct;
678
679 acct = nf_conn_acct_find(loser_ct);
680 if (acct) {
681 struct nf_conn_counter *counter = acct->counter;
71d8c47f
PNA
682 unsigned int bytes;
683
684 /* u32 should be fine since we must have seen one packet. */
685 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
686 nf_ct_acct_update(ct, ctinfo, bytes);
687 }
688}
689
690/* Resolve race on insertion if this protocol allows this. */
691static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
692 enum ip_conntrack_info ctinfo,
693 struct nf_conntrack_tuple_hash *h)
694{
695 /* This is the conntrack entry already in hashes that won race. */
696 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
697 struct nf_conntrack_l4proto *l4proto;
698
699 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
700 if (l4proto->allow_clash &&
6e699867 701 ((ct->status & IPS_NAT_DONE_MASK) == 0) &&
71d8c47f
PNA
702 !nf_ct_is_dying(ct) &&
703 atomic_inc_not_zero(&ct->ct_general.use)) {
97a6ad13
FW
704 enum ip_conntrack_info oldinfo;
705 struct nf_conn *loser_ct = nf_ct_get(skb, &oldinfo);
706
707 nf_ct_acct_merge(ct, ctinfo, loser_ct);
708 nf_conntrack_put(&loser_ct->ct_general);
c74454fa 709 nf_ct_set(skb, ct, oldinfo);
71d8c47f
PNA
710 return NF_ACCEPT;
711 }
712 NF_CT_STAT_INC(net, drop);
713 return NF_DROP;
714}
715
9fb9cbb1
YK
716/* Confirm a connection given skb; places it in hash table */
717int
3db05fea 718__nf_conntrack_confirm(struct sk_buff *skb)
9fb9cbb1 719{
308ac914 720 const struct nf_conntrack_zone *zone;
b476b72a 721 unsigned int hash, reply_hash;
df0933dc 722 struct nf_conntrack_tuple_hash *h;
9fb9cbb1 723 struct nf_conn *ct;
df0933dc 724 struct nf_conn_help *help;
a992ca2a 725 struct nf_conn_tstamp *tstamp;
ea781f19 726 struct hlist_nulls_node *n;
9fb9cbb1 727 enum ip_conntrack_info ctinfo;
400dad39 728 struct net *net;
93bb0ceb 729 unsigned int sequence;
71d8c47f 730 int ret = NF_DROP;
9fb9cbb1 731
3db05fea 732 ct = nf_ct_get(skb, &ctinfo);
400dad39 733 net = nf_ct_net(ct);
9fb9cbb1
YK
734
735 /* ipt_REJECT uses nf_conntrack_attach to attach related
736 ICMP/TCP RST packets in other direction. Actual packet
737 which created connection will be IP_CT_NEW or for an
738 expected connection, IP_CT_RELATED. */
739 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
740 return NF_ACCEPT;
741
5d0aa2cc 742 zone = nf_ct_zone(ct);
93bb0ceb
JDB
743 local_bh_disable();
744
745 do {
a3efd812 746 sequence = read_seqcount_begin(&nf_conntrack_generation);
93bb0ceb
JDB
747 /* reuse the hash saved before */
748 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
56d52d48 749 hash = scale_hash(hash);
deedb590 750 reply_hash = hash_conntrack(net,
93bb0ceb
JDB
751 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
752
753 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
9fb9cbb1
YK
754
755 /* We're not in hash table, and we refuse to set up related
93bb0ceb
JDB
756 * connections for unconfirmed conns. But packet copies and
757 * REJECT will give spurious warnings here.
758 */
9fb9cbb1
YK
759 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
760
25985edc 761 /* No external references means no one else could have
93bb0ceb
JDB
762 * confirmed us.
763 */
9fb9cbb1 764 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
0d53778e 765 pr_debug("Confirming conntrack %p\n", ct);
8ca3f5e9
PNA
766 /* We have to check the DYING flag after unlink to prevent
767 * a race against nf_ct_get_next_corpse() possibly called from
768 * user context, else we insert an already 'dead' hash, blocking
769 * further use of that particular connection -JM.
770 */
771 nf_ct_del_from_dying_or_unconfirmed_list(ct);
772
71d8c47f
PNA
773 if (unlikely(nf_ct_is_dying(ct))) {
774 nf_ct_add_to_dying_list(ct);
775 goto dying;
776 }
fc350777 777
9fb9cbb1
YK
778 /* See if there's one in the list already, including reverse:
779 NAT could have grabbed it without realizing, since we're
780 not in the hash. If there is, we lost race. */
56d52d48 781 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
86804348 782 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
e0c7d472 783 zone, net))
df0933dc 784 goto out;
86804348 785
56d52d48 786 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
86804348 787 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
e0c7d472 788 zone, net))
df0933dc 789 goto out;
9fb9cbb1 790
df0933dc
PM
791 /* Timer relative to confirmation time, not original
792 setting time, otherwise we'd get timer wrap in
793 weird delay cases. */
f330a7fd 794 ct->timeout += nfct_time_stamp;
df0933dc 795 atomic_inc(&ct->ct_general.use);
45eec341 796 ct->status |= IPS_CONFIRMED;
5c8ec910 797
a992ca2a
PNA
798 /* set conntrack timestamp, if enabled. */
799 tstamp = nf_conn_tstamp_find(ct);
800 if (tstamp) {
2456e855 801 if (skb->tstamp == 0)
e3192690 802 __net_timestamp(skb);
a992ca2a
PNA
803
804 tstamp->start = ktime_to_ns(skb->tstamp);
805 }
5c8ec910
PM
806 /* Since the lookup is lockless, hash insertion must be done after
807 * starting the timer and setting the CONFIRMED bit. The RCU barriers
808 * guarantee that no other CPU can find the conntrack before the above
809 * stores are visible.
810 */
b476b72a 811 __nf_conntrack_hash_insert(ct, hash, reply_hash);
93bb0ceb 812 nf_conntrack_double_unlock(hash, reply_hash);
93bb0ceb 813 local_bh_enable();
5c8ec910 814
df0933dc
PM
815 help = nfct_help(ct);
816 if (help && help->helper)
a71996fc 817 nf_conntrack_event_cache(IPCT_HELPER, ct);
17e6e4ea 818
df0933dc 819 nf_conntrack_event_cache(master_ct(ct) ?
a71996fc 820 IPCT_RELATED : IPCT_NEW, ct);
df0933dc 821 return NF_ACCEPT;
9fb9cbb1 822
df0933dc 823out:
8ca3f5e9 824 nf_ct_add_to_dying_list(ct);
71d8c47f
PNA
825 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
826dying:
93bb0ceb 827 nf_conntrack_double_unlock(hash, reply_hash);
0d55af87 828 NF_CT_STAT_INC(net, insert_failed);
93bb0ceb 829 local_bh_enable();
71d8c47f 830 return ret;
9fb9cbb1 831}
13b18339 832EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
9fb9cbb1
YK
833
834/* Returns true if a connection correspondings to the tuple (required
835 for NAT). */
836int
837nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
838 const struct nf_conn *ignored_conntrack)
839{
400dad39 840 struct net *net = nf_ct_net(ignored_conntrack);
308ac914 841 const struct nf_conntrack_zone *zone;
9fb9cbb1 842 struct nf_conntrack_tuple_hash *h;
5e3c61f9 843 struct hlist_nulls_head *ct_hash;
92e47ba8 844 unsigned int hash, hsize;
ea781f19 845 struct hlist_nulls_node *n;
5d0aa2cc 846 struct nf_conn *ct;
308ac914
DB
847
848 zone = nf_ct_zone(ignored_conntrack);
9fb9cbb1 849
2cf12348 850 rcu_read_lock();
95a8d19f 851 begin:
92e47ba8
LZ
852 nf_conntrack_get_ht(&ct_hash, &hsize);
853 hash = __hash_conntrack(net, tuple, hsize);
5e3c61f9
FW
854
855 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
5d0aa2cc 856 ct = nf_ct_tuplehash_to_ctrack(h);
f330a7fd
FW
857
858 if (ct == ignored_conntrack)
859 continue;
860
861 if (nf_ct_is_expired(ct)) {
862 nf_ct_gc_expired(ct);
863 continue;
864 }
865
866 if (nf_ct_key_equal(h, tuple, zone, net)) {
2cf12348
FW
867 NF_CT_STAT_INC_ATOMIC(net, found);
868 rcu_read_unlock();
ba419aff
PM
869 return 1;
870 }
ba419aff 871 }
95a8d19f
FW
872
873 if (get_nulls_value(n) != hash) {
874 NF_CT_STAT_INC_ATOMIC(net, search_restart);
875 goto begin;
876 }
877
2cf12348 878 rcu_read_unlock();
9fb9cbb1 879
ba419aff 880 return 0;
9fb9cbb1 881}
13b18339 882EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
9fb9cbb1 883
7ae7730f
PM
884#define NF_CT_EVICTION_RANGE 8
885
9fb9cbb1
YK
886/* There's a small race here where we may free a just-assured
887 connection. Too bad: we're in trouble anyway. */
242922a0
FW
888static unsigned int early_drop_list(struct net *net,
889 struct hlist_nulls_head *head)
9fb9cbb1 890{
9fb9cbb1 891 struct nf_conntrack_tuple_hash *h;
ea781f19 892 struct hlist_nulls_node *n;
242922a0
FW
893 unsigned int drops = 0;
894 struct nf_conn *tmp;
3e86638e 895
242922a0
FW
896 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
897 tmp = nf_ct_tuplehash_to_ctrack(h);
9fb9cbb1 898
f330a7fd
FW
899 if (nf_ct_is_expired(tmp)) {
900 nf_ct_gc_expired(tmp);
901 continue;
902 }
903
242922a0
FW
904 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
905 !net_eq(nf_ct_net(tmp), net) ||
906 nf_ct_is_dying(tmp))
907 continue;
76507f69 908
242922a0
FW
909 if (!atomic_inc_not_zero(&tmp->ct_general.use))
910 continue;
76507f69 911
242922a0
FW
912 /* kill only if still in same netns -- might have moved due to
913 * SLAB_DESTROY_BY_RCU rules.
914 *
915 * We steal the timer reference. If that fails timer has
916 * already fired or someone else deleted it. Just drop ref
917 * and move to next entry.
918 */
919 if (net_eq(nf_ct_net(tmp), net) &&
920 nf_ct_is_confirmed(tmp) &&
242922a0
FW
921 nf_ct_delete(tmp, 0, 0))
922 drops++;
923
924 nf_ct_put(tmp);
9fb9cbb1 925 }
3e86638e 926
242922a0
FW
927 return drops;
928}
9fb9cbb1 929
242922a0
FW
930static noinline int early_drop(struct net *net, unsigned int _hash)
931{
932 unsigned int i;
9fb9cbb1 933
242922a0
FW
934 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
935 struct hlist_nulls_head *ct_hash;
92e47ba8 936 unsigned int hash, hsize, drops;
242922a0 937
3101e0fc 938 rcu_read_lock();
92e47ba8
LZ
939 nf_conntrack_get_ht(&ct_hash, &hsize);
940 hash = reciprocal_scale(_hash++, hsize);
242922a0
FW
941
942 drops = early_drop_list(net, &ct_hash[hash]);
3101e0fc
LZ
943 rcu_read_unlock();
944
242922a0
FW
945 if (drops) {
946 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
947 return true;
74138511 948 }
9fb9cbb1 949 }
3e86638e 950
242922a0 951 return false;
9fb9cbb1
YK
952}
953
b87a2f91
FW
954static void gc_worker(struct work_struct *work)
955{
e5072053 956 unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
b87a2f91 957 unsigned int i, goal, buckets = 0, expired_count = 0;
b87a2f91 958 struct conntrack_gc_work *gc_work;
e0df8cae
FW
959 unsigned int ratio, scanned = 0;
960 unsigned long next_run;
b87a2f91
FW
961
962 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
963
e0df8cae 964 goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
b87a2f91
FW
965 i = gc_work->last_bucket;
966
967 do {
968 struct nf_conntrack_tuple_hash *h;
969 struct hlist_nulls_head *ct_hash;
970 struct hlist_nulls_node *n;
971 unsigned int hashsz;
972 struct nf_conn *tmp;
973
974 i++;
975 rcu_read_lock();
976
977 nf_conntrack_get_ht(&ct_hash, &hashsz);
978 if (i >= hashsz)
979 i = 0;
980
981 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
982 tmp = nf_ct_tuplehash_to_ctrack(h);
983
c023c0e4 984 scanned++;
b87a2f91
FW
985 if (nf_ct_is_expired(tmp)) {
986 nf_ct_gc_expired(tmp);
987 expired_count++;
988 continue;
989 }
990 }
991
992 /* could check get_nulls_value() here and restart if ct
993 * was moved to another chain. But given gc is best-effort
994 * we will just continue with next hash slot.
995 */
996 rcu_read_unlock();
997 cond_resched_rcu_qs();
524b698d 998 } while (++buckets < goal);
b87a2f91
FW
999
1000 if (gc_work->exiting)
1001 return;
1002
e0df8cae
FW
1003 /*
1004 * Eviction will normally happen from the packet path, and not
1005 * from this gc worker.
1006 *
1007 * This worker is only here to reap expired entries when system went
1008 * idle after a busy period.
1009 *
1010 * The heuristics below are supposed to balance conflicting goals:
1011 *
1012 * 1. Minimize time until we notice a stale entry
1013 * 2. Maximize scan intervals to not waste cycles
1014 *
e5072053 1015 * Normally, expire ratio will be close to 0.
e0df8cae 1016 *
e5072053
FW
1017 * As soon as a sizeable fraction of the entries have expired
1018 * increase scan frequency.
e0df8cae 1019 */
c023c0e4 1020 ratio = scanned ? expired_count * 100 / scanned : 0;
e5072053
FW
1021 if (ratio > GC_EVICT_RATIO) {
1022 gc_work->next_gc_run = min_interval;
e0df8cae 1023 } else {
e5072053 1024 unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
e0df8cae 1025
e5072053
FW
1026 BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1027
1028 gc_work->next_gc_run += min_interval;
1029 if (gc_work->next_gc_run > max)
1030 gc_work->next_gc_run = max;
e0df8cae 1031 }
c023c0e4 1032
e5072053 1033 next_run = gc_work->next_gc_run;
b87a2f91 1034 gc_work->last_bucket = i;
e0df8cae 1035 queue_delayed_work(system_long_wq, &gc_work->dwork, next_run);
b87a2f91
FW
1036}
1037
1038static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1039{
1040 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
e5072053 1041 gc_work->next_gc_run = HZ;
b87a2f91
FW
1042 gc_work->exiting = false;
1043}
1044
99f07e91 1045static struct nf_conn *
308ac914
DB
1046__nf_conntrack_alloc(struct net *net,
1047 const struct nf_conntrack_zone *zone,
99f07e91
CG
1048 const struct nf_conntrack_tuple *orig,
1049 const struct nf_conntrack_tuple *repl,
1050 gfp_t gfp, u32 hash)
9fb9cbb1 1051{
cd7fcbf1 1052 struct nf_conn *ct;
9fb9cbb1 1053
5251e2d2 1054 /* We don't want any race condition at early drop stage */
49ac8713 1055 atomic_inc(&net->ct.count);
5251e2d2 1056
76eb9460 1057 if (nf_conntrack_max &&
49ac8713 1058 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
93bb0ceb 1059 if (!early_drop(net, hash)) {
49ac8713 1060 atomic_dec(&net->ct.count);
e87cc472 1061 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
9fb9cbb1
YK
1062 return ERR_PTR(-ENOMEM);
1063 }
1064 }
1065
941297f4
ED
1066 /*
1067 * Do not use kmem_cache_zalloc(), as this cache uses
1068 * SLAB_DESTROY_BY_RCU.
1069 */
0c5366b3 1070 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
5e8018fc
DB
1071 if (ct == NULL)
1072 goto out;
1073
440f0d58 1074 spin_lock_init(&ct->lock);
c88130bc 1075 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
941297f4 1076 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
c88130bc 1077 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
99f07e91
CG
1078 /* save hash for reusing when confirming */
1079 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
c41884ce 1080 ct->status = 0;
c2d9ba9b 1081 write_pnet(&ct->ct_net, net);
c41884ce
FW
1082 memset(&ct->__nfct_init_offset[0], 0,
1083 offsetof(struct nf_conn, proto) -
1084 offsetof(struct nf_conn, __nfct_init_offset[0]));
5e8018fc 1085
6c8dee98 1086 nf_ct_zone_add(ct, zone);
5e8018fc 1087
e53376be
PNA
1088 /* Because we use RCU lookups, we set ct_general.use to zero before
1089 * this is inserted in any list.
941297f4 1090 */
e53376be 1091 atomic_set(&ct->ct_general.use, 0);
c88130bc 1092 return ct;
5e8018fc
DB
1093out:
1094 atomic_dec(&net->ct.count);
5d0aa2cc 1095 return ERR_PTR(-ENOMEM);
9fb9cbb1 1096}
99f07e91 1097
308ac914
DB
1098struct nf_conn *nf_conntrack_alloc(struct net *net,
1099 const struct nf_conntrack_zone *zone,
99f07e91
CG
1100 const struct nf_conntrack_tuple *orig,
1101 const struct nf_conntrack_tuple *repl,
1102 gfp_t gfp)
1103{
1104 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1105}
13b18339 1106EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
9fb9cbb1 1107
c88130bc 1108void nf_conntrack_free(struct nf_conn *ct)
76507f69 1109{
1d45209d
ED
1110 struct net *net = nf_ct_net(ct);
1111
e53376be
PNA
1112 /* A freed object has refcnt == 0, that's
1113 * the golden rule for SLAB_DESTROY_BY_RCU
1114 */
1115 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1116
ceeff754 1117 nf_ct_ext_destroy(ct);
ea781f19 1118 nf_ct_ext_free(ct);
0c5366b3 1119 kmem_cache_free(nf_conntrack_cachep, ct);
4e857c58 1120 smp_mb__before_atomic();
0c3c6c00 1121 atomic_dec(&net->ct.count);
76507f69 1122}
13b18339 1123EXPORT_SYMBOL_GPL(nf_conntrack_free);
9fb9cbb1 1124
c539f017 1125
9fb9cbb1
YK
1126/* Allocate a new conntrack: we return -ENOMEM if classification
1127 failed due to stress. Otherwise it really is unclassifiable. */
fc09e4a7 1128static noinline struct nf_conntrack_tuple_hash *
b2a15a60 1129init_conntrack(struct net *net, struct nf_conn *tmpl,
5a1fb391 1130 const struct nf_conntrack_tuple *tuple,
9fb9cbb1 1131 struct nf_conntrack_l3proto *l3proto,
605dcad6 1132 struct nf_conntrack_l4proto *l4proto,
9fb9cbb1 1133 struct sk_buff *skb,
60b5f8f7 1134 unsigned int dataoff, u32 hash)
9fb9cbb1 1135{
c88130bc 1136 struct nf_conn *ct;
3c158f7f 1137 struct nf_conn_help *help;
9fb9cbb1 1138 struct nf_conntrack_tuple repl_tuple;
b2a15a60 1139 struct nf_conntrack_ecache *ecache;
ca7433df 1140 struct nf_conntrack_expect *exp = NULL;
308ac914 1141 const struct nf_conntrack_zone *zone;
60b5f8f7 1142 struct nf_conn_timeout *timeout_ext;
5e8018fc 1143 struct nf_conntrack_zone tmp;
60b5f8f7 1144 unsigned int *timeouts;
9fb9cbb1 1145
605dcad6 1146 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
0d53778e 1147 pr_debug("Can't invert tuple.\n");
9fb9cbb1
YK
1148 return NULL;
1149 }
1150
5e8018fc 1151 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
99f07e91
CG
1152 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1153 hash);
0a9ee813 1154 if (IS_ERR(ct))
c88130bc 1155 return (struct nf_conntrack_tuple_hash *)ct;
9fb9cbb1 1156
4440a2ab
GF
1157 if (!nf_ct_add_synproxy(ct, tmpl)) {
1158 nf_conntrack_free(ct);
1159 return ERR_PTR(-ENOMEM);
48b1de4c
PM
1160 }
1161
60b5f8f7 1162 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
ae2d708e
PNA
1163 if (timeout_ext) {
1164 timeouts = nf_ct_timeout_data(timeout_ext);
1165 if (unlikely(!timeouts))
1166 timeouts = l4proto->get_timeouts(net);
1167 } else {
60b5f8f7 1168 timeouts = l4proto->get_timeouts(net);
ae2d708e 1169 }
60b5f8f7 1170
2c8503f5 1171 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
c88130bc 1172 nf_conntrack_free(ct);
ccd63c20 1173 pr_debug("can't track with proto module\n");
9fb9cbb1
YK
1174 return NULL;
1175 }
1176
60b5f8f7 1177 if (timeout_ext)
ae2d708e
PNA
1178 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1179 GFP_ATOMIC);
60b5f8f7 1180
58401572 1181 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1182 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
c539f017 1183 nf_ct_labels_ext_add(ct);
b2a15a60
PM
1184
1185 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1186 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1187 ecache ? ecache->expmask : 0,
1188 GFP_ATOMIC);
58401572 1189
ca7433df
JDB
1190 local_bh_disable();
1191 if (net->ct.expect_count) {
1192 spin_lock(&nf_conntrack_expect_lock);
1193 exp = nf_ct_find_expectation(net, zone, tuple);
1194 if (exp) {
ccd63c20 1195 pr_debug("expectation arrives ct=%p exp=%p\n",
ca7433df
JDB
1196 ct, exp);
1197 /* Welcome, Mr. Bond. We've been expecting you... */
1198 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1199 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1200 ct->master = exp->master;
1201 if (exp->helper) {
1202 help = nf_ct_helper_ext_add(ct, exp->helper,
1203 GFP_ATOMIC);
1204 if (help)
1205 rcu_assign_pointer(help->helper, exp->helper);
1206 }
ceceae1b 1207
9fb9cbb1 1208#ifdef CONFIG_NF_CONNTRACK_MARK
ca7433df 1209 ct->mark = exp->master->mark;
7c9728c3
JM
1210#endif
1211#ifdef CONFIG_NF_CONNTRACK_SECMARK
ca7433df 1212 ct->secmark = exp->master->secmark;
9fb9cbb1 1213#endif
ca7433df
JDB
1214 NF_CT_STAT_INC(net, expect_new);
1215 }
1216 spin_unlock(&nf_conntrack_expect_lock);
1217 }
8e8118f8 1218 if (!exp)
b2a15a60 1219 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
9fb9cbb1 1220
e53376be
PNA
1221 /* Now it is inserted into the unconfirmed list, bump refcount */
1222 nf_conntrack_get(&ct->ct_general);
b7779d06 1223 nf_ct_add_to_unconfirmed_list(ct);
9fb9cbb1 1224
ca7433df 1225 local_bh_enable();
9fb9cbb1
YK
1226
1227 if (exp) {
1228 if (exp->expectfn)
c88130bc 1229 exp->expectfn(ct, exp);
6823645d 1230 nf_ct_expect_put(exp);
9fb9cbb1
YK
1231 }
1232
c88130bc 1233 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
9fb9cbb1
YK
1234}
1235
fc09e4a7
FW
1236/* On success, returns 0, sets skb->_nfct | ctinfo */
1237static int
b2a15a60 1238resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
a702a65f 1239 struct sk_buff *skb,
9fb9cbb1
YK
1240 unsigned int dataoff,
1241 u_int16_t l3num,
1242 u_int8_t protonum,
1243 struct nf_conntrack_l3proto *l3proto,
fc09e4a7 1244 struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 1245{
308ac914 1246 const struct nf_conntrack_zone *zone;
9fb9cbb1
YK
1247 struct nf_conntrack_tuple tuple;
1248 struct nf_conntrack_tuple_hash *h;
fc09e4a7 1249 enum ip_conntrack_info ctinfo;
5e8018fc 1250 struct nf_conntrack_zone tmp;
9fb9cbb1 1251 struct nf_conn *ct;
99f07e91 1252 u32 hash;
9fb9cbb1 1253
bbe735e4 1254 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
a31f1adc 1255 dataoff, l3num, protonum, net, &tuple, l3proto,
605dcad6 1256 l4proto)) {
ccd63c20 1257 pr_debug("Can't get tuple\n");
fc09e4a7 1258 return 0;
9fb9cbb1
YK
1259 }
1260
1261 /* look for tuple match */
5e8018fc 1262 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1b8c8a9f 1263 hash = hash_conntrack_raw(&tuple, net);
99f07e91 1264 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
9fb9cbb1 1265 if (!h) {
b2a15a60 1266 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
60b5f8f7 1267 skb, dataoff, hash);
9fb9cbb1 1268 if (!h)
fc09e4a7 1269 return 0;
9fb9cbb1 1270 if (IS_ERR(h))
fc09e4a7 1271 return PTR_ERR(h);
9fb9cbb1
YK
1272 }
1273 ct = nf_ct_tuplehash_to_ctrack(h);
1274
1275 /* It exists; we have (non-exclusive) reference. */
1276 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
fc09e4a7 1277 ctinfo = IP_CT_ESTABLISHED_REPLY;
9fb9cbb1
YK
1278 } else {
1279 /* Once we've had two way comms, always ESTABLISHED. */
1280 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
ccd63c20 1281 pr_debug("normal packet for %p\n", ct);
fc09e4a7 1282 ctinfo = IP_CT_ESTABLISHED;
9fb9cbb1 1283 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
ccd63c20 1284 pr_debug("related packet for %p\n", ct);
fc09e4a7 1285 ctinfo = IP_CT_RELATED;
9fb9cbb1 1286 } else {
ccd63c20 1287 pr_debug("new packet for %p\n", ct);
fc09e4a7 1288 ctinfo = IP_CT_NEW;
9fb9cbb1 1289 }
9fb9cbb1 1290 }
fc09e4a7
FW
1291 nf_ct_set(skb, ct, ctinfo);
1292 return 0;
9fb9cbb1
YK
1293}
1294
1295unsigned int
a702a65f
AD
1296nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1297 struct sk_buff *skb)
9fb9cbb1 1298{
97a6ad13 1299 struct nf_conn *ct, *tmpl;
9fb9cbb1
YK
1300 enum ip_conntrack_info ctinfo;
1301 struct nf_conntrack_l3proto *l3proto;
605dcad6 1302 struct nf_conntrack_l4proto *l4proto;
2c8503f5 1303 unsigned int *timeouts;
9fb9cbb1
YK
1304 unsigned int dataoff;
1305 u_int8_t protonum;
9fb9cbb1
YK
1306 int ret;
1307
97a6ad13 1308 tmpl = nf_ct_get(skb, &ctinfo);
cc41c84b 1309 if (tmpl || ctinfo == IP_CT_UNTRACKED) {
b2a15a60 1310 /* Previously seen (loopback or untracked)? Ignore. */
cc41c84b
FW
1311 if ((tmpl && !nf_ct_is_template(tmpl)) ||
1312 ctinfo == IP_CT_UNTRACKED) {
b2a15a60
PM
1313 NF_CT_STAT_INC_ATOMIC(net, ignore);
1314 return NF_ACCEPT;
1315 }
a9e419dc 1316 skb->_nfct = 0;
9fb9cbb1
YK
1317 }
1318
e2361cb9 1319 /* rcu_read_lock()ed by nf_hook_thresh */
76108cea 1320 l3proto = __nf_ct_l3proto_find(pf);
3db05fea 1321 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
ffc30690
YK
1322 &dataoff, &protonum);
1323 if (ret <= 0) {
25985edc 1324 pr_debug("not prepared to track yet or error occurred\n");
0d55af87
AD
1325 NF_CT_STAT_INC_ATOMIC(net, error);
1326 NF_CT_STAT_INC_ATOMIC(net, invalid);
b2a15a60
PM
1327 ret = -ret;
1328 goto out;
9fb9cbb1
YK
1329 }
1330
76108cea 1331 l4proto = __nf_ct_l4proto_find(pf, protonum);
9fb9cbb1
YK
1332
1333 /* It may be an special packet, error, unclean...
1334 * inverse of the return code tells to the netfilter
1335 * core what to do with the packet. */
74c51a14 1336 if (l4proto->error != NULL) {
11df4b76 1337 ret = l4proto->error(net, tmpl, skb, dataoff, pf, hooknum);
74c51a14 1338 if (ret <= 0) {
0d55af87
AD
1339 NF_CT_STAT_INC_ATOMIC(net, error);
1340 NF_CT_STAT_INC_ATOMIC(net, invalid);
b2a15a60
PM
1341 ret = -ret;
1342 goto out;
74c51a14 1343 }
88ed01d1 1344 /* ICMP[v6] protocol trackers may assign one conntrack. */
a9e419dc 1345 if (skb->_nfct)
88ed01d1 1346 goto out;
9fb9cbb1 1347 }
08733a0c 1348repeat:
fc09e4a7
FW
1349 ret = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
1350 l3proto, l4proto);
1351 if (ret < 0) {
9fb9cbb1 1352 /* Too stressed to deal. */
0d55af87 1353 NF_CT_STAT_INC_ATOMIC(net, drop);
b2a15a60
PM
1354 ret = NF_DROP;
1355 goto out;
9fb9cbb1
YK
1356 }
1357
fc09e4a7
FW
1358 ct = nf_ct_get(skb, &ctinfo);
1359 if (!ct) {
1360 /* Not valid part of a connection */
1361 NF_CT_STAT_INC_ATOMIC(net, invalid);
1362 ret = NF_ACCEPT;
1363 goto out;
1364 }
9fb9cbb1 1365
60b5f8f7 1366 /* Decide what timeout policy we want to apply to this flow. */
84b5ee93 1367 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
60b5f8f7 1368
2c8503f5 1369 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
ec8d5409 1370 if (ret <= 0) {
9fb9cbb1
YK
1371 /* Invalid: inverse of the return code tells
1372 * the netfilter core what to do */
0d53778e 1373 pr_debug("nf_conntrack_in: Can't track with proto module\n");
97a6ad13 1374 nf_conntrack_put(&ct->ct_general);
a9e419dc 1375 skb->_nfct = 0;
0d55af87 1376 NF_CT_STAT_INC_ATOMIC(net, invalid);
7d1e0459
PNA
1377 if (ret == -NF_DROP)
1378 NF_CT_STAT_INC_ATOMIC(net, drop);
56a62e22
AB
1379 /* Special case: TCP tracker reports an attempt to reopen a
1380 * closed/aborted connection. We have to go back and create a
1381 * fresh conntrack.
1382 */
1383 if (ret == -NF_REPEAT)
1384 goto repeat;
b2a15a60
PM
1385 ret = -ret;
1386 goto out;
9fb9cbb1
YK
1387 }
1388
fc09e4a7
FW
1389 if (ctinfo == IP_CT_ESTABLISHED_REPLY &&
1390 !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
858b3133 1391 nf_conntrack_event_cache(IPCT_REPLY, ct);
b2a15a60 1392out:
56a62e22
AB
1393 if (tmpl)
1394 nf_ct_put(tmpl);
9fb9cbb1
YK
1395
1396 return ret;
1397}
13b18339 1398EXPORT_SYMBOL_GPL(nf_conntrack_in);
9fb9cbb1 1399
5f2b4c90
JE
1400bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1401 const struct nf_conntrack_tuple *orig)
9fb9cbb1 1402{
5f2b4c90 1403 bool ret;
923f4902
PM
1404
1405 rcu_read_lock();
1406 ret = nf_ct_invert_tuple(inverse, orig,
1407 __nf_ct_l3proto_find(orig->src.l3num),
1408 __nf_ct_l4proto_find(orig->src.l3num,
1409 orig->dst.protonum));
1410 rcu_read_unlock();
1411 return ret;
9fb9cbb1 1412}
13b18339 1413EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
9fb9cbb1 1414
5b1158e9
JK
1415/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1416 implicitly racy: see __nf_conntrack_confirm */
1417void nf_conntrack_alter_reply(struct nf_conn *ct,
1418 const struct nf_conntrack_tuple *newreply)
1419{
1420 struct nf_conn_help *help = nfct_help(ct);
1421
5b1158e9
JK
1422 /* Should be unconfirmed, so not in hash table yet */
1423 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1424
0d53778e 1425 pr_debug("Altering reply tuple of %p to ", ct);
3c9fba65 1426 nf_ct_dump_tuple(newreply);
5b1158e9
JK
1427
1428 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
ef1a5a50 1429 if (ct->master || (help && !hlist_empty(&help->expectations)))
c52fbb41 1430 return;
ceceae1b 1431
c52fbb41 1432 rcu_read_lock();
b2a15a60 1433 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
c52fbb41 1434 rcu_read_unlock();
5b1158e9 1435}
13b18339 1436EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
5b1158e9 1437
9fb9cbb1
YK
1438/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1439void __nf_ct_refresh_acct(struct nf_conn *ct,
1440 enum ip_conntrack_info ctinfo,
1441 const struct sk_buff *skb,
1442 unsigned long extra_jiffies,
1443 int do_acct)
1444{
9fb9cbb1
YK
1445 NF_CT_ASSERT(skb);
1446
997ae831 1447 /* Only update if this is not a fixed timeout */
47d95045
PM
1448 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1449 goto acct;
997ae831 1450
9fb9cbb1 1451 /* If not in hash table, timer will not be active yet */
f330a7fd
FW
1452 if (nf_ct_is_confirmed(ct))
1453 extra_jiffies += nfct_time_stamp;
9fb9cbb1 1454
f330a7fd 1455 ct->timeout = extra_jiffies;
47d95045 1456acct:
ba76738c
PNA
1457 if (do_acct)
1458 nf_ct_acct_update(ct, ctinfo, skb->len);
9fb9cbb1 1459}
13b18339 1460EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
9fb9cbb1 1461
ad66713f
FW
1462bool nf_ct_kill_acct(struct nf_conn *ct,
1463 enum ip_conntrack_info ctinfo,
1464 const struct sk_buff *skb)
51091764 1465{
ad66713f 1466 nf_ct_acct_update(ct, ctinfo, skb->len);
58401572 1467
f330a7fd 1468 return nf_ct_delete(ct, 0, 0);
51091764 1469}
ad66713f 1470EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
51091764 1471
c0cd1156 1472#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c1d10adb
PNA
1473
1474#include <linux/netfilter/nfnetlink.h>
1475#include <linux/netfilter/nfnetlink_conntrack.h>
57b47a53
IM
1476#include <linux/mutex.h>
1477
c1d10adb
PNA
1478/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1479 * in ip_conntrack_core, since we don't want the protocols to autoload
1480 * or depend on ctnetlink */
fdf70832 1481int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
1482 const struct nf_conntrack_tuple *tuple)
1483{
bae65be8
DM
1484 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1485 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1486 goto nla_put_failure;
c1d10adb
PNA
1487 return 0;
1488
df6fb868 1489nla_put_failure:
c1d10adb
PNA
1490 return -1;
1491}
fdf70832 1492EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
c1d10adb 1493
f73e924c
PM
1494const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1495 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1496 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
c1d10adb 1497};
f73e924c 1498EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
c1d10adb 1499
fdf70832 1500int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
1501 struct nf_conntrack_tuple *t)
1502{
df6fb868 1503 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
c1d10adb
PNA
1504 return -EINVAL;
1505
77236b6e
PM
1506 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1507 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
c1d10adb
PNA
1508
1509 return 0;
1510}
fdf70832 1511EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
5c0de29d
HE
1512
1513int nf_ct_port_nlattr_tuple_size(void)
1514{
1515 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1516}
1517EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
c1d10adb
PNA
1518#endif
1519
9fb9cbb1 1520/* Used by ipt_REJECT and ip6t_REJECT. */
312a0c16 1521static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
9fb9cbb1
YK
1522{
1523 struct nf_conn *ct;
1524 enum ip_conntrack_info ctinfo;
1525
1526 /* This ICMP is in reverse direction to the packet which caused it */
1527 ct = nf_ct_get(skb, &ctinfo);
1528 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
fb048833 1529 ctinfo = IP_CT_RELATED_REPLY;
9fb9cbb1
YK
1530 else
1531 ctinfo = IP_CT_RELATED;
1532
1533 /* Attach to new skbuff, and increment count */
c74454fa 1534 nf_ct_set(nskb, ct, ctinfo);
cb9c6836 1535 nf_conntrack_get(skb_nfct(nskb));
9fb9cbb1
YK
1536}
1537
9fb9cbb1 1538/* Bring out ya dead! */
df0933dc 1539static struct nf_conn *
400dad39 1540get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
9fb9cbb1
YK
1541 void *data, unsigned int *bucket)
1542{
df0933dc
PM
1543 struct nf_conntrack_tuple_hash *h;
1544 struct nf_conn *ct;
ea781f19 1545 struct hlist_nulls_node *n;
b7779d06 1546 int cpu;
93bb0ceb 1547 spinlock_t *lockp;
9fb9cbb1 1548
56d52d48 1549 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
93bb0ceb
JDB
1550 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1551 local_bh_disable();
b16c2919 1552 nf_conntrack_lock(lockp);
56d52d48
FW
1553 if (*bucket < nf_conntrack_htable_size) {
1554 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
93bb0ceb
JDB
1555 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1556 continue;
1557 ct = nf_ct_tuplehash_to_ctrack(h);
e0c7d472
FW
1558 if (net_eq(nf_ct_net(ct), net) &&
1559 iter(ct, data))
93bb0ceb
JDB
1560 goto found;
1561 }
df0933dc 1562 }
93bb0ceb
JDB
1563 spin_unlock(lockp);
1564 local_bh_enable();
d93c6258 1565 cond_resched();
601e68e1 1566 }
b7779d06
JDB
1567
1568 for_each_possible_cpu(cpu) {
1569 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1570
1571 spin_lock_bh(&pcpu->lock);
1572 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1573 ct = nf_ct_tuplehash_to_ctrack(h);
1574 if (iter(ct, data))
1575 set_bit(IPS_DYING_BIT, &ct->status);
1576 }
1577 spin_unlock_bh(&pcpu->lock);
d93c6258 1578 cond_resched();
b7779d06 1579 }
df0933dc
PM
1580 return NULL;
1581found:
c073e3fa 1582 atomic_inc(&ct->ct_general.use);
93bb0ceb
JDB
1583 spin_unlock(lockp);
1584 local_bh_enable();
df0933dc 1585 return ct;
9fb9cbb1
YK
1586}
1587
400dad39
AD
1588void nf_ct_iterate_cleanup(struct net *net,
1589 int (*iter)(struct nf_conn *i, void *data),
c655bc68 1590 void *data, u32 portid, int report)
9fb9cbb1 1591{
df0933dc 1592 struct nf_conn *ct;
9fb9cbb1
YK
1593 unsigned int bucket = 0;
1594
d93c6258
FW
1595 might_sleep();
1596
88b68bc5
FW
1597 if (atomic_read(&net->ct.count) == 0)
1598 return;
1599
400dad39 1600 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
9fb9cbb1 1601 /* Time to push up daises... */
9fb9cbb1 1602
f330a7fd 1603 nf_ct_delete(ct, portid, report);
9fb9cbb1 1604 nf_ct_put(ct);
d93c6258 1605 cond_resched();
9fb9cbb1
YK
1606 }
1607}
13b18339 1608EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
9fb9cbb1 1609
274d383b
PNA
1610static int kill_all(struct nf_conn *i, void *data)
1611{
1612 return 1;
1613}
1614
d862a662 1615void nf_ct_free_hashtable(void *hash, unsigned int size)
9fb9cbb1 1616{
d862a662 1617 if (is_vmalloc_addr(hash))
9fb9cbb1
YK
1618 vfree(hash);
1619 else
601e68e1 1620 free_pages((unsigned long)hash,
f205c5e0 1621 get_order(sizeof(struct hlist_head) * size));
9fb9cbb1 1622}
ac565e5f 1623EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
9fb9cbb1 1624
f94161c1 1625void nf_conntrack_cleanup_start(void)
9fb9cbb1 1626{
b87a2f91 1627 conntrack_gc_work.exiting = true;
f94161c1
G
1628 RCU_INIT_POINTER(ip_ct_attach, NULL);
1629}
1630
1631void nf_conntrack_cleanup_end(void)
1632{
1633 RCU_INIT_POINTER(nf_ct_destroy, NULL);
9edd7ca0 1634
b87a2f91 1635 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
56d52d48
FW
1636 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1637
04d87001 1638 nf_conntrack_proto_fini();
41d73ec0 1639 nf_conntrack_seqadj_fini();
5f69b8f5 1640 nf_conntrack_labels_fini();
5e615b22 1641 nf_conntrack_helper_fini();
8684094c 1642 nf_conntrack_timeout_fini();
3fe0f943 1643 nf_conntrack_ecache_fini();
73f4001a 1644 nf_conntrack_tstamp_fini();
b7ff3a1f 1645 nf_conntrack_acct_fini();
83b4dbe1 1646 nf_conntrack_expect_fini();
77571149
FW
1647
1648 kmem_cache_destroy(nf_conntrack_cachep);
08f6547d 1649}
9fb9cbb1 1650
f94161c1
G
1651/*
1652 * Mishearing the voices in his head, our hero wonders how he's
1653 * supposed to kill the mall.
1654 */
1655void nf_conntrack_cleanup_net(struct net *net)
08f6547d 1656{
dece40e8
VD
1657 LIST_HEAD(single);
1658
1659 list_add(&net->exit_list, &single);
1660 nf_conntrack_cleanup_net_list(&single);
1661}
1662
1663void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1664{
1665 int busy;
1666 struct net *net;
1667
f94161c1
G
1668 /*
1669 * This makes sure all current packets have passed through
1670 * netfilter framework. Roll on, two-stage module
1671 * delete...
1672 */
1673 synchronize_net();
dece40e8
VD
1674i_see_dead_people:
1675 busy = 0;
1676 list_for_each_entry(net, net_exit_list, exit_list) {
c655bc68 1677 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
dece40e8
VD
1678 if (atomic_read(&net->ct.count) != 0)
1679 busy = 1;
1680 }
1681 if (busy) {
9fb9cbb1
YK
1682 schedule();
1683 goto i_see_dead_people;
1684 }
1685
dece40e8 1686 list_for_each_entry(net, net_exit_list, exit_list) {
dece40e8
VD
1687 nf_conntrack_proto_pernet_fini(net);
1688 nf_conntrack_helper_pernet_fini(net);
1689 nf_conntrack_ecache_pernet_fini(net);
1690 nf_conntrack_tstamp_pernet_fini(net);
1691 nf_conntrack_acct_pernet_fini(net);
1692 nf_conntrack_expect_pernet_fini(net);
dece40e8 1693 free_percpu(net->ct.stat);
b7779d06 1694 free_percpu(net->ct.pcpu_lists);
dece40e8 1695 }
08f6547d
AD
1696}
1697
d862a662 1698void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
9fb9cbb1 1699{
ea781f19
ED
1700 struct hlist_nulls_head *hash;
1701 unsigned int nr_slots, i;
1702 size_t sz;
9fb9cbb1 1703
9cc1c73a
FW
1704 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1705 return NULL;
1706
ea781f19
ED
1707 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1708 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
9cc1c73a
FW
1709
1710 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1711 return NULL;
1712
ea781f19
ED
1713 sz = nr_slots * sizeof(struct hlist_nulls_head);
1714 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1715 get_order(sz));
f0ad4621 1716 if (!hash)
966567b7 1717 hash = vzalloc(sz);
9fb9cbb1 1718
ea781f19
ED
1719 if (hash && nulls)
1720 for (i = 0; i < nr_slots; i++)
1721 INIT_HLIST_NULLS_HEAD(&hash[i], i);
9fb9cbb1
YK
1722
1723 return hash;
1724}
ac565e5f 1725EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
9fb9cbb1 1726
3183ab89 1727int nf_conntrack_hash_resize(unsigned int hashsize)
9fb9cbb1 1728{
3183ab89
FW
1729 int i, bucket;
1730 unsigned int old_size;
ea781f19 1731 struct hlist_nulls_head *hash, *old_hash;
9fb9cbb1 1732 struct nf_conntrack_tuple_hash *h;
5d0aa2cc 1733 struct nf_conn *ct;
9fb9cbb1 1734
9fb9cbb1
YK
1735 if (!hashsize)
1736 return -EINVAL;
1737
d862a662 1738 hash = nf_ct_alloc_hashtable(&hashsize, 1);
9fb9cbb1
YK
1739 if (!hash)
1740 return -ENOMEM;
1741
3183ab89
FW
1742 old_size = nf_conntrack_htable_size;
1743 if (old_size == hashsize) {
1744 nf_ct_free_hashtable(hash, hashsize);
1745 return 0;
1746 }
1747
93bb0ceb
JDB
1748 local_bh_disable();
1749 nf_conntrack_all_lock();
a3efd812 1750 write_seqcount_begin(&nf_conntrack_generation);
93bb0ceb 1751
76507f69
PM
1752 /* Lookups in the old hash might happen in parallel, which means we
1753 * might get false negatives during connection lookup. New connections
1754 * created because of a false negative won't make it into the hash
93bb0ceb 1755 * though since that required taking the locks.
76507f69 1756 */
93bb0ceb 1757
56d52d48
FW
1758 for (i = 0; i < nf_conntrack_htable_size; i++) {
1759 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1760 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1761 struct nf_conntrack_tuple_hash, hnnode);
5d0aa2cc 1762 ct = nf_ct_tuplehash_to_ctrack(h);
ea781f19 1763 hlist_nulls_del_rcu(&h->hnnode);
1b8c8a9f
FW
1764 bucket = __hash_conntrack(nf_ct_net(ct),
1765 &h->tuple, hashsize);
ea781f19 1766 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
9fb9cbb1
YK
1767 }
1768 }
56d52d48
FW
1769 old_size = nf_conntrack_htable_size;
1770 old_hash = nf_conntrack_hash;
9fb9cbb1 1771
56d52d48
FW
1772 nf_conntrack_hash = hash;
1773 nf_conntrack_htable_size = hashsize;
93bb0ceb 1774
a3efd812 1775 write_seqcount_end(&nf_conntrack_generation);
93bb0ceb
JDB
1776 nf_conntrack_all_unlock();
1777 local_bh_enable();
9fb9cbb1 1778
5e3c61f9 1779 synchronize_net();
d862a662 1780 nf_ct_free_hashtable(old_hash, old_size);
9fb9cbb1
YK
1781 return 0;
1782}
3183ab89
FW
1783
1784int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1785{
1786 unsigned int hashsize;
1787 int rc;
1788
1789 if (current->nsproxy->net_ns != &init_net)
1790 return -EOPNOTSUPP;
1791
1792 /* On boot, we can set this without any fancy locking. */
1793 if (!nf_conntrack_htable_size)
1794 return param_set_uint(val, kp);
1795
1796 rc = kstrtouint(val, 0, &hashsize);
1797 if (rc)
1798 return rc;
1799
1800 return nf_conntrack_hash_resize(hashsize);
1801}
fae718dd 1802EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
9fb9cbb1 1803
fae718dd 1804module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
9fb9cbb1
YK
1805 &nf_conntrack_htable_size, 0600);
1806
f94161c1 1807int nf_conntrack_init_start(void)
9fb9cbb1 1808{
f205c5e0 1809 int max_factor = 8;
0c5366b3 1810 int ret = -ENOMEM;
cc41c84b 1811 int i;
93bb0ceb 1812
a3efd812
FW
1813 seqcount_init(&nf_conntrack_generation);
1814
d5d20912 1815 for (i = 0; i < CONNTRACK_LOCKS; i++)
93bb0ceb 1816 spin_lock_init(&nf_conntrack_locks[i]);
9fb9cbb1 1817
9fb9cbb1 1818 if (!nf_conntrack_htable_size) {
88eab472
ML
1819 /* Idea from tcp.c: use 1/16384 of memory.
1820 * On i386: 32MB machine has 512 buckets.
1821 * >= 1GB machines have 16384 buckets.
1822 * >= 4GB machines have 65536 buckets.
1823 */
9fb9cbb1 1824 nf_conntrack_htable_size
4481374c 1825 = (((totalram_pages << PAGE_SHIFT) / 16384)
f205c5e0 1826 / sizeof(struct hlist_head));
88eab472
ML
1827 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1828 nf_conntrack_htable_size = 65536;
1829 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
f205c5e0
PM
1830 nf_conntrack_htable_size = 16384;
1831 if (nf_conntrack_htable_size < 32)
1832 nf_conntrack_htable_size = 32;
1833
1834 /* Use a max. factor of four by default to get the same max as
1835 * with the old struct list_heads. When a table size is given
1836 * we use the old value of 8 to avoid reducing the max.
1837 * entries. */
1838 max_factor = 4;
9fb9cbb1 1839 }
56d52d48
FW
1840
1841 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1842 if (!nf_conntrack_hash)
1843 return -ENOMEM;
1844
f205c5e0 1845 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
8e5105a0 1846
0c5366b3 1847 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
a9e419dc
FW
1848 sizeof(struct nf_conn),
1849 NFCT_INFOMASK + 1,
5a75cdeb 1850 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
0c5366b3
FW
1851 if (!nf_conntrack_cachep)
1852 goto err_cachep;
1853
654d0fbd 1854 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
8e5105a0
PM
1855 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1856 nf_conntrack_max);
83b4dbe1
G
1857
1858 ret = nf_conntrack_expect_init();
1859 if (ret < 0)
1860 goto err_expect;
1861
b7ff3a1f
G
1862 ret = nf_conntrack_acct_init();
1863 if (ret < 0)
1864 goto err_acct;
1865
73f4001a
G
1866 ret = nf_conntrack_tstamp_init();
1867 if (ret < 0)
1868 goto err_tstamp;
1869
3fe0f943
G
1870 ret = nf_conntrack_ecache_init();
1871 if (ret < 0)
1872 goto err_ecache;
1873
8684094c
G
1874 ret = nf_conntrack_timeout_init();
1875 if (ret < 0)
1876 goto err_timeout;
1877
5e615b22
G
1878 ret = nf_conntrack_helper_init();
1879 if (ret < 0)
1880 goto err_helper;
1881
5f69b8f5
G
1882 ret = nf_conntrack_labels_init();
1883 if (ret < 0)
1884 goto err_labels;
1885
41d73ec0
PM
1886 ret = nf_conntrack_seqadj_init();
1887 if (ret < 0)
1888 goto err_seqadj;
1889
04d87001
G
1890 ret = nf_conntrack_proto_init();
1891 if (ret < 0)
1892 goto err_proto;
1893
b87a2f91 1894 conntrack_gc_work_init(&conntrack_gc_work);
e5072053 1895 queue_delayed_work(system_long_wq, &conntrack_gc_work.dwork, HZ);
b87a2f91 1896
08f6547d
AD
1897 return 0;
1898
04d87001 1899err_proto:
41d73ec0
PM
1900 nf_conntrack_seqadj_fini();
1901err_seqadj:
04d87001 1902 nf_conntrack_labels_fini();
5f69b8f5
G
1903err_labels:
1904 nf_conntrack_helper_fini();
5e615b22
G
1905err_helper:
1906 nf_conntrack_timeout_fini();
8684094c
G
1907err_timeout:
1908 nf_conntrack_ecache_fini();
3fe0f943
G
1909err_ecache:
1910 nf_conntrack_tstamp_fini();
73f4001a
G
1911err_tstamp:
1912 nf_conntrack_acct_fini();
b7ff3a1f
G
1913err_acct:
1914 nf_conntrack_expect_fini();
83b4dbe1 1915err_expect:
0c5366b3
FW
1916 kmem_cache_destroy(nf_conntrack_cachep);
1917err_cachep:
56d52d48 1918 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
08f6547d
AD
1919 return ret;
1920}
1921
f94161c1
G
1922void nf_conntrack_init_end(void)
1923{
1924 /* For use by REJECT target */
1925 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1926 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
f94161c1
G
1927}
1928
8cc20198
ED
1929/*
1930 * We need to use special "null" values, not used in hash table
1931 */
1932#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1933#define DYING_NULLS_VAL ((1<<30)+1)
252b3e8c 1934#define TEMPLATE_NULLS_VAL ((1<<30)+2)
8cc20198 1935
f94161c1 1936int nf_conntrack_init_net(struct net *net)
08f6547d 1937{
b7779d06
JDB
1938 int ret = -ENOMEM;
1939 int cpu;
ceceae1b 1940
cc41c84b 1941 BUILD_BUG_ON(IP_CT_UNTRACKED == IP_CT_NUMBER);
08f6547d 1942 atomic_set(&net->ct.count, 0);
b7779d06
JDB
1943
1944 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1945 if (!net->ct.pcpu_lists)
08f6547d 1946 goto err_stat;
b7779d06
JDB
1947
1948 for_each_possible_cpu(cpu) {
1949 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1950
1951 spin_lock_init(&pcpu->lock);
1952 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1953 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
08f6547d 1954 }
5b3501fa 1955
b7779d06
JDB
1956 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1957 if (!net->ct.stat)
1958 goto err_pcpu_lists;
1959
83b4dbe1 1960 ret = nf_conntrack_expect_pernet_init(net);
08f6547d
AD
1961 if (ret < 0)
1962 goto err_expect;
b7ff3a1f 1963 ret = nf_conntrack_acct_pernet_init(net);
58401572 1964 if (ret < 0)
08f6547d 1965 goto err_acct;
73f4001a 1966 ret = nf_conntrack_tstamp_pernet_init(net);
a992ca2a
PNA
1967 if (ret < 0)
1968 goto err_tstamp;
3fe0f943 1969 ret = nf_conntrack_ecache_pernet_init(net);
a0891aa6
PNA
1970 if (ret < 0)
1971 goto err_ecache;
5e615b22 1972 ret = nf_conntrack_helper_pernet_init(net);
a9006892
EL
1973 if (ret < 0)
1974 goto err_helper;
04d87001 1975 ret = nf_conntrack_proto_pernet_init(net);
f94161c1
G
1976 if (ret < 0)
1977 goto err_proto;
08f6547d 1978 return 0;
c539f017 1979
f94161c1 1980err_proto:
5e615b22 1981 nf_conntrack_helper_pernet_fini(net);
a9006892 1982err_helper:
3fe0f943 1983 nf_conntrack_ecache_pernet_fini(net);
a0891aa6 1984err_ecache:
73f4001a 1985 nf_conntrack_tstamp_pernet_fini(net);
a992ca2a 1986err_tstamp:
b7ff3a1f 1987 nf_conntrack_acct_pernet_fini(net);
08f6547d 1988err_acct:
83b4dbe1 1989 nf_conntrack_expect_pernet_fini(net);
08f6547d 1990err_expect:
0d55af87 1991 free_percpu(net->ct.stat);
b7779d06
JDB
1992err_pcpu_lists:
1993 free_percpu(net->ct.pcpu_lists);
0d55af87 1994err_stat:
08f6547d
AD
1995 return ret;
1996}