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