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