blob: a713666529386e9649b62eb99fb1a645b0e569c7 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* Connection state tracking for netfilter. This is separated from,
2 but required by, the NAT layer; it can also be used by an iptables
3 extension. */
4
5/* (C) 1999-2001 Paul `Rusty' Russell
Harald Weltedc808fe2006-03-20 17:56:32 -08006 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012 */
13
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080014#include <linux/types.h>
15#include <linux/netfilter.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/proc_fs.h>
19#include <linux/vmalloc.h>
20#include <linux/stddef.h>
21#include <linux/slab.h>
22#include <linux/random.h>
23#include <linux/jhash.h>
24#include <linux/err.h>
25#include <linux/percpu.h>
26#include <linux/moduleparam.h>
27#include <linux/notifier.h>
28#include <linux/kernel.h>
29#include <linux/netdevice.h>
30#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050031#include <linux/mm.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033#include <net/netfilter/nf_conntrack.h>
34#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010035#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010036#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037#include <net/netfilter/nf_conntrack_helper.h>
38#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070039#include <net/netfilter/nf_conntrack_extend.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040
Harald Weltedc808fe2006-03-20 17:56:32 -080041#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
43#if 0
44#define DEBUGP printk
45#else
46#define DEBUGP(format, args...)
47#endif
48
49DEFINE_RWLOCK(nf_conntrack_lock);
Patrick McHardy13b18332006-12-02 22:11:25 -080050EXPORT_SYMBOL_GPL(nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051
52/* nf_conntrack_standalone needs this */
53atomic_t nf_conntrack_count = ATOMIC_INIT(0);
Patrick McHardya999e682006-11-29 02:35:20 +010054EXPORT_SYMBOL_GPL(nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055
Patrick McHardy13b18332006-12-02 22:11:25 -080056void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
57EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
58
Martin Josefssone2b76062006-11-29 02:35:04 +010059unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080060EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
61
Brian Haley94aec082006-09-18 00:05:22 -070062int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010063EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080064
Brian Haley1192e402006-09-20 12:03:46 -070065struct list_head *nf_conntrack_hash __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080066EXPORT_SYMBOL_GPL(nf_conntrack_hash);
67
Martin Josefssone2b76062006-11-29 02:35:04 +010068struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080069EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
70
Brian Haley94aec082006-09-18 00:05:22 -070071unsigned int nf_ct_log_invalid __read_mostly;
Martin Josefsson7e5d03b2006-11-29 02:34:59 +010072LIST_HEAD(unconfirmed);
Brian Haley1192e402006-09-20 12:03:46 -070073static int nf_conntrack_vmalloc __read_mostly;
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -070074static struct kmem_cache *nf_conntrack_cachep __read_mostly;
Pablo Neira Ayuso4e3882f2006-03-22 13:55:11 -080075static unsigned int nf_conntrack_next_id;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010076
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
78EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
79
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080static int nf_conntrack_hash_rnd_initted;
81static unsigned int nf_conntrack_hash_rnd;
82
83static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
84 unsigned int size, unsigned int rnd)
85{
86 unsigned int a, b;
Sami Farin9b887902007-03-14 16:43:00 -070087
88 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
89 (tuple->src.l3num << 16) | tuple->dst.protonum);
90 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
91 (tuple->src.u.all << 16) | tuple->dst.u.all);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092
93 return jhash_2words(a, b, rnd) % size;
94}
95
96static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
97{
98 return __hash_conntrack(tuple, nf_conntrack_htable_size,
99 nf_conntrack_hash_rnd);
100}
101
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102int
103nf_ct_get_tuple(const struct sk_buff *skb,
104 unsigned int nhoff,
105 unsigned int dataoff,
106 u_int16_t l3num,
107 u_int8_t protonum,
108 struct nf_conntrack_tuple *tuple,
109 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100110 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800111{
112 NF_CT_TUPLE_U_BLANK(tuple);
113
114 tuple->src.l3num = l3num;
115 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
116 return 0;
117
118 tuple->dst.protonum = protonum;
119 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
120
Martin Josefsson605dcad2006-11-29 02:35:06 +0100121 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122}
Patrick McHardy13b18332006-12-02 22:11:25 -0800123EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124
125int
126nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
127 const struct nf_conntrack_tuple *orig,
128 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100129 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130{
131 NF_CT_TUPLE_U_BLANK(inverse);
132
133 inverse->src.l3num = orig->src.l3num;
134 if (l3proto->invert_tuple(inverse, orig) == 0)
135 return 0;
136
137 inverse->dst.dir = !orig->dst.dir;
138
139 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100140 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800141}
Patrick McHardy13b18332006-12-02 22:11:25 -0800142EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800143
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144static void
145clean_from_lists(struct nf_conn *ct)
146{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147 DEBUGP("clean_from_lists(%p)\n", ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700148 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
149 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150
151 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800152 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153}
154
155static void
156destroy_conntrack(struct nf_conntrack *nfct)
157{
158 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100159 struct nf_conntrack_l4proto *l4proto;
Patrick McHardy982d9a92007-02-12 11:14:11 -0800160 typeof(nf_conntrack_destroyed) destroyed;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161
162 DEBUGP("destroy_conntrack(%p)\n", ct);
163 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
164 NF_CT_ASSERT(!timer_pending(&ct->timeout));
165
166 nf_conntrack_event(IPCT_DESTROY, ct);
167 set_bit(IPS_DYING_BIT, &ct->status);
168
169 /* To make sure we don't get any weird locking issues here:
170 * destroy_conntrack() MUST NOT be called with a write lock
171 * to nf_conntrack_lock!!! -HW */
Patrick McHardy923f4902007-02-12 11:12:57 -0800172 rcu_read_lock();
Patrick McHardy923f4902007-02-12 11:12:57 -0800173 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
174 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100175 if (l4proto && l4proto->destroy)
176 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700178 nf_ct_ext_destroy(ct);
179
Patrick McHardy982d9a92007-02-12 11:14:11 -0800180 destroyed = rcu_dereference(nf_conntrack_destroyed);
181 if (destroyed)
182 destroyed(ct);
183
184 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185
186 write_lock_bh(&nf_conntrack_lock);
187 /* Expectations will have been removed in clean_from_lists,
188 * except TFTP can create an expectation on the first packet,
189 * before connection is in the list, so we need to clean here,
190 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800191 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192
193 /* We overload first tuple to link into unconfirmed list. */
194 if (!nf_ct_is_confirmed(ct)) {
195 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
196 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
197 }
198
199 NF_CT_STAT_INC(delete);
200 write_unlock_bh(&nf_conntrack_lock);
201
202 if (ct->master)
203 nf_ct_put(ct->master);
204
205 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
206 nf_conntrack_free(ct);
207}
208
209static void death_by_timeout(unsigned long ul_conntrack)
210{
211 struct nf_conn *ct = (void *)ul_conntrack;
Patrick McHardy5397e972007-05-19 14:23:52 -0700212 struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700213 struct nf_conntrack_helper *helper;
Patrick McHardy5397e972007-05-19 14:23:52 -0700214
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700215 if (help) {
216 rcu_read_lock();
217 helper = rcu_dereference(help->helper);
218 if (helper && helper->destroy)
219 helper->destroy(ct);
220 rcu_read_unlock();
221 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222
223 write_lock_bh(&nf_conntrack_lock);
224 /* Inside lock so preempt is disabled on module removal path.
225 * Otherwise we can get spurious warnings. */
226 NF_CT_STAT_INC(delete_list);
227 clean_from_lists(ct);
228 write_unlock_bh(&nf_conntrack_lock);
229 nf_ct_put(ct);
230}
231
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800232struct nf_conntrack_tuple_hash *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800233__nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
234 const struct nf_conn *ignored_conntrack)
235{
236 struct nf_conntrack_tuple_hash *h;
237 unsigned int hash = hash_conntrack(tuple);
238
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700240 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
241 nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242 NF_CT_STAT_INC(found);
243 return h;
244 }
245 NF_CT_STAT_INC(searched);
246 }
247
248 return NULL;
249}
Patrick McHardy13b18332006-12-02 22:11:25 -0800250EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800251
252/* Find a connection corresponding to a tuple. */
253struct nf_conntrack_tuple_hash *
254nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
255 const struct nf_conn *ignored_conntrack)
256{
257 struct nf_conntrack_tuple_hash *h;
258
259 read_lock_bh(&nf_conntrack_lock);
260 h = __nf_conntrack_find(tuple, ignored_conntrack);
261 if (h)
262 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
263 read_unlock_bh(&nf_conntrack_lock);
264
265 return h;
266}
Patrick McHardy13b18332006-12-02 22:11:25 -0800267EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800269static void __nf_conntrack_hash_insert(struct nf_conn *ct,
270 unsigned int hash,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800271 unsigned int repl_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800272{
273 ct->id = ++nf_conntrack_next_id;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700274 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
275 &nf_conntrack_hash[hash]);
276 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
277 &nf_conntrack_hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800278}
279
280void nf_conntrack_hash_insert(struct nf_conn *ct)
281{
282 unsigned int hash, repl_hash;
283
284 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
285 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
286
287 write_lock_bh(&nf_conntrack_lock);
288 __nf_conntrack_hash_insert(ct, hash, repl_hash);
289 write_unlock_bh(&nf_conntrack_lock);
290}
Patrick McHardy13b18332006-12-02 22:11:25 -0800291EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800292
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800293/* Confirm a connection given skb; places it in hash table */
294int
295__nf_conntrack_confirm(struct sk_buff **pskb)
296{
297 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700298 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800299 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700300 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301 enum ip_conntrack_info ctinfo;
302
303 ct = nf_ct_get(*pskb, &ctinfo);
304
305 /* ipt_REJECT uses nf_conntrack_attach to attach related
306 ICMP/TCP RST packets in other direction. Actual packet
307 which created connection will be IP_CT_NEW or for an
308 expected connection, IP_CT_RELATED. */
309 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
310 return NF_ACCEPT;
311
312 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
313 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
314
315 /* We're not in hash table, and we refuse to set up related
316 connections for unconfirmed conns. But packet copies and
317 REJECT will give spurious warnings here. */
318 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
319
320 /* No external references means noone else could have
321 confirmed us. */
322 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
323 DEBUGP("Confirming conntrack %p\n", ct);
324
325 write_lock_bh(&nf_conntrack_lock);
326
327 /* See if there's one in the list already, including reverse:
328 NAT could have grabbed it without realizing, since we're
329 not in the hash. If there is, we lost race. */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700330 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
331 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
332 &h->tuple))
333 goto out;
334 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
335 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
336 &h->tuple))
337 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800338
Patrick McHardydf0933d2006-09-20 11:57:53 -0700339 /* Remove from unconfirmed list */
340 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
341
342 __nf_conntrack_hash_insert(ct, hash, repl_hash);
343 /* Timer relative to confirmation time, not original
344 setting time, otherwise we'd get timer wrap in
345 weird delay cases. */
346 ct->timeout.expires += jiffies;
347 add_timer(&ct->timeout);
348 atomic_inc(&ct->ct_general.use);
349 set_bit(IPS_CONFIRMED_BIT, &ct->status);
350 NF_CT_STAT_INC(insert);
351 write_unlock_bh(&nf_conntrack_lock);
352 help = nfct_help(ct);
353 if (help && help->helper)
354 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700356 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
357 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
358 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800359#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700360 nf_conntrack_event_cache(master_ct(ct) ?
361 IPCT_RELATED : IPCT_NEW, *pskb);
362 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363
Patrick McHardydf0933d2006-09-20 11:57:53 -0700364out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800365 NF_CT_STAT_INC(insert_failed);
366 write_unlock_bh(&nf_conntrack_lock);
367 return NF_DROP;
368}
Patrick McHardy13b18332006-12-02 22:11:25 -0800369EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800370
371/* Returns true if a connection correspondings to the tuple (required
372 for NAT). */
373int
374nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
375 const struct nf_conn *ignored_conntrack)
376{
377 struct nf_conntrack_tuple_hash *h;
378
379 read_lock_bh(&nf_conntrack_lock);
380 h = __nf_conntrack_find(tuple, ignored_conntrack);
381 read_unlock_bh(&nf_conntrack_lock);
382
383 return h != NULL;
384}
Patrick McHardy13b18332006-12-02 22:11:25 -0800385EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386
387/* There's a small race here where we may free a just-assured
388 connection. Too bad: we're in trouble anyway. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800389static int early_drop(struct list_head *chain)
390{
391 /* Traverse backwards: gives us oldest, which is roughly LRU */
392 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700393 struct nf_conn *ct = NULL, *tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394 int dropped = 0;
395
396 read_lock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700397 list_for_each_entry_reverse(h, chain, list) {
398 tmp = nf_ct_tuplehash_to_ctrack(h);
399 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
400 ct = tmp;
401 atomic_inc(&ct->ct_general.use);
402 break;
403 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 }
405 read_unlock_bh(&nf_conntrack_lock);
406
407 if (!ct)
408 return dropped;
409
410 if (del_timer(&ct->timeout)) {
411 death_by_timeout((unsigned long)ct);
412 dropped = 1;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800413 NF_CT_STAT_INC_ATOMIC(early_drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414 }
415 nf_ct_put(ct);
416 return dropped;
417}
418
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700419struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
420 const struct nf_conntrack_tuple *repl)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421{
422 struct nf_conn *conntrack = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423
Harald Weltedc808fe2006-03-20 17:56:32 -0800424 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425 get_random_bytes(&nf_conntrack_hash_rnd, 4);
426 nf_conntrack_hash_rnd_initted = 1;
427 }
428
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700429 /* We don't want any race condition at early drop stage */
430 atomic_inc(&nf_conntrack_count);
431
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 if (nf_conntrack_max
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700433 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800434 unsigned int hash = hash_conntrack(orig);
435 /* Try dropping from this hash chain. */
436 if (!early_drop(&nf_conntrack_hash[hash])) {
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700437 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800438 if (net_ratelimit())
439 printk(KERN_WARNING
440 "nf_conntrack: table full, dropping"
441 " packet.\n");
442 return ERR_PTR(-ENOMEM);
443 }
444 }
445
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700446 conntrack = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 if (conntrack == NULL) {
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700448 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack.\n");
449 atomic_dec(&nf_conntrack_count);
450 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800451 }
452
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453 atomic_set(&conntrack->ct_general.use, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800454 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
455 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
456 /* Don't set timer yet: wait for confirmation */
Patrick McHardye6f689d2007-03-23 11:16:30 -0700457 setup_timer(&conntrack->timeout, death_by_timeout,
458 (unsigned long)conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700460 return conntrack;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461}
Patrick McHardy13b18332006-12-02 22:11:25 -0800462EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800463
464void nf_conntrack_free(struct nf_conn *conntrack)
465{
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700466 nf_ct_ext_free(conntrack);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700467 kmem_cache_free(nf_conntrack_cachep, conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 atomic_dec(&nf_conntrack_count);
469}
Patrick McHardy13b18332006-12-02 22:11:25 -0800470EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800471
472/* Allocate a new conntrack: we return -ENOMEM if classification
473 failed due to stress. Otherwise it really is unclassifiable. */
474static struct nf_conntrack_tuple_hash *
475init_conntrack(const struct nf_conntrack_tuple *tuple,
476 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100477 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800478 struct sk_buff *skb,
479 unsigned int dataoff)
480{
481 struct nf_conn *conntrack;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700482 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800483 struct nf_conntrack_tuple repl_tuple;
484 struct nf_conntrack_expect *exp;
485
Martin Josefsson605dcad2006-11-29 02:35:06 +0100486 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800487 DEBUGP("Can't invert tuple.\n");
488 return NULL;
489 }
490
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700491 conntrack = nf_conntrack_alloc(tuple, &repl_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800492 if (conntrack == NULL || IS_ERR(conntrack)) {
493 DEBUGP("Can't allocate conntrack.\n");
494 return (struct nf_conntrack_tuple_hash *)conntrack;
495 }
496
Martin Josefsson605dcad2006-11-29 02:35:06 +0100497 if (!l4proto->new(conntrack, skb, dataoff)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498 nf_conntrack_free(conntrack);
499 DEBUGP("init conntrack: can't track with proto module\n");
500 return NULL;
501 }
502
503 write_lock_bh(&nf_conntrack_lock);
504 exp = find_expectation(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505 if (exp) {
506 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
507 conntrack, exp);
508 /* Welcome, Mr. Bond. We've been expecting you... */
509 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
510 conntrack->master = exp->master;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700511 if (exp->helper) {
512 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
513 GFP_ATOMIC);
514 if (help)
515 rcu_assign_pointer(help->helper, exp->helper);
516 else
517 DEBUGP("failed to add helper extension area");
518 }
519
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520#ifdef CONFIG_NF_CONNTRACK_MARK
521 conntrack->mark = exp->master->mark;
522#endif
James Morris7c9728c2006-06-09 00:31:46 -0700523#ifdef CONFIG_NF_CONNTRACK_SECMARK
524 conntrack->secmark = exp->master->secmark;
525#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800526 nf_conntrack_get(&conntrack->master->ct_general);
527 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800528 } else {
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700529 struct nf_conntrack_helper *helper;
530
531 helper = __nf_ct_helper_find(&repl_tuple);
532 if (helper) {
533 help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER,
534 GFP_ATOMIC);
535 if (help)
536 /* not in hash table yet, so not strictly
537 necessary */
538 rcu_assign_pointer(help->helper, helper);
539 else
540 DEBUGP("failed to add helper extension area");
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700541 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800542 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800543 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544
545 /* Overload tuple linked list to put us in unconfirmed list. */
546 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
547
548 write_unlock_bh(&nf_conntrack_lock);
549
550 if (exp) {
551 if (exp->expectfn)
552 exp->expectfn(conntrack, exp);
553 nf_conntrack_expect_put(exp);
554 }
555
556 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
557}
558
559/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
560static inline struct nf_conn *
561resolve_normal_ct(struct sk_buff *skb,
562 unsigned int dataoff,
563 u_int16_t l3num,
564 u_int8_t protonum,
565 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100566 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800567 int *set_reply,
568 enum ip_conntrack_info *ctinfo)
569{
570 struct nf_conntrack_tuple tuple;
571 struct nf_conntrack_tuple_hash *h;
572 struct nf_conn *ct;
573
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300574 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800575 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100576 l4proto)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800577 DEBUGP("resolve_normal_ct: Can't get tuple\n");
578 return NULL;
579 }
580
581 /* look for tuple match */
582 h = nf_conntrack_find_get(&tuple, NULL);
583 if (!h) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100584 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800585 if (!h)
586 return NULL;
587 if (IS_ERR(h))
588 return (void *)h;
589 }
590 ct = nf_ct_tuplehash_to_ctrack(h);
591
592 /* It exists; we have (non-exclusive) reference. */
593 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
594 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
595 /* Please set reply bit if this packet OK */
596 *set_reply = 1;
597 } else {
598 /* Once we've had two way comms, always ESTABLISHED. */
599 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
600 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
601 *ctinfo = IP_CT_ESTABLISHED;
602 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
603 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
604 *ctinfo = IP_CT_RELATED;
605 } else {
606 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
607 *ctinfo = IP_CT_NEW;
608 }
609 *set_reply = 0;
610 }
611 skb->nfct = &ct->ct_general;
612 skb->nfctinfo = *ctinfo;
613 return ct;
614}
615
616unsigned int
617nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
618{
619 struct nf_conn *ct;
620 enum ip_conntrack_info ctinfo;
621 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100622 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800623 unsigned int dataoff;
624 u_int8_t protonum;
625 int set_reply = 0;
626 int ret;
627
628 /* Previously seen (loopback or untracked)? Ignore. */
629 if ((*pskb)->nfct) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800630 NF_CT_STAT_INC_ATOMIC(ignore);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800631 return NF_ACCEPT;
632 }
633
Patrick McHardy923f4902007-02-12 11:12:57 -0800634 /* rcu_read_lock()ed by nf_hook_slow */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800635 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
Patrick McHardy923f4902007-02-12 11:12:57 -0800636
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800637 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
638 DEBUGP("not prepared to track yet or error occured\n");
639 return -ret;
640 }
641
Martin Josefsson605dcad2006-11-29 02:35:06 +0100642 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800643
644 /* It may be an special packet, error, unclean...
645 * inverse of the return code tells to the netfilter
646 * core what to do with the packet. */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100647 if (l4proto->error != NULL &&
648 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800649 NF_CT_STAT_INC_ATOMIC(error);
650 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800651 return -ret;
652 }
653
Martin Josefsson605dcad2006-11-29 02:35:06 +0100654 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800655 &set_reply, &ctinfo);
656 if (!ct) {
657 /* Not valid part of a connection */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800658 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800659 return NF_ACCEPT;
660 }
661
662 if (IS_ERR(ct)) {
663 /* Too stressed to deal. */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800664 NF_CT_STAT_INC_ATOMIC(drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800665 return NF_DROP;
666 }
667
668 NF_CT_ASSERT((*pskb)->nfct);
669
Martin Josefsson605dcad2006-11-29 02:35:06 +0100670 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671 if (ret < 0) {
672 /* Invalid: inverse of the return code tells
673 * the netfilter core what to do */
674 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
675 nf_conntrack_put((*pskb)->nfct);
676 (*pskb)->nfct = NULL;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800677 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800678 return -ret;
679 }
680
681 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
682 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
683
684 return ret;
685}
Patrick McHardy13b18332006-12-02 22:11:25 -0800686EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800687
688int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
689 const struct nf_conntrack_tuple *orig)
690{
Patrick McHardy923f4902007-02-12 11:12:57 -0800691 int ret;
692
693 rcu_read_lock();
694 ret = nf_ct_invert_tuple(inverse, orig,
695 __nf_ct_l3proto_find(orig->src.l3num),
696 __nf_ct_l4proto_find(orig->src.l3num,
697 orig->dst.protonum));
698 rcu_read_unlock();
699 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800700}
Patrick McHardy13b18332006-12-02 22:11:25 -0800701EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800702
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800703/* Alter reply tuple (maybe alter helper). This is for NAT, and is
704 implicitly racy: see __nf_conntrack_confirm */
705void nf_conntrack_alter_reply(struct nf_conn *ct,
706 const struct nf_conntrack_tuple *newreply)
707{
708 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700709 struct nf_conntrack_helper *helper;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800710
711 write_lock_bh(&nf_conntrack_lock);
712 /* Should be unconfirmed, so not in hash table yet */
713 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
714
715 DEBUGP("Altering reply tuple of %p to ", ct);
716 NF_CT_DUMP_TUPLE(newreply);
717
718 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700719 if (ct->master || (help && help->expecting != 0))
720 goto out;
721
722 helper = __nf_ct_helper_find(newreply);
723 if (helper == NULL) {
724 if (help)
725 rcu_assign_pointer(help->helper, NULL);
726 goto out;
Yasuyuki Kozakai5d78a842007-05-10 14:16:24 -0700727 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700728
729 if (help == NULL) {
730 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_ATOMIC);
731 if (help == NULL) {
732 DEBUGP("failed to add helper extension area");
733 goto out;
734 }
735 } else {
736 memset(&help->help, 0, sizeof(help->help));
737 }
738
739 rcu_assign_pointer(help->helper, helper);
740out:
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800741 write_unlock_bh(&nf_conntrack_lock);
742}
Patrick McHardy13b18332006-12-02 22:11:25 -0800743EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800744
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800745/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
746void __nf_ct_refresh_acct(struct nf_conn *ct,
747 enum ip_conntrack_info ctinfo,
748 const struct sk_buff *skb,
749 unsigned long extra_jiffies,
750 int do_acct)
751{
752 int event = 0;
753
754 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
755 NF_CT_ASSERT(skb);
756
757 write_lock_bh(&nf_conntrack_lock);
758
Eric Leblond997ae832006-05-29 18:24:20 -0700759 /* Only update if this is not a fixed timeout */
760 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
761 write_unlock_bh(&nf_conntrack_lock);
762 return;
763 }
764
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800765 /* If not in hash table, timer will not be active yet */
766 if (!nf_ct_is_confirmed(ct)) {
767 ct->timeout.expires = extra_jiffies;
768 event = IPCT_REFRESH;
769 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100770 unsigned long newtime = jiffies + extra_jiffies;
771
772 /* Only update the timeout if the new timeout is at least
773 HZ jiffies from the old timeout. Need del_timer for race
774 avoidance (may already be dying). */
775 if (newtime - ct->timeout.expires >= HZ
776 && del_timer(&ct->timeout)) {
777 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800778 add_timer(&ct->timeout);
779 event = IPCT_REFRESH;
780 }
781 }
782
783#ifdef CONFIG_NF_CT_ACCT
784 if (do_acct) {
785 ct->counters[CTINFO2DIR(ctinfo)].packets++;
786 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300787 skb->len - skb_network_offset(skb);
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100788
789 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
790 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
791 event |= IPCT_COUNTER_FILLING;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800792 }
793#endif
794
795 write_unlock_bh(&nf_conntrack_lock);
796
797 /* must be unlocked when calling event cache */
798 if (event)
799 nf_conntrack_event_cache(event, skb);
800}
Patrick McHardy13b18332006-12-02 22:11:25 -0800801EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800802
Patrick McHardye281db5c2007-03-04 15:57:25 -0800803#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800804
805#include <linux/netfilter/nfnetlink.h>
806#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800807#include <linux/mutex.h>
808
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800809
810/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
811 * in ip_conntrack_core, since we don't want the protocols to autoload
812 * or depend on ctnetlink */
813int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
814 const struct nf_conntrack_tuple *tuple)
815{
816 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
817 &tuple->src.u.tcp.port);
818 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
819 &tuple->dst.u.tcp.port);
820 return 0;
821
822nfattr_failure:
823 return -1;
824}
Patrick McHardy13b18332006-12-02 22:11:25 -0800825EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800826
827static const size_t cta_min_proto[CTA_PROTO_MAX] = {
828 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
829 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
830};
831
832int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
833 struct nf_conntrack_tuple *t)
834{
835 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
836 return -EINVAL;
837
838 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
839 return -EINVAL;
840
Patrick McHardybff9a892006-12-02 22:05:08 -0800841 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
842 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800843
844 return 0;
845}
Patrick McHardy13b18332006-12-02 22:11:25 -0800846EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800847#endif
848
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800849/* Used by ipt_REJECT and ip6t_REJECT. */
850void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
851{
852 struct nf_conn *ct;
853 enum ip_conntrack_info ctinfo;
854
855 /* This ICMP is in reverse direction to the packet which caused it */
856 ct = nf_ct_get(skb, &ctinfo);
857 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
858 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
859 else
860 ctinfo = IP_CT_RELATED;
861
862 /* Attach to new skbuff, and increment count */
863 nskb->nfct = &ct->ct_general;
864 nskb->nfctinfo = ctinfo;
865 nf_conntrack_get(nskb->nfct);
866}
Patrick McHardy13b18332006-12-02 22:11:25 -0800867EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800868
869static inline int
870do_iter(const struct nf_conntrack_tuple_hash *i,
871 int (*iter)(struct nf_conn *i, void *data),
872 void *data)
873{
874 return iter(nf_ct_tuplehash_to_ctrack(i), data);
875}
876
877/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700878static struct nf_conn *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800879get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
880 void *data, unsigned int *bucket)
881{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700882 struct nf_conntrack_tuple_hash *h;
883 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800884
885 write_lock_bh(&nf_conntrack_lock);
886 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700887 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
888 ct = nf_ct_tuplehash_to_ctrack(h);
889 if (iter(ct, data))
890 goto found;
891 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800892 }
Patrick McHardydf0933d2006-09-20 11:57:53 -0700893 list_for_each_entry(h, &unconfirmed, list) {
894 ct = nf_ct_tuplehash_to_ctrack(h);
895 if (iter(ct, data))
Patrick McHardyec68e972007-03-04 15:57:01 -0800896 set_bit(IPS_DYING_BIT, &ct->status);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700897 }
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800898 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700899 return NULL;
900found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800901 atomic_inc(&ct->ct_general.use);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800902 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700903 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800904}
905
906void
907nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
908{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700909 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800910 unsigned int bucket = 0;
911
Patrick McHardydf0933d2006-09-20 11:57:53 -0700912 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800913 /* Time to push up daises... */
914 if (del_timer(&ct->timeout))
915 death_by_timeout((unsigned long)ct);
916 /* ... else the timer will get him soon. */
917
918 nf_ct_put(ct);
919 }
920}
Patrick McHardy13b18332006-12-02 22:11:25 -0800921EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800922
923static int kill_all(struct nf_conn *i, void *data)
924{
925 return 1;
926}
927
928static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
929{
930 if (vmalloced)
931 vfree(hash);
932 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800933 free_pages((unsigned long)hash,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800934 get_order(sizeof(struct list_head) * size));
935}
936
Randy Dunlap272491e2006-12-07 01:17:24 -0800937void nf_conntrack_flush(void)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800938{
939 nf_ct_iterate_cleanup(kill_all, NULL);
940}
Patrick McHardy13b18332006-12-02 22:11:25 -0800941EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800942
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800943/* Mishearing the voices in his head, our hero wonders how he's
944 supposed to kill the mall. */
945void nf_conntrack_cleanup(void)
946{
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800947 rcu_assign_pointer(ip_ct_attach, NULL);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -0800948
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800949 /* This makes sure all current packets have passed through
950 netfilter framework. Roll on, two-stage module
951 delete... */
952 synchronize_net();
953
954 nf_ct_event_cache_flush();
955 i_see_dead_people:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800956 nf_conntrack_flush();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800957 if (atomic_read(&nf_conntrack_count) != 0) {
958 schedule();
959 goto i_see_dead_people;
960 }
Patrick McHardy66365682005-12-05 13:36:50 -0800961 /* wait until all references to nf_conntrack_untracked are dropped */
962 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
963 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800964
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700965 rcu_assign_pointer(nf_ct_destroy, NULL);
966
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700967 kmem_cache_destroy(nf_conntrack_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800968 kmem_cache_destroy(nf_conntrack_expect_cachep);
969 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
970 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -0800971
Patrick McHardyac5357e2007-03-14 16:38:25 -0700972 nf_conntrack_proto_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700973 nf_conntrack_helper_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800974}
975
976static struct list_head *alloc_hashtable(int size, int *vmalloced)
977{
978 struct list_head *hash;
979 unsigned int i;
980
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800981 *vmalloced = 0;
982 hash = (void*)__get_free_pages(GFP_KERNEL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800983 get_order(sizeof(struct list_head)
984 * size));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800985 if (!hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800986 *vmalloced = 1;
987 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
988 hash = vmalloc(sizeof(struct list_head) * size);
989 }
990
991 if (hash)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800992 for (i = 0; i < size; i++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800993 INIT_LIST_HEAD(&hash[i]);
994
995 return hash;
996}
997
998int set_hashsize(const char *val, struct kernel_param *kp)
999{
1000 int i, bucket, hashsize, vmalloced;
1001 int old_vmalloced, old_size;
1002 int rnd;
1003 struct list_head *hash, *old_hash;
1004 struct nf_conntrack_tuple_hash *h;
1005
1006 /* On boot, we can set this without any fancy locking. */
1007 if (!nf_conntrack_htable_size)
1008 return param_set_uint(val, kp);
1009
1010 hashsize = simple_strtol(val, NULL, 0);
1011 if (!hashsize)
1012 return -EINVAL;
1013
1014 hash = alloc_hashtable(hashsize, &vmalloced);
1015 if (!hash)
1016 return -ENOMEM;
1017
1018 /* We have to rehahs for the new table anyway, so we also can
1019 * use a newrandom seed */
1020 get_random_bytes(&rnd, 4);
1021
1022 write_lock_bh(&nf_conntrack_lock);
1023 for (i = 0; i < nf_conntrack_htable_size; i++) {
1024 while (!list_empty(&nf_conntrack_hash[i])) {
1025 h = list_entry(nf_conntrack_hash[i].next,
1026 struct nf_conntrack_tuple_hash, list);
1027 list_del(&h->list);
1028 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1029 list_add_tail(&h->list, &hash[bucket]);
1030 }
1031 }
1032 old_size = nf_conntrack_htable_size;
1033 old_vmalloced = nf_conntrack_vmalloc;
1034 old_hash = nf_conntrack_hash;
1035
1036 nf_conntrack_htable_size = hashsize;
1037 nf_conntrack_vmalloc = vmalloced;
1038 nf_conntrack_hash = hash;
1039 nf_conntrack_hash_rnd = rnd;
1040 write_unlock_bh(&nf_conntrack_lock);
1041
1042 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1043 return 0;
1044}
1045
1046module_param_call(hashsize, set_hashsize, param_get_uint,
1047 &nf_conntrack_htable_size, 0600);
1048
1049int __init nf_conntrack_init(void)
1050{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001051 int ret;
1052
1053 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1054 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1055 if (!nf_conntrack_htable_size) {
1056 nf_conntrack_htable_size
1057 = (((num_physpages << PAGE_SHIFT) / 16384)
1058 / sizeof(struct list_head));
1059 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1060 nf_conntrack_htable_size = 8192;
1061 if (nf_conntrack_htable_size < 16)
1062 nf_conntrack_htable_size = 16;
1063 }
1064 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1065
1066 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1067 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1068 nf_conntrack_max);
1069
1070 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1071 &nf_conntrack_vmalloc);
1072 if (!nf_conntrack_hash) {
1073 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1074 goto err_out;
1075 }
1076
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001077 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1078 sizeof(struct nf_conn),
1079 0, 0, NULL, NULL);
1080 if (!nf_conntrack_cachep) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001081 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1082 goto err_free_hash;
1083 }
1084
1085 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1086 sizeof(struct nf_conntrack_expect),
1087 0, 0, NULL, NULL);
1088 if (!nf_conntrack_expect_cachep) {
1089 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1090 goto err_free_conntrack_slab;
1091 }
1092
Patrick McHardyac5357e2007-03-14 16:38:25 -07001093 ret = nf_conntrack_proto_init();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001094 if (ret < 0)
1095 goto out_free_expect_slab;
1096
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001097 ret = nf_conntrack_helper_init();
1098 if (ret < 0)
1099 goto out_fini_proto;
1100
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001101 /* For use by REJECT target */
Patrick McHardyc3a47ab2007-02-12 11:09:19 -08001102 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001103 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001104
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001105 /* Set up fake conntrack:
1106 - to never be deleted, not in any hashes */
1107 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1108 /* - and look it like as a confirmed connection */
1109 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1110
1111 return ret;
1112
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001113out_fini_proto:
1114 nf_conntrack_proto_fini();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001115out_free_expect_slab:
1116 kmem_cache_destroy(nf_conntrack_expect_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001117err_free_conntrack_slab:
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001118 kmem_cache_destroy(nf_conntrack_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001119err_free_hash:
1120 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1121 nf_conntrack_htable_size);
1122err_out:
1123 return -ENOMEM;
1124}