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