blob: ed44a09ae7392e6ff3fdac3145a10488ff37d694 [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
Martin Josefssone2b76062006-11-29 02:35:04 +010056unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080057EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
58
Brian Haley94aec082006-09-18 00:05:22 -070059int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010060EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080061
Patrick McHardyf205c5e2007-07-07 22:28:14 -070062struct hlist_head *nf_conntrack_hash __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080063EXPORT_SYMBOL_GPL(nf_conntrack_hash);
64
Martin Josefssone2b76062006-11-29 02:35:04 +010065struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080066EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
67
Brian Haley94aec082006-09-18 00:05:22 -070068unsigned int nf_ct_log_invalid __read_mostly;
Patrick McHardyf205c5e2007-07-07 22:28:14 -070069HLIST_HEAD(unconfirmed);
Brian Haley1192e402006-09-20 12:03:46 -070070static int nf_conntrack_vmalloc __read_mostly;
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -070071static struct kmem_cache *nf_conntrack_cachep __read_mostly;
Pablo Neira Ayuso4e3882f2006-03-22 13:55:11 -080072static unsigned int nf_conntrack_next_id;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010073
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
75EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
76
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077static int nf_conntrack_hash_rnd_initted;
78static unsigned int nf_conntrack_hash_rnd;
79
80static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
81 unsigned int size, unsigned int rnd)
82{
83 unsigned int a, b;
Sami Farin9b887902007-03-14 16:43:00 -070084
85 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
86 (tuple->src.l3num << 16) | tuple->dst.protonum);
87 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
88 (tuple->src.u.all << 16) | tuple->dst.u.all);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089
90 return jhash_2words(a, b, rnd) % size;
91}
92
93static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
94{
95 return __hash_conntrack(tuple, nf_conntrack_htable_size,
96 nf_conntrack_hash_rnd);
97}
98
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080099int
100nf_ct_get_tuple(const struct sk_buff *skb,
101 unsigned int nhoff,
102 unsigned int dataoff,
103 u_int16_t l3num,
104 u_int8_t protonum,
105 struct nf_conntrack_tuple *tuple,
106 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100107 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108{
109 NF_CT_TUPLE_U_BLANK(tuple);
110
111 tuple->src.l3num = l3num;
112 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
113 return 0;
114
115 tuple->dst.protonum = protonum;
116 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
117
Martin Josefsson605dcad2006-11-29 02:35:06 +0100118 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119}
Patrick McHardy13b18332006-12-02 22:11:25 -0800120EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800121
122int
123nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
124 const struct nf_conntrack_tuple *orig,
125 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100126 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127{
128 NF_CT_TUPLE_U_BLANK(inverse);
129
130 inverse->src.l3num = orig->src.l3num;
131 if (l3proto->invert_tuple(inverse, orig) == 0)
132 return 0;
133
134 inverse->dst.dir = !orig->dst.dir;
135
136 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100137 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800138}
Patrick McHardy13b18332006-12-02 22:11:25 -0800139EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800140
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800141static void
142clean_from_lists(struct nf_conn *ct)
143{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144 DEBUGP("clean_from_lists(%p)\n", ct);
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700145 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
146 hlist_del(&ct->tuplehash[IP_CT_DIR_REPLY].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147
148 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800149 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150}
151
152static void
153destroy_conntrack(struct nf_conntrack *nfct)
154{
155 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157
158 DEBUGP("destroy_conntrack(%p)\n", ct);
159 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
160 NF_CT_ASSERT(!timer_pending(&ct->timeout));
161
162 nf_conntrack_event(IPCT_DESTROY, ct);
163 set_bit(IPS_DYING_BIT, &ct->status);
164
165 /* To make sure we don't get any weird locking issues here:
166 * destroy_conntrack() MUST NOT be called with a write lock
167 * to nf_conntrack_lock!!! -HW */
Patrick McHardy923f4902007-02-12 11:12:57 -0800168 rcu_read_lock();
Patrick McHardy923f4902007-02-12 11:12:57 -0800169 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
170 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100171 if (l4proto && l4proto->destroy)
172 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700174 nf_ct_ext_destroy(ct);
175
Patrick McHardy982d9a92007-02-12 11:14:11 -0800176 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177
178 write_lock_bh(&nf_conntrack_lock);
179 /* Expectations will have been removed in clean_from_lists,
180 * except TFTP can create an expectation on the first packet,
181 * before connection is in the list, so we need to clean here,
182 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800183 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184
185 /* We overload first tuple to link into unconfirmed list. */
186 if (!nf_ct_is_confirmed(ct)) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700187 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
188 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189 }
190
191 NF_CT_STAT_INC(delete);
192 write_unlock_bh(&nf_conntrack_lock);
193
194 if (ct->master)
195 nf_ct_put(ct->master);
196
197 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
198 nf_conntrack_free(ct);
199}
200
201static void death_by_timeout(unsigned long ul_conntrack)
202{
203 struct nf_conn *ct = (void *)ul_conntrack;
Patrick McHardy5397e972007-05-19 14:23:52 -0700204 struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700205 struct nf_conntrack_helper *helper;
Patrick McHardy5397e972007-05-19 14:23:52 -0700206
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700207 if (help) {
208 rcu_read_lock();
209 helper = rcu_dereference(help->helper);
210 if (helper && helper->destroy)
211 helper->destroy(ct);
212 rcu_read_unlock();
213 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214
215 write_lock_bh(&nf_conntrack_lock);
216 /* Inside lock so preempt is disabled on module removal path.
217 * Otherwise we can get spurious warnings. */
218 NF_CT_STAT_INC(delete_list);
219 clean_from_lists(ct);
220 write_unlock_bh(&nf_conntrack_lock);
221 nf_ct_put(ct);
222}
223
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800224struct nf_conntrack_tuple_hash *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225__nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
226 const struct nf_conn *ignored_conntrack)
227{
228 struct nf_conntrack_tuple_hash *h;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700229 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230 unsigned int hash = hash_conntrack(tuple);
231
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700232 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700233 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
234 nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800235 NF_CT_STAT_INC(found);
236 return h;
237 }
238 NF_CT_STAT_INC(searched);
239 }
240
241 return NULL;
242}
Patrick McHardy13b18332006-12-02 22:11:25 -0800243EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244
245/* Find a connection corresponding to a tuple. */
246struct nf_conntrack_tuple_hash *
Patrick McHardy330f7db2007-07-07 22:28:42 -0700247nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248{
249 struct nf_conntrack_tuple_hash *h;
250
251 read_lock_bh(&nf_conntrack_lock);
Patrick McHardy330f7db2007-07-07 22:28:42 -0700252 h = __nf_conntrack_find(tuple, NULL);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253 if (h)
254 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
255 read_unlock_bh(&nf_conntrack_lock);
256
257 return h;
258}
Patrick McHardy13b18332006-12-02 22:11:25 -0800259EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800261static void __nf_conntrack_hash_insert(struct nf_conn *ct,
262 unsigned int hash,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800263 unsigned int repl_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800264{
265 ct->id = ++nf_conntrack_next_id;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700266 hlist_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
267 &nf_conntrack_hash[hash]);
268 hlist_add_head(&ct->tuplehash[IP_CT_DIR_REPLY].hnode,
269 &nf_conntrack_hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800270}
271
272void nf_conntrack_hash_insert(struct nf_conn *ct)
273{
274 unsigned int hash, repl_hash;
275
276 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
277 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
278
279 write_lock_bh(&nf_conntrack_lock);
280 __nf_conntrack_hash_insert(ct, hash, repl_hash);
281 write_unlock_bh(&nf_conntrack_lock);
282}
Patrick McHardy13b18332006-12-02 22:11:25 -0800283EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800284
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800285/* Confirm a connection given skb; places it in hash table */
286int
287__nf_conntrack_confirm(struct sk_buff **pskb)
288{
289 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700290 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700292 struct nf_conn_help *help;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700293 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294 enum ip_conntrack_info ctinfo;
295
296 ct = nf_ct_get(*pskb, &ctinfo);
297
298 /* ipt_REJECT uses nf_conntrack_attach to attach related
299 ICMP/TCP RST packets in other direction. Actual packet
300 which created connection will be IP_CT_NEW or for an
301 expected connection, IP_CT_RELATED. */
302 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
303 return NF_ACCEPT;
304
305 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
306 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
307
308 /* We're not in hash table, and we refuse to set up related
309 connections for unconfirmed conns. But packet copies and
310 REJECT will give spurious warnings here. */
311 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
312
313 /* No external references means noone else could have
314 confirmed us. */
315 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
316 DEBUGP("Confirming conntrack %p\n", ct);
317
318 write_lock_bh(&nf_conntrack_lock);
319
320 /* See if there's one in the list already, including reverse:
321 NAT could have grabbed it without realizing, since we're
322 not in the hash. If there is, we lost race. */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700323 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700324 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
325 &h->tuple))
326 goto out;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700327 hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700328 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
329 &h->tuple))
330 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800331
Patrick McHardydf0933d2006-09-20 11:57:53 -0700332 /* Remove from unconfirmed list */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700333 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700334
335 __nf_conntrack_hash_insert(ct, hash, repl_hash);
336 /* Timer relative to confirmation time, not original
337 setting time, otherwise we'd get timer wrap in
338 weird delay cases. */
339 ct->timeout.expires += jiffies;
340 add_timer(&ct->timeout);
341 atomic_inc(&ct->ct_general.use);
342 set_bit(IPS_CONFIRMED_BIT, &ct->status);
343 NF_CT_STAT_INC(insert);
344 write_unlock_bh(&nf_conntrack_lock);
345 help = nfct_help(ct);
346 if (help && help->helper)
347 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800348#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700349 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
350 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
351 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800352#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700353 nf_conntrack_event_cache(master_ct(ct) ?
354 IPCT_RELATED : IPCT_NEW, *pskb);
355 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356
Patrick McHardydf0933d2006-09-20 11:57:53 -0700357out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 NF_CT_STAT_INC(insert_failed);
359 write_unlock_bh(&nf_conntrack_lock);
360 return NF_DROP;
361}
Patrick McHardy13b18332006-12-02 22:11:25 -0800362EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363
364/* Returns true if a connection correspondings to the tuple (required
365 for NAT). */
366int
367nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
368 const struct nf_conn *ignored_conntrack)
369{
370 struct nf_conntrack_tuple_hash *h;
371
372 read_lock_bh(&nf_conntrack_lock);
373 h = __nf_conntrack_find(tuple, ignored_conntrack);
374 read_unlock_bh(&nf_conntrack_lock);
375
376 return h != NULL;
377}
Patrick McHardy13b18332006-12-02 22:11:25 -0800378EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379
380/* There's a small race here where we may free a just-assured
381 connection. Too bad: we're in trouble anyway. */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700382static int early_drop(struct hlist_head *chain)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700384 /* Use oldest entry, which is roughly LRU */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700386 struct nf_conn *ct = NULL, *tmp;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700387 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800388 int dropped = 0;
389
390 read_lock_bh(&nf_conntrack_lock);
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700391 hlist_for_each_entry(h, n, chain, hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700392 tmp = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700393 if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700394 ct = tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395 }
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700396 if (ct)
397 atomic_inc(&ct->ct_general.use);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 read_unlock_bh(&nf_conntrack_lock);
399
400 if (!ct)
401 return dropped;
402
403 if (del_timer(&ct->timeout)) {
404 death_by_timeout((unsigned long)ct);
405 dropped = 1;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800406 NF_CT_STAT_INC_ATOMIC(early_drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407 }
408 nf_ct_put(ct);
409 return dropped;
410}
411
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700412struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
413 const struct nf_conntrack_tuple *repl)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414{
415 struct nf_conn *conntrack = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416
Harald Weltedc808fe2006-03-20 17:56:32 -0800417 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418 get_random_bytes(&nf_conntrack_hash_rnd, 4);
419 nf_conntrack_hash_rnd_initted = 1;
420 }
421
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700422 /* We don't want any race condition at early drop stage */
423 atomic_inc(&nf_conntrack_count);
424
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425 if (nf_conntrack_max
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700426 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800427 unsigned int hash = hash_conntrack(orig);
428 /* Try dropping from this hash chain. */
429 if (!early_drop(&nf_conntrack_hash[hash])) {
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700430 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800431 if (net_ratelimit())
432 printk(KERN_WARNING
433 "nf_conntrack: table full, dropping"
434 " packet.\n");
435 return ERR_PTR(-ENOMEM);
436 }
437 }
438
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700439 conntrack = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440 if (conntrack == NULL) {
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700441 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack.\n");
442 atomic_dec(&nf_conntrack_count);
443 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800444 }
445
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800446 atomic_set(&conntrack->ct_general.use, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
448 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
449 /* Don't set timer yet: wait for confirmation */
Patrick McHardye6f689d2007-03-23 11:16:30 -0700450 setup_timer(&conntrack->timeout, death_by_timeout,
451 (unsigned long)conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800452
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700453 return conntrack;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800454}
Patrick McHardy13b18332006-12-02 22:11:25 -0800455EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456
457void nf_conntrack_free(struct nf_conn *conntrack)
458{
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700459 nf_ct_ext_free(conntrack);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700460 kmem_cache_free(nf_conntrack_cachep, conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461 atomic_dec(&nf_conntrack_count);
462}
Patrick McHardy13b18332006-12-02 22:11:25 -0800463EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800464
465/* Allocate a new conntrack: we return -ENOMEM if classification
466 failed due to stress. Otherwise it really is unclassifiable. */
467static struct nf_conntrack_tuple_hash *
468init_conntrack(const struct nf_conntrack_tuple *tuple,
469 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100470 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800471 struct sk_buff *skb,
472 unsigned int dataoff)
473{
474 struct nf_conn *conntrack;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700475 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 struct nf_conntrack_tuple repl_tuple;
477 struct nf_conntrack_expect *exp;
478
Martin Josefsson605dcad2006-11-29 02:35:06 +0100479 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 DEBUGP("Can't invert tuple.\n");
481 return NULL;
482 }
483
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700484 conntrack = nf_conntrack_alloc(tuple, &repl_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800485 if (conntrack == NULL || IS_ERR(conntrack)) {
486 DEBUGP("Can't allocate conntrack.\n");
487 return (struct nf_conntrack_tuple_hash *)conntrack;
488 }
489
Martin Josefsson605dcad2006-11-29 02:35:06 +0100490 if (!l4proto->new(conntrack, skb, dataoff)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800491 nf_conntrack_free(conntrack);
492 DEBUGP("init conntrack: can't track with proto module\n");
493 return NULL;
494 }
495
496 write_lock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -0700497 exp = nf_ct_find_expectation(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498 if (exp) {
499 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
500 conntrack, exp);
501 /* Welcome, Mr. Bond. We've been expecting you... */
502 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
503 conntrack->master = exp->master;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700504 if (exp->helper) {
505 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
506 GFP_ATOMIC);
507 if (help)
508 rcu_assign_pointer(help->helper, exp->helper);
509 else
510 DEBUGP("failed to add helper extension area");
511 }
512
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800513#ifdef CONFIG_NF_CONNTRACK_MARK
514 conntrack->mark = exp->master->mark;
515#endif
James Morris7c9728c2006-06-09 00:31:46 -0700516#ifdef CONFIG_NF_CONNTRACK_SECMARK
517 conntrack->secmark = exp->master->secmark;
518#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519 nf_conntrack_get(&conntrack->master->ct_general);
520 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800521 } else {
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700522 struct nf_conntrack_helper *helper;
523
524 helper = __nf_ct_helper_find(&repl_tuple);
525 if (helper) {
526 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
527 GFP_ATOMIC);
528 if (help)
529 /* not in hash table yet, so not strictly
530 necessary */
531 rcu_assign_pointer(help->helper, helper);
532 else
533 DEBUGP("failed to add helper extension area");
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700534 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800535 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800536 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800537
538 /* Overload tuple linked list to put us in unconfirmed list. */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700539 hlist_add_head(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
540 &unconfirmed);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800541
542 write_unlock_bh(&nf_conntrack_lock);
543
544 if (exp) {
545 if (exp->expectfn)
546 exp->expectfn(conntrack, exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700547 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800548 }
549
550 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
551}
552
553/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
554static inline struct nf_conn *
555resolve_normal_ct(struct sk_buff *skb,
556 unsigned int dataoff,
557 u_int16_t l3num,
558 u_int8_t protonum,
559 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100560 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800561 int *set_reply,
562 enum ip_conntrack_info *ctinfo)
563{
564 struct nf_conntrack_tuple tuple;
565 struct nf_conntrack_tuple_hash *h;
566 struct nf_conn *ct;
567
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300568 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100570 l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800571 DEBUGP("resolve_normal_ct: Can't get tuple\n");
572 return NULL;
573 }
574
575 /* look for tuple match */
Patrick McHardy330f7db2007-07-07 22:28:42 -0700576 h = nf_conntrack_find_get(&tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800577 if (!h) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100578 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800579 if (!h)
580 return NULL;
581 if (IS_ERR(h))
582 return (void *)h;
583 }
584 ct = nf_ct_tuplehash_to_ctrack(h);
585
586 /* It exists; we have (non-exclusive) reference. */
587 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
588 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
589 /* Please set reply bit if this packet OK */
590 *set_reply = 1;
591 } else {
592 /* Once we've had two way comms, always ESTABLISHED. */
593 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
594 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
595 *ctinfo = IP_CT_ESTABLISHED;
596 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
597 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
598 *ctinfo = IP_CT_RELATED;
599 } else {
600 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
601 *ctinfo = IP_CT_NEW;
602 }
603 *set_reply = 0;
604 }
605 skb->nfct = &ct->ct_general;
606 skb->nfctinfo = *ctinfo;
607 return ct;
608}
609
610unsigned int
611nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
612{
613 struct nf_conn *ct;
614 enum ip_conntrack_info ctinfo;
615 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100616 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800617 unsigned int dataoff;
618 u_int8_t protonum;
619 int set_reply = 0;
620 int ret;
621
622 /* Previously seen (loopback or untracked)? Ignore. */
623 if ((*pskb)->nfct) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800624 NF_CT_STAT_INC_ATOMIC(ignore);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800625 return NF_ACCEPT;
626 }
627
Patrick McHardy923f4902007-02-12 11:12:57 -0800628 /* rcu_read_lock()ed by nf_hook_slow */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800629 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
Patrick McHardy923f4902007-02-12 11:12:57 -0800630
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800631 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
632 DEBUGP("not prepared to track yet or error occured\n");
633 return -ret;
634 }
635
Martin Josefsson605dcad2006-11-29 02:35:06 +0100636 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800637
638 /* It may be an special packet, error, unclean...
639 * inverse of the return code tells to the netfilter
640 * core what to do with the packet. */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100641 if (l4proto->error != NULL &&
642 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800643 NF_CT_STAT_INC_ATOMIC(error);
644 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800645 return -ret;
646 }
647
Martin Josefsson605dcad2006-11-29 02:35:06 +0100648 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800649 &set_reply, &ctinfo);
650 if (!ct) {
651 /* Not valid part of a connection */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800652 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653 return NF_ACCEPT;
654 }
655
656 if (IS_ERR(ct)) {
657 /* Too stressed to deal. */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800658 NF_CT_STAT_INC_ATOMIC(drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800659 return NF_DROP;
660 }
661
662 NF_CT_ASSERT((*pskb)->nfct);
663
Martin Josefsson605dcad2006-11-29 02:35:06 +0100664 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800665 if (ret < 0) {
666 /* Invalid: inverse of the return code tells
667 * the netfilter core what to do */
668 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
669 nf_conntrack_put((*pskb)->nfct);
670 (*pskb)->nfct = NULL;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800671 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800672 return -ret;
673 }
674
675 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
676 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
677
678 return ret;
679}
Patrick McHardy13b18332006-12-02 22:11:25 -0800680EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800681
682int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
683 const struct nf_conntrack_tuple *orig)
684{
Patrick McHardy923f4902007-02-12 11:12:57 -0800685 int ret;
686
687 rcu_read_lock();
688 ret = nf_ct_invert_tuple(inverse, orig,
689 __nf_ct_l3proto_find(orig->src.l3num),
690 __nf_ct_l4proto_find(orig->src.l3num,
691 orig->dst.protonum));
692 rcu_read_unlock();
693 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800694}
Patrick McHardy13b18332006-12-02 22:11:25 -0800695EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800696
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800697/* Alter reply tuple (maybe alter helper). This is for NAT, and is
698 implicitly racy: see __nf_conntrack_confirm */
699void nf_conntrack_alter_reply(struct nf_conn *ct,
700 const struct nf_conntrack_tuple *newreply)
701{
702 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700703 struct nf_conntrack_helper *helper;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800704
705 write_lock_bh(&nf_conntrack_lock);
706 /* Should be unconfirmed, so not in hash table yet */
707 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
708
709 DEBUGP("Altering reply tuple of %p to ", ct);
710 NF_CT_DUMP_TUPLE(newreply);
711
712 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700713 if (ct->master || (help && help->expecting != 0))
714 goto out;
715
716 helper = __nf_ct_helper_find(newreply);
717 if (helper == NULL) {
718 if (help)
719 rcu_assign_pointer(help->helper, NULL);
720 goto out;
Yasuyuki Kozakai5d78a842007-05-10 14:16:24 -0700721 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700722
723 if (help == NULL) {
724 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_ATOMIC);
725 if (help == NULL) {
726 DEBUGP("failed to add helper extension area");
727 goto out;
728 }
729 } else {
730 memset(&help->help, 0, sizeof(help->help));
731 }
732
733 rcu_assign_pointer(help->helper, helper);
734out:
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800735 write_unlock_bh(&nf_conntrack_lock);
736}
Patrick McHardy13b18332006-12-02 22:11:25 -0800737EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800738
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800739/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
740void __nf_ct_refresh_acct(struct nf_conn *ct,
741 enum ip_conntrack_info ctinfo,
742 const struct sk_buff *skb,
743 unsigned long extra_jiffies,
744 int do_acct)
745{
746 int event = 0;
747
748 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
749 NF_CT_ASSERT(skb);
750
751 write_lock_bh(&nf_conntrack_lock);
752
Eric Leblond997ae832006-05-29 18:24:20 -0700753 /* Only update if this is not a fixed timeout */
754 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
755 write_unlock_bh(&nf_conntrack_lock);
756 return;
757 }
758
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800759 /* If not in hash table, timer will not be active yet */
760 if (!nf_ct_is_confirmed(ct)) {
761 ct->timeout.expires = extra_jiffies;
762 event = IPCT_REFRESH;
763 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100764 unsigned long newtime = jiffies + extra_jiffies;
765
766 /* Only update the timeout if the new timeout is at least
767 HZ jiffies from the old timeout. Need del_timer for race
768 avoidance (may already be dying). */
769 if (newtime - ct->timeout.expires >= HZ
770 && del_timer(&ct->timeout)) {
771 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800772 add_timer(&ct->timeout);
773 event = IPCT_REFRESH;
774 }
775 }
776
777#ifdef CONFIG_NF_CT_ACCT
778 if (do_acct) {
779 ct->counters[CTINFO2DIR(ctinfo)].packets++;
780 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300781 skb->len - skb_network_offset(skb);
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100782
783 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
784 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
785 event |= IPCT_COUNTER_FILLING;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800786 }
787#endif
788
789 write_unlock_bh(&nf_conntrack_lock);
790
791 /* must be unlocked when calling event cache */
792 if (event)
793 nf_conntrack_event_cache(event, skb);
794}
Patrick McHardy13b18332006-12-02 22:11:25 -0800795EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800796
Patrick McHardye281db5c2007-03-04 15:57:25 -0800797#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800798
799#include <linux/netfilter/nfnetlink.h>
800#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800801#include <linux/mutex.h>
802
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800803
804/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
805 * in ip_conntrack_core, since we don't want the protocols to autoload
806 * or depend on ctnetlink */
807int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
808 const struct nf_conntrack_tuple *tuple)
809{
810 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
811 &tuple->src.u.tcp.port);
812 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
813 &tuple->dst.u.tcp.port);
814 return 0;
815
816nfattr_failure:
817 return -1;
818}
Patrick McHardy13b18332006-12-02 22:11:25 -0800819EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800820
821static const size_t cta_min_proto[CTA_PROTO_MAX] = {
822 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
823 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
824};
825
826int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
827 struct nf_conntrack_tuple *t)
828{
829 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
830 return -EINVAL;
831
832 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
833 return -EINVAL;
834
Patrick McHardybff9a892006-12-02 22:05:08 -0800835 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
836 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800837
838 return 0;
839}
Patrick McHardy13b18332006-12-02 22:11:25 -0800840EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800841#endif
842
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800843/* Used by ipt_REJECT and ip6t_REJECT. */
844void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
845{
846 struct nf_conn *ct;
847 enum ip_conntrack_info ctinfo;
848
849 /* This ICMP is in reverse direction to the packet which caused it */
850 ct = nf_ct_get(skb, &ctinfo);
851 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
852 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
853 else
854 ctinfo = IP_CT_RELATED;
855
856 /* Attach to new skbuff, and increment count */
857 nskb->nfct = &ct->ct_general;
858 nskb->nfctinfo = ctinfo;
859 nf_conntrack_get(nskb->nfct);
860}
Patrick McHardy13b18332006-12-02 22:11:25 -0800861EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800862
863static inline int
864do_iter(const struct nf_conntrack_tuple_hash *i,
865 int (*iter)(struct nf_conn *i, void *data),
866 void *data)
867{
868 return iter(nf_ct_tuplehash_to_ctrack(i), data);
869}
870
871/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700872static struct nf_conn *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800873get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
874 void *data, unsigned int *bucket)
875{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700876 struct nf_conntrack_tuple_hash *h;
877 struct nf_conn *ct;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700878 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800879
880 write_lock_bh(&nf_conntrack_lock);
881 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700882 hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700883 ct = nf_ct_tuplehash_to_ctrack(h);
884 if (iter(ct, data))
885 goto found;
886 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800887 }
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700888 hlist_for_each_entry(h, n, &unconfirmed, hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700889 ct = nf_ct_tuplehash_to_ctrack(h);
890 if (iter(ct, data))
Patrick McHardyec68e972007-03-04 15:57:01 -0800891 set_bit(IPS_DYING_BIT, &ct->status);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700892 }
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800893 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700894 return NULL;
895found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800896 atomic_inc(&ct->ct_general.use);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800897 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700898 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800899}
900
901void
902nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
903{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700904 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800905 unsigned int bucket = 0;
906
Patrick McHardydf0933d2006-09-20 11:57:53 -0700907 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800908 /* Time to push up daises... */
909 if (del_timer(&ct->timeout))
910 death_by_timeout((unsigned long)ct);
911 /* ... else the timer will get him soon. */
912
913 nf_ct_put(ct);
914 }
915}
Patrick McHardy13b18332006-12-02 22:11:25 -0800916EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800917
918static int kill_all(struct nf_conn *i, void *data)
919{
920 return 1;
921}
922
Patrick McHardyac565e52007-07-07 22:30:08 -0700923void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800924{
925 if (vmalloced)
926 vfree(hash);
927 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800928 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700929 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800930}
Patrick McHardyac565e52007-07-07 22:30:08 -0700931EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800932
Randy Dunlap272491e2006-12-07 01:17:24 -0800933void nf_conntrack_flush(void)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800934{
935 nf_ct_iterate_cleanup(kill_all, NULL);
936}
Patrick McHardy13b18332006-12-02 22:11:25 -0800937EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800938
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800939/* Mishearing the voices in his head, our hero wonders how he's
940 supposed to kill the mall. */
941void nf_conntrack_cleanup(void)
942{
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800943 rcu_assign_pointer(ip_ct_attach, NULL);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -0800944
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800945 /* This makes sure all current packets have passed through
946 netfilter framework. Roll on, two-stage module
947 delete... */
948 synchronize_net();
949
950 nf_ct_event_cache_flush();
951 i_see_dead_people:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800952 nf_conntrack_flush();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800953 if (atomic_read(&nf_conntrack_count) != 0) {
954 schedule();
955 goto i_see_dead_people;
956 }
Patrick McHardy66365682005-12-05 13:36:50 -0800957 /* wait until all references to nf_conntrack_untracked are dropped */
958 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
959 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800960
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700961 rcu_assign_pointer(nf_ct_destroy, NULL);
962
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700963 kmem_cache_destroy(nf_conntrack_cachep);
Patrick McHardyac565e52007-07-07 22:30:08 -0700964 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
965 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -0800966
Patrick McHardyac5357e2007-03-14 16:38:25 -0700967 nf_conntrack_proto_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700968 nf_conntrack_helper_fini();
Patrick McHardye9c1b082007-07-07 22:32:53 -0700969 nf_conntrack_expect_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800970}
971
Patrick McHardyac565e52007-07-07 22:30:08 -0700972struct hlist_head *nf_ct_alloc_hashtable(int *sizep, int *vmalloced)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800973{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700974 struct hlist_head *hash;
Patrick McHardy8e5105a2007-07-07 22:27:33 -0700975 unsigned int size, i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800976
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800977 *vmalloced = 0;
Patrick McHardy8e5105a2007-07-07 22:27:33 -0700978
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700979 size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800980 hash = (void*)__get_free_pages(GFP_KERNEL,
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700981 get_order(sizeof(struct hlist_head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800982 * size));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800983 if (!hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800984 *vmalloced = 1;
985 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700986 hash = vmalloc(sizeof(struct hlist_head) * size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800987 }
988
989 if (hash)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800990 for (i = 0; i < size; i++)
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700991 INIT_HLIST_HEAD(&hash[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800992
993 return hash;
994}
Patrick McHardyac565e52007-07-07 22:30:08 -0700995EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800996
997int set_hashsize(const char *val, struct kernel_param *kp)
998{
999 int i, bucket, hashsize, vmalloced;
1000 int old_vmalloced, old_size;
1001 int rnd;
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001002 struct hlist_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001003 struct nf_conntrack_tuple_hash *h;
1004
1005 /* On boot, we can set this without any fancy locking. */
1006 if (!nf_conntrack_htable_size)
1007 return param_set_uint(val, kp);
1008
1009 hashsize = simple_strtol(val, NULL, 0);
1010 if (!hashsize)
1011 return -EINVAL;
1012
Patrick McHardyac565e52007-07-07 22:30:08 -07001013 hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001014 if (!hash)
1015 return -ENOMEM;
1016
1017 /* We have to rehahs for the new table anyway, so we also can
1018 * use a newrandom seed */
1019 get_random_bytes(&rnd, 4);
1020
1021 write_lock_bh(&nf_conntrack_lock);
1022 for (i = 0; i < nf_conntrack_htable_size; i++) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001023 while (!hlist_empty(&nf_conntrack_hash[i])) {
1024 h = hlist_entry(nf_conntrack_hash[i].first,
1025 struct nf_conntrack_tuple_hash, hnode);
1026 hlist_del(&h->hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001027 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001028 hlist_add_head(&h->hnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001029 }
1030 }
1031 old_size = nf_conntrack_htable_size;
1032 old_vmalloced = nf_conntrack_vmalloc;
1033 old_hash = nf_conntrack_hash;
1034
1035 nf_conntrack_htable_size = hashsize;
1036 nf_conntrack_vmalloc = vmalloced;
1037 nf_conntrack_hash = hash;
1038 nf_conntrack_hash_rnd = rnd;
1039 write_unlock_bh(&nf_conntrack_lock);
1040
Patrick McHardyac565e52007-07-07 22:30:08 -07001041 nf_ct_free_hashtable(old_hash, old_vmalloced, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001042 return 0;
1043}
1044
1045module_param_call(hashsize, set_hashsize, param_get_uint,
1046 &nf_conntrack_htable_size, 0600);
1047
1048int __init nf_conntrack_init(void)
1049{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001050 int max_factor = 8;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001051 int ret;
1052
1053 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001054 * machine has 512 buckets. >= 1GB machines have 16384 buckets. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001055 if (!nf_conntrack_htable_size) {
1056 nf_conntrack_htable_size
1057 = (((num_physpages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001058 / sizeof(struct hlist_head));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001059 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001060 nf_conntrack_htable_size = 16384;
1061 if (nf_conntrack_htable_size < 32)
1062 nf_conntrack_htable_size = 32;
1063
1064 /* Use a max. factor of four by default to get the same max as
1065 * with the old struct list_heads. When a table size is given
1066 * we use the old value of 8 to avoid reducing the max.
1067 * entries. */
1068 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001069 }
Patrick McHardyac565e52007-07-07 22:30:08 -07001070 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
1071 &nf_conntrack_vmalloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001072 if (!nf_conntrack_hash) {
1073 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1074 goto err_out;
1075 }
1076
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001077 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001078
1079 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1080 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1081 nf_conntrack_max);
1082
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001083 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1084 sizeof(struct nf_conn),
1085 0, 0, NULL, NULL);
1086 if (!nf_conntrack_cachep) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001087 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1088 goto err_free_hash;
1089 }
1090
Patrick McHardyac5357e2007-03-14 16:38:25 -07001091 ret = nf_conntrack_proto_init();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001092 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001093 goto err_free_conntrack_slab;
1094
1095 ret = nf_conntrack_expect_init();
1096 if (ret < 0)
1097 goto out_fini_proto;
Patrick McHardy933a41e2006-11-29 02:35:18 +01001098
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001099 ret = nf_conntrack_helper_init();
1100 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001101 goto out_fini_expect;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001102
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001103 /* For use by REJECT target */
Patrick McHardyc3a47ab2007-02-12 11:09:19 -08001104 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001105 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001106
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001107 /* Set up fake conntrack:
1108 - to never be deleted, not in any hashes */
1109 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1110 /* - and look it like as a confirmed connection */
1111 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1112
1113 return ret;
1114
Patrick McHardye9c1b082007-07-07 22:32:53 -07001115out_fini_expect:
1116 nf_conntrack_expect_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001117out_fini_proto:
1118 nf_conntrack_proto_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001119err_free_conntrack_slab:
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001120 kmem_cache_destroy(nf_conntrack_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001121err_free_hash:
Patrick McHardyac565e52007-07-07 22:30:08 -07001122 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
1123 nf_conntrack_htable_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001124err_out:
1125 return -ENOMEM;
1126}