blob: c188edea24921f4644af6183943023ea22d4545c [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>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070040#include <net/netfilter/nf_conntrack_acct.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041
Harald Weltedc808fe2006-03-20 17:56:32 -080042#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043
Patrick McHardyf8ba1af2008-01-31 04:38:58 -080044DEFINE_SPINLOCK(nf_conntrack_lock);
Patrick McHardy13b18332006-12-02 22:11:25 -080045EXPORT_SYMBOL_GPL(nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046
Martin Josefssone2b76062006-11-29 02:35:04 +010047unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080048EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
49
Brian Haley94aec082006-09-18 00:05:22 -070050int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +010051EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -080052
Martin Josefssone2b76062006-11-29 02:35:04 +010053struct nf_conn nf_conntrack_untracked __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -080054EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
55
Brian Haley94aec082006-09-18 00:05:22 -070056unsigned int nf_ct_log_invalid __read_mostly;
Patrick McHardyf205c5e2007-07-07 22:28:14 -070057HLIST_HEAD(unconfirmed);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -070058static struct kmem_cache *nf_conntrack_cachep __read_mostly;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010059
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
61EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
62
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063static int nf_conntrack_hash_rnd_initted;
64static unsigned int nf_conntrack_hash_rnd;
65
66static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
67 unsigned int size, unsigned int rnd)
68{
Patrick McHardy07949352008-01-31 04:40:52 -080069 unsigned int n;
70 u_int32_t h;
Sami Farin9b887902007-03-14 16:43:00 -070071
Patrick McHardy07949352008-01-31 04:40:52 -080072 /* The direction must be ignored, so we hash everything up to the
73 * destination ports (which is a multiple of 4) and treat the last
74 * three bytes manually.
75 */
76 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
77 h = jhash2((u32 *)tuple, n,
78 rnd ^ (((__force __u16)tuple->dst.u.all << 16) |
79 tuple->dst.protonum));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080
Patrick McHardy07949352008-01-31 04:40:52 -080081 return ((u64)h * size) >> 32;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082}
83
84static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
85{
86 return __hash_conntrack(tuple, nf_conntrack_htable_size,
87 nf_conntrack_hash_rnd);
88}
89
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +020090bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091nf_ct_get_tuple(const struct sk_buff *skb,
92 unsigned int nhoff,
93 unsigned int dataoff,
94 u_int16_t l3num,
95 u_int8_t protonum,
96 struct nf_conntrack_tuple *tuple,
97 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +010098 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080099{
Philip Craig443a70d2008-04-29 03:35:10 -0700100 memset(tuple, 0, sizeof(*tuple));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101
102 tuple->src.l3num = l3num;
103 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200104 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800105
106 tuple->dst.protonum = protonum;
107 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
108
Martin Josefsson605dcad2006-11-29 02:35:06 +0100109 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110}
Patrick McHardy13b18332006-12-02 22:11:25 -0800111EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800112
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200113bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
114 u_int16_t l3num, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700115{
116 struct nf_conntrack_l3proto *l3proto;
117 struct nf_conntrack_l4proto *l4proto;
118 unsigned int protoff;
119 u_int8_t protonum;
120 int ret;
121
122 rcu_read_lock();
123
124 l3proto = __nf_ct_l3proto_find(l3num);
125 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
126 if (ret != NF_ACCEPT) {
127 rcu_read_unlock();
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200128 return false;
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700129 }
130
131 l4proto = __nf_ct_l4proto_find(l3num, protonum);
132
133 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, tuple,
134 l3proto, l4proto);
135
136 rcu_read_unlock();
137 return ret;
138}
139EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
140
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200141bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800142nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
143 const struct nf_conntrack_tuple *orig,
144 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100145 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146{
Philip Craig443a70d2008-04-29 03:35:10 -0700147 memset(inverse, 0, sizeof(*inverse));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148
149 inverse->src.l3num = orig->src.l3num;
150 if (l3proto->invert_tuple(inverse, orig) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200151 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800152
153 inverse->dst.dir = !orig->dst.dir;
154
155 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157}
Patrick McHardy13b18332006-12-02 22:11:25 -0800158EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160static void
161clean_from_lists(struct nf_conn *ct)
162{
Patrick McHardy0d537782007-07-07 22:39:38 -0700163 pr_debug("clean_from_lists(%p)\n", ct);
Patrick McHardy76507f62008-01-31 04:38:38 -0800164 hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
165 hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166
167 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800168 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169}
170
171static void
172destroy_conntrack(struct nf_conntrack *nfct)
173{
174 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100175 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176
Patrick McHardy0d537782007-07-07 22:39:38 -0700177 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
179 NF_CT_ASSERT(!timer_pending(&ct->timeout));
180
181 nf_conntrack_event(IPCT_DESTROY, ct);
182 set_bit(IPS_DYING_BIT, &ct->status);
183
184 /* To make sure we don't get any weird locking issues here:
185 * destroy_conntrack() MUST NOT be called with a write lock
186 * to nf_conntrack_lock!!! -HW */
Patrick McHardy923f4902007-02-12 11:12:57 -0800187 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200188 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Martin Josefsson605dcad2006-11-29 02:35:06 +0100189 if (l4proto && l4proto->destroy)
190 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191
Patrick McHardy982d9a92007-02-12 11:14:11 -0800192 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800194 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195 /* Expectations will have been removed in clean_from_lists,
196 * except TFTP can create an expectation on the first packet,
197 * before connection is in the list, so we need to clean here,
198 * too. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800199 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200
201 /* We overload first tuple to link into unconfirmed list. */
202 if (!nf_ct_is_confirmed(ct)) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700203 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
204 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 }
206
207 NF_CT_STAT_INC(delete);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800208 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
210 if (ct->master)
211 nf_ct_put(ct->master);
212
Patrick McHardy0d537782007-07-07 22:39:38 -0700213 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 nf_conntrack_free(ct);
215}
216
217static void death_by_timeout(unsigned long ul_conntrack)
218{
219 struct nf_conn *ct = (void *)ul_conntrack;
Patrick McHardy5397e972007-05-19 14:23:52 -0700220 struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700221 struct nf_conntrack_helper *helper;
Patrick McHardy5397e972007-05-19 14:23:52 -0700222
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700223 if (help) {
224 rcu_read_lock();
225 helper = rcu_dereference(help->helper);
226 if (helper && helper->destroy)
227 helper->destroy(ct);
228 rcu_read_unlock();
229 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800231 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232 /* Inside lock so preempt is disabled on module removal path.
233 * Otherwise we can get spurious warnings. */
234 NF_CT_STAT_INC(delete_list);
235 clean_from_lists(ct);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800236 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800237 nf_ct_put(ct);
238}
239
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800240struct nf_conntrack_tuple_hash *
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200241__nf_conntrack_find(struct net *net, const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242{
243 struct nf_conntrack_tuple_hash *h;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700244 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245 unsigned int hash = hash_conntrack(tuple);
246
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800247 /* Disable BHs the entire time since we normally need to disable them
248 * at least once for the stats anyway.
249 */
250 local_bh_disable();
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200251 hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnode) {
Patrick McHardyba419af2008-01-31 04:39:23 -0800252 if (nf_ct_tuple_equal(tuple, &h->tuple)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253 NF_CT_STAT_INC(found);
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800254 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255 return h;
256 }
257 NF_CT_STAT_INC(searched);
258 }
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800259 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260
261 return NULL;
262}
Patrick McHardy13b18332006-12-02 22:11:25 -0800263EXPORT_SYMBOL_GPL(__nf_conntrack_find);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800264
265/* Find a connection corresponding to a tuple. */
266struct nf_conntrack_tuple_hash *
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200267nf_conntrack_find_get(struct net *net, const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268{
269 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800270 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271
Patrick McHardy76507f62008-01-31 04:38:38 -0800272 rcu_read_lock();
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200273 h = __nf_conntrack_find(net, tuple);
Patrick McHardy76507f62008-01-31 04:38:38 -0800274 if (h) {
275 ct = nf_ct_tuplehash_to_ctrack(h);
276 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
277 h = NULL;
278 }
279 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800280
281 return h;
282}
Patrick McHardy13b18332006-12-02 22:11:25 -0800283EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800284
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800285static void __nf_conntrack_hash_insert(struct nf_conn *ct,
286 unsigned int hash,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800287 unsigned int repl_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800288{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200289 struct net *net = nf_ct_net(ct);
290
Patrick McHardy76507f62008-01-31 04:38:38 -0800291 hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200292 &net->ct.hash[hash]);
Patrick McHardy76507f62008-01-31 04:38:38 -0800293 hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode,
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200294 &net->ct.hash[repl_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800295}
296
297void nf_conntrack_hash_insert(struct nf_conn *ct)
298{
299 unsigned int hash, repl_hash;
300
301 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
302 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
303
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800304 spin_lock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800305 __nf_conntrack_hash_insert(ct, hash, repl_hash);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800306 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800307}
Patrick McHardy13b18332006-12-02 22:11:25 -0800308EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800309
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800310/* Confirm a connection given skb; places it in hash table */
311int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700312__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313{
314 unsigned int hash, repl_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700315 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800316 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700317 struct nf_conn_help *help;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700318 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200320 struct net *net;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800321
Herbert Xu3db05fe2007-10-15 00:53:15 -0700322 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200323 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800324
325 /* ipt_REJECT uses nf_conntrack_attach to attach related
326 ICMP/TCP RST packets in other direction. Actual packet
327 which created connection will be IP_CT_NEW or for an
328 expected connection, IP_CT_RELATED. */
329 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
330 return NF_ACCEPT;
331
332 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
333 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
334
335 /* We're not in hash table, and we refuse to set up related
336 connections for unconfirmed conns. But packet copies and
337 REJECT will give spurious warnings here. */
338 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
339
340 /* No external references means noone else could have
341 confirmed us. */
342 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700343 pr_debug("Confirming conntrack %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800344
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800345 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346
347 /* See if there's one in the list already, including reverse:
348 NAT could have grabbed it without realizing, since we're
349 not in the hash. If there is, we lost race. */
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200350 hlist_for_each_entry(h, n, &net->ct.hash[hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700351 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
352 &h->tuple))
353 goto out;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200354 hlist_for_each_entry(h, n, &net->ct.hash[repl_hash], hnode)
Patrick McHardydf0933d2006-09-20 11:57:53 -0700355 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
356 &h->tuple))
357 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358
Patrick McHardydf0933d2006-09-20 11:57:53 -0700359 /* Remove from unconfirmed list */
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700360 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700361
362 __nf_conntrack_hash_insert(ct, hash, repl_hash);
363 /* Timer relative to confirmation time, not original
364 setting time, otherwise we'd get timer wrap in
365 weird delay cases. */
366 ct->timeout.expires += jiffies;
367 add_timer(&ct->timeout);
368 atomic_inc(&ct->ct_general.use);
369 set_bit(IPS_CONFIRMED_BIT, &ct->status);
370 NF_CT_STAT_INC(insert);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800371 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700372 help = nfct_help(ct);
373 if (help && help->helper)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700374 nf_conntrack_event_cache(IPCT_HELPER, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800375#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardydf0933d2006-09-20 11:57:53 -0700376 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
377 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700378 nf_conntrack_event_cache(IPCT_NATINFO, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379#endif
Patrick McHardydf0933d2006-09-20 11:57:53 -0700380 nf_conntrack_event_cache(master_ct(ct) ?
Herbert Xu3db05fe2007-10-15 00:53:15 -0700381 IPCT_RELATED : IPCT_NEW, skb);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700382 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383
Patrick McHardydf0933d2006-09-20 11:57:53 -0700384out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 NF_CT_STAT_INC(insert_failed);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800386 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387 return NF_DROP;
388}
Patrick McHardy13b18332006-12-02 22:11:25 -0800389EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390
391/* Returns true if a connection correspondings to the tuple (required
392 for NAT). */
393int
394nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
395 const struct nf_conn *ignored_conntrack)
396{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200397 struct net *net = nf_ct_net(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 struct nf_conntrack_tuple_hash *h;
Patrick McHardyba419af2008-01-31 04:39:23 -0800399 struct hlist_node *n;
400 unsigned int hash = hash_conntrack(tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800402 /* Disable BHs the entire time since we need to disable them at
403 * least once for the stats anyway.
404 */
405 rcu_read_lock_bh();
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200406 hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnode) {
Patrick McHardyba419af2008-01-31 04:39:23 -0800407 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
408 nf_ct_tuple_equal(tuple, &h->tuple)) {
409 NF_CT_STAT_INC(found);
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800410 rcu_read_unlock_bh();
Patrick McHardyba419af2008-01-31 04:39:23 -0800411 return 1;
412 }
413 NF_CT_STAT_INC(searched);
414 }
Patrick McHardy4e29e9e2008-02-27 12:07:47 -0800415 rcu_read_unlock_bh();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416
Patrick McHardyba419af2008-01-31 04:39:23 -0800417 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418}
Patrick McHardy13b18332006-12-02 22:11:25 -0800419EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420
Patrick McHardy7ae77302007-07-07 22:37:38 -0700421#define NF_CT_EVICTION_RANGE 8
422
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423/* There's a small race here where we may free a just-assured
424 connection. Too bad: we're in trouble anyway. */
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200425static noinline int early_drop(struct net *net, unsigned int hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700427 /* Use oldest entry, which is roughly LRU */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800428 struct nf_conntrack_tuple_hash *h;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700429 struct nf_conn *ct = NULL, *tmp;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700430 struct hlist_node *n;
Patrick McHardy7ae77302007-07-07 22:37:38 -0700431 unsigned int i, cnt = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432 int dropped = 0;
433
Patrick McHardy76507f62008-01-31 04:38:38 -0800434 rcu_read_lock();
Patrick McHardy7ae77302007-07-07 22:37:38 -0700435 for (i = 0; i < nf_conntrack_htable_size; i++) {
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200436 hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash],
Patrick McHardy76507f62008-01-31 04:38:38 -0800437 hnode) {
Patrick McHardy7ae77302007-07-07 22:37:38 -0700438 tmp = nf_ct_tuplehash_to_ctrack(h);
439 if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
440 ct = tmp;
441 cnt++;
442 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800443
444 if (ct && unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
445 ct = NULL;
Patrick McHardy7ae77302007-07-07 22:37:38 -0700446 if (ct || cnt >= NF_CT_EVICTION_RANGE)
447 break;
448 hash = (hash + 1) % nf_conntrack_htable_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800450 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800451
452 if (!ct)
453 return dropped;
454
455 if (del_timer(&ct->timeout)) {
456 death_by_timeout((unsigned long)ct);
457 dropped = 1;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800458 NF_CT_STAT_INC_ATOMIC(early_drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459 }
460 nf_ct_put(ct);
461 return dropped;
462}
463
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200464struct nf_conn *nf_conntrack_alloc(struct net *net,
465 const struct nf_conntrack_tuple *orig,
Pablo Neira Ayusob891c5a2008-07-08 02:35:55 -0700466 const struct nf_conntrack_tuple *repl,
467 gfp_t gfp)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800469 struct nf_conn *ct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470
Harald Weltedc808fe2006-03-20 17:56:32 -0800471 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472 get_random_bytes(&nf_conntrack_hash_rnd, 4);
473 nf_conntrack_hash_rnd_initted = 1;
474 }
475
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700476 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200477 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700478
Patrick McHardy76eb9462008-01-31 04:41:44 -0800479 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200480 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481 unsigned int hash = hash_conntrack(orig);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200482 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200483 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 if (net_ratelimit())
485 printk(KERN_WARNING
486 "nf_conntrack: table full, dropping"
487 " packet.\n");
488 return ERR_PTR(-ENOMEM);
489 }
490 }
491
Pablo Neira Ayusob891c5a2008-07-08 02:35:55 -0700492 ct = kmem_cache_zalloc(nf_conntrack_cachep, gfp);
Patrick McHardyc88130b2008-01-31 04:42:11 -0800493 if (ct == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700494 pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n");
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200495 atomic_dec(&net->ct.count);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -0700496 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497 }
498
Patrick McHardyc88130b2008-01-31 04:42:11 -0800499 atomic_set(&ct->ct_general.use, 1);
500 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
501 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800502 /* Don't set timer yet: wait for confirmation */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800503 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200504#ifdef CONFIG_NET_NS
505 ct->ct_net = net;
506#endif
Patrick McHardyc88130b2008-01-31 04:42:11 -0800507 INIT_RCU_HEAD(&ct->rcu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800508
Patrick McHardyc88130b2008-01-31 04:42:11 -0800509 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800510}
Patrick McHardy13b18332006-12-02 22:11:25 -0800511EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512
Patrick McHardy76507f62008-01-31 04:38:38 -0800513static void nf_conntrack_free_rcu(struct rcu_head *head)
514{
515 struct nf_conn *ct = container_of(head, struct nf_conn, rcu);
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200516 struct net *net = nf_ct_net(ct);
Patrick McHardy76507f62008-01-31 04:38:38 -0800517
518 nf_ct_ext_free(ct);
519 kmem_cache_free(nf_conntrack_cachep, ct);
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200520 atomic_dec(&net->ct.count);
Patrick McHardy76507f62008-01-31 04:38:38 -0800521}
522
Patrick McHardyc88130b2008-01-31 04:42:11 -0800523void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800524{
Patrick McHardyceeff752008-06-11 17:51:10 -0700525 nf_ct_ext_destroy(ct);
Patrick McHardyc88130b2008-01-31 04:42:11 -0800526 call_rcu(&ct->rcu, nf_conntrack_free_rcu);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800527}
Patrick McHardy13b18332006-12-02 22:11:25 -0800528EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800529
530/* Allocate a new conntrack: we return -ENOMEM if classification
531 failed due to stress. Otherwise it really is unclassifiable. */
532static struct nf_conntrack_tuple_hash *
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200533init_conntrack(struct net *net,
534 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800535 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100536 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800537 struct sk_buff *skb,
538 unsigned int dataoff)
539{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800540 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700541 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800542 struct nf_conntrack_tuple repl_tuple;
543 struct nf_conntrack_expect *exp;
544
Martin Josefsson605dcad2006-11-29 02:35:06 +0100545 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700546 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800547 return NULL;
548 }
549
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200550 ct = nf_conntrack_alloc(net, tuple, &repl_tuple, GFP_ATOMIC);
Patrick McHardyc88130b2008-01-31 04:42:11 -0800551 if (ct == NULL || IS_ERR(ct)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700552 pr_debug("Can't allocate conntrack.\n");
Patrick McHardyc88130b2008-01-31 04:42:11 -0800553 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800554 }
555
Patrick McHardyc88130b2008-01-31 04:42:11 -0800556 if (!l4proto->new(ct, skb, dataoff)) {
557 nf_conntrack_free(ct);
Patrick McHardy0d537782007-07-07 22:39:38 -0700558 pr_debug("init conntrack: can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800559 return NULL;
560 }
561
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700562 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
563
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800564 spin_lock_bh(&nf_conntrack_lock);
Alexey Dobriyan9b03f382008-10-08 11:35:03 +0200565 exp = nf_ct_find_expectation(net, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800566 if (exp) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700567 pr_debug("conntrack: expectation arrives ct=%p exp=%p\n",
Patrick McHardyc88130b2008-01-31 04:42:11 -0800568 ct, exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800569 /* Welcome, Mr. Bond. We've been expecting you... */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800570 __set_bit(IPS_EXPECTED_BIT, &ct->status);
571 ct->master = exp->master;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700572 if (exp->helper) {
Patrick McHardyc88130b2008-01-31 04:42:11 -0800573 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700574 if (help)
575 rcu_assign_pointer(help->helper, exp->helper);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700576 }
577
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800578#ifdef CONFIG_NF_CONNTRACK_MARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800579 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800580#endif
James Morris7c9728c2006-06-09 00:31:46 -0700581#ifdef CONFIG_NF_CONNTRACK_SECMARK
Patrick McHardyc88130b2008-01-31 04:42:11 -0800582 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -0700583#endif
Patrick McHardyc88130b2008-01-31 04:42:11 -0800584 nf_conntrack_get(&ct->master->ct_general);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800585 NF_CT_STAT_INC(expect_new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800586 } else {
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700587 struct nf_conntrack_helper *helper;
588
589 helper = __nf_ct_helper_find(&repl_tuple);
590 if (helper) {
Patrick McHardyc88130b2008-01-31 04:42:11 -0800591 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700592 if (help)
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700593 rcu_assign_pointer(help->helper, helper);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700594 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800595 NF_CT_STAT_INC(new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -0800596 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800597
598 /* Overload tuple linked list to put us in unconfirmed list. */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800599 hlist_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode, &unconfirmed);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800600
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800601 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800602
603 if (exp) {
604 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -0800605 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -0700606 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800607 }
608
Patrick McHardyc88130b2008-01-31 04:42:11 -0800609 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800610}
611
612/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
613static inline struct nf_conn *
614resolve_normal_ct(struct sk_buff *skb,
615 unsigned int dataoff,
616 u_int16_t l3num,
617 u_int8_t protonum,
618 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100619 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800620 int *set_reply,
621 enum ip_conntrack_info *ctinfo)
622{
623 struct nf_conntrack_tuple tuple;
624 struct nf_conntrack_tuple_hash *h;
625 struct nf_conn *ct;
626
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300627 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800628 dataoff, l3num, protonum, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100629 l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700630 pr_debug("resolve_normal_ct: Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800631 return NULL;
632 }
633
634 /* look for tuple match */
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200635 h = nf_conntrack_find_get(&init_net, &tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800636 if (!h) {
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200637 h = init_conntrack(&init_net, &tuple, l3proto, l4proto, skb,
638 dataoff);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800639 if (!h)
640 return NULL;
641 if (IS_ERR(h))
642 return (void *)h;
643 }
644 ct = nf_ct_tuplehash_to_ctrack(h);
645
646 /* It exists; we have (non-exclusive) reference. */
647 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
648 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
649 /* Please set reply bit if this packet OK */
650 *set_reply = 1;
651 } else {
652 /* Once we've had two way comms, always ESTABLISHED. */
653 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700654 pr_debug("nf_conntrack_in: normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800655 *ctinfo = IP_CT_ESTABLISHED;
656 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700657 pr_debug("nf_conntrack_in: related packet for %p\n",
658 ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800659 *ctinfo = IP_CT_RELATED;
660 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700661 pr_debug("nf_conntrack_in: new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800662 *ctinfo = IP_CT_NEW;
663 }
664 *set_reply = 0;
665 }
666 skb->nfct = &ct->ct_general;
667 skb->nfctinfo = *ctinfo;
668 return ct;
669}
670
671unsigned int
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200672nf_conntrack_in(u_int8_t pf, unsigned int hooknum, struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800673{
674 struct nf_conn *ct;
675 enum ip_conntrack_info ctinfo;
676 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100677 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800678 unsigned int dataoff;
679 u_int8_t protonum;
680 int set_reply = 0;
681 int ret;
682
683 /* Previously seen (loopback or untracked)? Ignore. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700684 if (skb->nfct) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800685 NF_CT_STAT_INC_ATOMIC(ignore);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800686 return NF_ACCEPT;
687 }
688
Patrick McHardy923f4902007-02-12 11:12:57 -0800689 /* rcu_read_lock()ed by nf_hook_slow */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200690 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700691 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700692 &dataoff, &protonum);
693 if (ret <= 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700694 pr_debug("not prepared to track yet or error occured\n");
Yasuyuki Kozakaid87d8462007-07-14 20:44:23 -0700695 NF_CT_STAT_INC_ATOMIC(error);
696 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800697 return -ret;
698 }
699
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200700 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800701
702 /* It may be an special packet, error, unclean...
703 * inverse of the return code tells to the netfilter
704 * core what to do with the packet. */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100705 if (l4proto->error != NULL &&
Herbert Xu3db05fe2007-10-15 00:53:15 -0700706 (ret = l4proto->error(skb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800707 NF_CT_STAT_INC_ATOMIC(error);
708 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800709 return -ret;
710 }
711
Herbert Xu3db05fe2007-10-15 00:53:15 -0700712 ct = resolve_normal_ct(skb, dataoff, pf, protonum, l3proto, l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800713 &set_reply, &ctinfo);
714 if (!ct) {
715 /* Not valid part of a connection */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800716 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800717 return NF_ACCEPT;
718 }
719
720 if (IS_ERR(ct)) {
721 /* Too stressed to deal. */
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800722 NF_CT_STAT_INC_ATOMIC(drop);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800723 return NF_DROP;
724 }
725
Herbert Xu3db05fe2007-10-15 00:53:15 -0700726 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800727
Herbert Xu3db05fe2007-10-15 00:53:15 -0700728 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800729 if (ret < 0) {
730 /* Invalid: inverse of the return code tells
731 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -0700732 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -0700733 nf_conntrack_put(skb->nfct);
734 skb->nfct = NULL;
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800735 NF_CT_STAT_INC_ATOMIC(invalid);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800736 return -ret;
737 }
738
739 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700740 nf_conntrack_event_cache(IPCT_STATUS, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800741
742 return ret;
743}
Patrick McHardy13b18332006-12-02 22:11:25 -0800744EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800745
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200746bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
747 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800748{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200749 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -0800750
751 rcu_read_lock();
752 ret = nf_ct_invert_tuple(inverse, orig,
753 __nf_ct_l3proto_find(orig->src.l3num),
754 __nf_ct_l4proto_find(orig->src.l3num,
755 orig->dst.protonum));
756 rcu_read_unlock();
757 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800758}
Patrick McHardy13b18332006-12-02 22:11:25 -0800759EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800760
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800761/* Alter reply tuple (maybe alter helper). This is for NAT, and is
762 implicitly racy: see __nf_conntrack_confirm */
763void nf_conntrack_alter_reply(struct nf_conn *ct,
764 const struct nf_conntrack_tuple *newreply)
765{
766 struct nf_conn_help *help = nfct_help(ct);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700767 struct nf_conntrack_helper *helper;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800768
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800769 /* Should be unconfirmed, so not in hash table yet */
770 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
771
Patrick McHardy0d537782007-07-07 22:39:38 -0700772 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200773 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800774
775 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +0200776 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800777 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700778
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800779 rcu_read_lock();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700780 helper = __nf_ct_helper_find(newreply);
781 if (helper == NULL) {
782 if (help)
783 rcu_assign_pointer(help->helper, NULL);
784 goto out;
Yasuyuki Kozakai5d78a842007-05-10 14:16:24 -0700785 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700786
787 if (help == NULL) {
Patrick McHardyb5605802007-07-07 22:35:56 -0700788 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
789 if (help == NULL)
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700790 goto out;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700791 } else {
792 memset(&help->help, 0, sizeof(help->help));
793 }
794
795 rcu_assign_pointer(help->helper, helper);
796out:
Patrick McHardyc52fbb42008-01-31 04:37:36 -0800797 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800798}
Patrick McHardy13b18332006-12-02 22:11:25 -0800799EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800800
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800801/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
802void __nf_ct_refresh_acct(struct nf_conn *ct,
803 enum ip_conntrack_info ctinfo,
804 const struct sk_buff *skb,
805 unsigned long extra_jiffies,
806 int do_acct)
807{
808 int event = 0;
809
810 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
811 NF_CT_ASSERT(skb);
812
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800813 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800814
Eric Leblond997ae832006-05-29 18:24:20 -0700815 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -0800816 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
817 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -0700818
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800819 /* If not in hash table, timer will not be active yet */
820 if (!nf_ct_is_confirmed(ct)) {
821 ct->timeout.expires = extra_jiffies;
822 event = IPCT_REFRESH;
823 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +0100824 unsigned long newtime = jiffies + extra_jiffies;
825
826 /* Only update the timeout if the new timeout is at least
827 HZ jiffies from the old timeout. Need del_timer for race
828 avoidance (may already be dying). */
829 if (newtime - ct->timeout.expires >= HZ
830 && del_timer(&ct->timeout)) {
831 ct->timeout.expires = newtime;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800832 add_timer(&ct->timeout);
833 event = IPCT_REFRESH;
834 }
835 }
836
Patrick McHardy47d95042008-01-31 04:36:31 -0800837acct:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800838 if (do_acct) {
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700839 struct nf_conn_counter *acct;
Martin Josefsson3ffd5ee2006-11-29 02:35:10 +0100840
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700841 acct = nf_conn_acct_find(ct);
842 if (acct) {
843 acct[CTINFO2DIR(ctinfo)].packets++;
844 acct[CTINFO2DIR(ctinfo)].bytes +=
845 skb->len - skb_network_offset(skb);
846 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800847 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800848
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800849 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800850
851 /* must be unlocked when calling event cache */
852 if (event)
853 nf_conntrack_event_cache(event, skb);
854}
Patrick McHardy13b18332006-12-02 22:11:25 -0800855EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800856
David S. Miller4c889492008-07-14 20:22:38 -0700857bool __nf_ct_kill_acct(struct nf_conn *ct,
858 enum ip_conntrack_info ctinfo,
859 const struct sk_buff *skb,
860 int do_acct)
Patrick McHardy51091762008-06-09 15:59:06 -0700861{
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700862 if (do_acct) {
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700863 struct nf_conn_counter *acct;
864
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700865 spin_lock_bh(&nf_conntrack_lock);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700866 acct = nf_conn_acct_find(ct);
867 if (acct) {
868 acct[CTINFO2DIR(ctinfo)].packets++;
869 acct[CTINFO2DIR(ctinfo)].bytes +=
870 skb->len - skb_network_offset(skb);
871 }
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700872 spin_unlock_bh(&nf_conntrack_lock);
873 }
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700874
David S. Miller4c889492008-07-14 20:22:38 -0700875 if (del_timer(&ct->timeout)) {
Patrick McHardy51091762008-06-09 15:59:06 -0700876 ct->timeout.function((unsigned long)ct);
David S. Miller4c889492008-07-14 20:22:38 -0700877 return true;
878 }
879 return false;
Patrick McHardy51091762008-06-09 15:59:06 -0700880}
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700881EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -0700882
Patrick McHardye281db5c2007-03-04 15:57:25 -0800883#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800884
885#include <linux/netfilter/nfnetlink.h>
886#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -0800887#include <linux/mutex.h>
888
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800889/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
890 * in ip_conntrack_core, since we don't want the protocols to autoload
891 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -0700892int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800893 const struct nf_conntrack_tuple *tuple)
894{
Patrick McHardy77236b62007-12-17 22:29:45 -0800895 NLA_PUT_BE16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port);
896 NLA_PUT_BE16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800897 return 0;
898
Patrick McHardydf6fb862007-09-28 14:37:03 -0700899nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800900 return -1;
901}
Patrick McHardyfdf70832007-09-28 14:37:41 -0700902EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800903
Patrick McHardyf73e9242007-09-28 14:39:55 -0700904const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
905 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
906 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800907};
Patrick McHardyf73e9242007-09-28 14:39:55 -0700908EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800909
Patrick McHardyfdf70832007-09-28 14:37:41 -0700910int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800911 struct nf_conntrack_tuple *t)
912{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700913 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800914 return -EINVAL;
915
Patrick McHardy77236b62007-12-17 22:29:45 -0800916 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
917 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800918
919 return 0;
920}
Patrick McHardyfdf70832007-09-28 14:37:41 -0700921EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800922#endif
923
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800924/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardyb334aad2008-01-14 23:48:57 -0800925static void nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800926{
927 struct nf_conn *ct;
928 enum ip_conntrack_info ctinfo;
929
930 /* This ICMP is in reverse direction to the packet which caused it */
931 ct = nf_ct_get(skb, &ctinfo);
932 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
933 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
934 else
935 ctinfo = IP_CT_RELATED;
936
937 /* Attach to new skbuff, and increment count */
938 nskb->nfct = &ct->ct_general;
939 nskb->nfctinfo = ctinfo;
940 nf_conntrack_get(nskb->nfct);
941}
942
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800943/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700944static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200945get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800946 void *data, unsigned int *bucket)
947{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700948 struct nf_conntrack_tuple_hash *h;
949 struct nf_conn *ct;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700950 struct hlist_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800951
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800952 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800953 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200954 hlist_for_each_entry(h, n, &net->ct.hash[*bucket], hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700955 ct = nf_ct_tuplehash_to_ctrack(h);
956 if (iter(ct, data))
957 goto found;
958 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800959 }
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700960 hlist_for_each_entry(h, n, &unconfirmed, hnode) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700961 ct = nf_ct_tuplehash_to_ctrack(h);
962 if (iter(ct, data))
Patrick McHardyec68e972007-03-04 15:57:01 -0800963 set_bit(IPS_DYING_BIT, &ct->status);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700964 }
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800965 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700966 return NULL;
967found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -0800968 atomic_inc(&ct->ct_general.use);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -0800969 spin_unlock_bh(&nf_conntrack_lock);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700970 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800971}
972
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200973void nf_ct_iterate_cleanup(struct net *net,
974 int (*iter)(struct nf_conn *i, void *data),
975 void *data)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800976{
Patrick McHardydf0933d2006-09-20 11:57:53 -0700977 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800978 unsigned int bucket = 0;
979
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200980 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800981 /* Time to push up daises... */
982 if (del_timer(&ct->timeout))
983 death_by_timeout((unsigned long)ct);
984 /* ... else the timer will get him soon. */
985
986 nf_ct_put(ct);
987 }
988}
Patrick McHardy13b18332006-12-02 22:11:25 -0800989EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800990
991static int kill_all(struct nf_conn *i, void *data)
992{
993 return 1;
994}
995
Stephen Hemminger96eb24d2008-01-31 04:07:29 -0800996void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800997{
998 if (vmalloced)
999 vfree(hash);
1000 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001001 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001002 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001003}
Patrick McHardyac565e52007-07-07 22:30:08 -07001004EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001005
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001006void nf_conntrack_flush(struct net *net)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001007{
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001008 nf_ct_iterate_cleanup(net, kill_all, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001009}
Patrick McHardy13b18332006-12-02 22:11:25 -08001010EXPORT_SYMBOL_GPL(nf_conntrack_flush);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001011
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001012/* Mishearing the voices in his head, our hero wonders how he's
1013 supposed to kill the mall. */
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +02001014void nf_conntrack_cleanup(struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001015{
Patrick McHardyc3a47ab2007-02-12 11:09:19 -08001016 rcu_assign_pointer(ip_ct_attach, NULL);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001017
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001018 /* This makes sure all current packets have passed through
1019 netfilter framework. Roll on, two-stage module
1020 delete... */
1021 synchronize_net();
1022
1023 nf_ct_event_cache_flush();
1024 i_see_dead_people:
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001025 nf_conntrack_flush(net);
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001026 if (atomic_read(&net->ct.count) != 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001027 schedule();
1028 goto i_see_dead_people;
1029 }
Patrick McHardy66365682005-12-05 13:36:50 -08001030 /* wait until all references to nf_conntrack_untracked are dropped */
1031 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1032 schedule();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001033
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001034 rcu_assign_pointer(nf_ct_destroy, NULL);
1035
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001036 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001037 nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
Patrick McHardyac565e52007-07-07 22:30:08 -07001038 nf_conntrack_htable_size);
KOVACS Krisztian5a6f294e42005-11-15 16:47:34 -08001039
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001040 nf_conntrack_acct_fini();
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001041 nf_conntrack_expect_fini(net);
Krzysztof Piotr Oledzki9714be72008-08-06 02:35:44 -07001042 nf_conntrack_helper_fini();
1043 nf_conntrack_proto_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001044}
1045
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001046struct hlist_head *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001047{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001048 struct hlist_head *hash;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001049 unsigned int size, i;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001050
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001051 *vmalloced = 0;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001052
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001053 size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head));
Andrew Morton29b67492007-10-29 21:41:19 -07001054 hash = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001055 get_order(sizeof(struct hlist_head)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001056 * size));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001057 if (!hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001058 *vmalloced = 1;
1059 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001060 hash = vmalloc(sizeof(struct hlist_head) * size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001061 }
1062
1063 if (hash)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001064 for (i = 0; i < size; i++)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001065 INIT_HLIST_HEAD(&hash[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001066
1067 return hash;
1068}
Patrick McHardyac565e52007-07-07 22:30:08 -07001069EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001070
Patrick McHardyfae718d2007-12-24 21:09:10 -08001071int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001072{
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001073 int i, bucket, vmalloced, old_vmalloced;
1074 unsigned int hashsize, old_size;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001075 int rnd;
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001076 struct hlist_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001077 struct nf_conntrack_tuple_hash *h;
1078
1079 /* On boot, we can set this without any fancy locking. */
1080 if (!nf_conntrack_htable_size)
1081 return param_set_uint(val, kp);
1082
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001083 hashsize = simple_strtoul(val, NULL, 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001084 if (!hashsize)
1085 return -EINVAL;
1086
Patrick McHardyac565e52007-07-07 22:30:08 -07001087 hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001088 if (!hash)
1089 return -ENOMEM;
1090
1091 /* We have to rehahs for the new table anyway, so we also can
1092 * use a newrandom seed */
1093 get_random_bytes(&rnd, 4);
1094
Patrick McHardy76507f62008-01-31 04:38:38 -08001095 /* Lookups in the old hash might happen in parallel, which means we
1096 * might get false negatives during connection lookup. New connections
1097 * created because of a false negative won't make it into the hash
1098 * though since that required taking the lock.
1099 */
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001100 spin_lock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001101 for (i = 0; i < nf_conntrack_htable_size; i++) {
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001102 while (!hlist_empty(&init_net.ct.hash[i])) {
1103 h = hlist_entry(init_net.ct.hash[i].first,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001104 struct nf_conntrack_tuple_hash, hnode);
Patrick McHardy76507f62008-01-31 04:38:38 -08001105 hlist_del_rcu(&h->hnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001106 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001107 hlist_add_head(&h->hnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001108 }
1109 }
1110 old_size = nf_conntrack_htable_size;
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001111 old_vmalloced = init_net.ct.hash_vmalloc;
1112 old_hash = init_net.ct.hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001113
1114 nf_conntrack_htable_size = hashsize;
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001115 init_net.ct.hash_vmalloc = vmalloced;
1116 init_net.ct.hash = hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001117 nf_conntrack_hash_rnd = rnd;
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001118 spin_unlock_bh(&nf_conntrack_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001119
Patrick McHardyac565e52007-07-07 22:30:08 -07001120 nf_ct_free_hashtable(old_hash, old_vmalloced, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001121 return 0;
1122}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001123EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001124
Patrick McHardyfae718d2007-12-24 21:09:10 -08001125module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001126 &nf_conntrack_htable_size, 0600);
1127
Alexey Dobriyandfdb8d72008-10-08 11:35:02 +02001128int nf_conntrack_init(struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001129{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001130 int max_factor = 8;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001131 int ret;
1132
1133 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001134 * machine has 512 buckets. >= 1GB machines have 16384 buckets. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001135 if (!nf_conntrack_htable_size) {
1136 nf_conntrack_htable_size
1137 = (((num_physpages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001138 / sizeof(struct hlist_head));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001139 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001140 nf_conntrack_htable_size = 16384;
1141 if (nf_conntrack_htable_size < 32)
1142 nf_conntrack_htable_size = 32;
1143
1144 /* Use a max. factor of four by default to get the same max as
1145 * with the old struct list_heads. When a table size is given
1146 * we use the old value of 8 to avoid reducing the max.
1147 * entries. */
1148 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001149 }
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001150 atomic_set(&net->ct.count, 0);
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001151 net->ct.hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
1152 &net->ct.hash_vmalloc);
1153 if (!net->ct.hash) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001154 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1155 goto err_out;
1156 }
1157
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001158 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001159
1160 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1161 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1162 nf_conntrack_max);
1163
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001164 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1165 sizeof(struct nf_conn),
Paul Mundt20c2df82007-07-20 10:11:58 +09001166 0, 0, NULL);
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001167 if (!nf_conntrack_cachep) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001168 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1169 goto err_free_hash;
1170 }
1171
Patrick McHardyac5357e2007-03-14 16:38:25 -07001172 ret = nf_conntrack_proto_init();
Patrick McHardy933a41e2006-11-29 02:35:18 +01001173 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001174 goto err_free_conntrack_slab;
1175
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001176 ret = nf_conntrack_expect_init(net);
Patrick McHardye9c1b082007-07-07 22:32:53 -07001177 if (ret < 0)
1178 goto out_fini_proto;
Patrick McHardy933a41e2006-11-29 02:35:18 +01001179
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001180 ret = nf_conntrack_helper_init();
1181 if (ret < 0)
Patrick McHardye9c1b082007-07-07 22:32:53 -07001182 goto out_fini_expect;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001183
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001184 ret = nf_conntrack_acct_init();
1185 if (ret < 0)
1186 goto out_fini_helper;
1187
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001188 /* For use by REJECT target */
Patrick McHardyb334aad2008-01-14 23:48:57 -08001189 rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07001190 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001191
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001192 /* Set up fake conntrack:
1193 - to never be deleted, not in any hashes */
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001194#ifdef CONFIG_NET_NS
1195 nf_conntrack_untracked.ct_net = &init_net;
1196#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001197 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1198 /* - and look it like as a confirmed connection */
1199 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1200
1201 return ret;
1202
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001203out_fini_helper:
1204 nf_conntrack_helper_fini();
Patrick McHardye9c1b082007-07-07 22:32:53 -07001205out_fini_expect:
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001206 nf_conntrack_expect_fini(net);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001207out_fini_proto:
1208 nf_conntrack_proto_fini();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001209err_free_conntrack_slab:
Yasuyuki Kozakaidacd2a12007-07-07 22:25:51 -07001210 kmem_cache_destroy(nf_conntrack_cachep);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001211err_free_hash:
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001212 nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
Patrick McHardyac565e52007-07-07 22:30:08 -07001213 nf_conntrack_htable_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001214err_out:
1215 return -ENOMEM;
1216}