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