]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nf_conntrack_core.c
[NETFILTER]: More __read_mostly annotations
[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
YK
7 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
14 * - new API and handling of conntrack/nat helpers
15 * - now capable of multiple expectations for one master
16 * 16 Jul 2002: Harald Welte <laforge@gnumonks.org>
17 * - add usage/reference counts to ip_conntrack_expect
18 * - export ip_conntrack[_expect]_{find_get,put} functions
19 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
20 * - generalize L3 protocol denendent part.
21 * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
22 * - add support various size of conntrack structures.
dc808fe2
HW
23 * 26 Jan 2006: Harald Welte <laforge@netfilter.org>
24 * - restructure nf_conn (introduce nf_conn_help)
25 * - redesign 'features' how they were originally intended
b9f78f9f
PNA
26 * 26 Feb 2006: Pablo Neira Ayuso <pablo@eurodev.net>
27 * - add support for L3 protocol module load on demand.
9fb9cbb1
YK
28 *
29 * Derived from net/ipv4/netfilter/ip_conntrack_core.c
30 */
31
9fb9cbb1
YK
32#include <linux/types.h>
33#include <linux/netfilter.h>
34#include <linux/module.h>
35#include <linux/skbuff.h>
36#include <linux/proc_fs.h>
37#include <linux/vmalloc.h>
38#include <linux/stddef.h>
39#include <linux/slab.h>
40#include <linux/random.h>
41#include <linux/jhash.h>
42#include <linux/err.h>
43#include <linux/percpu.h>
44#include <linux/moduleparam.h>
45#include <linux/notifier.h>
46#include <linux/kernel.h>
47#include <linux/netdevice.h>
48#include <linux/socket.h>
49
50/* This rwlock protects the main hash table, protocol/helper/expected
51 registrations, conntrack timers*/
52#define ASSERT_READ_LOCK(x)
53#define ASSERT_WRITE_LOCK(x)
54
55#include <net/netfilter/nf_conntrack.h>
56#include <net/netfilter/nf_conntrack_l3proto.h>
57#include <net/netfilter/nf_conntrack_protocol.h>
77ab9cff 58#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1
YK
59#include <net/netfilter/nf_conntrack_helper.h>
60#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 61
dc808fe2 62#define NF_CONNTRACK_VERSION "0.5.0"
9fb9cbb1
YK
63
64#if 0
65#define DEBUGP printk
66#else
67#define DEBUGP(format, args...)
68#endif
69
70DEFINE_RWLOCK(nf_conntrack_lock);
71
72/* nf_conntrack_standalone needs this */
73atomic_t nf_conntrack_count = ATOMIC_INIT(0);
74
75void (*nf_conntrack_destroyed)(struct nf_conn *conntrack) = NULL;
e2b7606c 76unsigned int nf_conntrack_htable_size __read_mostly;
94aec08e 77int nf_conntrack_max __read_mostly;
1192e403 78struct list_head *nf_conntrack_hash __read_mostly;
e2b7606c 79struct nf_conn nf_conntrack_untracked __read_mostly;
94aec08e 80unsigned int nf_ct_log_invalid __read_mostly;
7e5d03bb 81LIST_HEAD(unconfirmed);
1192e403 82static int nf_conntrack_vmalloc __read_mostly;
9fb9cbb1 83
4e3882f7 84static unsigned int nf_conntrack_next_id;
77ab9cff 85
9fb9cbb1
YK
86DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
87EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
88
89/*
90 * This scheme offers various size of "struct nf_conn" dependent on
91 * features(helper, nat, ...)
92 */
93
94#define NF_CT_FEATURES_NAMELEN 256
95static struct {
96 /* name of slab cache. printed in /proc/slabinfo */
97 char *name;
98
99 /* size of slab cache */
100 size_t size;
101
102 /* slab cache pointer */
103 kmem_cache_t *cachep;
104
105 /* allocated slab cache + modules which uses this slab cache */
106 int use;
107
9fb9cbb1
YK
108} nf_ct_cache[NF_CT_F_NUM];
109
110/* protect members of nf_ct_cache except of "use" */
111DEFINE_RWLOCK(nf_ct_cache_lock);
112
113/* This avoids calling kmem_cache_create() with same name simultaneously */
57b47a53 114static DEFINE_MUTEX(nf_ct_cache_mutex);
9fb9cbb1 115
9fb9cbb1
YK
116static int nf_conntrack_hash_rnd_initted;
117static unsigned int nf_conntrack_hash_rnd;
118
119static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
120 unsigned int size, unsigned int rnd)
121{
122 unsigned int a, b;
123 a = jhash((void *)tuple->src.u3.all, sizeof(tuple->src.u3.all),
124 ((tuple->src.l3num) << 16) | tuple->dst.protonum);
125 b = jhash((void *)tuple->dst.u3.all, sizeof(tuple->dst.u3.all),
126 (tuple->src.u.all << 16) | tuple->dst.u.all);
127
128 return jhash_2words(a, b, rnd) % size;
129}
130
131static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
132{
133 return __hash_conntrack(tuple, nf_conntrack_htable_size,
134 nf_conntrack_hash_rnd);
135}
136
9fb9cbb1 137int nf_conntrack_register_cache(u_int32_t features, const char *name,
dc808fe2 138 size_t size)
9fb9cbb1
YK
139{
140 int ret = 0;
141 char *cache_name;
142 kmem_cache_t *cachep;
143
144 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
145 features, name, size);
146
147 if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) {
148 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
149 features);
150 return -EINVAL;
151 }
152
57b47a53 153 mutex_lock(&nf_ct_cache_mutex);
9fb9cbb1
YK
154
155 write_lock_bh(&nf_ct_cache_lock);
156 /* e.g: multiple helpers are loaded */
157 if (nf_ct_cache[features].use > 0) {
158 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
159 if ((!strncmp(nf_ct_cache[features].name, name,
160 NF_CT_FEATURES_NAMELEN))
dc808fe2 161 && nf_ct_cache[features].size == size) {
9fb9cbb1
YK
162 DEBUGP("nf_conntrack_register_cache: reusing.\n");
163 nf_ct_cache[features].use++;
164 ret = 0;
165 } else
166 ret = -EBUSY;
167
168 write_unlock_bh(&nf_ct_cache_lock);
57b47a53 169 mutex_unlock(&nf_ct_cache_mutex);
9fb9cbb1
YK
170 return ret;
171 }
172 write_unlock_bh(&nf_ct_cache_lock);
173
174 /*
175 * The memory space for name of slab cache must be alive until
176 * cache is destroyed.
177 */
178 cache_name = kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN, GFP_ATOMIC);
179 if (cache_name == NULL) {
180 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
181 ret = -ENOMEM;
182 goto out_up_mutex;
183 }
184
185 if (strlcpy(cache_name, name, NF_CT_FEATURES_NAMELEN)
186 >= NF_CT_FEATURES_NAMELEN) {
187 printk("nf_conntrack_register_cache: name too long\n");
188 ret = -EINVAL;
189 goto out_free_name;
190 }
191
192 cachep = kmem_cache_create(cache_name, size, 0, 0,
193 NULL, NULL);
194 if (!cachep) {
195 printk("nf_conntrack_register_cache: Can't create slab cache "
196 "for the features = 0x%x\n", features);
197 ret = -ENOMEM;
198 goto out_free_name;
199 }
200
201 write_lock_bh(&nf_ct_cache_lock);
202 nf_ct_cache[features].use = 1;
203 nf_ct_cache[features].size = size;
9fb9cbb1
YK
204 nf_ct_cache[features].cachep = cachep;
205 nf_ct_cache[features].name = cache_name;
206 write_unlock_bh(&nf_ct_cache_lock);
207
208 goto out_up_mutex;
209
210out_free_name:
211 kfree(cache_name);
212out_up_mutex:
57b47a53 213 mutex_unlock(&nf_ct_cache_mutex);
9fb9cbb1
YK
214 return ret;
215}
216
217/* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
218void nf_conntrack_unregister_cache(u_int32_t features)
219{
220 kmem_cache_t *cachep;
221 char *name;
222
223 /*
224 * This assures that kmem_cache_create() isn't called before destroying
225 * slab cache.
226 */
227 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features);
57b47a53 228 mutex_lock(&nf_ct_cache_mutex);
9fb9cbb1
YK
229
230 write_lock_bh(&nf_ct_cache_lock);
231 if (--nf_ct_cache[features].use > 0) {
232 write_unlock_bh(&nf_ct_cache_lock);
57b47a53 233 mutex_unlock(&nf_ct_cache_mutex);
9fb9cbb1
YK
234 return;
235 }
236 cachep = nf_ct_cache[features].cachep;
237 name = nf_ct_cache[features].name;
238 nf_ct_cache[features].cachep = NULL;
239 nf_ct_cache[features].name = NULL;
9fb9cbb1
YK
240 nf_ct_cache[features].size = 0;
241 write_unlock_bh(&nf_ct_cache_lock);
242
243 synchronize_net();
244
245 kmem_cache_destroy(cachep);
246 kfree(name);
247
57b47a53 248 mutex_unlock(&nf_ct_cache_mutex);
9fb9cbb1
YK
249}
250
251int
252nf_ct_get_tuple(const struct sk_buff *skb,
253 unsigned int nhoff,
254 unsigned int dataoff,
255 u_int16_t l3num,
256 u_int8_t protonum,
257 struct nf_conntrack_tuple *tuple,
258 const struct nf_conntrack_l3proto *l3proto,
259 const struct nf_conntrack_protocol *protocol)
260{
261 NF_CT_TUPLE_U_BLANK(tuple);
262
263 tuple->src.l3num = l3num;
264 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
265 return 0;
266
267 tuple->dst.protonum = protonum;
268 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
269
270 return protocol->pkt_to_tuple(skb, dataoff, tuple);
271}
272
273int
274nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
275 const struct nf_conntrack_tuple *orig,
276 const struct nf_conntrack_l3proto *l3proto,
277 const struct nf_conntrack_protocol *protocol)
278{
279 NF_CT_TUPLE_U_BLANK(inverse);
280
281 inverse->src.l3num = orig->src.l3num;
282 if (l3proto->invert_tuple(inverse, orig) == 0)
283 return 0;
284
285 inverse->dst.dir = !orig->dst.dir;
286
287 inverse->dst.protonum = orig->dst.protonum;
288 return protocol->invert_tuple(inverse, orig);
289}
290
9fb9cbb1
YK
291static void
292clean_from_lists(struct nf_conn *ct)
293{
9fb9cbb1
YK
294 DEBUGP("clean_from_lists(%p)\n", ct);
295 ASSERT_WRITE_LOCK(&nf_conntrack_lock);
df0933dc
PM
296 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
297 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
9fb9cbb1
YK
298
299 /* Destroy all pending expectations */
c1d10adb 300 nf_ct_remove_expectations(ct);
9fb9cbb1
YK
301}
302
303static void
304destroy_conntrack(struct nf_conntrack *nfct)
305{
306 struct nf_conn *ct = (struct nf_conn *)nfct;
307 struct nf_conntrack_l3proto *l3proto;
308 struct nf_conntrack_protocol *proto;
309
310 DEBUGP("destroy_conntrack(%p)\n", ct);
311 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
312 NF_CT_ASSERT(!timer_pending(&ct->timeout));
313
314 nf_conntrack_event(IPCT_DESTROY, ct);
315 set_bit(IPS_DYING_BIT, &ct->status);
316
317 /* To make sure we don't get any weird locking issues here:
318 * destroy_conntrack() MUST NOT be called with a write lock
319 * to nf_conntrack_lock!!! -HW */
c1d10adb 320 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num);
9fb9cbb1
YK
321 if (l3proto && l3proto->destroy)
322 l3proto->destroy(ct);
323
c1d10adb 324 proto = __nf_ct_proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
9fb9cbb1
YK
325 if (proto && proto->destroy)
326 proto->destroy(ct);
327
328 if (nf_conntrack_destroyed)
329 nf_conntrack_destroyed(ct);
330
331 write_lock_bh(&nf_conntrack_lock);
332 /* Expectations will have been removed in clean_from_lists,
333 * except TFTP can create an expectation on the first packet,
334 * before connection is in the list, so we need to clean here,
335 * too. */
c1d10adb 336 nf_ct_remove_expectations(ct);
9fb9cbb1
YK
337
338 /* We overload first tuple to link into unconfirmed list. */
339 if (!nf_ct_is_confirmed(ct)) {
340 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
341 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
342 }
343
344 NF_CT_STAT_INC(delete);
345 write_unlock_bh(&nf_conntrack_lock);
346
347 if (ct->master)
348 nf_ct_put(ct->master);
349
350 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
351 nf_conntrack_free(ct);
352}
353
354static void death_by_timeout(unsigned long ul_conntrack)
355{
356 struct nf_conn *ct = (void *)ul_conntrack;
357
358 write_lock_bh(&nf_conntrack_lock);
359 /* Inside lock so preempt is disabled on module removal path.
360 * Otherwise we can get spurious warnings. */
361 NF_CT_STAT_INC(delete_list);
362 clean_from_lists(ct);
363 write_unlock_bh(&nf_conntrack_lock);
364 nf_ct_put(ct);
365}
366
c1d10adb 367struct nf_conntrack_tuple_hash *
9fb9cbb1
YK
368__nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
369 const struct nf_conn *ignored_conntrack)
370{
371 struct nf_conntrack_tuple_hash *h;
372 unsigned int hash = hash_conntrack(tuple);
373
374 ASSERT_READ_LOCK(&nf_conntrack_lock);
375 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
df0933dc
PM
376 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
377 nf_ct_tuple_equal(tuple, &h->tuple)) {
9fb9cbb1
YK
378 NF_CT_STAT_INC(found);
379 return h;
380 }
381 NF_CT_STAT_INC(searched);
382 }
383
384 return NULL;
385}
386
387/* Find a connection corresponding to a tuple. */
388struct nf_conntrack_tuple_hash *
389nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
390 const struct nf_conn *ignored_conntrack)
391{
392 struct nf_conntrack_tuple_hash *h;
393
394 read_lock_bh(&nf_conntrack_lock);
395 h = __nf_conntrack_find(tuple, ignored_conntrack);
396 if (h)
397 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
398 read_unlock_bh(&nf_conntrack_lock);
399
400 return h;
401}
402
c1d10adb
PNA
403static void __nf_conntrack_hash_insert(struct nf_conn *ct,
404 unsigned int hash,
405 unsigned int repl_hash)
406{
407 ct->id = ++nf_conntrack_next_id;
df0933dc
PM
408 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
409 &nf_conntrack_hash[hash]);
410 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
411 &nf_conntrack_hash[repl_hash]);
c1d10adb
PNA
412}
413
414void nf_conntrack_hash_insert(struct nf_conn *ct)
415{
416 unsigned int hash, repl_hash;
417
418 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
419 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
420
421 write_lock_bh(&nf_conntrack_lock);
422 __nf_conntrack_hash_insert(ct, hash, repl_hash);
423 write_unlock_bh(&nf_conntrack_lock);
424}
425
9fb9cbb1
YK
426/* Confirm a connection given skb; places it in hash table */
427int
428__nf_conntrack_confirm(struct sk_buff **pskb)
429{
430 unsigned int hash, repl_hash;
df0933dc 431 struct nf_conntrack_tuple_hash *h;
9fb9cbb1 432 struct nf_conn *ct;
df0933dc 433 struct nf_conn_help *help;
9fb9cbb1
YK
434 enum ip_conntrack_info ctinfo;
435
436 ct = nf_ct_get(*pskb, &ctinfo);
437
438 /* ipt_REJECT uses nf_conntrack_attach to attach related
439 ICMP/TCP RST packets in other direction. Actual packet
440 which created connection will be IP_CT_NEW or for an
441 expected connection, IP_CT_RELATED. */
442 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
443 return NF_ACCEPT;
444
445 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
446 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
447
448 /* We're not in hash table, and we refuse to set up related
449 connections for unconfirmed conns. But packet copies and
450 REJECT will give spurious warnings here. */
451 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
452
453 /* No external references means noone else could have
454 confirmed us. */
455 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
456 DEBUGP("Confirming conntrack %p\n", ct);
457
458 write_lock_bh(&nf_conntrack_lock);
459
460 /* See if there's one in the list already, including reverse:
461 NAT could have grabbed it without realizing, since we're
462 not in the hash. If there is, we lost race. */
df0933dc
PM
463 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
464 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
465 &h->tuple))
466 goto out;
467 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
468 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
469 &h->tuple))
470 goto out;
9fb9cbb1 471
df0933dc
PM
472 /* Remove from unconfirmed list */
473 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
474
475 __nf_conntrack_hash_insert(ct, hash, repl_hash);
476 /* Timer relative to confirmation time, not original
477 setting time, otherwise we'd get timer wrap in
478 weird delay cases. */
479 ct->timeout.expires += jiffies;
480 add_timer(&ct->timeout);
481 atomic_inc(&ct->ct_general.use);
482 set_bit(IPS_CONFIRMED_BIT, &ct->status);
483 NF_CT_STAT_INC(insert);
484 write_unlock_bh(&nf_conntrack_lock);
485 help = nfct_help(ct);
486 if (help && help->helper)
487 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
9fb9cbb1 488#ifdef CONFIG_NF_NAT_NEEDED
df0933dc
PM
489 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
490 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
491 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
9fb9cbb1 492#endif
df0933dc
PM
493 nf_conntrack_event_cache(master_ct(ct) ?
494 IPCT_RELATED : IPCT_NEW, *pskb);
495 return NF_ACCEPT;
9fb9cbb1 496
df0933dc 497out:
9fb9cbb1
YK
498 NF_CT_STAT_INC(insert_failed);
499 write_unlock_bh(&nf_conntrack_lock);
500 return NF_DROP;
501}
502
503/* Returns true if a connection correspondings to the tuple (required
504 for NAT). */
505int
506nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
507 const struct nf_conn *ignored_conntrack)
508{
509 struct nf_conntrack_tuple_hash *h;
510
511 read_lock_bh(&nf_conntrack_lock);
512 h = __nf_conntrack_find(tuple, ignored_conntrack);
513 read_unlock_bh(&nf_conntrack_lock);
514
515 return h != NULL;
516}
517
518/* There's a small race here where we may free a just-assured
519 connection. Too bad: we're in trouble anyway. */
9fb9cbb1
YK
520static int early_drop(struct list_head *chain)
521{
522 /* Traverse backwards: gives us oldest, which is roughly LRU */
523 struct nf_conntrack_tuple_hash *h;
df0933dc 524 struct nf_conn *ct = NULL, *tmp;
9fb9cbb1
YK
525 int dropped = 0;
526
527 read_lock_bh(&nf_conntrack_lock);
df0933dc
PM
528 list_for_each_entry_reverse(h, chain, list) {
529 tmp = nf_ct_tuplehash_to_ctrack(h);
530 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
531 ct = tmp;
532 atomic_inc(&ct->ct_general.use);
533 break;
534 }
9fb9cbb1
YK
535 }
536 read_unlock_bh(&nf_conntrack_lock);
537
538 if (!ct)
539 return dropped;
540
541 if (del_timer(&ct->timeout)) {
542 death_by_timeout((unsigned long)ct);
543 dropped = 1;
544 NF_CT_STAT_INC(early_drop);
545 }
546 nf_ct_put(ct);
547 return dropped;
548}
549
9fb9cbb1
YK
550static struct nf_conn *
551__nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
552 const struct nf_conntrack_tuple *repl,
553 const struct nf_conntrack_l3proto *l3proto)
554{
555 struct nf_conn *conntrack = NULL;
556 u_int32_t features = 0;
dc808fe2 557 struct nf_conntrack_helper *helper;
9fb9cbb1 558
dc808fe2 559 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
9fb9cbb1
YK
560 get_random_bytes(&nf_conntrack_hash_rnd, 4);
561 nf_conntrack_hash_rnd_initted = 1;
562 }
563
5251e2d2
PNA
564 /* We don't want any race condition at early drop stage */
565 atomic_inc(&nf_conntrack_count);
566
9fb9cbb1 567 if (nf_conntrack_max
5251e2d2 568 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
9fb9cbb1
YK
569 unsigned int hash = hash_conntrack(orig);
570 /* Try dropping from this hash chain. */
571 if (!early_drop(&nf_conntrack_hash[hash])) {
5251e2d2 572 atomic_dec(&nf_conntrack_count);
9fb9cbb1
YK
573 if (net_ratelimit())
574 printk(KERN_WARNING
575 "nf_conntrack: table full, dropping"
576 " packet.\n");
577 return ERR_PTR(-ENOMEM);
578 }
579 }
580
581 /* find features needed by this conntrack. */
582 features = l3proto->get_features(orig);
dc808fe2
HW
583
584 /* FIXME: protect helper list per RCU */
9fb9cbb1 585 read_lock_bh(&nf_conntrack_lock);
dc808fe2
HW
586 helper = __nf_ct_helper_find(repl);
587 if (helper)
9fb9cbb1
YK
588 features |= NF_CT_F_HELP;
589 read_unlock_bh(&nf_conntrack_lock);
590
591 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
592
593 read_lock_bh(&nf_ct_cache_lock);
594
dc808fe2 595 if (unlikely(!nf_ct_cache[features].use)) {
9fb9cbb1
YK
596 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
597 features);
598 goto out;
599 }
600
601 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
602 if (conntrack == NULL) {
603 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
604 goto out;
605 }
606
607 memset(conntrack, 0, nf_ct_cache[features].size);
608 conntrack->features = features;
9fb9cbb1
YK
609 atomic_set(&conntrack->ct_general.use, 1);
610 conntrack->ct_general.destroy = destroy_conntrack;
611 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
612 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
613 /* Don't set timer yet: wait for confirmation */
614 init_timer(&conntrack->timeout);
615 conntrack->timeout.data = (unsigned long)conntrack;
616 conntrack->timeout.function = death_by_timeout;
5251e2d2 617 read_unlock_bh(&nf_ct_cache_lock);
9fb9cbb1 618
5251e2d2 619 return conntrack;
9fb9cbb1
YK
620out:
621 read_unlock_bh(&nf_ct_cache_lock);
5251e2d2 622 atomic_dec(&nf_conntrack_count);
9fb9cbb1
YK
623 return conntrack;
624}
625
626struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
627 const struct nf_conntrack_tuple *repl)
628{
629 struct nf_conntrack_l3proto *l3proto;
630
c1d10adb 631 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
9fb9cbb1
YK
632 return __nf_conntrack_alloc(orig, repl, l3proto);
633}
634
635void nf_conntrack_free(struct nf_conn *conntrack)
636{
637 u_int32_t features = conntrack->features;
638 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
639 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
640 conntrack);
641 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
642 atomic_dec(&nf_conntrack_count);
643}
644
645/* Allocate a new conntrack: we return -ENOMEM if classification
646 failed due to stress. Otherwise it really is unclassifiable. */
647static struct nf_conntrack_tuple_hash *
648init_conntrack(const struct nf_conntrack_tuple *tuple,
649 struct nf_conntrack_l3proto *l3proto,
650 struct nf_conntrack_protocol *protocol,
651 struct sk_buff *skb,
652 unsigned int dataoff)
653{
654 struct nf_conn *conntrack;
655 struct nf_conntrack_tuple repl_tuple;
656 struct nf_conntrack_expect *exp;
657
658 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, protocol)) {
659 DEBUGP("Can't invert tuple.\n");
660 return NULL;
661 }
662
663 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto);
664 if (conntrack == NULL || IS_ERR(conntrack)) {
665 DEBUGP("Can't allocate conntrack.\n");
666 return (struct nf_conntrack_tuple_hash *)conntrack;
667 }
668
669 if (!protocol->new(conntrack, skb, dataoff)) {
670 nf_conntrack_free(conntrack);
671 DEBUGP("init conntrack: can't track with proto module\n");
672 return NULL;
673 }
674
675 write_lock_bh(&nf_conntrack_lock);
676 exp = find_expectation(tuple);
677
678 if (exp) {
679 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
680 conntrack, exp);
681 /* Welcome, Mr. Bond. We've been expecting you... */
682 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
683 conntrack->master = exp->master;
684#ifdef CONFIG_NF_CONNTRACK_MARK
685 conntrack->mark = exp->master->mark;
7c9728c3
JM
686#endif
687#ifdef CONFIG_NF_CONNTRACK_SECMARK
688 conntrack->secmark = exp->master->secmark;
9fb9cbb1
YK
689#endif
690 nf_conntrack_get(&conntrack->master->ct_general);
691 NF_CT_STAT_INC(expect_new);
22e7410b
YK
692 } else {
693 struct nf_conn_help *help = nfct_help(conntrack);
694
695 if (help)
696 help->helper = __nf_ct_helper_find(&repl_tuple);
9fb9cbb1 697 NF_CT_STAT_INC(new);
22e7410b 698 }
9fb9cbb1
YK
699
700 /* Overload tuple linked list to put us in unconfirmed list. */
701 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
702
703 write_unlock_bh(&nf_conntrack_lock);
704
705 if (exp) {
706 if (exp->expectfn)
707 exp->expectfn(conntrack, exp);
708 nf_conntrack_expect_put(exp);
709 }
710
711 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
712}
713
714/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
715static inline struct nf_conn *
716resolve_normal_ct(struct sk_buff *skb,
717 unsigned int dataoff,
718 u_int16_t l3num,
719 u_int8_t protonum,
720 struct nf_conntrack_l3proto *l3proto,
721 struct nf_conntrack_protocol *proto,
722 int *set_reply,
723 enum ip_conntrack_info *ctinfo)
724{
725 struct nf_conntrack_tuple tuple;
726 struct nf_conntrack_tuple_hash *h;
727 struct nf_conn *ct;
728
729 if (!nf_ct_get_tuple(skb, (unsigned int)(skb->nh.raw - skb->data),
730 dataoff, l3num, protonum, &tuple, l3proto,
731 proto)) {
732 DEBUGP("resolve_normal_ct: Can't get tuple\n");
733 return NULL;
734 }
735
736 /* look for tuple match */
737 h = nf_conntrack_find_get(&tuple, NULL);
738 if (!h) {
739 h = init_conntrack(&tuple, l3proto, proto, skb, dataoff);
740 if (!h)
741 return NULL;
742 if (IS_ERR(h))
743 return (void *)h;
744 }
745 ct = nf_ct_tuplehash_to_ctrack(h);
746
747 /* It exists; we have (non-exclusive) reference. */
748 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
749 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
750 /* Please set reply bit if this packet OK */
751 *set_reply = 1;
752 } else {
753 /* Once we've had two way comms, always ESTABLISHED. */
754 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
755 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
756 *ctinfo = IP_CT_ESTABLISHED;
757 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
758 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
759 *ctinfo = IP_CT_RELATED;
760 } else {
761 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
762 *ctinfo = IP_CT_NEW;
763 }
764 *set_reply = 0;
765 }
766 skb->nfct = &ct->ct_general;
767 skb->nfctinfo = *ctinfo;
768 return ct;
769}
770
771unsigned int
772nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
773{
774 struct nf_conn *ct;
775 enum ip_conntrack_info ctinfo;
776 struct nf_conntrack_l3proto *l3proto;
777 struct nf_conntrack_protocol *proto;
778 unsigned int dataoff;
779 u_int8_t protonum;
780 int set_reply = 0;
781 int ret;
782
783 /* Previously seen (loopback or untracked)? Ignore. */
784 if ((*pskb)->nfct) {
785 NF_CT_STAT_INC(ignore);
786 return NF_ACCEPT;
787 }
788
c1d10adb 789 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
9fb9cbb1
YK
790 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
791 DEBUGP("not prepared to track yet or error occured\n");
792 return -ret;
793 }
794
c1d10adb 795 proto = __nf_ct_proto_find((u_int16_t)pf, protonum);
9fb9cbb1
YK
796
797 /* It may be an special packet, error, unclean...
798 * inverse of the return code tells to the netfilter
799 * core what to do with the packet. */
800 if (proto->error != NULL &&
801 (ret = proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
802 NF_CT_STAT_INC(error);
803 NF_CT_STAT_INC(invalid);
804 return -ret;
805 }
806
807 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, proto,
808 &set_reply, &ctinfo);
809 if (!ct) {
810 /* Not valid part of a connection */
811 NF_CT_STAT_INC(invalid);
812 return NF_ACCEPT;
813 }
814
815 if (IS_ERR(ct)) {
816 /* Too stressed to deal. */
817 NF_CT_STAT_INC(drop);
818 return NF_DROP;
819 }
820
821 NF_CT_ASSERT((*pskb)->nfct);
822
823 ret = proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
824 if (ret < 0) {
825 /* Invalid: inverse of the return code tells
826 * the netfilter core what to do */
827 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
828 nf_conntrack_put((*pskb)->nfct);
829 (*pskb)->nfct = NULL;
830 NF_CT_STAT_INC(invalid);
831 return -ret;
832 }
833
834 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
835 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
836
837 return ret;
838}
839
840int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
841 const struct nf_conntrack_tuple *orig)
842{
843 return nf_ct_invert_tuple(inverse, orig,
c1d10adb
PNA
844 __nf_ct_l3proto_find(orig->src.l3num),
845 __nf_ct_proto_find(orig->src.l3num,
846 orig->dst.protonum));
9fb9cbb1
YK
847}
848
9fb9cbb1
YK
849/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
850void __nf_ct_refresh_acct(struct nf_conn *ct,
851 enum ip_conntrack_info ctinfo,
852 const struct sk_buff *skb,
853 unsigned long extra_jiffies,
854 int do_acct)
855{
856 int event = 0;
857
858 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
859 NF_CT_ASSERT(skb);
860
861 write_lock_bh(&nf_conntrack_lock);
862
997ae831
EL
863 /* Only update if this is not a fixed timeout */
864 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
865 write_unlock_bh(&nf_conntrack_lock);
866 return;
867 }
868
9fb9cbb1
YK
869 /* If not in hash table, timer will not be active yet */
870 if (!nf_ct_is_confirmed(ct)) {
871 ct->timeout.expires = extra_jiffies;
872 event = IPCT_REFRESH;
873 } else {
874 /* Need del_timer for race avoidance (may already be dying). */
875 if (del_timer(&ct->timeout)) {
876 ct->timeout.expires = jiffies + extra_jiffies;
877 add_timer(&ct->timeout);
878 event = IPCT_REFRESH;
879 }
880 }
881
882#ifdef CONFIG_NF_CT_ACCT
883 if (do_acct) {
884 ct->counters[CTINFO2DIR(ctinfo)].packets++;
885 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
886 skb->len - (unsigned int)(skb->nh.raw - skb->data);
887 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
888 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
889 event |= IPCT_COUNTER_FILLING;
890 }
891#endif
892
893 write_unlock_bh(&nf_conntrack_lock);
894
895 /* must be unlocked when calling event cache */
896 if (event)
897 nf_conntrack_event_cache(event, skb);
898}
899
c1d10adb
PNA
900#if defined(CONFIG_NF_CT_NETLINK) || \
901 defined(CONFIG_NF_CT_NETLINK_MODULE)
902
903#include <linux/netfilter/nfnetlink.h>
904#include <linux/netfilter/nfnetlink_conntrack.h>
57b47a53
IM
905#include <linux/mutex.h>
906
c1d10adb
PNA
907
908/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
909 * in ip_conntrack_core, since we don't want the protocols to autoload
910 * or depend on ctnetlink */
911int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
912 const struct nf_conntrack_tuple *tuple)
913{
914 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
915 &tuple->src.u.tcp.port);
916 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
917 &tuple->dst.u.tcp.port);
918 return 0;
919
920nfattr_failure:
921 return -1;
922}
923
924static const size_t cta_min_proto[CTA_PROTO_MAX] = {
925 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
926 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
927};
928
929int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
930 struct nf_conntrack_tuple *t)
931{
932 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
933 return -EINVAL;
934
935 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
936 return -EINVAL;
937
938 t->src.u.tcp.port =
939 *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
940 t->dst.u.tcp.port =
941 *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
942
943 return 0;
944}
945#endif
946
9fb9cbb1
YK
947/* Used by ipt_REJECT and ip6t_REJECT. */
948void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
949{
950 struct nf_conn *ct;
951 enum ip_conntrack_info ctinfo;
952
953 /* This ICMP is in reverse direction to the packet which caused it */
954 ct = nf_ct_get(skb, &ctinfo);
955 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
956 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
957 else
958 ctinfo = IP_CT_RELATED;
959
960 /* Attach to new skbuff, and increment count */
961 nskb->nfct = &ct->ct_general;
962 nskb->nfctinfo = ctinfo;
963 nf_conntrack_get(nskb->nfct);
964}
965
966static inline int
967do_iter(const struct nf_conntrack_tuple_hash *i,
968 int (*iter)(struct nf_conn *i, void *data),
969 void *data)
970{
971 return iter(nf_ct_tuplehash_to_ctrack(i), data);
972}
973
974/* Bring out ya dead! */
df0933dc 975static struct nf_conn *
9fb9cbb1
YK
976get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
977 void *data, unsigned int *bucket)
978{
df0933dc
PM
979 struct nf_conntrack_tuple_hash *h;
980 struct nf_conn *ct;
9fb9cbb1
YK
981
982 write_lock_bh(&nf_conntrack_lock);
983 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
df0933dc
PM
984 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
985 ct = nf_ct_tuplehash_to_ctrack(h);
986 if (iter(ct, data))
987 goto found;
988 }
9fb9cbb1 989 }
df0933dc
PM
990 list_for_each_entry(h, &unconfirmed, list) {
991 ct = nf_ct_tuplehash_to_ctrack(h);
992 if (iter(ct, data))
993 goto found;
994 }
c073e3fa 995 write_unlock_bh(&nf_conntrack_lock);
df0933dc
PM
996 return NULL;
997found:
c073e3fa 998 atomic_inc(&ct->ct_general.use);
9fb9cbb1 999 write_unlock_bh(&nf_conntrack_lock);
df0933dc 1000 return ct;
9fb9cbb1
YK
1001}
1002
1003void
1004nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1005{
df0933dc 1006 struct nf_conn *ct;
9fb9cbb1
YK
1007 unsigned int bucket = 0;
1008
df0933dc 1009 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
9fb9cbb1
YK
1010 /* Time to push up daises... */
1011 if (del_timer(&ct->timeout))
1012 death_by_timeout((unsigned long)ct);
1013 /* ... else the timer will get him soon. */
1014
1015 nf_ct_put(ct);
1016 }
1017}
1018
1019static int kill_all(struct nf_conn *i, void *data)
1020{
1021 return 1;
1022}
1023
1024static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1025{
1026 if (vmalloced)
1027 vfree(hash);
1028 else
1029 free_pages((unsigned long)hash,
1030 get_order(sizeof(struct list_head) * size));
1031}
1032
c1d10adb
PNA
1033void nf_conntrack_flush()
1034{
1035 nf_ct_iterate_cleanup(kill_all, NULL);
1036}
1037
9fb9cbb1
YK
1038/* Mishearing the voices in his head, our hero wonders how he's
1039 supposed to kill the mall. */
1040void nf_conntrack_cleanup(void)
1041{
1042 int i;
1043
7d3cdc6b
YK
1044 ip_ct_attach = NULL;
1045
9fb9cbb1
YK
1046 /* This makes sure all current packets have passed through
1047 netfilter framework. Roll on, two-stage module
1048 delete... */
1049 synchronize_net();
1050
1051 nf_ct_event_cache_flush();
1052 i_see_dead_people:
c1d10adb 1053 nf_conntrack_flush();
9fb9cbb1
YK
1054 if (atomic_read(&nf_conntrack_count) != 0) {
1055 schedule();
1056 goto i_see_dead_people;
1057 }
6636568c
PM
1058 /* wait until all references to nf_conntrack_untracked are dropped */
1059 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1060 schedule();
9fb9cbb1
YK
1061
1062 for (i = 0; i < NF_CT_F_NUM; i++) {
1063 if (nf_ct_cache[i].use == 0)
1064 continue;
1065
1066 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1067 nf_ct_cache[i].use = 1;
1068 nf_conntrack_unregister_cache(i);
1069 }
1070 kmem_cache_destroy(nf_conntrack_expect_cachep);
1071 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1072 nf_conntrack_htable_size);
5a6f294e
KK
1073
1074 /* free l3proto protocol tables */
1075 for (i = 0; i < PF_MAX; i++)
1076 if (nf_ct_protos[i]) {
1077 kfree(nf_ct_protos[i]);
1078 nf_ct_protos[i] = NULL;
1079 }
9fb9cbb1
YK
1080}
1081
1082static struct list_head *alloc_hashtable(int size, int *vmalloced)
1083{
1084 struct list_head *hash;
1085 unsigned int i;
1086
1087 *vmalloced = 0;
1088 hash = (void*)__get_free_pages(GFP_KERNEL,
1089 get_order(sizeof(struct list_head)
1090 * size));
1091 if (!hash) {
1092 *vmalloced = 1;
1093 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1094 hash = vmalloc(sizeof(struct list_head) * size);
1095 }
1096
1097 if (hash)
1098 for (i = 0; i < size; i++)
1099 INIT_LIST_HEAD(&hash[i]);
1100
1101 return hash;
1102}
1103
1104int set_hashsize(const char *val, struct kernel_param *kp)
1105{
1106 int i, bucket, hashsize, vmalloced;
1107 int old_vmalloced, old_size;
1108 int rnd;
1109 struct list_head *hash, *old_hash;
1110 struct nf_conntrack_tuple_hash *h;
1111
1112 /* On boot, we can set this without any fancy locking. */
1113 if (!nf_conntrack_htable_size)
1114 return param_set_uint(val, kp);
1115
1116 hashsize = simple_strtol(val, NULL, 0);
1117 if (!hashsize)
1118 return -EINVAL;
1119
1120 hash = alloc_hashtable(hashsize, &vmalloced);
1121 if (!hash)
1122 return -ENOMEM;
1123
1124 /* We have to rehahs for the new table anyway, so we also can
1125 * use a newrandom seed */
1126 get_random_bytes(&rnd, 4);
1127
1128 write_lock_bh(&nf_conntrack_lock);
1129 for (i = 0; i < nf_conntrack_htable_size; i++) {
1130 while (!list_empty(&nf_conntrack_hash[i])) {
1131 h = list_entry(nf_conntrack_hash[i].next,
1132 struct nf_conntrack_tuple_hash, list);
1133 list_del(&h->list);
1134 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1135 list_add_tail(&h->list, &hash[bucket]);
1136 }
1137 }
1138 old_size = nf_conntrack_htable_size;
1139 old_vmalloced = nf_conntrack_vmalloc;
1140 old_hash = nf_conntrack_hash;
1141
1142 nf_conntrack_htable_size = hashsize;
1143 nf_conntrack_vmalloc = vmalloced;
1144 nf_conntrack_hash = hash;
1145 nf_conntrack_hash_rnd = rnd;
1146 write_unlock_bh(&nf_conntrack_lock);
1147
1148 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1149 return 0;
1150}
1151
1152module_param_call(hashsize, set_hashsize, param_get_uint,
1153 &nf_conntrack_htable_size, 0600);
1154
1155int __init nf_conntrack_init(void)
1156{
1157 unsigned int i;
1158 int ret;
1159
1160 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1161 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1162 if (!nf_conntrack_htable_size) {
1163 nf_conntrack_htable_size
1164 = (((num_physpages << PAGE_SHIFT) / 16384)
1165 / sizeof(struct list_head));
1166 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1167 nf_conntrack_htable_size = 8192;
1168 if (nf_conntrack_htable_size < 16)
1169 nf_conntrack_htable_size = 16;
1170 }
1171 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1172
1173 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1174 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1175 nf_conntrack_max);
1176
1177 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1178 &nf_conntrack_vmalloc);
1179 if (!nf_conntrack_hash) {
1180 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1181 goto err_out;
1182 }
1183
1184 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
dc808fe2 1185 sizeof(struct nf_conn));
9fb9cbb1
YK
1186 if (ret < 0) {
1187 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1188 goto err_free_hash;
1189 }
1190
1191 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1192 sizeof(struct nf_conntrack_expect),
1193 0, 0, NULL, NULL);
1194 if (!nf_conntrack_expect_cachep) {
1195 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1196 goto err_free_conntrack_slab;
1197 }
1198
1199 /* Don't NEED lock here, but good form anyway. */
1200 write_lock_bh(&nf_conntrack_lock);
1201 for (i = 0; i < PF_MAX; i++)
1202 nf_ct_l3protos[i] = &nf_conntrack_generic_l3proto;
1203 write_unlock_bh(&nf_conntrack_lock);
1204
7d3cdc6b
YK
1205 /* For use by REJECT target */
1206 ip_ct_attach = __nf_conntrack_attach;
1207
9fb9cbb1
YK
1208 /* Set up fake conntrack:
1209 - to never be deleted, not in any hashes */
1210 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1211 /* - and look it like as a confirmed connection */
1212 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1213
1214 return ret;
1215
1216err_free_conntrack_slab:
1217 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1218err_free_hash:
1219 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1220 nf_conntrack_htable_size);
1221err_out:
1222 return -ENOMEM;
1223}