blob: 1a3673d558682d6dbc33122d2381278bcbc6f493 [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
Patrick McHardyf8ba1af2008-01-31 04:38:58 -080043DEFINE_SPINLOCK(nf_conntrack_lock);
Patrick McHardy13b18332006-12-02 22:11:25 -080044EXPORT_SYMBOL_GPL(nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045
46/* nf_conntrack_standalone needs this */
47atomic_t nf_conntrack_count = ATOMIC_INIT(0);
Patrick McHardya999e682006-11-29 02:35:20 +010048EXPORT_SYMBOL_GPL(nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080049
Martin Josefssone2b76062006-11-29 02:35:04 +010050unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080051EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
52
Brian Haley94aec082006-09-18 00:05:22 -070053int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010054EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080055
Patrick McHardyf205c5e2007-07-07 22:28:14 -070056struct hlist_head *nf_conntrack_hash __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080057EXPORT_SYMBOL_GPL(nf_conntrack_hash);
58
Martin Josefssone2b76062006-11-29 02:35:04 +010059struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080060EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
61
Brian Haley94aec082006-09-18 00:05:22 -070062unsigned int nf_ct_log_invalid __read_mostly;
Patrick McHardyf205c5e2007-07-07 22:28:14 -070063HLIST_HEAD(unconfirmed);
Brian Haley1192e402006-09-20 12:03:46 -070064static int nf_conntrack_vmalloc __read_mostly;
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -070065static struct kmem_cache *nf_conntrack_cachep __read_mostly;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010066
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
68EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
69
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070static int nf_conntrack_hash_rnd_initted;
71static unsigned int nf_conntrack_hash_rnd;
72
73static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
74 unsigned int size, unsigned int rnd)
75{
Patrick McHardy07949352008-01-31 04:40:52 -080076 unsigned int n;
77 u_int32_t h;
Sami Farin9b887902007-03-14 16:43:00 -070078
Patrick McHardy07949352008-01-31 04:40:52 -080079 /* The direction must be ignored, so we hash everything up to the
80 * destination ports (which is a multiple of 4) and treat the last
81 * three bytes manually.
82 */
83 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
84 h = jhash2((u32 *)tuple, n,
85 rnd ^ (((__force __u16)tuple->dst.u.all << 16) |
86 tuple->dst.protonum));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080087
Patrick McHardy07949352008-01-31 04:40:52 -080088 return ((u64)h * size) >> 32;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089}
90
91static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
92{
93 return __hash_conntrack(tuple, nf_conntrack_htable_size,
94 nf_conntrack_hash_rnd);
95}
96
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097int
98nf_ct_get_tuple(const struct sk_buff *skb,
99 unsigned int nhoff,
100 unsigned int dataoff,
101 u_int16_t l3num,
102 u_int8_t protonum,
103 struct nf_conntrack_tuple *tuple,
104 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100105 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106{
107 NF_CT_TUPLE_U_BLANK(tuple);
108
109 tuple->src.l3num = l3num;
110 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
111 return 0;
112
113 tuple->dst.protonum = protonum;
114 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
115
Martin Josefsson605dcad2006-11-29 02:35:06 +0100116 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117}
Patrick McHardy13b18332006-12-02 22:11:25 -0800118EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700120int nf_ct_get_tuplepr(const struct sk_buff *skb,
121 unsigned int nhoff,
122 u_int16_t l3num,
123 struct nf_conntrack_tuple *tuple)
124{
125 struct nf_conntrack_l3proto *l3proto;
126 struct nf_conntrack_l4proto *l4proto;
127 unsigned int protoff;
128 u_int8_t protonum;
129 int ret;
130
131 rcu_read_lock();
132
133 l3proto = __nf_ct_l3proto_find(l3num);
134 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
135 if (ret != NF_ACCEPT) {
136 rcu_read_unlock();
137 return 0;
138 }
139
140 l4proto = __nf_ct_l4proto_find(l3num, protonum);
141
142 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, tuple,
143 l3proto, l4proto);
144
145 rcu_read_unlock();
146 return ret;
147}
148EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
149
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150int
151nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
152 const struct nf_conntrack_tuple *orig,
153 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100154 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155{
156 NF_CT_TUPLE_U_BLANK(inverse);
157
158 inverse->src.l3num = orig->src.l3num;
159 if (l3proto->invert_tuple(inverse, orig) == 0)
160 return 0;
161
162 inverse->dst.dir = !orig->dst.dir;
163
164 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100165 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166}
Patrick McHardy13b18332006-12-02 22:11:25 -0800167EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169static void
170clean_from_lists(struct nf_conn *ct)
171{
Patrick McHardy0d537782007-07-07 22:39:38 -0700172 pr_debug("clean_from_lists(%p)\n", ct);
Patrick McHardy76507f62008-01-31 04:38:38 -0800173 hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
174 hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800175
176 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800177 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178}
179
180static void
181destroy_conntrack(struct nf_conntrack *nfct)
182{
183 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100184 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185
Patrick McHardy0d537782007-07-07 22:39:38 -0700186 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
188 NF_CT_ASSERT(!timer_pending(&ct->timeout));
189
190 nf_conntrack_event(IPCT_DESTROY, ct);
191 set_bit(IPS_DYING_BIT, &ct->status);
192
193 /* To make sure we don't get any weird locking issues here:
194 * destroy_conntrack() MUST NOT be called with a write lock
195 * to nf_conntrack_lock!!! -HW */
Patrick McHardy923f4902007-02-12 11:12:57 -0800196 rcu_read_lock();
Patrick McHardy923f4902007-02-12 11:12:57 -0800197 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
198 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100199 if (l4proto && l4proto->destroy)
200 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700202 nf_ct_ext_destroy(ct);
203
Patrick McHardy982d9a92007-02-12 11:14:11 -0800204 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800206 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207 /* Expectations will have been removed in clean_from_lists,
208 * except TFTP can create an expectation on the first packet,
209 * before connection is in the list, so we need to clean here,
210 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800211 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212
213 /* We overload first tuple to link into unconfirmed list. */
214 if (!nf_ct_is_confirmed(ct)) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700215 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
216 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800217 }
218
219 NF_CT_STAT_INC(delete);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800220 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221
222 if (ct->master)
223 nf_ct_put(ct->master);
224
Patrick McHardy0d537782007-07-07 22:39:38 -0700225 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226 nf_conntrack_free(ct);
227}
228
229static void death_by_timeout(unsigned long ul_conntrack)
230{
231 struct nf_conn *ct = (void *)ul_conntrack;
Patrick McHardy5397e972007-05-19 14:23:52 -0700232 struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700233 struct nf_conntrack_helper *helper;
Patrick McHardy5397e972007-05-19 14:23:52 -0700234
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700235 if (help) {
236 rcu_read_lock();
237 helper = rcu_dereference(help->helper);
238 if (helper && helper->destroy)
239 helper->destroy(ct);
240 rcu_read_unlock();
241 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800243 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244 /* Inside lock so preempt is disabled on module removal path.
245 * Otherwise we can get spurious warnings. */
246 NF_CT_STAT_INC(delete_list);
247 clean_from_lists(ct);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800248 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800249 nf_ct_put(ct);
250}
251
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800252struct nf_conntrack_tuple_hash *
Patrick McHardyba419af2008-01-31 04:39:23 -0800253__nf_conntrack_find(const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254{
255 struct nf_conntrack_tuple_hash *h;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700256 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 unsigned int hash = hash_conntrack(tuple);
258
Patrick McHardy76507f62008-01-31 04:38:38 -0800259 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], hnode) {
Patrick McHardyba419af2008-01-31 04:39:23 -0800260 if (nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800261 NF_CT_STAT_INC(found);
262 return h;
263 }
264 NF_CT_STAT_INC(searched);
265 }
266
267 return NULL;
268}
Patrick McHardy13b18332006-12-02 22:11:25 -0800269EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270
271/* Find a connection corresponding to a tuple. */
272struct nf_conntrack_tuple_hash *
Patrick McHardy330f7db2007-07-07 22:28:42 -0700273nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800274{
275 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800276 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800277
Patrick McHardy76507f62008-01-31 04:38:38 -0800278 rcu_read_lock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800279 h = __nf_conntrack_find(tuple);
Patrick McHardy76507f62008-01-31 04:38:38 -0800280 if (h) {
281 ct = nf_ct_tuplehash_to_ctrack(h);
282 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
283 h = NULL;
284 }
285 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800286
287 return h;
288}
Patrick McHardy13b18332006-12-02 22:11:25 -0800289EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800291static void __nf_conntrack_hash_insert(struct nf_conn *ct,
292 unsigned int hash,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800293 unsigned int repl_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800294{
Patrick McHardy76507f62008-01-31 04:38:38 -0800295 hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
296 &nf_conntrack_hash[hash]);
297 hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode,
298 &nf_conntrack_hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800299}
300
301void nf_conntrack_hash_insert(struct nf_conn *ct)
302{
303 unsigned int hash, repl_hash;
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
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800308 spin_lock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800309 __nf_conntrack_hash_insert(ct, hash, repl_hash);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800310 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800311}
Patrick McHardy13b18332006-12-02 22:11:25 -0800312EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800313
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800314/* Confirm a connection given skb; places it in hash table */
315int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700316__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800317{
318 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700319 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700321 struct nf_conn_help *help;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700322 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323 enum ip_conntrack_info ctinfo;
324
Herbert Xu3db05fe2007-10-15 00:53:15 -0700325 ct = nf_ct_get(skb, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800326
327 /* ipt_REJECT uses nf_conntrack_attach to attach related
328 ICMP/TCP RST packets in other direction. Actual packet
329 which created connection will be IP_CT_NEW or for an
330 expected connection, IP_CT_RELATED. */
331 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
332 return NF_ACCEPT;
333
334 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
335 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
336
337 /* We're not in hash table, and we refuse to set up related
338 connections for unconfirmed conns. But packet copies and
339 REJECT will give spurious warnings here. */
340 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
341
342 /* No external references means noone else could have
343 confirmed us. */
344 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700345 pr_debug("Confirming conntrack %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800347 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800348
349 /* See if there's one in the list already, including reverse:
350 NAT could have grabbed it without realizing, since we're
351 not in the hash. If there is, we lost race. */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700352 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700353 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
354 &h->tuple))
355 goto out;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700356 hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700357 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
358 &h->tuple))
359 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800360
Patrick McHardydf0933d2006-09-20 11:57:53 -0700361 /* Remove from unconfirmed list */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700362 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700363
364 __nf_conntrack_hash_insert(ct, hash, repl_hash);
365 /* Timer relative to confirmation time, not original
366 setting time, otherwise we'd get timer wrap in
367 weird delay cases. */
368 ct->timeout.expires += jiffies;
369 add_timer(&ct->timeout);
370 atomic_inc(&ct->ct_general.use);
371 set_bit(IPS_CONFIRMED_BIT, &ct->status);
372 NF_CT_STAT_INC(insert);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800373 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700374 help = nfct_help(ct);
375 if (help && help->helper)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700376 nf_conntrack_event_cache(IPCT_HELPER, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700378 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
379 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700380 nf_conntrack_event_cache(IPCT_NATINFO, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800381#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700382 nf_conntrack_event_cache(master_ct(ct) ?
Herbert Xu3db05fe2007-10-15 00:53:15 -0700383 IPCT_RELATED : IPCT_NEW, skb);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700384 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385
Patrick McHardydf0933d2006-09-20 11:57:53 -0700386out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387 NF_CT_STAT_INC(insert_failed);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800388 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800389 return NF_DROP;
390}
Patrick McHardy13b18332006-12-02 22:11:25 -0800391EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392
393/* Returns true if a connection correspondings to the tuple (required
394 for NAT). */
395int
396nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
397 const struct nf_conn *ignored_conntrack)
398{
399 struct nf_conntrack_tuple_hash *h;
Patrick McHardyba419af2008-01-31 04:39:23 -0800400 struct hlist_node *n;
401 unsigned int hash = hash_conntrack(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800402
Patrick McHardy76507f62008-01-31 04:38:38 -0800403 rcu_read_lock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800404 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], hnode) {
405 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
406 nf_ct_tuple_equal(tuple, &h->tuple)) {
407 NF_CT_STAT_INC(found);
408 rcu_read_unlock();
409 return 1;
410 }
411 NF_CT_STAT_INC(searched);
412 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800413 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414
Patrick McHardyba419af2008-01-31 04:39:23 -0800415 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416}
Patrick McHardy13b18332006-12-02 22:11:25 -0800417EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418
Patrick McHardy7ae77302007-07-07 22:37:38 -0700419#define NF_CT_EVICTION_RANGE 8
420
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421/* There's a small race here where we may free a just-assured
422 connection. Too bad: we're in trouble anyway. */
Patrick McHardy76eb9462008-01-31 04:41:44 -0800423static noinline int early_drop(unsigned int hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800424{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700425 /* Use oldest entry, which is roughly LRU */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700427 struct nf_conn *ct = NULL, *tmp;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700428 struct hlist_node *n;
Patrick McHardy7ae77302007-07-07 22:37:38 -0700429 unsigned int i, cnt = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800430 int dropped = 0;
431
Patrick McHardy76507f62008-01-31 04:38:38 -0800432 rcu_read_lock();
Patrick McHardy7ae77302007-07-07 22:37:38 -0700433 for (i = 0; i < nf_conntrack_htable_size; i++) {
Patrick McHardy76507f62008-01-31 04:38:38 -0800434 hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash],
435 hnode) {
Patrick McHardy7ae77302007-07-07 22:37:38 -0700436 tmp = nf_ct_tuplehash_to_ctrack(h);
437 if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
438 ct = tmp;
439 cnt++;
440 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800441
442 if (ct && unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
443 ct = NULL;
Patrick McHardy7ae77302007-07-07 22:37:38 -0700444 if (ct || cnt >= NF_CT_EVICTION_RANGE)
445 break;
446 hash = (hash + 1) % nf_conntrack_htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800448 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449
450 if (!ct)
451 return dropped;
452
453 if (del_timer(&ct->timeout)) {
454 death_by_timeout((unsigned long)ct);
455 dropped = 1;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800456 NF_CT_STAT_INC_ATOMIC(early_drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800457 }
458 nf_ct_put(ct);
459 return dropped;
460}
461
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700462struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
463 const struct nf_conntrack_tuple *repl)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800464{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800465 struct nf_conn *ct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466
Harald Weltedc808fe2006-03-20 17:56:32 -0800467 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 get_random_bytes(&nf_conntrack_hash_rnd, 4);
469 nf_conntrack_hash_rnd_initted = 1;
470 }
471
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700472 /* We don't want any race condition at early drop stage */
473 atomic_inc(&nf_conntrack_count);
474
Patrick McHardy76eb9462008-01-31 04:41:44 -0800475 if (nf_conntrack_max &&
476 unlikely(atomic_read(&nf_conntrack_count) > nf_conntrack_max)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 unsigned int hash = hash_conntrack(orig);
Patrick McHardy7ae77302007-07-07 22:37:38 -0700478 if (!early_drop(hash)) {
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700479 atomic_dec(&nf_conntrack_count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 if (net_ratelimit())
481 printk(KERN_WARNING
482 "nf_conntrack: table full, dropping"
483 " packet.\n");
484 return ERR_PTR(-ENOMEM);
485 }
486 }
487
Patrick McHardyc88130b2008-01-31 04:42:11 -0800488 ct = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC);
489 if (ct == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700490 pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n");
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700491 atomic_dec(&nf_conntrack_count);
492 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800493 }
494
Patrick McHardyc88130b2008-01-31 04:42:11 -0800495 atomic_set(&ct->ct_general.use, 1);
496 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
497 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498 /* Don't set timer yet: wait for confirmation */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800499 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
500 INIT_RCU_HEAD(&ct->rcu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800501
Patrick McHardyc88130b2008-01-31 04:42:11 -0800502 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800503}
Patrick McHardy13b18332006-12-02 22:11:25 -0800504EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505
Patrick McHardy76507f62008-01-31 04:38:38 -0800506static void nf_conntrack_free_rcu(struct rcu_head *head)
507{
508 struct nf_conn *ct = container_of(head, struct nf_conn, rcu);
509
510 nf_ct_ext_free(ct);
511 kmem_cache_free(nf_conntrack_cachep, ct);
512 atomic_dec(&nf_conntrack_count);
513}
514
Patrick McHardyc88130b2008-01-31 04:42:11 -0800515void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800517 call_rcu(&ct->rcu, nf_conntrack_free_rcu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800518}
Patrick McHardy13b18332006-12-02 22:11:25 -0800519EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520
521/* Allocate a new conntrack: we return -ENOMEM if classification
522 failed due to stress. Otherwise it really is unclassifiable. */
523static struct nf_conntrack_tuple_hash *
524init_conntrack(const struct nf_conntrack_tuple *tuple,
525 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100526 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800527 struct sk_buff *skb,
528 unsigned int dataoff)
529{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800530 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700531 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800532 struct nf_conntrack_tuple repl_tuple;
533 struct nf_conntrack_expect *exp;
534
Martin Josefsson605dcad2006-11-29 02:35:06 +0100535 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700536 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800537 return NULL;
538 }
539
Patrick McHardyc88130b2008-01-31 04:42:11 -0800540 ct = nf_conntrack_alloc(tuple, &repl_tuple);
541 if (ct == NULL || IS_ERR(ct)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700542 pr_debug("Can't allocate conntrack.\n");
Patrick McHardyc88130b2008-01-31 04:42:11 -0800543 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544 }
545
Patrick McHardyc88130b2008-01-31 04:42:11 -0800546 if (!l4proto->new(ct, skb, dataoff)) {
547 nf_conntrack_free(ct);
Patrick McHardy0d537782007-07-07 22:39:38 -0700548 pr_debug("init conntrack: can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800549 return NULL;
550 }
551
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800552 spin_lock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -0700553 exp = nf_ct_find_expectation(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800554 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700555 pr_debug("conntrack: expectation arrives ct=%p exp=%p\n",
Patrick McHardyc88130b2008-01-31 04:42:11 -0800556 ct, exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800557 /* Welcome, Mr. Bond. We've been expecting you... */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800558 __set_bit(IPS_EXPECTED_BIT, &ct->status);
559 ct->master = exp->master;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700560 if (exp->helper) {
Patrick McHardyc88130b2008-01-31 04:42:11 -0800561 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700562 if (help)
563 rcu_assign_pointer(help->helper, exp->helper);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700564 }
565
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800566#ifdef CONFIG_NF_CONNTRACK_MARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800567 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800568#endif
James Morris7c9728c2006-06-09 00:31:46 -0700569#ifdef CONFIG_NF_CONNTRACK_SECMARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800570 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -0700571#endif
Patrick McHardyc88130b2008-01-31 04:42:11 -0800572 nf_conntrack_get(&ct->master->ct_general);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800573 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800574 } else {
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700575 struct nf_conntrack_helper *helper;
576
577 helper = __nf_ct_helper_find(&repl_tuple);
578 if (helper) {
Patrick McHardyc88130b2008-01-31 04:42:11 -0800579 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700580 if (help)
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700581 rcu_assign_pointer(help->helper, helper);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700582 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800583 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800584 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800585
586 /* Overload tuple linked list to put us in unconfirmed list. */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800587 hlist_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode, &unconfirmed);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800588
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800589 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800590
591 if (exp) {
592 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800593 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700594 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800595 }
596
Patrick McHardyc88130b2008-01-31 04:42:11 -0800597 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800598}
599
600/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
601static inline struct nf_conn *
602resolve_normal_ct(struct sk_buff *skb,
603 unsigned int dataoff,
604 u_int16_t l3num,
605 u_int8_t protonum,
606 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100607 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800608 int *set_reply,
609 enum ip_conntrack_info *ctinfo)
610{
611 struct nf_conntrack_tuple tuple;
612 struct nf_conntrack_tuple_hash *h;
613 struct nf_conn *ct;
614
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300615 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800616 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100617 l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700618 pr_debug("resolve_normal_ct: Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800619 return NULL;
620 }
621
622 /* look for tuple match */
Patrick McHardy330f7db2007-07-07 22:28:42 -0700623 h = nf_conntrack_find_get(&tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800624 if (!h) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100625 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800626 if (!h)
627 return NULL;
628 if (IS_ERR(h))
629 return (void *)h;
630 }
631 ct = nf_ct_tuplehash_to_ctrack(h);
632
633 /* It exists; we have (non-exclusive) reference. */
634 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
635 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
636 /* Please set reply bit if this packet OK */
637 *set_reply = 1;
638 } else {
639 /* Once we've had two way comms, always ESTABLISHED. */
640 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700641 pr_debug("nf_conntrack_in: normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642 *ctinfo = IP_CT_ESTABLISHED;
643 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700644 pr_debug("nf_conntrack_in: related packet for %p\n",
645 ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800646 *ctinfo = IP_CT_RELATED;
647 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700648 pr_debug("nf_conntrack_in: new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800649 *ctinfo = IP_CT_NEW;
650 }
651 *set_reply = 0;
652 }
653 skb->nfct = &ct->ct_general;
654 skb->nfctinfo = *ctinfo;
655 return ct;
656}
657
658unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700659nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800660{
661 struct nf_conn *ct;
662 enum ip_conntrack_info ctinfo;
663 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100664 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800665 unsigned int dataoff;
666 u_int8_t protonum;
667 int set_reply = 0;
668 int ret;
669
670 /* Previously seen (loopback or untracked)? Ignore. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700671 if (skb->nfct) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800672 NF_CT_STAT_INC_ATOMIC(ignore);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800673 return NF_ACCEPT;
674 }
675
Patrick McHardy923f4902007-02-12 11:12:57 -0800676 /* rcu_read_lock()ed by nf_hook_slow */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800677 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700678 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700679 &dataoff, &protonum);
680 if (ret <= 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700681 pr_debug("not prepared to track yet or error occured\n");
Yasuyuki Kozakaid87d8462007-07-14 20:44:23 -0700682 NF_CT_STAT_INC_ATOMIC(error);
683 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800684 return -ret;
685 }
686
Martin Josefsson605dcad2006-11-29 02:35:06 +0100687 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800688
689 /* It may be an special packet, error, unclean...
690 * inverse of the return code tells to the netfilter
691 * core what to do with the packet. */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100692 if (l4proto->error != NULL &&
Herbert Xu3db05fe2007-10-15 00:53:15 -0700693 (ret = l4proto->error(skb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800694 NF_CT_STAT_INC_ATOMIC(error);
695 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800696 return -ret;
697 }
698
Herbert Xu3db05fe2007-10-15 00:53:15 -0700699 ct = resolve_normal_ct(skb, dataoff, pf, protonum, l3proto, l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800700 &set_reply, &ctinfo);
701 if (!ct) {
702 /* Not valid part of a connection */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800703 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800704 return NF_ACCEPT;
705 }
706
707 if (IS_ERR(ct)) {
708 /* Too stressed to deal. */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800709 NF_CT_STAT_INC_ATOMIC(drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800710 return NF_DROP;
711 }
712
Herbert Xu3db05fe2007-10-15 00:53:15 -0700713 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800714
Herbert Xu3db05fe2007-10-15 00:53:15 -0700715 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800716 if (ret < 0) {
717 /* Invalid: inverse of the return code tells
718 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -0700719 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -0700720 nf_conntrack_put(skb->nfct);
721 skb->nfct = NULL;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800722 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800723 return -ret;
724 }
725
726 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700727 nf_conntrack_event_cache(IPCT_STATUS, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800728
729 return ret;
730}
Patrick McHardy13b18332006-12-02 22:11:25 -0800731EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800732
733int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
734 const struct nf_conntrack_tuple *orig)
735{
Patrick McHardy923f4902007-02-12 11:12:57 -0800736 int ret;
737
738 rcu_read_lock();
739 ret = nf_ct_invert_tuple(inverse, orig,
740 __nf_ct_l3proto_find(orig->src.l3num),
741 __nf_ct_l4proto_find(orig->src.l3num,
742 orig->dst.protonum));
743 rcu_read_unlock();
744 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800745}
Patrick McHardy13b18332006-12-02 22:11:25 -0800746EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800747
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800748/* Alter reply tuple (maybe alter helper). This is for NAT, and is
749 implicitly racy: see __nf_conntrack_confirm */
750void nf_conntrack_alter_reply(struct nf_conn *ct,
751 const struct nf_conntrack_tuple *newreply)
752{
753 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700754 struct nf_conntrack_helper *helper;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800755
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800756 /* Should be unconfirmed, so not in hash table yet */
757 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
758
Patrick McHardy0d537782007-07-07 22:39:38 -0700759 pr_debug("Altering reply tuple of %p to ", ct);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800760 NF_CT_DUMP_TUPLE(newreply);
761
762 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700763 if (ct->master || (help && help->expecting != 0))
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800764 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700765
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800766 rcu_read_lock();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700767 helper = __nf_ct_helper_find(newreply);
768 if (helper == NULL) {
769 if (help)
770 rcu_assign_pointer(help->helper, NULL);
771 goto out;
Yasuyuki Kozakai5d78a842007-05-10 14:16:24 -0700772 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700773
774 if (help == NULL) {
Patrick McHardyb5605802007-07-07 22:35:56 -0700775 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
776 if (help == NULL)
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700777 goto out;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700778 } else {
779 memset(&help->help, 0, sizeof(help->help));
780 }
781
782 rcu_assign_pointer(help->helper, helper);
783out:
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800784 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800785}
Patrick McHardy13b18332006-12-02 22:11:25 -0800786EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800787
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800788/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
789void __nf_ct_refresh_acct(struct nf_conn *ct,
790 enum ip_conntrack_info ctinfo,
791 const struct sk_buff *skb,
792 unsigned long extra_jiffies,
793 int do_acct)
794{
795 int event = 0;
796
797 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
798 NF_CT_ASSERT(skb);
799
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800800 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800801
Eric Leblond997ae832006-05-29 18:24:20 -0700802 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -0800803 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
804 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -0700805
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800806 /* If not in hash table, timer will not be active yet */
807 if (!nf_ct_is_confirmed(ct)) {
808 ct->timeout.expires = extra_jiffies;
809 event = IPCT_REFRESH;
810 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100811 unsigned long newtime = jiffies + extra_jiffies;
812
813 /* Only update the timeout if the new timeout is at least
814 HZ jiffies from the old timeout. Need del_timer for race
815 avoidance (may already be dying). */
816 if (newtime - ct->timeout.expires >= HZ
817 && del_timer(&ct->timeout)) {
818 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800819 add_timer(&ct->timeout);
820 event = IPCT_REFRESH;
821 }
822 }
823
Patrick McHardy47d95042008-01-31 04:36:31 -0800824acct:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800825#ifdef CONFIG_NF_CT_ACCT
826 if (do_acct) {
827 ct->counters[CTINFO2DIR(ctinfo)].packets++;
828 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300829 skb->len - skb_network_offset(skb);
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100830
831 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
832 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
833 event |= IPCT_COUNTER_FILLING;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800834 }
835#endif
836
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800837 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800838
839 /* must be unlocked when calling event cache */
840 if (event)
841 nf_conntrack_event_cache(event, skb);
842}
Patrick McHardy13b18332006-12-02 22:11:25 -0800843EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800844
Patrick McHardye281db5c2007-03-04 15:57:25 -0800845#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800846
847#include <linux/netfilter/nfnetlink.h>
848#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800849#include <linux/mutex.h>
850
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800851/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
852 * in ip_conntrack_core, since we don't want the protocols to autoload
853 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -0700854int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800855 const struct nf_conntrack_tuple *tuple)
856{
Patrick McHardy77236b62007-12-17 22:29:45 -0800857 NLA_PUT_BE16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port);
858 NLA_PUT_BE16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800859 return 0;
860
Patrick McHardydf6fb862007-09-28 14:37:03 -0700861nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800862 return -1;
863}
Patrick McHardyfdf70832007-09-28 14:37:41 -0700864EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800865
Patrick McHardyf73e9242007-09-28 14:39:55 -0700866const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
867 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
868 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800869};
Patrick McHardyf73e9242007-09-28 14:39:55 -0700870EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800871
Patrick McHardyfdf70832007-09-28 14:37:41 -0700872int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800873 struct nf_conntrack_tuple *t)
874{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700875 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800876 return -EINVAL;
877
Patrick McHardy77236b62007-12-17 22:29:45 -0800878 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
879 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800880
881 return 0;
882}
Patrick McHardyfdf70832007-09-28 14:37:41 -0700883EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800884#endif
885
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800886/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardyb334aad2008-01-14 23:48:57 -0800887static void nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800888{
889 struct nf_conn *ct;
890 enum ip_conntrack_info ctinfo;
891
892 /* This ICMP is in reverse direction to the packet which caused it */
893 ct = nf_ct_get(skb, &ctinfo);
894 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
895 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
896 else
897 ctinfo = IP_CT_RELATED;
898
899 /* Attach to new skbuff, and increment count */
900 nskb->nfct = &ct->ct_general;
901 nskb->nfctinfo = ctinfo;
902 nf_conntrack_get(nskb->nfct);
903}
904
905static inline int
906do_iter(const struct nf_conntrack_tuple_hash *i,
907 int (*iter)(struct nf_conn *i, void *data),
908 void *data)
909{
910 return iter(nf_ct_tuplehash_to_ctrack(i), data);
911}
912
913/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700914static struct nf_conn *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800915get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
916 void *data, unsigned int *bucket)
917{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700918 struct nf_conntrack_tuple_hash *h;
919 struct nf_conn *ct;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700920 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800921
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800922 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800923 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700924 hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700925 ct = nf_ct_tuplehash_to_ctrack(h);
926 if (iter(ct, data))
927 goto found;
928 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800929 }
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700930 hlist_for_each_entry(h, n, &unconfirmed, hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700931 ct = nf_ct_tuplehash_to_ctrack(h);
932 if (iter(ct, data))
Patrick McHardyec68e972007-03-04 15:57:01 -0800933 set_bit(IPS_DYING_BIT, &ct->status);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700934 }
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800935 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700936 return NULL;
937found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800938 atomic_inc(&ct->ct_general.use);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800939 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700940 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800941}
942
943void
944nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
945{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700946 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800947 unsigned int bucket = 0;
948
Patrick McHardydf0933d2006-09-20 11:57:53 -0700949 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800950 /* Time to push up daises... */
951 if (del_timer(&ct->timeout))
952 death_by_timeout((unsigned long)ct);
953 /* ... else the timer will get him soon. */
954
955 nf_ct_put(ct);
956 }
957}
Patrick McHardy13b18332006-12-02 22:11:25 -0800958EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800959
960static int kill_all(struct nf_conn *i, void *data)
961{
962 return 1;
963}
964
Stephen Hemminger96eb24d2008-01-31 04:07:29 -0800965void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800966{
967 if (vmalloced)
968 vfree(hash);
969 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800970 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700971 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800972}
Patrick McHardyac565e52007-07-07 22:30:08 -0700973EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800974
Randy Dunlap272491e2006-12-07 01:17:24 -0800975void nf_conntrack_flush(void)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800976{
977 nf_ct_iterate_cleanup(kill_all, NULL);
978}
Patrick McHardy13b18332006-12-02 22:11:25 -0800979EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800980
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800981/* Mishearing the voices in his head, our hero wonders how he's
982 supposed to kill the mall. */
983void nf_conntrack_cleanup(void)
984{
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800985 rcu_assign_pointer(ip_ct_attach, NULL);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -0800986
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800987 /* This makes sure all current packets have passed through
988 netfilter framework. Roll on, two-stage module
989 delete... */
990 synchronize_net();
991
992 nf_ct_event_cache_flush();
993 i_see_dead_people:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800994 nf_conntrack_flush();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800995 if (atomic_read(&nf_conntrack_count) != 0) {
996 schedule();
997 goto i_see_dead_people;
998 }
Patrick McHardy66365682005-12-05 13:36:50 -0800999 /* wait until all references to nf_conntrack_untracked are dropped */
1000 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1001 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001002
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001003 rcu_assign_pointer(nf_ct_destroy, NULL);
1004
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001005 kmem_cache_destroy(nf_conntrack_cachep);
Patrick McHardyac565e52007-07-07 22:30:08 -07001006 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
1007 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -08001008
Patrick McHardyac5357e2007-03-14 16:38:25 -07001009 nf_conntrack_proto_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001010 nf_conntrack_helper_fini();
Patrick McHardye9c1b082007-07-07 22:32:53 -07001011 nf_conntrack_expect_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001012}
1013
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001014struct hlist_head *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001015{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001016 struct hlist_head *hash;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001017 unsigned int size, i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001018
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001019 *vmalloced = 0;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001020
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001021 size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head));
Andrew Morton29b67492007-10-29 21:41:19 -07001022 hash = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001023 get_order(sizeof(struct hlist_head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001024 * size));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001025 if (!hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001026 *vmalloced = 1;
1027 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001028 hash = vmalloc(sizeof(struct hlist_head) * size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001029 }
1030
1031 if (hash)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001032 for (i = 0; i < size; i++)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001033 INIT_HLIST_HEAD(&hash[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001034
1035 return hash;
1036}
Patrick McHardyac565e52007-07-07 22:30:08 -07001037EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001038
Patrick McHardyfae718d2007-12-24 21:09:10 -08001039int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001040{
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001041 int i, bucket, vmalloced, old_vmalloced;
1042 unsigned int hashsize, old_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001043 int rnd;
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001044 struct hlist_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001045 struct nf_conntrack_tuple_hash *h;
1046
1047 /* On boot, we can set this without any fancy locking. */
1048 if (!nf_conntrack_htable_size)
1049 return param_set_uint(val, kp);
1050
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001051 hashsize = simple_strtoul(val, NULL, 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001052 if (!hashsize)
1053 return -EINVAL;
1054
Patrick McHardyac565e52007-07-07 22:30:08 -07001055 hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001056 if (!hash)
1057 return -ENOMEM;
1058
1059 /* We have to rehahs for the new table anyway, so we also can
1060 * use a newrandom seed */
1061 get_random_bytes(&rnd, 4);
1062
Patrick McHardy76507f62008-01-31 04:38:38 -08001063 /* Lookups in the old hash might happen in parallel, which means we
1064 * might get false negatives during connection lookup. New connections
1065 * created because of a false negative won't make it into the hash
1066 * though since that required taking the lock.
1067 */
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001068 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001069 for (i = 0; i < nf_conntrack_htable_size; i++) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001070 while (!hlist_empty(&nf_conntrack_hash[i])) {
1071 h = hlist_entry(nf_conntrack_hash[i].first,
1072 struct nf_conntrack_tuple_hash, hnode);
Patrick McHardy76507f62008-01-31 04:38:38 -08001073 hlist_del_rcu(&h->hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001074 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001075 hlist_add_head(&h->hnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001076 }
1077 }
1078 old_size = nf_conntrack_htable_size;
1079 old_vmalloced = nf_conntrack_vmalloc;
1080 old_hash = nf_conntrack_hash;
1081
1082 nf_conntrack_htable_size = hashsize;
1083 nf_conntrack_vmalloc = vmalloced;
1084 nf_conntrack_hash = hash;
1085 nf_conntrack_hash_rnd = rnd;
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001086 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001087
Patrick McHardyac565e52007-07-07 22:30:08 -07001088 nf_ct_free_hashtable(old_hash, old_vmalloced, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001089 return 0;
1090}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001091EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001092
Patrick McHardyfae718d2007-12-24 21:09:10 -08001093module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001094 &nf_conntrack_htable_size, 0600);
1095
1096int __init nf_conntrack_init(void)
1097{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001098 int max_factor = 8;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001099 int ret;
1100
1101 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001102 * machine has 512 buckets. >= 1GB machines have 16384 buckets. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001103 if (!nf_conntrack_htable_size) {
1104 nf_conntrack_htable_size
1105 = (((num_physpages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001106 / sizeof(struct hlist_head));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001107 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001108 nf_conntrack_htable_size = 16384;
1109 if (nf_conntrack_htable_size < 32)
1110 nf_conntrack_htable_size = 32;
1111
1112 /* Use a max. factor of four by default to get the same max as
1113 * with the old struct list_heads. When a table size is given
1114 * we use the old value of 8 to avoid reducing the max.
1115 * entries. */
1116 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001117 }
Patrick McHardyac565e52007-07-07 22:30:08 -07001118 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
1119 &nf_conntrack_vmalloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001120 if (!nf_conntrack_hash) {
1121 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1122 goto err_out;
1123 }
1124
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001125 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001126
1127 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1128 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1129 nf_conntrack_max);
1130
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001131 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1132 sizeof(struct nf_conn),
Paul Mundt20c2df82007-07-20 10:11:58 +09001133 0, 0, NULL);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001134 if (!nf_conntrack_cachep) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001135 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1136 goto err_free_hash;
1137 }
1138
Patrick McHardyac5357e2007-03-14 16:38:25 -07001139 ret = nf_conntrack_proto_init();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001140 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001141 goto err_free_conntrack_slab;
1142
1143 ret = nf_conntrack_expect_init();
1144 if (ret < 0)
1145 goto out_fini_proto;
Patrick McHardy933a41e2006-11-29 02:35:18 +01001146
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001147 ret = nf_conntrack_helper_init();
1148 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001149 goto out_fini_expect;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001150
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001151 /* For use by REJECT target */
Patrick McHardyb334aad2008-01-14 23:48:57 -08001152 rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001153 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001154
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001155 /* Set up fake conntrack:
1156 - to never be deleted, not in any hashes */
1157 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1158 /* - and look it like as a confirmed connection */
1159 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1160
1161 return ret;
1162
Patrick McHardye9c1b082007-07-07 22:32:53 -07001163out_fini_expect:
1164 nf_conntrack_expect_fini();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001165out_fini_proto:
1166 nf_conntrack_proto_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001167err_free_conntrack_slab:
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001168 kmem_cache_destroy(nf_conntrack_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001169err_free_hash:
Patrick McHardyac565e52007-07-07 22:30:08 -07001170 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
1171 nf_conntrack_htable_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001172err_out:
1173 return -ENOMEM;
1174}