]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* 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 | |
dc808fe2 | 6 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
9fb9cbb1 YK |
7 | * (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. | |
9fb9cbb1 YK |
12 | */ |
13 | ||
9fb9cbb1 YK |
14 | #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> | |
d7fe0f24 | 31 | #include <linux/mm.h> |
9fb9cbb1 | 32 | |
9fb9cbb1 YK |
33 | #include <net/netfilter/nf_conntrack.h> |
34 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 35 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 36 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 YK |
37 | #include <net/netfilter/nf_conntrack_helper.h> |
38 | #include <net/netfilter/nf_conntrack_core.h> | |
ecfab2c9 | 39 | #include <net/netfilter/nf_conntrack_extend.h> |
9fb9cbb1 | 40 | |
dc808fe2 | 41 | #define NF_CONNTRACK_VERSION "0.5.0" |
9fb9cbb1 | 42 | |
f8ba1aff | 43 | DEFINE_SPINLOCK(nf_conntrack_lock); |
13b18339 | 44 | EXPORT_SYMBOL_GPL(nf_conntrack_lock); |
9fb9cbb1 YK |
45 | |
46 | /* nf_conntrack_standalone needs this */ | |
47 | atomic_t nf_conntrack_count = ATOMIC_INIT(0); | |
a999e683 | 48 | EXPORT_SYMBOL_GPL(nf_conntrack_count); |
9fb9cbb1 | 49 | |
e2b7606c | 50 | unsigned int nf_conntrack_htable_size __read_mostly; |
13b18339 PM |
51 | EXPORT_SYMBOL_GPL(nf_conntrack_htable_size); |
52 | ||
94aec08e | 53 | int nf_conntrack_max __read_mostly; |
a999e683 | 54 | EXPORT_SYMBOL_GPL(nf_conntrack_max); |
13b18339 | 55 | |
f205c5e0 | 56 | struct hlist_head *nf_conntrack_hash __read_mostly; |
13b18339 PM |
57 | EXPORT_SYMBOL_GPL(nf_conntrack_hash); |
58 | ||
e2b7606c | 59 | struct nf_conn nf_conntrack_untracked __read_mostly; |
13b18339 PM |
60 | EXPORT_SYMBOL_GPL(nf_conntrack_untracked); |
61 | ||
94aec08e | 62 | unsigned int nf_ct_log_invalid __read_mostly; |
f205c5e0 | 63 | HLIST_HEAD(unconfirmed); |
1192e403 | 64 | static int nf_conntrack_vmalloc __read_mostly; |
dacd2a1a | 65 | static struct kmem_cache *nf_conntrack_cachep __read_mostly; |
77ab9cff | 66 | |
9fb9cbb1 YK |
67 | DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat); |
68 | EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat); | |
69 | ||
9fb9cbb1 YK |
70 | static int nf_conntrack_hash_rnd_initted; |
71 | static unsigned int nf_conntrack_hash_rnd; | |
72 | ||
73 | static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple, | |
74 | unsigned int size, unsigned int rnd) | |
75 | { | |
76 | unsigned int a, b; | |
9b887909 SF |
77 | |
78 | a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all), | |
79 | (tuple->src.l3num << 16) | tuple->dst.protonum); | |
80 | b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all), | |
a34c4589 AV |
81 | ((__force __u16)tuple->src.u.all << 16) | |
82 | (__force __u16)tuple->dst.u.all); | |
9fb9cbb1 | 83 | |
34498825 | 84 | return ((u64)jhash_2words(a, b, rnd) * size) >> 32; |
9fb9cbb1 YK |
85 | } |
86 | ||
87 | static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple) | |
88 | { | |
89 | return __hash_conntrack(tuple, nf_conntrack_htable_size, | |
90 | nf_conntrack_hash_rnd); | |
91 | } | |
92 | ||
9fb9cbb1 YK |
93 | int |
94 | nf_ct_get_tuple(const struct sk_buff *skb, | |
95 | unsigned int nhoff, | |
96 | unsigned int dataoff, | |
97 | u_int16_t l3num, | |
98 | u_int8_t protonum, | |
99 | struct nf_conntrack_tuple *tuple, | |
100 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 101 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 YK |
102 | { |
103 | NF_CT_TUPLE_U_BLANK(tuple); | |
104 | ||
105 | tuple->src.l3num = l3num; | |
106 | if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0) | |
107 | return 0; | |
108 | ||
109 | tuple->dst.protonum = protonum; | |
110 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
111 | ||
605dcad6 | 112 | return l4proto->pkt_to_tuple(skb, dataoff, tuple); |
9fb9cbb1 | 113 | } |
13b18339 | 114 | EXPORT_SYMBOL_GPL(nf_ct_get_tuple); |
9fb9cbb1 | 115 | |
e2a3123f YK |
116 | int nf_ct_get_tuplepr(const struct sk_buff *skb, |
117 | unsigned int nhoff, | |
118 | u_int16_t l3num, | |
119 | struct nf_conntrack_tuple *tuple) | |
120 | { | |
121 | struct nf_conntrack_l3proto *l3proto; | |
122 | struct nf_conntrack_l4proto *l4proto; | |
123 | unsigned int protoff; | |
124 | u_int8_t protonum; | |
125 | int ret; | |
126 | ||
127 | rcu_read_lock(); | |
128 | ||
129 | l3proto = __nf_ct_l3proto_find(l3num); | |
130 | ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum); | |
131 | if (ret != NF_ACCEPT) { | |
132 | rcu_read_unlock(); | |
133 | return 0; | |
134 | } | |
135 | ||
136 | l4proto = __nf_ct_l4proto_find(l3num, protonum); | |
137 | ||
138 | ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, tuple, | |
139 | l3proto, l4proto); | |
140 | ||
141 | rcu_read_unlock(); | |
142 | return ret; | |
143 | } | |
144 | EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr); | |
145 | ||
9fb9cbb1 YK |
146 | int |
147 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, | |
148 | const struct nf_conntrack_tuple *orig, | |
149 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 150 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 YK |
151 | { |
152 | NF_CT_TUPLE_U_BLANK(inverse); | |
153 | ||
154 | inverse->src.l3num = orig->src.l3num; | |
155 | if (l3proto->invert_tuple(inverse, orig) == 0) | |
156 | return 0; | |
157 | ||
158 | inverse->dst.dir = !orig->dst.dir; | |
159 | ||
160 | inverse->dst.protonum = orig->dst.protonum; | |
605dcad6 | 161 | return l4proto->invert_tuple(inverse, orig); |
9fb9cbb1 | 162 | } |
13b18339 | 163 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuple); |
9fb9cbb1 | 164 | |
9fb9cbb1 YK |
165 | static void |
166 | clean_from_lists(struct nf_conn *ct) | |
167 | { | |
0d53778e | 168 | pr_debug("clean_from_lists(%p)\n", ct); |
76507f69 PM |
169 | hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); |
170 | hlist_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode); | |
9fb9cbb1 YK |
171 | |
172 | /* Destroy all pending expectations */ | |
c1d10adb | 173 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
174 | } |
175 | ||
176 | static void | |
177 | destroy_conntrack(struct nf_conntrack *nfct) | |
178 | { | |
179 | struct nf_conn *ct = (struct nf_conn *)nfct; | |
605dcad6 | 180 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 | 181 | |
0d53778e | 182 | pr_debug("destroy_conntrack(%p)\n", ct); |
9fb9cbb1 YK |
183 | NF_CT_ASSERT(atomic_read(&nfct->use) == 0); |
184 | NF_CT_ASSERT(!timer_pending(&ct->timeout)); | |
185 | ||
186 | nf_conntrack_event(IPCT_DESTROY, ct); | |
187 | set_bit(IPS_DYING_BIT, &ct->status); | |
188 | ||
189 | /* To make sure we don't get any weird locking issues here: | |
190 | * destroy_conntrack() MUST NOT be called with a write lock | |
191 | * to nf_conntrack_lock!!! -HW */ | |
923f4902 | 192 | rcu_read_lock(); |
923f4902 PM |
193 | l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num, |
194 | ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum); | |
605dcad6 MJ |
195 | if (l4proto && l4proto->destroy) |
196 | l4proto->destroy(ct); | |
9fb9cbb1 | 197 | |
ecfab2c9 YK |
198 | nf_ct_ext_destroy(ct); |
199 | ||
982d9a9c | 200 | rcu_read_unlock(); |
9fb9cbb1 | 201 | |
f8ba1aff | 202 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
203 | /* Expectations will have been removed in clean_from_lists, |
204 | * except TFTP can create an expectation on the first packet, | |
205 | * before connection is in the list, so we need to clean here, | |
206 | * too. */ | |
c1d10adb | 207 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
208 | |
209 | /* We overload first tuple to link into unconfirmed list. */ | |
210 | if (!nf_ct_is_confirmed(ct)) { | |
f205c5e0 PM |
211 | BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode)); |
212 | hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); | |
9fb9cbb1 YK |
213 | } |
214 | ||
215 | NF_CT_STAT_INC(delete); | |
f8ba1aff | 216 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
217 | |
218 | if (ct->master) | |
219 | nf_ct_put(ct->master); | |
220 | ||
0d53778e | 221 | pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct); |
9fb9cbb1 YK |
222 | nf_conntrack_free(ct); |
223 | } | |
224 | ||
225 | static void death_by_timeout(unsigned long ul_conntrack) | |
226 | { | |
227 | struct nf_conn *ct = (void *)ul_conntrack; | |
5397e97d | 228 | struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 229 | struct nf_conntrack_helper *helper; |
5397e97d | 230 | |
3c158f7f PM |
231 | if (help) { |
232 | rcu_read_lock(); | |
233 | helper = rcu_dereference(help->helper); | |
234 | if (helper && helper->destroy) | |
235 | helper->destroy(ct); | |
236 | rcu_read_unlock(); | |
237 | } | |
9fb9cbb1 | 238 | |
f8ba1aff | 239 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
240 | /* Inside lock so preempt is disabled on module removal path. |
241 | * Otherwise we can get spurious warnings. */ | |
242 | NF_CT_STAT_INC(delete_list); | |
243 | clean_from_lists(ct); | |
f8ba1aff | 244 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
245 | nf_ct_put(ct); |
246 | } | |
247 | ||
c1d10adb | 248 | struct nf_conntrack_tuple_hash * |
9fb9cbb1 YK |
249 | __nf_conntrack_find(const struct nf_conntrack_tuple *tuple, |
250 | const struct nf_conn *ignored_conntrack) | |
251 | { | |
252 | struct nf_conntrack_tuple_hash *h; | |
f205c5e0 | 253 | struct hlist_node *n; |
9fb9cbb1 YK |
254 | unsigned int hash = hash_conntrack(tuple); |
255 | ||
76507f69 | 256 | hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], hnode) { |
df0933dc PM |
257 | if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack && |
258 | nf_ct_tuple_equal(tuple, &h->tuple)) { | |
9fb9cbb1 YK |
259 | NF_CT_STAT_INC(found); |
260 | return h; | |
261 | } | |
262 | NF_CT_STAT_INC(searched); | |
263 | } | |
264 | ||
265 | return NULL; | |
266 | } | |
13b18339 | 267 | EXPORT_SYMBOL_GPL(__nf_conntrack_find); |
9fb9cbb1 YK |
268 | |
269 | /* Find a connection corresponding to a tuple. */ | |
270 | struct nf_conntrack_tuple_hash * | |
330f7db5 | 271 | nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple) |
9fb9cbb1 YK |
272 | { |
273 | struct nf_conntrack_tuple_hash *h; | |
76507f69 | 274 | struct nf_conn *ct; |
9fb9cbb1 | 275 | |
76507f69 | 276 | rcu_read_lock(); |
330f7db5 | 277 | h = __nf_conntrack_find(tuple, NULL); |
76507f69 PM |
278 | if (h) { |
279 | ct = nf_ct_tuplehash_to_ctrack(h); | |
280 | if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) | |
281 | h = NULL; | |
282 | } | |
283 | rcu_read_unlock(); | |
9fb9cbb1 YK |
284 | |
285 | return h; | |
286 | } | |
13b18339 | 287 | EXPORT_SYMBOL_GPL(nf_conntrack_find_get); |
9fb9cbb1 | 288 | |
c1d10adb PNA |
289 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
290 | unsigned int hash, | |
601e68e1 | 291 | unsigned int repl_hash) |
c1d10adb | 292 | { |
76507f69 PM |
293 | hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode, |
294 | &nf_conntrack_hash[hash]); | |
295 | hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode, | |
296 | &nf_conntrack_hash[repl_hash]); | |
c1d10adb PNA |
297 | } |
298 | ||
299 | void nf_conntrack_hash_insert(struct nf_conn *ct) | |
300 | { | |
301 | unsigned int hash, repl_hash; | |
302 | ||
303 | hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); | |
304 | repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); | |
305 | ||
f8ba1aff | 306 | spin_lock_bh(&nf_conntrack_lock); |
c1d10adb | 307 | __nf_conntrack_hash_insert(ct, hash, repl_hash); |
f8ba1aff | 308 | spin_unlock_bh(&nf_conntrack_lock); |
c1d10adb | 309 | } |
13b18339 | 310 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert); |
c1d10adb | 311 | |
9fb9cbb1 YK |
312 | /* Confirm a connection given skb; places it in hash table */ |
313 | int | |
3db05fea | 314 | __nf_conntrack_confirm(struct sk_buff *skb) |
9fb9cbb1 YK |
315 | { |
316 | unsigned int hash, repl_hash; | |
df0933dc | 317 | struct nf_conntrack_tuple_hash *h; |
9fb9cbb1 | 318 | struct nf_conn *ct; |
df0933dc | 319 | struct nf_conn_help *help; |
f205c5e0 | 320 | struct hlist_node *n; |
9fb9cbb1 YK |
321 | enum ip_conntrack_info ctinfo; |
322 | ||
3db05fea | 323 | ct = nf_ct_get(skb, &ctinfo); |
9fb9cbb1 YK |
324 | |
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)); | |
0d53778e | 343 | pr_debug("Confirming conntrack %p\n", ct); |
9fb9cbb1 | 344 | |
f8ba1aff | 345 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
346 | |
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. */ | |
f205c5e0 | 350 | hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) |
df0933dc PM |
351 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
352 | &h->tuple)) | |
353 | goto out; | |
f205c5e0 | 354 | hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode) |
df0933dc PM |
355 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
356 | &h->tuple)) | |
357 | goto out; | |
9fb9cbb1 | 358 | |
df0933dc | 359 | /* Remove from unconfirmed list */ |
f205c5e0 | 360 | hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); |
df0933dc PM |
361 | |
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); | |
f8ba1aff | 371 | spin_unlock_bh(&nf_conntrack_lock); |
df0933dc PM |
372 | help = nfct_help(ct); |
373 | if (help && help->helper) | |
3db05fea | 374 | nf_conntrack_event_cache(IPCT_HELPER, skb); |
9fb9cbb1 | 375 | #ifdef CONFIG_NF_NAT_NEEDED |
df0933dc PM |
376 | if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) || |
377 | test_bit(IPS_DST_NAT_DONE_BIT, &ct->status)) | |
3db05fea | 378 | nf_conntrack_event_cache(IPCT_NATINFO, skb); |
9fb9cbb1 | 379 | #endif |
df0933dc | 380 | nf_conntrack_event_cache(master_ct(ct) ? |
3db05fea | 381 | IPCT_RELATED : IPCT_NEW, skb); |
df0933dc | 382 | return NF_ACCEPT; |
9fb9cbb1 | 383 | |
df0933dc | 384 | out: |
9fb9cbb1 | 385 | NF_CT_STAT_INC(insert_failed); |
f8ba1aff | 386 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
387 | return NF_DROP; |
388 | } | |
13b18339 | 389 | EXPORT_SYMBOL_GPL(__nf_conntrack_confirm); |
9fb9cbb1 YK |
390 | |
391 | /* Returns true if a connection correspondings to the tuple (required | |
392 | for NAT). */ | |
393 | int | |
394 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, | |
395 | const struct nf_conn *ignored_conntrack) | |
396 | { | |
397 | struct nf_conntrack_tuple_hash *h; | |
398 | ||
76507f69 | 399 | rcu_read_lock(); |
9fb9cbb1 | 400 | h = __nf_conntrack_find(tuple, ignored_conntrack); |
76507f69 | 401 | rcu_read_unlock(); |
9fb9cbb1 YK |
402 | |
403 | return h != NULL; | |
404 | } | |
13b18339 | 405 | EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken); |
9fb9cbb1 | 406 | |
7ae7730f PM |
407 | #define NF_CT_EVICTION_RANGE 8 |
408 | ||
9fb9cbb1 YK |
409 | /* There's a small race here where we may free a just-assured |
410 | connection. Too bad: we're in trouble anyway. */ | |
7ae7730f | 411 | static int early_drop(unsigned int hash) |
9fb9cbb1 | 412 | { |
f205c5e0 | 413 | /* Use oldest entry, which is roughly LRU */ |
9fb9cbb1 | 414 | struct nf_conntrack_tuple_hash *h; |
df0933dc | 415 | struct nf_conn *ct = NULL, *tmp; |
f205c5e0 | 416 | struct hlist_node *n; |
7ae7730f | 417 | unsigned int i, cnt = 0; |
9fb9cbb1 YK |
418 | int dropped = 0; |
419 | ||
76507f69 | 420 | rcu_read_lock(); |
7ae7730f | 421 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
76507f69 PM |
422 | hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], |
423 | hnode) { | |
7ae7730f PM |
424 | tmp = nf_ct_tuplehash_to_ctrack(h); |
425 | if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) | |
426 | ct = tmp; | |
427 | cnt++; | |
428 | } | |
76507f69 PM |
429 | |
430 | if (ct && unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) | |
431 | ct = NULL; | |
7ae7730f PM |
432 | if (ct || cnt >= NF_CT_EVICTION_RANGE) |
433 | break; | |
434 | hash = (hash + 1) % nf_conntrack_htable_size; | |
9fb9cbb1 | 435 | } |
76507f69 | 436 | rcu_read_unlock(); |
9fb9cbb1 YK |
437 | |
438 | if (!ct) | |
439 | return dropped; | |
440 | ||
441 | if (del_timer(&ct->timeout)) { | |
442 | death_by_timeout((unsigned long)ct); | |
443 | dropped = 1; | |
c0e912d7 | 444 | NF_CT_STAT_INC_ATOMIC(early_drop); |
9fb9cbb1 YK |
445 | } |
446 | nf_ct_put(ct); | |
447 | return dropped; | |
448 | } | |
449 | ||
dacd2a1a YK |
450 | struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
451 | const struct nf_conntrack_tuple *repl) | |
9fb9cbb1 YK |
452 | { |
453 | struct nf_conn *conntrack = NULL; | |
9fb9cbb1 | 454 | |
dc808fe2 | 455 | if (unlikely(!nf_conntrack_hash_rnd_initted)) { |
9fb9cbb1 YK |
456 | get_random_bytes(&nf_conntrack_hash_rnd, 4); |
457 | nf_conntrack_hash_rnd_initted = 1; | |
458 | } | |
459 | ||
5251e2d2 PNA |
460 | /* We don't want any race condition at early drop stage */ |
461 | atomic_inc(&nf_conntrack_count); | |
462 | ||
9fb9cbb1 | 463 | if (nf_conntrack_max |
5251e2d2 | 464 | && atomic_read(&nf_conntrack_count) > nf_conntrack_max) { |
9fb9cbb1 | 465 | unsigned int hash = hash_conntrack(orig); |
7ae7730f | 466 | if (!early_drop(hash)) { |
5251e2d2 | 467 | atomic_dec(&nf_conntrack_count); |
9fb9cbb1 YK |
468 | if (net_ratelimit()) |
469 | printk(KERN_WARNING | |
470 | "nf_conntrack: table full, dropping" | |
471 | " packet.\n"); | |
472 | return ERR_PTR(-ENOMEM); | |
473 | } | |
474 | } | |
475 | ||
dacd2a1a | 476 | conntrack = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC); |
9fb9cbb1 | 477 | if (conntrack == NULL) { |
0d53778e | 478 | pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n"); |
dacd2a1a YK |
479 | atomic_dec(&nf_conntrack_count); |
480 | return ERR_PTR(-ENOMEM); | |
9fb9cbb1 YK |
481 | } |
482 | ||
9fb9cbb1 | 483 | atomic_set(&conntrack->ct_general.use, 1); |
9fb9cbb1 YK |
484 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
485 | conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; | |
486 | /* Don't set timer yet: wait for confirmation */ | |
e6f689db PM |
487 | setup_timer(&conntrack->timeout, death_by_timeout, |
488 | (unsigned long)conntrack); | |
76507f69 | 489 | INIT_RCU_HEAD(&conntrack->rcu); |
9fb9cbb1 | 490 | |
9fb9cbb1 YK |
491 | return conntrack; |
492 | } | |
13b18339 | 493 | EXPORT_SYMBOL_GPL(nf_conntrack_alloc); |
9fb9cbb1 | 494 | |
76507f69 | 495 | static void nf_conntrack_free_rcu(struct rcu_head *head) |
9fb9cbb1 | 496 | { |
76507f69 PM |
497 | struct nf_conn *ct = container_of(head, struct nf_conn, rcu); |
498 | ||
499 | nf_ct_ext_free(ct); | |
500 | kmem_cache_free(nf_conntrack_cachep, ct); | |
9fb9cbb1 YK |
501 | atomic_dec(&nf_conntrack_count); |
502 | } | |
76507f69 PM |
503 | |
504 | void nf_conntrack_free(struct nf_conn *conntrack) | |
505 | { | |
506 | call_rcu(&conntrack->rcu, nf_conntrack_free_rcu); | |
507 | } | |
13b18339 | 508 | EXPORT_SYMBOL_GPL(nf_conntrack_free); |
9fb9cbb1 YK |
509 | |
510 | /* Allocate a new conntrack: we return -ENOMEM if classification | |
511 | failed due to stress. Otherwise it really is unclassifiable. */ | |
512 | static struct nf_conntrack_tuple_hash * | |
513 | init_conntrack(const struct nf_conntrack_tuple *tuple, | |
514 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 515 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 YK |
516 | struct sk_buff *skb, |
517 | unsigned int dataoff) | |
518 | { | |
519 | struct nf_conn *conntrack; | |
3c158f7f | 520 | struct nf_conn_help *help; |
9fb9cbb1 YK |
521 | struct nf_conntrack_tuple repl_tuple; |
522 | struct nf_conntrack_expect *exp; | |
523 | ||
605dcad6 | 524 | if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) { |
0d53778e | 525 | pr_debug("Can't invert tuple.\n"); |
9fb9cbb1 YK |
526 | return NULL; |
527 | } | |
528 | ||
dacd2a1a | 529 | conntrack = nf_conntrack_alloc(tuple, &repl_tuple); |
9fb9cbb1 | 530 | if (conntrack == NULL || IS_ERR(conntrack)) { |
0d53778e | 531 | pr_debug("Can't allocate conntrack.\n"); |
9fb9cbb1 YK |
532 | return (struct nf_conntrack_tuple_hash *)conntrack; |
533 | } | |
534 | ||
605dcad6 | 535 | if (!l4proto->new(conntrack, skb, dataoff)) { |
9fb9cbb1 | 536 | nf_conntrack_free(conntrack); |
0d53778e | 537 | pr_debug("init conntrack: can't track with proto module\n"); |
9fb9cbb1 YK |
538 | return NULL; |
539 | } | |
540 | ||
f8ba1aff | 541 | spin_lock_bh(&nf_conntrack_lock); |
6823645d | 542 | exp = nf_ct_find_expectation(tuple); |
9fb9cbb1 | 543 | if (exp) { |
0d53778e PM |
544 | pr_debug("conntrack: expectation arrives ct=%p exp=%p\n", |
545 | conntrack, exp); | |
9fb9cbb1 YK |
546 | /* Welcome, Mr. Bond. We've been expecting you... */ |
547 | __set_bit(IPS_EXPECTED_BIT, &conntrack->status); | |
548 | conntrack->master = exp->master; | |
ceceae1b | 549 | if (exp->helper) { |
b560580a | 550 | help = nf_ct_helper_ext_add(conntrack, GFP_ATOMIC); |
ceceae1b YK |
551 | if (help) |
552 | rcu_assign_pointer(help->helper, exp->helper); | |
ceceae1b YK |
553 | } |
554 | ||
9fb9cbb1 YK |
555 | #ifdef CONFIG_NF_CONNTRACK_MARK |
556 | conntrack->mark = exp->master->mark; | |
7c9728c3 JM |
557 | #endif |
558 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
559 | conntrack->secmark = exp->master->secmark; | |
9fb9cbb1 YK |
560 | #endif |
561 | nf_conntrack_get(&conntrack->master->ct_general); | |
562 | NF_CT_STAT_INC(expect_new); | |
22e7410b | 563 | } else { |
ceceae1b YK |
564 | struct nf_conntrack_helper *helper; |
565 | ||
566 | helper = __nf_ct_helper_find(&repl_tuple); | |
567 | if (helper) { | |
b560580a | 568 | help = nf_ct_helper_ext_add(conntrack, GFP_ATOMIC); |
ceceae1b | 569 | if (help) |
ceceae1b | 570 | rcu_assign_pointer(help->helper, helper); |
3c158f7f | 571 | } |
9fb9cbb1 | 572 | NF_CT_STAT_INC(new); |
22e7410b | 573 | } |
9fb9cbb1 YK |
574 | |
575 | /* Overload tuple linked list to put us in unconfirmed list. */ | |
f205c5e0 PM |
576 | hlist_add_head(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].hnode, |
577 | &unconfirmed); | |
9fb9cbb1 | 578 | |
f8ba1aff | 579 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
580 | |
581 | if (exp) { | |
582 | if (exp->expectfn) | |
583 | exp->expectfn(conntrack, exp); | |
6823645d | 584 | nf_ct_expect_put(exp); |
9fb9cbb1 YK |
585 | } |
586 | ||
587 | return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL]; | |
588 | } | |
589 | ||
590 | /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ | |
591 | static inline struct nf_conn * | |
592 | resolve_normal_ct(struct sk_buff *skb, | |
593 | unsigned int dataoff, | |
594 | u_int16_t l3num, | |
595 | u_int8_t protonum, | |
596 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 597 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 YK |
598 | int *set_reply, |
599 | enum ip_conntrack_info *ctinfo) | |
600 | { | |
601 | struct nf_conntrack_tuple tuple; | |
602 | struct nf_conntrack_tuple_hash *h; | |
603 | struct nf_conn *ct; | |
604 | ||
bbe735e4 | 605 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), |
9fb9cbb1 | 606 | dataoff, l3num, protonum, &tuple, l3proto, |
605dcad6 | 607 | l4proto)) { |
0d53778e | 608 | pr_debug("resolve_normal_ct: Can't get tuple\n"); |
9fb9cbb1 YK |
609 | return NULL; |
610 | } | |
611 | ||
612 | /* look for tuple match */ | |
330f7db5 | 613 | h = nf_conntrack_find_get(&tuple); |
9fb9cbb1 | 614 | if (!h) { |
605dcad6 | 615 | h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff); |
9fb9cbb1 YK |
616 | if (!h) |
617 | return NULL; | |
618 | if (IS_ERR(h)) | |
619 | return (void *)h; | |
620 | } | |
621 | ct = nf_ct_tuplehash_to_ctrack(h); | |
622 | ||
623 | /* It exists; we have (non-exclusive) reference. */ | |
624 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { | |
625 | *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY; | |
626 | /* Please set reply bit if this packet OK */ | |
627 | *set_reply = 1; | |
628 | } else { | |
629 | /* Once we've had two way comms, always ESTABLISHED. */ | |
630 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { | |
0d53778e | 631 | pr_debug("nf_conntrack_in: normal packet for %p\n", ct); |
9fb9cbb1 YK |
632 | *ctinfo = IP_CT_ESTABLISHED; |
633 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { | |
0d53778e PM |
634 | pr_debug("nf_conntrack_in: related packet for %p\n", |
635 | ct); | |
9fb9cbb1 YK |
636 | *ctinfo = IP_CT_RELATED; |
637 | } else { | |
0d53778e | 638 | pr_debug("nf_conntrack_in: new packet for %p\n", ct); |
9fb9cbb1 YK |
639 | *ctinfo = IP_CT_NEW; |
640 | } | |
641 | *set_reply = 0; | |
642 | } | |
643 | skb->nfct = &ct->ct_general; | |
644 | skb->nfctinfo = *ctinfo; | |
645 | return ct; | |
646 | } | |
647 | ||
648 | unsigned int | |
3db05fea | 649 | nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff *skb) |
9fb9cbb1 YK |
650 | { |
651 | struct nf_conn *ct; | |
652 | enum ip_conntrack_info ctinfo; | |
653 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 654 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 YK |
655 | unsigned int dataoff; |
656 | u_int8_t protonum; | |
657 | int set_reply = 0; | |
658 | int ret; | |
659 | ||
660 | /* Previously seen (loopback or untracked)? Ignore. */ | |
3db05fea | 661 | if (skb->nfct) { |
c0e912d7 | 662 | NF_CT_STAT_INC_ATOMIC(ignore); |
9fb9cbb1 YK |
663 | return NF_ACCEPT; |
664 | } | |
665 | ||
923f4902 | 666 | /* rcu_read_lock()ed by nf_hook_slow */ |
c1d10adb | 667 | l3proto = __nf_ct_l3proto_find((u_int16_t)pf); |
3db05fea | 668 | ret = l3proto->get_l4proto(skb, skb_network_offset(skb), |
ffc30690 YK |
669 | &dataoff, &protonum); |
670 | if (ret <= 0) { | |
0d53778e | 671 | pr_debug("not prepared to track yet or error occured\n"); |
d87d8469 YK |
672 | NF_CT_STAT_INC_ATOMIC(error); |
673 | NF_CT_STAT_INC_ATOMIC(invalid); | |
9fb9cbb1 YK |
674 | return -ret; |
675 | } | |
676 | ||
605dcad6 | 677 | l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum); |
9fb9cbb1 YK |
678 | |
679 | /* It may be an special packet, error, unclean... | |
680 | * inverse of the return code tells to the netfilter | |
681 | * core what to do with the packet. */ | |
605dcad6 | 682 | if (l4proto->error != NULL && |
3db05fea | 683 | (ret = l4proto->error(skb, dataoff, &ctinfo, pf, hooknum)) <= 0) { |
c0e912d7 PM |
684 | NF_CT_STAT_INC_ATOMIC(error); |
685 | NF_CT_STAT_INC_ATOMIC(invalid); | |
9fb9cbb1 YK |
686 | return -ret; |
687 | } | |
688 | ||
3db05fea | 689 | ct = resolve_normal_ct(skb, dataoff, pf, protonum, l3proto, l4proto, |
9fb9cbb1 YK |
690 | &set_reply, &ctinfo); |
691 | if (!ct) { | |
692 | /* Not valid part of a connection */ | |
c0e912d7 | 693 | NF_CT_STAT_INC_ATOMIC(invalid); |
9fb9cbb1 YK |
694 | return NF_ACCEPT; |
695 | } | |
696 | ||
697 | if (IS_ERR(ct)) { | |
698 | /* Too stressed to deal. */ | |
c0e912d7 | 699 | NF_CT_STAT_INC_ATOMIC(drop); |
9fb9cbb1 YK |
700 | return NF_DROP; |
701 | } | |
702 | ||
3db05fea | 703 | NF_CT_ASSERT(skb->nfct); |
9fb9cbb1 | 704 | |
3db05fea | 705 | ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum); |
9fb9cbb1 YK |
706 | if (ret < 0) { |
707 | /* Invalid: inverse of the return code tells | |
708 | * the netfilter core what to do */ | |
0d53778e | 709 | pr_debug("nf_conntrack_in: Can't track with proto module\n"); |
3db05fea HX |
710 | nf_conntrack_put(skb->nfct); |
711 | skb->nfct = NULL; | |
c0e912d7 | 712 | NF_CT_STAT_INC_ATOMIC(invalid); |
9fb9cbb1 YK |
713 | return -ret; |
714 | } | |
715 | ||
716 | if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
3db05fea | 717 | nf_conntrack_event_cache(IPCT_STATUS, skb); |
9fb9cbb1 YK |
718 | |
719 | return ret; | |
720 | } | |
13b18339 | 721 | EXPORT_SYMBOL_GPL(nf_conntrack_in); |
9fb9cbb1 YK |
722 | |
723 | int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, | |
724 | const struct nf_conntrack_tuple *orig) | |
725 | { | |
923f4902 PM |
726 | int ret; |
727 | ||
728 | rcu_read_lock(); | |
729 | ret = nf_ct_invert_tuple(inverse, orig, | |
730 | __nf_ct_l3proto_find(orig->src.l3num), | |
731 | __nf_ct_l4proto_find(orig->src.l3num, | |
732 | orig->dst.protonum)); | |
733 | rcu_read_unlock(); | |
734 | return ret; | |
9fb9cbb1 | 735 | } |
13b18339 | 736 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr); |
9fb9cbb1 | 737 | |
5b1158e9 JK |
738 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
739 | implicitly racy: see __nf_conntrack_confirm */ | |
740 | void nf_conntrack_alter_reply(struct nf_conn *ct, | |
741 | const struct nf_conntrack_tuple *newreply) | |
742 | { | |
743 | struct nf_conn_help *help = nfct_help(ct); | |
ceceae1b | 744 | struct nf_conntrack_helper *helper; |
5b1158e9 | 745 | |
5b1158e9 JK |
746 | /* Should be unconfirmed, so not in hash table yet */ |
747 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); | |
748 | ||
0d53778e | 749 | pr_debug("Altering reply tuple of %p to ", ct); |
5b1158e9 JK |
750 | NF_CT_DUMP_TUPLE(newreply); |
751 | ||
752 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; | |
ceceae1b | 753 | if (ct->master || (help && help->expecting != 0)) |
c52fbb41 | 754 | return; |
ceceae1b | 755 | |
c52fbb41 | 756 | rcu_read_lock(); |
ceceae1b YK |
757 | helper = __nf_ct_helper_find(newreply); |
758 | if (helper == NULL) { | |
759 | if (help) | |
760 | rcu_assign_pointer(help->helper, NULL); | |
761 | goto out; | |
5d78a849 | 762 | } |
ceceae1b YK |
763 | |
764 | if (help == NULL) { | |
b560580a PM |
765 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
766 | if (help == NULL) | |
ceceae1b | 767 | goto out; |
ceceae1b YK |
768 | } else { |
769 | memset(&help->help, 0, sizeof(help->help)); | |
770 | } | |
771 | ||
772 | rcu_assign_pointer(help->helper, helper); | |
773 | out: | |
c52fbb41 | 774 | rcu_read_unlock(); |
5b1158e9 | 775 | } |
13b18339 | 776 | EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply); |
5b1158e9 | 777 | |
9fb9cbb1 YK |
778 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
779 | void __nf_ct_refresh_acct(struct nf_conn *ct, | |
780 | enum ip_conntrack_info ctinfo, | |
781 | const struct sk_buff *skb, | |
782 | unsigned long extra_jiffies, | |
783 | int do_acct) | |
784 | { | |
785 | int event = 0; | |
786 | ||
787 | NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct); | |
788 | NF_CT_ASSERT(skb); | |
789 | ||
f8ba1aff | 790 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 | 791 | |
997ae831 | 792 | /* Only update if this is not a fixed timeout */ |
47d95045 PM |
793 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) |
794 | goto acct; | |
997ae831 | 795 | |
9fb9cbb1 YK |
796 | /* If not in hash table, timer will not be active yet */ |
797 | if (!nf_ct_is_confirmed(ct)) { | |
798 | ct->timeout.expires = extra_jiffies; | |
799 | event = IPCT_REFRESH; | |
800 | } else { | |
be00c8e4 MJ |
801 | unsigned long newtime = jiffies + extra_jiffies; |
802 | ||
803 | /* Only update the timeout if the new timeout is at least | |
804 | HZ jiffies from the old timeout. Need del_timer for race | |
805 | avoidance (may already be dying). */ | |
806 | if (newtime - ct->timeout.expires >= HZ | |
807 | && del_timer(&ct->timeout)) { | |
808 | ct->timeout.expires = newtime; | |
9fb9cbb1 YK |
809 | add_timer(&ct->timeout); |
810 | event = IPCT_REFRESH; | |
811 | } | |
812 | } | |
813 | ||
47d95045 | 814 | acct: |
9fb9cbb1 YK |
815 | #ifdef CONFIG_NF_CT_ACCT |
816 | if (do_acct) { | |
817 | ct->counters[CTINFO2DIR(ctinfo)].packets++; | |
818 | ct->counters[CTINFO2DIR(ctinfo)].bytes += | |
bbe735e4 | 819 | skb->len - skb_network_offset(skb); |
3ffd5eeb MJ |
820 | |
821 | if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000) | |
822 | || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000)) | |
823 | event |= IPCT_COUNTER_FILLING; | |
9fb9cbb1 YK |
824 | } |
825 | #endif | |
826 | ||
f8ba1aff | 827 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 YK |
828 | |
829 | /* must be unlocked when calling event cache */ | |
830 | if (event) | |
831 | nf_conntrack_event_cache(event, skb); | |
832 | } | |
13b18339 | 833 | EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct); |
9fb9cbb1 | 834 | |
e281db5c | 835 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c1d10adb PNA |
836 | |
837 | #include <linux/netfilter/nfnetlink.h> | |
838 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
57b47a53 IM |
839 | #include <linux/mutex.h> |
840 | ||
c1d10adb PNA |
841 | /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be |
842 | * in ip_conntrack_core, since we don't want the protocols to autoload | |
843 | * or depend on ctnetlink */ | |
fdf70832 | 844 | int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
845 | const struct nf_conntrack_tuple *tuple) |
846 | { | |
77236b6e PM |
847 | NLA_PUT_BE16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port); |
848 | NLA_PUT_BE16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port); | |
c1d10adb PNA |
849 | return 0; |
850 | ||
df6fb868 | 851 | nla_put_failure: |
c1d10adb PNA |
852 | return -1; |
853 | } | |
fdf70832 | 854 | EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr); |
c1d10adb | 855 | |
f73e924c PM |
856 | const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = { |
857 | [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 }, | |
858 | [CTA_PROTO_DST_PORT] = { .type = NLA_U16 }, | |
c1d10adb | 859 | }; |
f73e924c | 860 | EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy); |
c1d10adb | 861 | |
fdf70832 | 862 | int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
863 | struct nf_conntrack_tuple *t) |
864 | { | |
df6fb868 | 865 | if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT]) |
c1d10adb PNA |
866 | return -EINVAL; |
867 | ||
77236b6e PM |
868 | t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]); |
869 | t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]); | |
c1d10adb PNA |
870 | |
871 | return 0; | |
872 | } | |
fdf70832 | 873 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple); |
c1d10adb PNA |
874 | #endif |
875 | ||
9fb9cbb1 | 876 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
b334aadc | 877 | static void nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb) |
9fb9cbb1 YK |
878 | { |
879 | struct nf_conn *ct; | |
880 | enum ip_conntrack_info ctinfo; | |
881 | ||
882 | /* This ICMP is in reverse direction to the packet which caused it */ | |
883 | ct = nf_ct_get(skb, &ctinfo); | |
884 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) | |
885 | ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY; | |
886 | else | |
887 | ctinfo = IP_CT_RELATED; | |
888 | ||
889 | /* Attach to new skbuff, and increment count */ | |
890 | nskb->nfct = &ct->ct_general; | |
891 | nskb->nfctinfo = ctinfo; | |
892 | nf_conntrack_get(nskb->nfct); | |
893 | } | |
894 | ||
895 | static inline int | |
896 | do_iter(const struct nf_conntrack_tuple_hash *i, | |
897 | int (*iter)(struct nf_conn *i, void *data), | |
898 | void *data) | |
899 | { | |
900 | return iter(nf_ct_tuplehash_to_ctrack(i), data); | |
901 | } | |
902 | ||
903 | /* Bring out ya dead! */ | |
df0933dc | 904 | static struct nf_conn * |
9fb9cbb1 YK |
905 | get_next_corpse(int (*iter)(struct nf_conn *i, void *data), |
906 | void *data, unsigned int *bucket) | |
907 | { | |
df0933dc PM |
908 | struct nf_conntrack_tuple_hash *h; |
909 | struct nf_conn *ct; | |
f205c5e0 | 910 | struct hlist_node *n; |
9fb9cbb1 | 911 | |
f8ba1aff | 912 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 | 913 | for (; *bucket < nf_conntrack_htable_size; (*bucket)++) { |
f205c5e0 | 914 | hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) { |
df0933dc PM |
915 | ct = nf_ct_tuplehash_to_ctrack(h); |
916 | if (iter(ct, data)) | |
917 | goto found; | |
918 | } | |
601e68e1 | 919 | } |
f205c5e0 | 920 | hlist_for_each_entry(h, n, &unconfirmed, hnode) { |
df0933dc PM |
921 | ct = nf_ct_tuplehash_to_ctrack(h); |
922 | if (iter(ct, data)) | |
ec68e97d | 923 | set_bit(IPS_DYING_BIT, &ct->status); |
df0933dc | 924 | } |
f8ba1aff | 925 | spin_unlock_bh(&nf_conntrack_lock); |
df0933dc PM |
926 | return NULL; |
927 | found: | |
c073e3fa | 928 | atomic_inc(&ct->ct_general.use); |
f8ba1aff | 929 | spin_unlock_bh(&nf_conntrack_lock); |
df0933dc | 930 | return ct; |
9fb9cbb1 YK |
931 | } |
932 | ||
933 | void | |
934 | nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data) | |
935 | { | |
df0933dc | 936 | struct nf_conn *ct; |
9fb9cbb1 YK |
937 | unsigned int bucket = 0; |
938 | ||
df0933dc | 939 | while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) { |
9fb9cbb1 YK |
940 | /* Time to push up daises... */ |
941 | if (del_timer(&ct->timeout)) | |
942 | death_by_timeout((unsigned long)ct); | |
943 | /* ... else the timer will get him soon. */ | |
944 | ||
945 | nf_ct_put(ct); | |
946 | } | |
947 | } | |
13b18339 | 948 | EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup); |
9fb9cbb1 YK |
949 | |
950 | static int kill_all(struct nf_conn *i, void *data) | |
951 | { | |
952 | return 1; | |
953 | } | |
954 | ||
96eb24d7 | 955 | void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, unsigned int size) |
9fb9cbb1 YK |
956 | { |
957 | if (vmalloced) | |
958 | vfree(hash); | |
959 | else | |
601e68e1 | 960 | free_pages((unsigned long)hash, |
f205c5e0 | 961 | get_order(sizeof(struct hlist_head) * size)); |
9fb9cbb1 | 962 | } |
ac565e5f | 963 | EXPORT_SYMBOL_GPL(nf_ct_free_hashtable); |
9fb9cbb1 | 964 | |
272491ef | 965 | void nf_conntrack_flush(void) |
c1d10adb PNA |
966 | { |
967 | nf_ct_iterate_cleanup(kill_all, NULL); | |
968 | } | |
13b18339 | 969 | EXPORT_SYMBOL_GPL(nf_conntrack_flush); |
c1d10adb | 970 | |
9fb9cbb1 YK |
971 | /* Mishearing the voices in his head, our hero wonders how he's |
972 | supposed to kill the mall. */ | |
973 | void nf_conntrack_cleanup(void) | |
974 | { | |
c3a47ab3 | 975 | rcu_assign_pointer(ip_ct_attach, NULL); |
7d3cdc6b | 976 | |
9fb9cbb1 YK |
977 | /* This makes sure all current packets have passed through |
978 | netfilter framework. Roll on, two-stage module | |
979 | delete... */ | |
980 | synchronize_net(); | |
981 | ||
982 | nf_ct_event_cache_flush(); | |
983 | i_see_dead_people: | |
c1d10adb | 984 | nf_conntrack_flush(); |
9fb9cbb1 YK |
985 | if (atomic_read(&nf_conntrack_count) != 0) { |
986 | schedule(); | |
987 | goto i_see_dead_people; | |
988 | } | |
6636568c PM |
989 | /* wait until all references to nf_conntrack_untracked are dropped */ |
990 | while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1) | |
991 | schedule(); | |
9fb9cbb1 | 992 | |
de6e05c4 YK |
993 | rcu_assign_pointer(nf_ct_destroy, NULL); |
994 | ||
dacd2a1a | 995 | kmem_cache_destroy(nf_conntrack_cachep); |
ac565e5f PM |
996 | nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc, |
997 | nf_conntrack_htable_size); | |
5a6f294e | 998 | |
ac5357eb | 999 | nf_conntrack_proto_fini(); |
ceceae1b | 1000 | nf_conntrack_helper_fini(); |
e9c1b084 | 1001 | nf_conntrack_expect_fini(); |
9fb9cbb1 YK |
1002 | } |
1003 | ||
96eb24d7 | 1004 | struct hlist_head *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced) |
9fb9cbb1 | 1005 | { |
f205c5e0 | 1006 | struct hlist_head *hash; |
8e5105a0 | 1007 | unsigned int size, i; |
9fb9cbb1 | 1008 | |
601e68e1 | 1009 | *vmalloced = 0; |
8e5105a0 | 1010 | |
f205c5e0 | 1011 | size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head)); |
29b67497 | 1012 | hash = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN, |
f205c5e0 | 1013 | get_order(sizeof(struct hlist_head) |
9fb9cbb1 | 1014 | * size)); |
601e68e1 | 1015 | if (!hash) { |
9fb9cbb1 YK |
1016 | *vmalloced = 1; |
1017 | printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n"); | |
f205c5e0 | 1018 | hash = vmalloc(sizeof(struct hlist_head) * size); |
9fb9cbb1 YK |
1019 | } |
1020 | ||
1021 | if (hash) | |
601e68e1 | 1022 | for (i = 0; i < size; i++) |
f205c5e0 | 1023 | INIT_HLIST_HEAD(&hash[i]); |
9fb9cbb1 YK |
1024 | |
1025 | return hash; | |
1026 | } | |
ac565e5f | 1027 | EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable); |
9fb9cbb1 | 1028 | |
fae718dd | 1029 | int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) |
9fb9cbb1 | 1030 | { |
96eb24d7 SH |
1031 | int i, bucket, vmalloced, old_vmalloced; |
1032 | unsigned int hashsize, old_size; | |
9fb9cbb1 | 1033 | int rnd; |
f205c5e0 | 1034 | struct hlist_head *hash, *old_hash; |
9fb9cbb1 YK |
1035 | struct nf_conntrack_tuple_hash *h; |
1036 | ||
1037 | /* On boot, we can set this without any fancy locking. */ | |
1038 | if (!nf_conntrack_htable_size) | |
1039 | return param_set_uint(val, kp); | |
1040 | ||
96eb24d7 | 1041 | hashsize = simple_strtoul(val, NULL, 0); |
9fb9cbb1 YK |
1042 | if (!hashsize) |
1043 | return -EINVAL; | |
1044 | ||
ac565e5f | 1045 | hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced); |
9fb9cbb1 YK |
1046 | if (!hash) |
1047 | return -ENOMEM; | |
1048 | ||
1049 | /* We have to rehahs for the new table anyway, so we also can | |
1050 | * use a newrandom seed */ | |
1051 | get_random_bytes(&rnd, 4); | |
1052 | ||
76507f69 PM |
1053 | /* Lookups in the old hash might happen in parallel, which means we |
1054 | * might get false negatives during connection lookup. New connections | |
1055 | * created because of a false negative won't make it into the hash | |
1056 | * though since that required taking the lock. | |
1057 | */ | |
f8ba1aff | 1058 | spin_lock_bh(&nf_conntrack_lock); |
9fb9cbb1 | 1059 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
f205c5e0 PM |
1060 | while (!hlist_empty(&nf_conntrack_hash[i])) { |
1061 | h = hlist_entry(nf_conntrack_hash[i].first, | |
1062 | struct nf_conntrack_tuple_hash, hnode); | |
76507f69 | 1063 | hlist_del_rcu(&h->hnode); |
9fb9cbb1 | 1064 | bucket = __hash_conntrack(&h->tuple, hashsize, rnd); |
f205c5e0 | 1065 | hlist_add_head(&h->hnode, &hash[bucket]); |
9fb9cbb1 YK |
1066 | } |
1067 | } | |
1068 | old_size = nf_conntrack_htable_size; | |
1069 | old_vmalloced = nf_conntrack_vmalloc; | |
1070 | old_hash = nf_conntrack_hash; | |
1071 | ||
1072 | nf_conntrack_htable_size = hashsize; | |
1073 | nf_conntrack_vmalloc = vmalloced; | |
1074 | nf_conntrack_hash = hash; | |
1075 | nf_conntrack_hash_rnd = rnd; | |
f8ba1aff | 1076 | spin_unlock_bh(&nf_conntrack_lock); |
9fb9cbb1 | 1077 | |
ac565e5f | 1078 | nf_ct_free_hashtable(old_hash, old_vmalloced, old_size); |
9fb9cbb1 YK |
1079 | return 0; |
1080 | } | |
fae718dd | 1081 | EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize); |
9fb9cbb1 | 1082 | |
fae718dd | 1083 | module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint, |
9fb9cbb1 YK |
1084 | &nf_conntrack_htable_size, 0600); |
1085 | ||
1086 | int __init nf_conntrack_init(void) | |
1087 | { | |
f205c5e0 | 1088 | int max_factor = 8; |
9fb9cbb1 YK |
1089 | int ret; |
1090 | ||
1091 | /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB | |
f205c5e0 | 1092 | * machine has 512 buckets. >= 1GB machines have 16384 buckets. */ |
9fb9cbb1 YK |
1093 | if (!nf_conntrack_htable_size) { |
1094 | nf_conntrack_htable_size | |
1095 | = (((num_physpages << PAGE_SHIFT) / 16384) | |
f205c5e0 | 1096 | / sizeof(struct hlist_head)); |
9fb9cbb1 | 1097 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
f205c5e0 PM |
1098 | nf_conntrack_htable_size = 16384; |
1099 | if (nf_conntrack_htable_size < 32) | |
1100 | nf_conntrack_htable_size = 32; | |
1101 | ||
1102 | /* Use a max. factor of four by default to get the same max as | |
1103 | * with the old struct list_heads. When a table size is given | |
1104 | * we use the old value of 8 to avoid reducing the max. | |
1105 | * entries. */ | |
1106 | max_factor = 4; | |
9fb9cbb1 | 1107 | } |
ac565e5f PM |
1108 | nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, |
1109 | &nf_conntrack_vmalloc); | |
9fb9cbb1 YK |
1110 | if (!nf_conntrack_hash) { |
1111 | printk(KERN_ERR "Unable to create nf_conntrack_hash\n"); | |
1112 | goto err_out; | |
1113 | } | |
1114 | ||
f205c5e0 | 1115 | nf_conntrack_max = max_factor * nf_conntrack_htable_size; |
8e5105a0 PM |
1116 | |
1117 | printk("nf_conntrack version %s (%u buckets, %d max)\n", | |
1118 | NF_CONNTRACK_VERSION, nf_conntrack_htable_size, | |
1119 | nf_conntrack_max); | |
1120 | ||
dacd2a1a YK |
1121 | nf_conntrack_cachep = kmem_cache_create("nf_conntrack", |
1122 | sizeof(struct nf_conn), | |
20c2df83 | 1123 | 0, 0, NULL); |
dacd2a1a | 1124 | if (!nf_conntrack_cachep) { |
9fb9cbb1 YK |
1125 | printk(KERN_ERR "Unable to create nf_conn slab cache\n"); |
1126 | goto err_free_hash; | |
1127 | } | |
1128 | ||
e9c1b084 PM |
1129 | ret = nf_conntrack_proto_init(); |
1130 | if (ret < 0) | |
9fb9cbb1 | 1131 | goto err_free_conntrack_slab; |
9fb9cbb1 | 1132 | |
e9c1b084 | 1133 | ret = nf_conntrack_expect_init(); |
933a41e7 | 1134 | if (ret < 0) |
e9c1b084 | 1135 | goto out_fini_proto; |
933a41e7 | 1136 | |
ceceae1b YK |
1137 | ret = nf_conntrack_helper_init(); |
1138 | if (ret < 0) | |
e9c1b084 | 1139 | goto out_fini_expect; |
ceceae1b | 1140 | |
7d3cdc6b | 1141 | /* For use by REJECT target */ |
b334aadc | 1142 | rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach); |
de6e05c4 | 1143 | rcu_assign_pointer(nf_ct_destroy, destroy_conntrack); |
7d3cdc6b | 1144 | |
9fb9cbb1 YK |
1145 | /* Set up fake conntrack: |
1146 | - to never be deleted, not in any hashes */ | |
1147 | atomic_set(&nf_conntrack_untracked.ct_general.use, 1); | |
1148 | /* - and look it like as a confirmed connection */ | |
1149 | set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status); | |
1150 | ||
1151 | return ret; | |
1152 | ||
e9c1b084 PM |
1153 | out_fini_expect: |
1154 | nf_conntrack_expect_fini(); | |
ceceae1b YK |
1155 | out_fini_proto: |
1156 | nf_conntrack_proto_fini(); | |
9fb9cbb1 | 1157 | err_free_conntrack_slab: |
dacd2a1a | 1158 | kmem_cache_destroy(nf_conntrack_cachep); |
9fb9cbb1 | 1159 | err_free_hash: |
ac565e5f PM |
1160 | nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc, |
1161 | nf_conntrack_htable_size); | |
9fb9cbb1 YK |
1162 | err_out: |
1163 | return -ENOMEM; | |
1164 | } |