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