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