blob: 9b3158ce46e10b1f7ee7ae1c7c0bcc90fac1541c [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.
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.
Harald Weltedc808fe2006-03-20 17:56:32 -080023 * 26 Jan 2006: Harald Welte <laforge@netfilter.org>
24 * - restructure nf_conn (introduce nf_conn_help)
25 * - redesign 'features' how they were originally intended
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080026 * 26 Feb 2006: Pablo Neira Ayuso <pablo@eurodev.net>
27 * - add support for L3 protocol module load on demand.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028 *
29 * Derived from net/ipv4/netfilter/ip_conntrack_core.c
30 */
31
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032#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>
Al Virod7fe0f22006-12-03 23:15:30 -050049#include <linux/mm.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051#include <net/netfilter/nf_conntrack.h>
52#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010053#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010054#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055#include <net/netfilter/nf_conntrack_helper.h>
56#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057
Harald Weltedc808fe2006-03-20 17:56:32 -080058#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059
60#if 0
61#define DEBUGP printk
62#else
63#define DEBUGP(format, args...)
64#endif
65
66DEFINE_RWLOCK(nf_conntrack_lock);
Patrick McHardy13b18332006-12-02 22:11:25 -080067EXPORT_SYMBOL_GPL(nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068
69/* nf_conntrack_standalone needs this */
70atomic_t nf_conntrack_count = ATOMIC_INIT(0);
Patrick McHardya999e682006-11-29 02:35:20 +010071EXPORT_SYMBOL_GPL(nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072
Patrick McHardy13b18332006-12-02 22:11:25 -080073void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
74EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
75
Martin Josefssone2b76062006-11-29 02:35:04 +010076unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080077EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
78
Brian Haley94aec082006-09-18 00:05:22 -070079int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010080EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080081
Brian Haley1192e402006-09-20 12:03:46 -070082struct list_head *nf_conntrack_hash __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080083EXPORT_SYMBOL_GPL(nf_conntrack_hash);
84
Martin Josefssone2b76062006-11-29 02:35:04 +010085struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080086EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
87
Brian Haley94aec082006-09-18 00:05:22 -070088unsigned int nf_ct_log_invalid __read_mostly;
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010089LIST_HEAD(unconfirmed);
Brian Haley1192e402006-09-20 12:03:46 -070090static int nf_conntrack_vmalloc __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091
Pablo Neira Ayuso4e3882f2006-03-22 13:55:11 -080092static unsigned int nf_conntrack_next_id;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010093
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094DEFINE_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 */
111 kmem_cache_t *cachep;
112
113 /* allocated slab cache + modules which uses this slab cache */
114 int use;
115
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800116} 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 */
Ingo Molnar57b47a52006-03-20 22:35:41 -0800122static DEFINE_MUTEX(nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124static 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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800145int nf_conntrack_register_cache(u_int32_t features, const char *name,
Harald Weltedc808fe2006-03-20 17:56:32 -0800146 size_t size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147{
148 int ret = 0;
149 char *cache_name;
150 kmem_cache_t *cachep;
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
Ingo Molnar57b47a52006-03-20 22:35:41 -0800161 mutex_lock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162
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))
Harald Weltedc808fe2006-03-20 17:56:32 -0800169 && nf_ct_cache[features].size == size) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170 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);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800177 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178 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;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212 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:
Ingo Molnar57b47a52006-03-20 22:35:41 -0800221 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222 return ret;
223}
Patrick McHardy13b18332006-12-02 22:11:25 -0800224EXPORT_SYMBOL_GPL(nf_conntrack_register_cache);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225
226/* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
227void nf_conntrack_unregister_cache(u_int32_t features)
228{
229 kmem_cache_t *cachep;
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);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800237 mutex_lock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800238
239 write_lock_bh(&nf_ct_cache_lock);
240 if (--nf_ct_cache[features].use > 0) {
241 write_unlock_bh(&nf_ct_cache_lock);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800242 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800243 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;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800249 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
Ingo Molnar57b47a52006-03-20 22:35:41 -0800257 mutex_unlock(&nf_ct_cache_mutex);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800258}
Patrick McHardy13b18332006-12-02 22:11:25 -0800259EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260
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,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100269 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270{
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
Martin Josefsson605dcad2006-11-29 02:35:06 +0100280 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800281}
Patrick McHardy13b18332006-12-02 22:11:25 -0800282EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800283
284int
285nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
286 const struct nf_conntrack_tuple *orig,
287 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100288 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800289{
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;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100299 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300}
Patrick McHardy13b18332006-12-02 22:11:25 -0800301EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303static void
304clean_from_lists(struct nf_conn *ct)
305{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306 DEBUGP("clean_from_lists(%p)\n", ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700307 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
308 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800309
310 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800311 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312}
313
314static void
315destroy_conntrack(struct nf_conntrack *nfct)
316{
317 struct nf_conn *ct = (struct nf_conn *)nfct;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800318 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100320 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800321
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
Patrick McHardyf09943f2006-12-02 22:09:41 -0800329 if (help && help->helper && help->helper->destroy)
330 help->helper->destroy(ct);
331
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800332 /* 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 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800335 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336 if (l3proto && l3proto->destroy)
337 l3proto->destroy(ct);
338
Martin Josefsson605dcad2006-11-29 02:35:06 +0100339 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
340 if (l4proto && l4proto->destroy)
341 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800342
343 if (nf_conntrack_destroyed)
344 nf_conntrack_destroyed(ct);
345
346 write_lock_bh(&nf_conntrack_lock);
347 /* Expectations will have been removed in clean_from_lists,
348 * except TFTP can create an expectation on the first packet,
349 * before connection is in the list, so we need to clean here,
350 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800351 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800352
353 /* We overload first tuple to link into unconfirmed list. */
354 if (!nf_ct_is_confirmed(ct)) {
355 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
356 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
357 }
358
359 NF_CT_STAT_INC(delete);
360 write_unlock_bh(&nf_conntrack_lock);
361
362 if (ct->master)
363 nf_ct_put(ct->master);
364
365 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
366 nf_conntrack_free(ct);
367}
368
369static void death_by_timeout(unsigned long ul_conntrack)
370{
371 struct nf_conn *ct = (void *)ul_conntrack;
372
373 write_lock_bh(&nf_conntrack_lock);
374 /* Inside lock so preempt is disabled on module removal path.
375 * Otherwise we can get spurious warnings. */
376 NF_CT_STAT_INC(delete_list);
377 clean_from_lists(ct);
378 write_unlock_bh(&nf_conntrack_lock);
379 nf_ct_put(ct);
380}
381
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800382struct nf_conntrack_tuple_hash *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383__nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
384 const struct nf_conn *ignored_conntrack)
385{
386 struct nf_conntrack_tuple_hash *h;
387 unsigned int hash = hash_conntrack(tuple);
388
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800389 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700390 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
391 nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392 NF_CT_STAT_INC(found);
393 return h;
394 }
395 NF_CT_STAT_INC(searched);
396 }
397
398 return NULL;
399}
Patrick McHardy13b18332006-12-02 22:11:25 -0800400EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401
402/* Find a connection corresponding to a tuple. */
403struct nf_conntrack_tuple_hash *
404nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
405 const struct nf_conn *ignored_conntrack)
406{
407 struct nf_conntrack_tuple_hash *h;
408
409 read_lock_bh(&nf_conntrack_lock);
410 h = __nf_conntrack_find(tuple, ignored_conntrack);
411 if (h)
412 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
413 read_unlock_bh(&nf_conntrack_lock);
414
415 return h;
416}
Patrick McHardy13b18332006-12-02 22:11:25 -0800417EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800419static void __nf_conntrack_hash_insert(struct nf_conn *ct,
420 unsigned int hash,
421 unsigned int repl_hash)
422{
423 ct->id = ++nf_conntrack_next_id;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700424 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
425 &nf_conntrack_hash[hash]);
426 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
427 &nf_conntrack_hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800428}
429
430void nf_conntrack_hash_insert(struct nf_conn *ct)
431{
432 unsigned int hash, repl_hash;
433
434 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
435 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
436
437 write_lock_bh(&nf_conntrack_lock);
438 __nf_conntrack_hash_insert(ct, hash, repl_hash);
439 write_unlock_bh(&nf_conntrack_lock);
440}
Patrick McHardy13b18332006-12-02 22:11:25 -0800441EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800442
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800443/* Confirm a connection given skb; places it in hash table */
444int
445__nf_conntrack_confirm(struct sk_buff **pskb)
446{
447 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700448 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700450 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800451 enum ip_conntrack_info ctinfo;
452
453 ct = nf_ct_get(*pskb, &ctinfo);
454
455 /* ipt_REJECT uses nf_conntrack_attach to attach related
456 ICMP/TCP RST packets in other direction. Actual packet
457 which created connection will be IP_CT_NEW or for an
458 expected connection, IP_CT_RELATED. */
459 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
460 return NF_ACCEPT;
461
462 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
463 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
464
465 /* We're not in hash table, and we refuse to set up related
466 connections for unconfirmed conns. But packet copies and
467 REJECT will give spurious warnings here. */
468 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
469
470 /* No external references means noone else could have
471 confirmed us. */
472 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
473 DEBUGP("Confirming conntrack %p\n", ct);
474
475 write_lock_bh(&nf_conntrack_lock);
476
477 /* See if there's one in the list already, including reverse:
478 NAT could have grabbed it without realizing, since we're
479 not in the hash. If there is, we lost race. */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700480 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
481 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
482 &h->tuple))
483 goto out;
484 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
485 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
486 &h->tuple))
487 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488
Patrick McHardydf0933d2006-09-20 11:57:53 -0700489 /* Remove from unconfirmed list */
490 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
491
492 __nf_conntrack_hash_insert(ct, hash, repl_hash);
493 /* Timer relative to confirmation time, not original
494 setting time, otherwise we'd get timer wrap in
495 weird delay cases. */
496 ct->timeout.expires += jiffies;
497 add_timer(&ct->timeout);
498 atomic_inc(&ct->ct_general.use);
499 set_bit(IPS_CONFIRMED_BIT, &ct->status);
500 NF_CT_STAT_INC(insert);
501 write_unlock_bh(&nf_conntrack_lock);
502 help = nfct_help(ct);
503 if (help && help->helper)
504 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700506 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
507 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
508 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800509#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700510 nf_conntrack_event_cache(master_ct(ct) ?
511 IPCT_RELATED : IPCT_NEW, *pskb);
512 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800513
Patrick McHardydf0933d2006-09-20 11:57:53 -0700514out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515 NF_CT_STAT_INC(insert_failed);
516 write_unlock_bh(&nf_conntrack_lock);
517 return NF_DROP;
518}
Patrick McHardy13b18332006-12-02 22:11:25 -0800519EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520
521/* Returns true if a connection correspondings to the tuple (required
522 for NAT). */
523int
524nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
525 const struct nf_conn *ignored_conntrack)
526{
527 struct nf_conntrack_tuple_hash *h;
528
529 read_lock_bh(&nf_conntrack_lock);
530 h = __nf_conntrack_find(tuple, ignored_conntrack);
531 read_unlock_bh(&nf_conntrack_lock);
532
533 return h != NULL;
534}
Patrick McHardy13b18332006-12-02 22:11:25 -0800535EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800536
537/* There's a small race here where we may free a just-assured
538 connection. Too bad: we're in trouble anyway. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800539static int early_drop(struct list_head *chain)
540{
541 /* Traverse backwards: gives us oldest, which is roughly LRU */
542 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700543 struct nf_conn *ct = NULL, *tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544 int dropped = 0;
545
546 read_lock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700547 list_for_each_entry_reverse(h, chain, list) {
548 tmp = nf_ct_tuplehash_to_ctrack(h);
549 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
550 ct = tmp;
551 atomic_inc(&ct->ct_general.use);
552 break;
553 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800554 }
555 read_unlock_bh(&nf_conntrack_lock);
556
557 if (!ct)
558 return dropped;
559
560 if (del_timer(&ct->timeout)) {
561 death_by_timeout((unsigned long)ct);
562 dropped = 1;
563 NF_CT_STAT_INC(early_drop);
564 }
565 nf_ct_put(ct);
566 return dropped;
567}
568
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569static struct nf_conn *
570__nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
571 const struct nf_conntrack_tuple *repl,
Patrick McHardy9457d852006-12-02 22:05:25 -0800572 const struct nf_conntrack_l3proto *l3proto,
573 u_int32_t features)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800574{
575 struct nf_conn *conntrack = NULL;
Harald Weltedc808fe2006-03-20 17:56:32 -0800576 struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800577
Harald Weltedc808fe2006-03-20 17:56:32 -0800578 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800579 get_random_bytes(&nf_conntrack_hash_rnd, 4);
580 nf_conntrack_hash_rnd_initted = 1;
581 }
582
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700583 /* We don't want any race condition at early drop stage */
584 atomic_inc(&nf_conntrack_count);
585
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800586 if (nf_conntrack_max
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700587 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800588 unsigned int hash = hash_conntrack(orig);
589 /* Try dropping from this hash chain. */
590 if (!early_drop(&nf_conntrack_hash[hash])) {
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700591 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800592 if (net_ratelimit())
593 printk(KERN_WARNING
594 "nf_conntrack: table full, dropping"
595 " packet.\n");
596 return ERR_PTR(-ENOMEM);
597 }
598 }
599
600 /* find features needed by this conntrack. */
Patrick McHardy9457d852006-12-02 22:05:25 -0800601 features |= l3proto->get_features(orig);
Harald Weltedc808fe2006-03-20 17:56:32 -0800602
603 /* FIXME: protect helper list per RCU */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800604 read_lock_bh(&nf_conntrack_lock);
Harald Weltedc808fe2006-03-20 17:56:32 -0800605 helper = __nf_ct_helper_find(repl);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800606 /* NAT might want to assign a helper later */
607 if (helper || features & NF_CT_F_NAT)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800608 features |= NF_CT_F_HELP;
609 read_unlock_bh(&nf_conntrack_lock);
610
611 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
612
613 read_lock_bh(&nf_ct_cache_lock);
614
Harald Weltedc808fe2006-03-20 17:56:32 -0800615 if (unlikely(!nf_ct_cache[features].use)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800616 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
617 features);
618 goto out;
619 }
620
621 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
622 if (conntrack == NULL) {
623 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
624 goto out;
625 }
626
627 memset(conntrack, 0, nf_ct_cache[features].size);
628 conntrack->features = features;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800629 atomic_set(&conntrack->ct_general.use, 1);
630 conntrack->ct_general.destroy = destroy_conntrack;
631 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
632 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
633 /* Don't set timer yet: wait for confirmation */
634 init_timer(&conntrack->timeout);
635 conntrack->timeout.data = (unsigned long)conntrack;
636 conntrack->timeout.function = death_by_timeout;
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700637 read_unlock_bh(&nf_ct_cache_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800638
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700639 return conntrack;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800640out:
641 read_unlock_bh(&nf_ct_cache_lock);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700642 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800643 return conntrack;
644}
645
646struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
647 const struct nf_conntrack_tuple *repl)
648{
649 struct nf_conntrack_l3proto *l3proto;
650
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800651 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
Patrick McHardy9457d852006-12-02 22:05:25 -0800652 return __nf_conntrack_alloc(orig, repl, l3proto, 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653}
Patrick McHardy13b18332006-12-02 22:11:25 -0800654EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800655
656void nf_conntrack_free(struct nf_conn *conntrack)
657{
658 u_int32_t features = conntrack->features;
659 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
660 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
661 conntrack);
662 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
663 atomic_dec(&nf_conntrack_count);
664}
Patrick McHardy13b18332006-12-02 22:11:25 -0800665EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800666
667/* Allocate a new conntrack: we return -ENOMEM if classification
668 failed due to stress. Otherwise it really is unclassifiable. */
669static struct nf_conntrack_tuple_hash *
670init_conntrack(const struct nf_conntrack_tuple *tuple,
671 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100672 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800673 struct sk_buff *skb,
674 unsigned int dataoff)
675{
676 struct nf_conn *conntrack;
677 struct nf_conntrack_tuple repl_tuple;
678 struct nf_conntrack_expect *exp;
Patrick McHardy9457d852006-12-02 22:05:25 -0800679 u_int32_t features = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800680
Martin Josefsson605dcad2006-11-29 02:35:06 +0100681 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800682 DEBUGP("Can't invert tuple.\n");
683 return NULL;
684 }
685
Patrick McHardy9457d852006-12-02 22:05:25 -0800686 read_lock_bh(&nf_conntrack_lock);
687 exp = __nf_conntrack_expect_find(tuple);
688 if (exp && exp->helper)
689 features = NF_CT_F_HELP;
690 read_unlock_bh(&nf_conntrack_lock);
691
692 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800693 if (conntrack == NULL || IS_ERR(conntrack)) {
694 DEBUGP("Can't allocate conntrack.\n");
695 return (struct nf_conntrack_tuple_hash *)conntrack;
696 }
697
Martin Josefsson605dcad2006-11-29 02:35:06 +0100698 if (!l4proto->new(conntrack, skb, dataoff)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800699 nf_conntrack_free(conntrack);
700 DEBUGP("init conntrack: can't track with proto module\n");
701 return NULL;
702 }
703
704 write_lock_bh(&nf_conntrack_lock);
705 exp = find_expectation(tuple);
706
707 if (exp) {
708 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
709 conntrack, exp);
710 /* Welcome, Mr. Bond. We've been expecting you... */
711 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
712 conntrack->master = exp->master;
Patrick McHardy9457d852006-12-02 22:05:25 -0800713 if (exp->helper)
714 nfct_help(conntrack)->helper = exp->helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800715#ifdef CONFIG_NF_CONNTRACK_MARK
716 conntrack->mark = exp->master->mark;
717#endif
James Morris7c9728c2006-06-09 00:31:46 -0700718#ifdef CONFIG_NF_CONNTRACK_SECMARK
719 conntrack->secmark = exp->master->secmark;
720#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800721 nf_conntrack_get(&conntrack->master->ct_general);
722 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800723 } else {
724 struct nf_conn_help *help = nfct_help(conntrack);
725
726 if (help)
727 help->helper = __nf_ct_helper_find(&repl_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800728 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800729 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800730
731 /* Overload tuple linked list to put us in unconfirmed list. */
732 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
733
734 write_unlock_bh(&nf_conntrack_lock);
735
736 if (exp) {
737 if (exp->expectfn)
738 exp->expectfn(conntrack, exp);
739 nf_conntrack_expect_put(exp);
740 }
741
742 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
743}
744
745/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
746static inline struct nf_conn *
747resolve_normal_ct(struct sk_buff *skb,
748 unsigned int dataoff,
749 u_int16_t l3num,
750 u_int8_t protonum,
751 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100752 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800753 int *set_reply,
754 enum ip_conntrack_info *ctinfo)
755{
756 struct nf_conntrack_tuple tuple;
757 struct nf_conntrack_tuple_hash *h;
758 struct nf_conn *ct;
759
760 if (!nf_ct_get_tuple(skb, (unsigned int)(skb->nh.raw - skb->data),
761 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100762 l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800763 DEBUGP("resolve_normal_ct: Can't get tuple\n");
764 return NULL;
765 }
766
767 /* look for tuple match */
768 h = nf_conntrack_find_get(&tuple, NULL);
769 if (!h) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100770 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800771 if (!h)
772 return NULL;
773 if (IS_ERR(h))
774 return (void *)h;
775 }
776 ct = nf_ct_tuplehash_to_ctrack(h);
777
778 /* It exists; we have (non-exclusive) reference. */
779 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
780 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
781 /* Please set reply bit if this packet OK */
782 *set_reply = 1;
783 } else {
784 /* Once we've had two way comms, always ESTABLISHED. */
785 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
786 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
787 *ctinfo = IP_CT_ESTABLISHED;
788 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
789 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
790 *ctinfo = IP_CT_RELATED;
791 } else {
792 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
793 *ctinfo = IP_CT_NEW;
794 }
795 *set_reply = 0;
796 }
797 skb->nfct = &ct->ct_general;
798 skb->nfctinfo = *ctinfo;
799 return ct;
800}
801
802unsigned int
803nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
804{
805 struct nf_conn *ct;
806 enum ip_conntrack_info ctinfo;
807 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100808 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800809 unsigned int dataoff;
810 u_int8_t protonum;
811 int set_reply = 0;
812 int ret;
813
814 /* Previously seen (loopback or untracked)? Ignore. */
815 if ((*pskb)->nfct) {
816 NF_CT_STAT_INC(ignore);
817 return NF_ACCEPT;
818 }
819
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800820 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
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) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800833 NF_CT_STAT_INC(error);
834 NF_CT_STAT_INC(invalid);
835 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 */
842 NF_CT_STAT_INC(invalid);
843 return NF_ACCEPT;
844 }
845
846 if (IS_ERR(ct)) {
847 /* Too stressed to deal. */
848 NF_CT_STAT_INC(drop);
849 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;
861 NF_CT_STAT_INC(invalid);
862 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{
875 return nf_ct_invert_tuple(inverse, orig,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800876 __nf_ct_l3proto_find(orig->src.l3num),
Martin Josefsson605dcad2006-11-29 02:35:06 +0100877 __nf_ct_l4proto_find(orig->src.l3num,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800878 orig->dst.protonum));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800879}
Patrick McHardy13b18332006-12-02 22:11:25 -0800880EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800881
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800882/* Alter reply tuple (maybe alter helper). This is for NAT, and is
883 implicitly racy: see __nf_conntrack_confirm */
884void nf_conntrack_alter_reply(struct nf_conn *ct,
885 const struct nf_conntrack_tuple *newreply)
886{
887 struct nf_conn_help *help = nfct_help(ct);
888
889 write_lock_bh(&nf_conntrack_lock);
890 /* Should be unconfirmed, so not in hash table yet */
891 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
892
893 DEBUGP("Altering reply tuple of %p to ", ct);
894 NF_CT_DUMP_TUPLE(newreply);
895
896 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
897 if (!ct->master && help && help->expecting == 0)
898 help->helper = __nf_ct_helper_find(newreply);
899 write_unlock_bh(&nf_conntrack_lock);
900}
Patrick McHardy13b18332006-12-02 22:11:25 -0800901EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800902
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800903/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
904void __nf_ct_refresh_acct(struct nf_conn *ct,
905 enum ip_conntrack_info ctinfo,
906 const struct sk_buff *skb,
907 unsigned long extra_jiffies,
908 int do_acct)
909{
910 int event = 0;
911
912 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
913 NF_CT_ASSERT(skb);
914
915 write_lock_bh(&nf_conntrack_lock);
916
Eric Leblond997ae832006-05-29 18:24:20 -0700917 /* Only update if this is not a fixed timeout */
918 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
919 write_unlock_bh(&nf_conntrack_lock);
920 return;
921 }
922
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800923 /* If not in hash table, timer will not be active yet */
924 if (!nf_ct_is_confirmed(ct)) {
925 ct->timeout.expires = extra_jiffies;
926 event = IPCT_REFRESH;
927 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100928 unsigned long newtime = jiffies + extra_jiffies;
929
930 /* Only update the timeout if the new timeout is at least
931 HZ jiffies from the old timeout. Need del_timer for race
932 avoidance (may already be dying). */
933 if (newtime - ct->timeout.expires >= HZ
934 && del_timer(&ct->timeout)) {
935 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800936 add_timer(&ct->timeout);
937 event = IPCT_REFRESH;
938 }
939 }
940
941#ifdef CONFIG_NF_CT_ACCT
942 if (do_acct) {
943 ct->counters[CTINFO2DIR(ctinfo)].packets++;
944 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
945 skb->len - (unsigned int)(skb->nh.raw - skb->data);
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100946
947 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
948 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
949 event |= IPCT_COUNTER_FILLING;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800950 }
951#endif
952
953 write_unlock_bh(&nf_conntrack_lock);
954
955 /* must be unlocked when calling event cache */
956 if (event)
957 nf_conntrack_event_cache(event, skb);
958}
Patrick McHardy13b18332006-12-02 22:11:25 -0800959EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800960
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800961#if defined(CONFIG_NF_CT_NETLINK) || \
962 defined(CONFIG_NF_CT_NETLINK_MODULE)
963
964#include <linux/netfilter/nfnetlink.h>
965#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800966#include <linux/mutex.h>
967
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800968
969/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
970 * in ip_conntrack_core, since we don't want the protocols to autoload
971 * or depend on ctnetlink */
972int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
973 const struct nf_conntrack_tuple *tuple)
974{
975 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
976 &tuple->src.u.tcp.port);
977 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
978 &tuple->dst.u.tcp.port);
979 return 0;
980
981nfattr_failure:
982 return -1;
983}
Patrick McHardy13b18332006-12-02 22:11:25 -0800984EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800985
986static const size_t cta_min_proto[CTA_PROTO_MAX] = {
987 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
988 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
989};
990
991int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
992 struct nf_conntrack_tuple *t)
993{
994 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
995 return -EINVAL;
996
997 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
998 return -EINVAL;
999
Patrick McHardybff9a892006-12-02 22:05:08 -08001000 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
1001 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001002
1003 return 0;
1004}
Patrick McHardy13b18332006-12-02 22:11:25 -08001005EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001006#endif
1007
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001008/* Used by ipt_REJECT and ip6t_REJECT. */
1009void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
1010{
1011 struct nf_conn *ct;
1012 enum ip_conntrack_info ctinfo;
1013
1014 /* This ICMP is in reverse direction to the packet which caused it */
1015 ct = nf_ct_get(skb, &ctinfo);
1016 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1017 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
1018 else
1019 ctinfo = IP_CT_RELATED;
1020
1021 /* Attach to new skbuff, and increment count */
1022 nskb->nfct = &ct->ct_general;
1023 nskb->nfctinfo = ctinfo;
1024 nf_conntrack_get(nskb->nfct);
1025}
Patrick McHardy13b18332006-12-02 22:11:25 -08001026EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001027
1028static inline int
1029do_iter(const struct nf_conntrack_tuple_hash *i,
1030 int (*iter)(struct nf_conn *i, void *data),
1031 void *data)
1032{
1033 return iter(nf_ct_tuplehash_to_ctrack(i), data);
1034}
1035
1036/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001037static struct nf_conn *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001038get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1039 void *data, unsigned int *bucket)
1040{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001041 struct nf_conntrack_tuple_hash *h;
1042 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001043
1044 write_lock_bh(&nf_conntrack_lock);
1045 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001046 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
1047 ct = nf_ct_tuplehash_to_ctrack(h);
1048 if (iter(ct, data))
1049 goto found;
1050 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001051 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001052 list_for_each_entry(h, &unconfirmed, list) {
1053 ct = nf_ct_tuplehash_to_ctrack(h);
1054 if (iter(ct, data))
1055 goto found;
1056 }
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001057 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -07001058 return NULL;
1059found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001060 atomic_inc(&ct->ct_general.use);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001061 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -07001062 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063}
1064
1065void
1066nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1067{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001068 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001069 unsigned int bucket = 0;
1070
Patrick McHardydf0933d2006-09-20 11:57:53 -07001071 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001072 /* Time to push up daises... */
1073 if (del_timer(&ct->timeout))
1074 death_by_timeout((unsigned long)ct);
1075 /* ... else the timer will get him soon. */
1076
1077 nf_ct_put(ct);
1078 }
1079}
Patrick McHardy13b18332006-12-02 22:11:25 -08001080EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001081
1082static int kill_all(struct nf_conn *i, void *data)
1083{
1084 return 1;
1085}
1086
1087static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1088{
1089 if (vmalloced)
1090 vfree(hash);
1091 else
1092 free_pages((unsigned long)hash,
1093 get_order(sizeof(struct list_head) * size));
1094}
1095
Randy Dunlap272491e2006-12-07 01:17:24 -08001096void nf_conntrack_flush(void)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001097{
1098 nf_ct_iterate_cleanup(kill_all, NULL);
1099}
Patrick McHardy13b18332006-12-02 22:11:25 -08001100EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001101
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001102/* Mishearing the voices in his head, our hero wonders how he's
1103 supposed to kill the mall. */
1104void nf_conntrack_cleanup(void)
1105{
1106 int i;
1107
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001108 ip_ct_attach = NULL;
1109
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001110 /* This makes sure all current packets have passed through
1111 netfilter framework. Roll on, two-stage module
1112 delete... */
1113 synchronize_net();
1114
1115 nf_ct_event_cache_flush();
1116 i_see_dead_people:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001117 nf_conntrack_flush();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001118 if (atomic_read(&nf_conntrack_count) != 0) {
1119 schedule();
1120 goto i_see_dead_people;
1121 }
Patrick McHardy66365682005-12-05 13:36:50 -08001122 /* wait until all references to nf_conntrack_untracked are dropped */
1123 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1124 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001125
1126 for (i = 0; i < NF_CT_F_NUM; i++) {
1127 if (nf_ct_cache[i].use == 0)
1128 continue;
1129
1130 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1131 nf_ct_cache[i].use = 1;
1132 nf_conntrack_unregister_cache(i);
1133 }
1134 kmem_cache_destroy(nf_conntrack_expect_cachep);
1135 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1136 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -08001137
Patrick McHardy933a41e2006-11-29 02:35:18 +01001138 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_generic);
1139
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -08001140 /* free l3proto protocol tables */
1141 for (i = 0; i < PF_MAX; i++)
1142 if (nf_ct_protos[i]) {
1143 kfree(nf_ct_protos[i]);
1144 nf_ct_protos[i] = NULL;
1145 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001146}
1147
1148static struct list_head *alloc_hashtable(int size, int *vmalloced)
1149{
1150 struct list_head *hash;
1151 unsigned int i;
1152
1153 *vmalloced = 0;
1154 hash = (void*)__get_free_pages(GFP_KERNEL,
1155 get_order(sizeof(struct list_head)
1156 * size));
1157 if (!hash) {
1158 *vmalloced = 1;
1159 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1160 hash = vmalloc(sizeof(struct list_head) * size);
1161 }
1162
1163 if (hash)
1164 for (i = 0; i < size; i++)
1165 INIT_LIST_HEAD(&hash[i]);
1166
1167 return hash;
1168}
1169
1170int set_hashsize(const char *val, struct kernel_param *kp)
1171{
1172 int i, bucket, hashsize, vmalloced;
1173 int old_vmalloced, old_size;
1174 int rnd;
1175 struct list_head *hash, *old_hash;
1176 struct nf_conntrack_tuple_hash *h;
1177
1178 /* On boot, we can set this without any fancy locking. */
1179 if (!nf_conntrack_htable_size)
1180 return param_set_uint(val, kp);
1181
1182 hashsize = simple_strtol(val, NULL, 0);
1183 if (!hashsize)
1184 return -EINVAL;
1185
1186 hash = alloc_hashtable(hashsize, &vmalloced);
1187 if (!hash)
1188 return -ENOMEM;
1189
1190 /* We have to rehahs for the new table anyway, so we also can
1191 * use a newrandom seed */
1192 get_random_bytes(&rnd, 4);
1193
1194 write_lock_bh(&nf_conntrack_lock);
1195 for (i = 0; i < nf_conntrack_htable_size; i++) {
1196 while (!list_empty(&nf_conntrack_hash[i])) {
1197 h = list_entry(nf_conntrack_hash[i].next,
1198 struct nf_conntrack_tuple_hash, list);
1199 list_del(&h->list);
1200 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1201 list_add_tail(&h->list, &hash[bucket]);
1202 }
1203 }
1204 old_size = nf_conntrack_htable_size;
1205 old_vmalloced = nf_conntrack_vmalloc;
1206 old_hash = nf_conntrack_hash;
1207
1208 nf_conntrack_htable_size = hashsize;
1209 nf_conntrack_vmalloc = vmalloced;
1210 nf_conntrack_hash = hash;
1211 nf_conntrack_hash_rnd = rnd;
1212 write_unlock_bh(&nf_conntrack_lock);
1213
1214 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1215 return 0;
1216}
1217
1218module_param_call(hashsize, set_hashsize, param_get_uint,
1219 &nf_conntrack_htable_size, 0600);
1220
1221int __init nf_conntrack_init(void)
1222{
1223 unsigned int i;
1224 int ret;
1225
1226 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1227 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1228 if (!nf_conntrack_htable_size) {
1229 nf_conntrack_htable_size
1230 = (((num_physpages << PAGE_SHIFT) / 16384)
1231 / sizeof(struct list_head));
1232 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1233 nf_conntrack_htable_size = 8192;
1234 if (nf_conntrack_htable_size < 16)
1235 nf_conntrack_htable_size = 16;
1236 }
1237 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1238
1239 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1240 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1241 nf_conntrack_max);
1242
1243 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1244 &nf_conntrack_vmalloc);
1245 if (!nf_conntrack_hash) {
1246 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1247 goto err_out;
1248 }
1249
1250 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
Harald Weltedc808fe2006-03-20 17:56:32 -08001251 sizeof(struct nf_conn));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001252 if (ret < 0) {
1253 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1254 goto err_free_hash;
1255 }
1256
1257 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1258 sizeof(struct nf_conntrack_expect),
1259 0, 0, NULL, NULL);
1260 if (!nf_conntrack_expect_cachep) {
1261 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1262 goto err_free_conntrack_slab;
1263 }
1264
Patrick McHardy933a41e2006-11-29 02:35:18 +01001265 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_generic);
1266 if (ret < 0)
1267 goto out_free_expect_slab;
1268
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001269 /* Don't NEED lock here, but good form anyway. */
1270 write_lock_bh(&nf_conntrack_lock);
Martin Josefssonae5718f2006-11-29 02:35:08 +01001271 for (i = 0; i < AF_MAX; i++)
Martin Josefsson605dcad2006-11-29 02:35:06 +01001272 nf_ct_l3protos[i] = &nf_conntrack_l3proto_generic;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001273 write_unlock_bh(&nf_conntrack_lock);
1274
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001275 /* For use by REJECT target */
1276 ip_ct_attach = __nf_conntrack_attach;
1277
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001278 /* Set up fake conntrack:
1279 - to never be deleted, not in any hashes */
1280 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1281 /* - and look it like as a confirmed connection */
1282 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1283
1284 return ret;
1285
Patrick McHardy933a41e2006-11-29 02:35:18 +01001286out_free_expect_slab:
1287 kmem_cache_destroy(nf_conntrack_expect_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001288err_free_conntrack_slab:
1289 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1290err_free_hash:
1291 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1292 nf_conntrack_htable_size);
1293err_out:
1294 return -ENOMEM;
1295}