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