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