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