blob: 914506e6c787f46da81f5606b1ecc4faf32b949a [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* 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
Harald Weltedc808fe2006-03-20 17:56:32 -08006 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 * (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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012 */
13
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080014#include <linux/types.h>
15#include <linux/netfilter.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/proc_fs.h>
19#include <linux/vmalloc.h>
20#include <linux/stddef.h>
21#include <linux/slab.h>
22#include <linux/random.h>
23#include <linux/jhash.h>
24#include <linux/err.h>
25#include <linux/percpu.h>
26#include <linux/moduleparam.h>
27#include <linux/notifier.h>
28#include <linux/kernel.h>
29#include <linux/netdevice.h>
30#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050031#include <linux/mm.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033#include <net/netfilter/nf_conntrack.h>
34#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010035#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010036#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037#include <net/netfilter/nf_conntrack_helper.h>
38#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070039#include <net/netfilter/nf_conntrack_extend.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040
Harald Weltedc808fe2006-03-20 17:56:32 -080041#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
43#if 0
44#define DEBUGP printk
45#else
46#define DEBUGP(format, args...)
47#endif
48
49DEFINE_RWLOCK(nf_conntrack_lock);
Patrick McHardy13b18332006-12-02 22:11:25 -080050EXPORT_SYMBOL_GPL(nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051
52/* nf_conntrack_standalone needs this */
53atomic_t nf_conntrack_count = ATOMIC_INIT(0);
Patrick McHardya999e682006-11-29 02:35:20 +010054EXPORT_SYMBOL_GPL(nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055
Patrick McHardy13b18332006-12-02 22:11:25 -080056void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
57EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
58
Martin Josefssone2b76062006-11-29 02:35:04 +010059unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080060EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
61
Brian Haley94aec082006-09-18 00:05:22 -070062int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010063EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080064
Brian Haley1192e402006-09-20 12:03:46 -070065struct list_head *nf_conntrack_hash __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080066EXPORT_SYMBOL_GPL(nf_conntrack_hash);
67
Martin Josefssone2b76062006-11-29 02:35:04 +010068struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080069EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
70
Brian Haley94aec082006-09-18 00:05:22 -070071unsigned int nf_ct_log_invalid __read_mostly;
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010072LIST_HEAD(unconfirmed);
Brian Haley1192e402006-09-20 12:03:46 -070073static int nf_conntrack_vmalloc __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074
Pablo Neira Ayuso4e3882f2006-03-22 13:55:11 -080075static unsigned int nf_conntrack_next_id;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010076
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
78EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
79
80/*
81 * This scheme offers various size of "struct nf_conn" dependent on
82 * features(helper, nat, ...)
83 */
84
85#define NF_CT_FEATURES_NAMELEN 256
86static struct {
87 /* name of slab cache. printed in /proc/slabinfo */
88 char *name;
89
90 /* size of slab cache */
91 size_t size;
92
93 /* slab cache pointer */
Christoph Lametere18b8902006-12-06 20:33:20 -080094 struct kmem_cache *cachep;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095
96 /* allocated slab cache + modules which uses this slab cache */
97 int use;
98
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080099} nf_ct_cache[NF_CT_F_NUM];
100
101/* protect members of nf_ct_cache except of "use" */
102DEFINE_RWLOCK(nf_ct_cache_lock);
103
104/* This avoids calling kmem_cache_create() with same name simultaneously */
Ingo Molnar57b47a52006-03-20 22:35:41 -0800105static DEFINE_MUTEX(nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107static int nf_conntrack_hash_rnd_initted;
108static unsigned int nf_conntrack_hash_rnd;
109
110static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
111 unsigned int size, unsigned int rnd)
112{
113 unsigned int a, b;
Sami Farin9b887902007-03-14 16:43:00 -0700114
115 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
116 (tuple->src.l3num << 16) | tuple->dst.protonum);
117 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
118 (tuple->src.u.all << 16) | tuple->dst.u.all);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119
120 return jhash_2words(a, b, rnd) % size;
121}
122
123static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
124{
125 return __hash_conntrack(tuple, nf_conntrack_htable_size,
126 nf_conntrack_hash_rnd);
127}
128
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800129int nf_conntrack_register_cache(u_int32_t features, const char *name,
Harald Weltedc808fe2006-03-20 17:56:32 -0800130 size_t size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800131{
132 int ret = 0;
133 char *cache_name;
Christoph Lametere18b8902006-12-06 20:33:20 -0800134 struct kmem_cache *cachep;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800135
136 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
137 features, name, size);
138
139 if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) {
140 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
141 features);
142 return -EINVAL;
143 }
144
Ingo Molnar57b47a52006-03-20 22:35:41 -0800145 mutex_lock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146
147 write_lock_bh(&nf_ct_cache_lock);
148 /* e.g: multiple helpers are loaded */
149 if (nf_ct_cache[features].use > 0) {
150 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
151 if ((!strncmp(nf_ct_cache[features].name, name,
152 NF_CT_FEATURES_NAMELEN))
Harald Weltedc808fe2006-03-20 17:56:32 -0800153 && nf_ct_cache[features].size == size) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154 DEBUGP("nf_conntrack_register_cache: reusing.\n");
155 nf_ct_cache[features].use++;
156 ret = 0;
157 } else
158 ret = -EBUSY;
159
160 write_unlock_bh(&nf_ct_cache_lock);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800161 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 return ret;
163 }
164 write_unlock_bh(&nf_ct_cache_lock);
165
166 /*
167 * The memory space for name of slab cache must be alive until
168 * cache is destroyed.
169 */
170 cache_name = kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN, GFP_ATOMIC);
171 if (cache_name == NULL) {
172 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
173 ret = -ENOMEM;
174 goto out_up_mutex;
175 }
176
177 if (strlcpy(cache_name, name, NF_CT_FEATURES_NAMELEN)
178 >= NF_CT_FEATURES_NAMELEN) {
179 printk("nf_conntrack_register_cache: name too long\n");
180 ret = -EINVAL;
181 goto out_free_name;
182 }
183
184 cachep = kmem_cache_create(cache_name, size, 0, 0,
185 NULL, NULL);
186 if (!cachep) {
187 printk("nf_conntrack_register_cache: Can't create slab cache "
188 "for the features = 0x%x\n", features);
189 ret = -ENOMEM;
190 goto out_free_name;
191 }
192
193 write_lock_bh(&nf_ct_cache_lock);
194 nf_ct_cache[features].use = 1;
195 nf_ct_cache[features].size = size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800196 nf_ct_cache[features].cachep = cachep;
197 nf_ct_cache[features].name = cache_name;
198 write_unlock_bh(&nf_ct_cache_lock);
199
200 goto out_up_mutex;
201
202out_free_name:
203 kfree(cache_name);
204out_up_mutex:
Ingo Molnar57b47a52006-03-20 22:35:41 -0800205 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800206 return ret;
207}
Patrick McHardy13b18332006-12-02 22:11:25 -0800208EXPORT_SYMBOL_GPL(nf_conntrack_register_cache);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
210/* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
211void nf_conntrack_unregister_cache(u_int32_t features)
212{
Christoph Lametere18b8902006-12-06 20:33:20 -0800213 struct kmem_cache *cachep;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 char *name;
215
216 /*
217 * This assures that kmem_cache_create() isn't called before destroying
218 * slab cache.
219 */
220 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800221 mutex_lock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222
223 write_lock_bh(&nf_ct_cache_lock);
224 if (--nf_ct_cache[features].use > 0) {
225 write_unlock_bh(&nf_ct_cache_lock);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800226 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800227 return;
228 }
229 cachep = nf_ct_cache[features].cachep;
230 name = nf_ct_cache[features].name;
231 nf_ct_cache[features].cachep = NULL;
232 nf_ct_cache[features].name = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800233 nf_ct_cache[features].size = 0;
234 write_unlock_bh(&nf_ct_cache_lock);
235
236 synchronize_net();
237
238 kmem_cache_destroy(cachep);
239 kfree(name);
240
Ingo Molnar57b47a52006-03-20 22:35:41 -0800241 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242}
Patrick McHardy13b18332006-12-02 22:11:25 -0800243EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244
245int
246nf_ct_get_tuple(const struct sk_buff *skb,
247 unsigned int nhoff,
248 unsigned int dataoff,
249 u_int16_t l3num,
250 u_int8_t protonum,
251 struct nf_conntrack_tuple *tuple,
252 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100253 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254{
255 NF_CT_TUPLE_U_BLANK(tuple);
256
257 tuple->src.l3num = l3num;
258 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
259 return 0;
260
261 tuple->dst.protonum = protonum;
262 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
263
Martin Josefsson605dcad2006-11-29 02:35:06 +0100264 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800265}
Patrick McHardy13b18332006-12-02 22:11:25 -0800266EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267
268int
269nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
270 const struct nf_conntrack_tuple *orig,
271 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100272 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800273{
274 NF_CT_TUPLE_U_BLANK(inverse);
275
276 inverse->src.l3num = orig->src.l3num;
277 if (l3proto->invert_tuple(inverse, orig) == 0)
278 return 0;
279
280 inverse->dst.dir = !orig->dst.dir;
281
282 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100283 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800284}
Patrick McHardy13b18332006-12-02 22:11:25 -0800285EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800287static void
288clean_from_lists(struct nf_conn *ct)
289{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290 DEBUGP("clean_from_lists(%p)\n", ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700291 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
292 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800293
294 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800295 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800296}
297
298static void
299destroy_conntrack(struct nf_conntrack *nfct)
300{
301 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100302 struct nf_conntrack_l4proto *l4proto;
Patrick McHardy982d9a92007-02-12 11:14:11 -0800303 typeof(nf_conntrack_destroyed) destroyed;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304
305 DEBUGP("destroy_conntrack(%p)\n", ct);
306 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
307 NF_CT_ASSERT(!timer_pending(&ct->timeout));
308
309 nf_conntrack_event(IPCT_DESTROY, ct);
310 set_bit(IPS_DYING_BIT, &ct->status);
311
312 /* To make sure we don't get any weird locking issues here:
313 * destroy_conntrack() MUST NOT be called with a write lock
314 * to nf_conntrack_lock!!! -HW */
Patrick McHardy923f4902007-02-12 11:12:57 -0800315 rcu_read_lock();
Patrick McHardy923f4902007-02-12 11:12:57 -0800316 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
317 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100318 if (l4proto && l4proto->destroy)
319 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700321 nf_ct_ext_destroy(ct);
322
Patrick McHardy982d9a92007-02-12 11:14:11 -0800323 destroyed = rcu_dereference(nf_conntrack_destroyed);
324 if (destroyed)
325 destroyed(ct);
326
327 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328
329 write_lock_bh(&nf_conntrack_lock);
330 /* Expectations will have been removed in clean_from_lists,
331 * except TFTP can create an expectation on the first packet,
332 * before connection is in the list, so we need to clean here,
333 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800334 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800335
336 /* We overload first tuple to link into unconfirmed list. */
337 if (!nf_ct_is_confirmed(ct)) {
338 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
339 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
340 }
341
342 NF_CT_STAT_INC(delete);
343 write_unlock_bh(&nf_conntrack_lock);
344
345 if (ct->master)
346 nf_ct_put(ct->master);
347
348 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
349 nf_conntrack_free(ct);
350}
351
352static void death_by_timeout(unsigned long ul_conntrack)
353{
354 struct nf_conn *ct = (void *)ul_conntrack;
Patrick McHardy5397e972007-05-19 14:23:52 -0700355 struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700356 struct nf_conntrack_helper *helper;
Patrick McHardy5397e972007-05-19 14:23:52 -0700357
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700358 if (help) {
359 rcu_read_lock();
360 helper = rcu_dereference(help->helper);
361 if (helper && helper->destroy)
362 helper->destroy(ct);
363 rcu_read_unlock();
364 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365
366 write_lock_bh(&nf_conntrack_lock);
367 /* Inside lock so preempt is disabled on module removal path.
368 * Otherwise we can get spurious warnings. */
369 NF_CT_STAT_INC(delete_list);
370 clean_from_lists(ct);
371 write_unlock_bh(&nf_conntrack_lock);
372 nf_ct_put(ct);
373}
374
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800375struct nf_conntrack_tuple_hash *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800376__nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
377 const struct nf_conn *ignored_conntrack)
378{
379 struct nf_conntrack_tuple_hash *h;
380 unsigned int hash = hash_conntrack(tuple);
381
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700383 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
384 nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 NF_CT_STAT_INC(found);
386 return h;
387 }
388 NF_CT_STAT_INC(searched);
389 }
390
391 return NULL;
392}
Patrick McHardy13b18332006-12-02 22:11:25 -0800393EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394
395/* Find a connection corresponding to a tuple. */
396struct nf_conntrack_tuple_hash *
397nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
398 const struct nf_conn *ignored_conntrack)
399{
400 struct nf_conntrack_tuple_hash *h;
401
402 read_lock_bh(&nf_conntrack_lock);
403 h = __nf_conntrack_find(tuple, ignored_conntrack);
404 if (h)
405 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
406 read_unlock_bh(&nf_conntrack_lock);
407
408 return h;
409}
Patrick McHardy13b18332006-12-02 22:11:25 -0800410EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800412static void __nf_conntrack_hash_insert(struct nf_conn *ct,
413 unsigned int hash,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800414 unsigned int repl_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800415{
416 ct->id = ++nf_conntrack_next_id;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700417 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
418 &nf_conntrack_hash[hash]);
419 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
420 &nf_conntrack_hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800421}
422
423void nf_conntrack_hash_insert(struct nf_conn *ct)
424{
425 unsigned int hash, repl_hash;
426
427 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
428 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
429
430 write_lock_bh(&nf_conntrack_lock);
431 __nf_conntrack_hash_insert(ct, hash, repl_hash);
432 write_unlock_bh(&nf_conntrack_lock);
433}
Patrick McHardy13b18332006-12-02 22:11:25 -0800434EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800435
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800436/* Confirm a connection given skb; places it in hash table */
437int
438__nf_conntrack_confirm(struct sk_buff **pskb)
439{
440 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700441 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700443 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800444 enum ip_conntrack_info ctinfo;
445
446 ct = nf_ct_get(*pskb, &ctinfo);
447
448 /* ipt_REJECT uses nf_conntrack_attach to attach related
449 ICMP/TCP RST packets in other direction. Actual packet
450 which created connection will be IP_CT_NEW or for an
451 expected connection, IP_CT_RELATED. */
452 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
453 return NF_ACCEPT;
454
455 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
456 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
457
458 /* We're not in hash table, and we refuse to set up related
459 connections for unconfirmed conns. But packet copies and
460 REJECT will give spurious warnings here. */
461 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
462
463 /* No external references means noone else could have
464 confirmed us. */
465 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
466 DEBUGP("Confirming conntrack %p\n", ct);
467
468 write_lock_bh(&nf_conntrack_lock);
469
470 /* See if there's one in the list already, including reverse:
471 NAT could have grabbed it without realizing, since we're
472 not in the hash. If there is, we lost race. */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700473 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
474 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
475 &h->tuple))
476 goto out;
477 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
478 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
479 &h->tuple))
480 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481
Patrick McHardydf0933d2006-09-20 11:57:53 -0700482 /* Remove from unconfirmed list */
483 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
484
485 __nf_conntrack_hash_insert(ct, hash, repl_hash);
486 /* Timer relative to confirmation time, not original
487 setting time, otherwise we'd get timer wrap in
488 weird delay cases. */
489 ct->timeout.expires += jiffies;
490 add_timer(&ct->timeout);
491 atomic_inc(&ct->ct_general.use);
492 set_bit(IPS_CONFIRMED_BIT, &ct->status);
493 NF_CT_STAT_INC(insert);
494 write_unlock_bh(&nf_conntrack_lock);
495 help = nfct_help(ct);
496 if (help && help->helper)
497 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700499 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
500 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
501 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800502#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700503 nf_conntrack_event_cache(master_ct(ct) ?
504 IPCT_RELATED : IPCT_NEW, *pskb);
505 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800506
Patrick McHardydf0933d2006-09-20 11:57:53 -0700507out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800508 NF_CT_STAT_INC(insert_failed);
509 write_unlock_bh(&nf_conntrack_lock);
510 return NF_DROP;
511}
Patrick McHardy13b18332006-12-02 22:11:25 -0800512EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800513
514/* Returns true if a connection correspondings to the tuple (required
515 for NAT). */
516int
517nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
518 const struct nf_conn *ignored_conntrack)
519{
520 struct nf_conntrack_tuple_hash *h;
521
522 read_lock_bh(&nf_conntrack_lock);
523 h = __nf_conntrack_find(tuple, ignored_conntrack);
524 read_unlock_bh(&nf_conntrack_lock);
525
526 return h != NULL;
527}
Patrick McHardy13b18332006-12-02 22:11:25 -0800528EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800529
530/* There's a small race here where we may free a just-assured
531 connection. Too bad: we're in trouble anyway. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800532static int early_drop(struct list_head *chain)
533{
534 /* Traverse backwards: gives us oldest, which is roughly LRU */
535 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700536 struct nf_conn *ct = NULL, *tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800537 int dropped = 0;
538
539 read_lock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700540 list_for_each_entry_reverse(h, chain, list) {
541 tmp = nf_ct_tuplehash_to_ctrack(h);
542 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
543 ct = tmp;
544 atomic_inc(&ct->ct_general.use);
545 break;
546 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800547 }
548 read_unlock_bh(&nf_conntrack_lock);
549
550 if (!ct)
551 return dropped;
552
553 if (del_timer(&ct->timeout)) {
554 death_by_timeout((unsigned long)ct);
555 dropped = 1;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800556 NF_CT_STAT_INC_ATOMIC(early_drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800557 }
558 nf_ct_put(ct);
559 return dropped;
560}
561
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800562static struct nf_conn *
563__nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
564 const struct nf_conntrack_tuple *repl,
Patrick McHardy9457d852006-12-02 22:05:25 -0800565 const struct nf_conntrack_l3proto *l3proto,
566 u_int32_t features)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800567{
568 struct nf_conn *conntrack = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569
Harald Weltedc808fe2006-03-20 17:56:32 -0800570 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800571 get_random_bytes(&nf_conntrack_hash_rnd, 4);
572 nf_conntrack_hash_rnd_initted = 1;
573 }
574
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700575 /* We don't want any race condition at early drop stage */
576 atomic_inc(&nf_conntrack_count);
577
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578 if (nf_conntrack_max
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700579 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800580 unsigned int hash = hash_conntrack(orig);
581 /* Try dropping from this hash chain. */
582 if (!early_drop(&nf_conntrack_hash[hash])) {
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700583 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800584 if (net_ratelimit())
585 printk(KERN_WARNING
586 "nf_conntrack: table full, dropping"
587 " packet.\n");
588 return ERR_PTR(-ENOMEM);
589 }
590 }
591
592 /* find features needed by this conntrack. */
Patrick McHardy9457d852006-12-02 22:05:25 -0800593 features |= l3proto->get_features(orig);
Harald Weltedc808fe2006-03-20 17:56:32 -0800594
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800595 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
596
597 read_lock_bh(&nf_ct_cache_lock);
598
Harald Weltedc808fe2006-03-20 17:56:32 -0800599 if (unlikely(!nf_ct_cache[features].use)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800600 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
601 features);
602 goto out;
603 }
604
605 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
606 if (conntrack == NULL) {
607 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
608 goto out;
609 }
610
611 memset(conntrack, 0, nf_ct_cache[features].size);
612 conntrack->features = features;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800613 atomic_set(&conntrack->ct_general.use, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800614 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
615 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
616 /* Don't set timer yet: wait for confirmation */
Patrick McHardye6f689d2007-03-23 11:16:30 -0700617 setup_timer(&conntrack->timeout, death_by_timeout,
618 (unsigned long)conntrack);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700619 read_unlock_bh(&nf_ct_cache_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800620
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700621 return conntrack;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800622out:
623 read_unlock_bh(&nf_ct_cache_lock);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700624 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625 return conntrack;
626}
627
628struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
629 const struct nf_conntrack_tuple *repl)
630{
631 struct nf_conntrack_l3proto *l3proto;
Patrick McHardy923f4902007-02-12 11:12:57 -0800632 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800633
Patrick McHardy923f4902007-02-12 11:12:57 -0800634 rcu_read_lock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800635 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
Patrick McHardy923f4902007-02-12 11:12:57 -0800636 ct = __nf_conntrack_alloc(orig, repl, l3proto, 0);
637 rcu_read_unlock();
638
639 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800640}
Patrick McHardy13b18332006-12-02 22:11:25 -0800641EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642
643void nf_conntrack_free(struct nf_conn *conntrack)
644{
645 u_int32_t features = conntrack->features;
646 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700647 nf_ct_ext_free(conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800648 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
649 conntrack);
650 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
651 atomic_dec(&nf_conntrack_count);
652}
Patrick McHardy13b18332006-12-02 22:11:25 -0800653EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800654
655/* Allocate a new conntrack: we return -ENOMEM if classification
656 failed due to stress. Otherwise it really is unclassifiable. */
657static struct nf_conntrack_tuple_hash *
658init_conntrack(const struct nf_conntrack_tuple *tuple,
659 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100660 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800661 struct sk_buff *skb,
662 unsigned int dataoff)
663{
664 struct nf_conn *conntrack;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700665 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800666 struct nf_conntrack_tuple repl_tuple;
667 struct nf_conntrack_expect *exp;
Patrick McHardy9457d852006-12-02 22:05:25 -0800668 u_int32_t features = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800669
Martin Josefsson605dcad2006-11-29 02:35:06 +0100670 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671 DEBUGP("Can't invert tuple.\n");
672 return NULL;
673 }
674
Patrick McHardy9457d852006-12-02 22:05:25 -0800675 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800676 if (conntrack == NULL || IS_ERR(conntrack)) {
677 DEBUGP("Can't allocate conntrack.\n");
678 return (struct nf_conntrack_tuple_hash *)conntrack;
679 }
680
Martin Josefsson605dcad2006-11-29 02:35:06 +0100681 if (!l4proto->new(conntrack, skb, dataoff)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800682 nf_conntrack_free(conntrack);
683 DEBUGP("init conntrack: can't track with proto module\n");
684 return NULL;
685 }
686
687 write_lock_bh(&nf_conntrack_lock);
688 exp = find_expectation(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800689 if (exp) {
690 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
691 conntrack, exp);
692 /* Welcome, Mr. Bond. We've been expecting you... */
693 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
694 conntrack->master = exp->master;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700695 if (exp->helper) {
696 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
697 GFP_ATOMIC);
698 if (help)
699 rcu_assign_pointer(help->helper, exp->helper);
700 else
701 DEBUGP("failed to add helper extension area");
702 }
703
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800704#ifdef CONFIG_NF_CONNTRACK_MARK
705 conntrack->mark = exp->master->mark;
706#endif
James Morris7c9728c2006-06-09 00:31:46 -0700707#ifdef CONFIG_NF_CONNTRACK_SECMARK
708 conntrack->secmark = exp->master->secmark;
709#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800710 nf_conntrack_get(&conntrack->master->ct_general);
711 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800712 } else {
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700713 struct nf_conntrack_helper *helper;
714
715 helper = __nf_ct_helper_find(&repl_tuple);
716 if (helper) {
717 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
718 GFP_ATOMIC);
719 if (help)
720 /* not in hash table yet, so not strictly
721 necessary */
722 rcu_assign_pointer(help->helper, helper);
723 else
724 DEBUGP("failed to add helper extension area");
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700725 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800726 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800727 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800728
729 /* Overload tuple linked list to put us in unconfirmed list. */
730 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
731
732 write_unlock_bh(&nf_conntrack_lock);
733
734 if (exp) {
735 if (exp->expectfn)
736 exp->expectfn(conntrack, exp);
737 nf_conntrack_expect_put(exp);
738 }
739
740 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
741}
742
743/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
744static inline struct nf_conn *
745resolve_normal_ct(struct sk_buff *skb,
746 unsigned int dataoff,
747 u_int16_t l3num,
748 u_int8_t protonum,
749 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100750 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800751 int *set_reply,
752 enum ip_conntrack_info *ctinfo)
753{
754 struct nf_conntrack_tuple tuple;
755 struct nf_conntrack_tuple_hash *h;
756 struct nf_conn *ct;
757
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300758 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800759 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100760 l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800761 DEBUGP("resolve_normal_ct: Can't get tuple\n");
762 return NULL;
763 }
764
765 /* look for tuple match */
766 h = nf_conntrack_find_get(&tuple, NULL);
767 if (!h) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100768 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800769 if (!h)
770 return NULL;
771 if (IS_ERR(h))
772 return (void *)h;
773 }
774 ct = nf_ct_tuplehash_to_ctrack(h);
775
776 /* It exists; we have (non-exclusive) reference. */
777 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
778 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
779 /* Please set reply bit if this packet OK */
780 *set_reply = 1;
781 } else {
782 /* Once we've had two way comms, always ESTABLISHED. */
783 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
784 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
785 *ctinfo = IP_CT_ESTABLISHED;
786 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
787 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
788 *ctinfo = IP_CT_RELATED;
789 } else {
790 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
791 *ctinfo = IP_CT_NEW;
792 }
793 *set_reply = 0;
794 }
795 skb->nfct = &ct->ct_general;
796 skb->nfctinfo = *ctinfo;
797 return ct;
798}
799
800unsigned int
801nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
802{
803 struct nf_conn *ct;
804 enum ip_conntrack_info ctinfo;
805 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100806 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800807 unsigned int dataoff;
808 u_int8_t protonum;
809 int set_reply = 0;
810 int ret;
811
812 /* Previously seen (loopback or untracked)? Ignore. */
813 if ((*pskb)->nfct) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800814 NF_CT_STAT_INC_ATOMIC(ignore);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800815 return NF_ACCEPT;
816 }
817
Patrick McHardy923f4902007-02-12 11:12:57 -0800818 /* rcu_read_lock()ed by nf_hook_slow */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800819 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
Patrick McHardy923f4902007-02-12 11:12:57 -0800820
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800821 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
822 DEBUGP("not prepared to track yet or error occured\n");
823 return -ret;
824 }
825
Martin Josefsson605dcad2006-11-29 02:35:06 +0100826 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800827
828 /* It may be an special packet, error, unclean...
829 * inverse of the return code tells to the netfilter
830 * core what to do with the packet. */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100831 if (l4proto->error != NULL &&
832 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800833 NF_CT_STAT_INC_ATOMIC(error);
834 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800835 return -ret;
836 }
837
Martin Josefsson605dcad2006-11-29 02:35:06 +0100838 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800839 &set_reply, &ctinfo);
840 if (!ct) {
841 /* Not valid part of a connection */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800842 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800843 return NF_ACCEPT;
844 }
845
846 if (IS_ERR(ct)) {
847 /* Too stressed to deal. */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800848 NF_CT_STAT_INC_ATOMIC(drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800849 return NF_DROP;
850 }
851
852 NF_CT_ASSERT((*pskb)->nfct);
853
Martin Josefsson605dcad2006-11-29 02:35:06 +0100854 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800855 if (ret < 0) {
856 /* Invalid: inverse of the return code tells
857 * the netfilter core what to do */
858 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
859 nf_conntrack_put((*pskb)->nfct);
860 (*pskb)->nfct = NULL;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800861 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800862 return -ret;
863 }
864
865 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
866 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
867
868 return ret;
869}
Patrick McHardy13b18332006-12-02 22:11:25 -0800870EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800871
872int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
873 const struct nf_conntrack_tuple *orig)
874{
Patrick McHardy923f4902007-02-12 11:12:57 -0800875 int ret;
876
877 rcu_read_lock();
878 ret = nf_ct_invert_tuple(inverse, orig,
879 __nf_ct_l3proto_find(orig->src.l3num),
880 __nf_ct_l4proto_find(orig->src.l3num,
881 orig->dst.protonum));
882 rcu_read_unlock();
883 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800884}
Patrick McHardy13b18332006-12-02 22:11:25 -0800885EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800886
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800887/* Alter reply tuple (maybe alter helper). This is for NAT, and is
888 implicitly racy: see __nf_conntrack_confirm */
889void nf_conntrack_alter_reply(struct nf_conn *ct,
890 const struct nf_conntrack_tuple *newreply)
891{
892 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700893 struct nf_conntrack_helper *helper;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800894
895 write_lock_bh(&nf_conntrack_lock);
896 /* Should be unconfirmed, so not in hash table yet */
897 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
898
899 DEBUGP("Altering reply tuple of %p to ", ct);
900 NF_CT_DUMP_TUPLE(newreply);
901
902 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700903 if (ct->master || (help && help->expecting != 0))
904 goto out;
905
906 helper = __nf_ct_helper_find(newreply);
907 if (helper == NULL) {
908 if (help)
909 rcu_assign_pointer(help->helper, NULL);
910 goto out;
Yasuyuki Kozakai5d78a842007-05-10 14:16:24 -0700911 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700912
913 if (help == NULL) {
914 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_ATOMIC);
915 if (help == NULL) {
916 DEBUGP("failed to add helper extension area");
917 goto out;
918 }
919 } else {
920 memset(&help->help, 0, sizeof(help->help));
921 }
922
923 rcu_assign_pointer(help->helper, helper);
924out:
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800925 write_unlock_bh(&nf_conntrack_lock);
926}
Patrick McHardy13b18332006-12-02 22:11:25 -0800927EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800928
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800929/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
930void __nf_ct_refresh_acct(struct nf_conn *ct,
931 enum ip_conntrack_info ctinfo,
932 const struct sk_buff *skb,
933 unsigned long extra_jiffies,
934 int do_acct)
935{
936 int event = 0;
937
938 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
939 NF_CT_ASSERT(skb);
940
941 write_lock_bh(&nf_conntrack_lock);
942
Eric Leblond997ae832006-05-29 18:24:20 -0700943 /* Only update if this is not a fixed timeout */
944 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
945 write_unlock_bh(&nf_conntrack_lock);
946 return;
947 }
948
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800949 /* If not in hash table, timer will not be active yet */
950 if (!nf_ct_is_confirmed(ct)) {
951 ct->timeout.expires = extra_jiffies;
952 event = IPCT_REFRESH;
953 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100954 unsigned long newtime = jiffies + extra_jiffies;
955
956 /* Only update the timeout if the new timeout is at least
957 HZ jiffies from the old timeout. Need del_timer for race
958 avoidance (may already be dying). */
959 if (newtime - ct->timeout.expires >= HZ
960 && del_timer(&ct->timeout)) {
961 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800962 add_timer(&ct->timeout);
963 event = IPCT_REFRESH;
964 }
965 }
966
967#ifdef CONFIG_NF_CT_ACCT
968 if (do_acct) {
969 ct->counters[CTINFO2DIR(ctinfo)].packets++;
970 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300971 skb->len - skb_network_offset(skb);
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100972
973 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
974 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
975 event |= IPCT_COUNTER_FILLING;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800976 }
977#endif
978
979 write_unlock_bh(&nf_conntrack_lock);
980
981 /* must be unlocked when calling event cache */
982 if (event)
983 nf_conntrack_event_cache(event, skb);
984}
Patrick McHardy13b18332006-12-02 22:11:25 -0800985EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800986
Patrick McHardye281db5c2007-03-04 15:57:25 -0800987#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800988
989#include <linux/netfilter/nfnetlink.h>
990#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800991#include <linux/mutex.h>
992
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800993
994/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
995 * in ip_conntrack_core, since we don't want the protocols to autoload
996 * or depend on ctnetlink */
997int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
998 const struct nf_conntrack_tuple *tuple)
999{
1000 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
1001 &tuple->src.u.tcp.port);
1002 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
1003 &tuple->dst.u.tcp.port);
1004 return 0;
1005
1006nfattr_failure:
1007 return -1;
1008}
Patrick McHardy13b18332006-12-02 22:11:25 -08001009EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001010
1011static const size_t cta_min_proto[CTA_PROTO_MAX] = {
1012 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
1013 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
1014};
1015
1016int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
1017 struct nf_conntrack_tuple *t)
1018{
1019 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
1020 return -EINVAL;
1021
1022 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
1023 return -EINVAL;
1024
Patrick McHardybff9a892006-12-02 22:05:08 -08001025 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
1026 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001027
1028 return 0;
1029}
Patrick McHardy13b18332006-12-02 22:11:25 -08001030EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001031#endif
1032
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001033/* Used by ipt_REJECT and ip6t_REJECT. */
1034void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
1035{
1036 struct nf_conn *ct;
1037 enum ip_conntrack_info ctinfo;
1038
1039 /* This ICMP is in reverse direction to the packet which caused it */
1040 ct = nf_ct_get(skb, &ctinfo);
1041 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1042 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
1043 else
1044 ctinfo = IP_CT_RELATED;
1045
1046 /* Attach to new skbuff, and increment count */
1047 nskb->nfct = &ct->ct_general;
1048 nskb->nfctinfo = ctinfo;
1049 nf_conntrack_get(nskb->nfct);
1050}
Patrick McHardy13b18332006-12-02 22:11:25 -08001051EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001052
1053static inline int
1054do_iter(const struct nf_conntrack_tuple_hash *i,
1055 int (*iter)(struct nf_conn *i, void *data),
1056 void *data)
1057{
1058 return iter(nf_ct_tuplehash_to_ctrack(i), data);
1059}
1060
1061/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001062static struct nf_conn *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1064 void *data, unsigned int *bucket)
1065{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001066 struct nf_conntrack_tuple_hash *h;
1067 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001068
1069 write_lock_bh(&nf_conntrack_lock);
1070 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001071 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
1072 ct = nf_ct_tuplehash_to_ctrack(h);
1073 if (iter(ct, data))
1074 goto found;
1075 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001076 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001077 list_for_each_entry(h, &unconfirmed, list) {
1078 ct = nf_ct_tuplehash_to_ctrack(h);
1079 if (iter(ct, data))
Patrick McHardyec68e972007-03-04 15:57:01 -08001080 set_bit(IPS_DYING_BIT, &ct->status);
Patrick McHardydf0933d2006-09-20 11:57:53 -07001081 }
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001082 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -07001083 return NULL;
1084found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001085 atomic_inc(&ct->ct_general.use);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001086 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -07001087 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001088}
1089
1090void
1091nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1092{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001093 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001094 unsigned int bucket = 0;
1095
Patrick McHardydf0933d2006-09-20 11:57:53 -07001096 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001097 /* Time to push up daises... */
1098 if (del_timer(&ct->timeout))
1099 death_by_timeout((unsigned long)ct);
1100 /* ... else the timer will get him soon. */
1101
1102 nf_ct_put(ct);
1103 }
1104}
Patrick McHardy13b18332006-12-02 22:11:25 -08001105EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001106
1107static int kill_all(struct nf_conn *i, void *data)
1108{
1109 return 1;
1110}
1111
1112static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1113{
1114 if (vmalloced)
1115 vfree(hash);
1116 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001117 free_pages((unsigned long)hash,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001118 get_order(sizeof(struct list_head) * size));
1119}
1120
Randy Dunlap272491e2006-12-07 01:17:24 -08001121void nf_conntrack_flush(void)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001122{
1123 nf_ct_iterate_cleanup(kill_all, NULL);
1124}
Patrick McHardy13b18332006-12-02 22:11:25 -08001125EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001126
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001127/* Mishearing the voices in his head, our hero wonders how he's
1128 supposed to kill the mall. */
1129void nf_conntrack_cleanup(void)
1130{
1131 int i;
1132
Patrick McHardyc3a47ab2007-02-12 11:09:19 -08001133 rcu_assign_pointer(ip_ct_attach, NULL);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001134
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001135 /* This makes sure all current packets have passed through
1136 netfilter framework. Roll on, two-stage module
1137 delete... */
1138 synchronize_net();
1139
1140 nf_ct_event_cache_flush();
1141 i_see_dead_people:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001142 nf_conntrack_flush();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001143 if (atomic_read(&nf_conntrack_count) != 0) {
1144 schedule();
1145 goto i_see_dead_people;
1146 }
Patrick McHardy66365682005-12-05 13:36:50 -08001147 /* wait until all references to nf_conntrack_untracked are dropped */
1148 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1149 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001150
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001151 rcu_assign_pointer(nf_ct_destroy, NULL);
1152
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001153 for (i = 0; i < NF_CT_F_NUM; i++) {
1154 if (nf_ct_cache[i].use == 0)
1155 continue;
1156
1157 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1158 nf_ct_cache[i].use = 1;
1159 nf_conntrack_unregister_cache(i);
1160 }
1161 kmem_cache_destroy(nf_conntrack_expect_cachep);
1162 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1163 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -08001164
Patrick McHardyac5357e2007-03-14 16:38:25 -07001165 nf_conntrack_proto_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001166 nf_conntrack_helper_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001167}
1168
1169static struct list_head *alloc_hashtable(int size, int *vmalloced)
1170{
1171 struct list_head *hash;
1172 unsigned int i;
1173
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001174 *vmalloced = 0;
1175 hash = (void*)__get_free_pages(GFP_KERNEL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001176 get_order(sizeof(struct list_head)
1177 * size));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001178 if (!hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001179 *vmalloced = 1;
1180 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1181 hash = vmalloc(sizeof(struct list_head) * size);
1182 }
1183
1184 if (hash)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001185 for (i = 0; i < size; i++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001186 INIT_LIST_HEAD(&hash[i]);
1187
1188 return hash;
1189}
1190
1191int set_hashsize(const char *val, struct kernel_param *kp)
1192{
1193 int i, bucket, hashsize, vmalloced;
1194 int old_vmalloced, old_size;
1195 int rnd;
1196 struct list_head *hash, *old_hash;
1197 struct nf_conntrack_tuple_hash *h;
1198
1199 /* On boot, we can set this without any fancy locking. */
1200 if (!nf_conntrack_htable_size)
1201 return param_set_uint(val, kp);
1202
1203 hashsize = simple_strtol(val, NULL, 0);
1204 if (!hashsize)
1205 return -EINVAL;
1206
1207 hash = alloc_hashtable(hashsize, &vmalloced);
1208 if (!hash)
1209 return -ENOMEM;
1210
1211 /* We have to rehahs for the new table anyway, so we also can
1212 * use a newrandom seed */
1213 get_random_bytes(&rnd, 4);
1214
1215 write_lock_bh(&nf_conntrack_lock);
1216 for (i = 0; i < nf_conntrack_htable_size; i++) {
1217 while (!list_empty(&nf_conntrack_hash[i])) {
1218 h = list_entry(nf_conntrack_hash[i].next,
1219 struct nf_conntrack_tuple_hash, list);
1220 list_del(&h->list);
1221 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1222 list_add_tail(&h->list, &hash[bucket]);
1223 }
1224 }
1225 old_size = nf_conntrack_htable_size;
1226 old_vmalloced = nf_conntrack_vmalloc;
1227 old_hash = nf_conntrack_hash;
1228
1229 nf_conntrack_htable_size = hashsize;
1230 nf_conntrack_vmalloc = vmalloced;
1231 nf_conntrack_hash = hash;
1232 nf_conntrack_hash_rnd = rnd;
1233 write_unlock_bh(&nf_conntrack_lock);
1234
1235 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1236 return 0;
1237}
1238
1239module_param_call(hashsize, set_hashsize, param_get_uint,
1240 &nf_conntrack_htable_size, 0600);
1241
1242int __init nf_conntrack_init(void)
1243{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001244 int ret;
1245
1246 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1247 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1248 if (!nf_conntrack_htable_size) {
1249 nf_conntrack_htable_size
1250 = (((num_physpages << PAGE_SHIFT) / 16384)
1251 / sizeof(struct list_head));
1252 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1253 nf_conntrack_htable_size = 8192;
1254 if (nf_conntrack_htable_size < 16)
1255 nf_conntrack_htable_size = 16;
1256 }
1257 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1258
1259 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1260 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1261 nf_conntrack_max);
1262
1263 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1264 &nf_conntrack_vmalloc);
1265 if (!nf_conntrack_hash) {
1266 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1267 goto err_out;
1268 }
1269
1270 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
Harald Weltedc808fe2006-03-20 17:56:32 -08001271 sizeof(struct nf_conn));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001272 if (ret < 0) {
1273 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1274 goto err_free_hash;
1275 }
1276
1277 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1278 sizeof(struct nf_conntrack_expect),
1279 0, 0, NULL, NULL);
1280 if (!nf_conntrack_expect_cachep) {
1281 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1282 goto err_free_conntrack_slab;
1283 }
1284
Patrick McHardyac5357e2007-03-14 16:38:25 -07001285 ret = nf_conntrack_proto_init();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001286 if (ret < 0)
1287 goto out_free_expect_slab;
1288
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001289 ret = nf_conntrack_helper_init();
1290 if (ret < 0)
1291 goto out_fini_proto;
1292
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001293 /* For use by REJECT target */
Patrick McHardyc3a47ab2007-02-12 11:09:19 -08001294 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001295 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001296
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001297 /* Set up fake conntrack:
1298 - to never be deleted, not in any hashes */
1299 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1300 /* - and look it like as a confirmed connection */
1301 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1302
1303 return ret;
1304
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001305out_fini_proto:
1306 nf_conntrack_proto_fini();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001307out_free_expect_slab:
1308 kmem_cache_destroy(nf_conntrack_expect_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001309err_free_conntrack_slab:
1310 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1311err_free_hash:
1312 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1313 nf_conntrack_htable_size);
1314err_out:
1315 return -ENOMEM;
1316}