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 | 7 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
f229f6ce | 8 | * (C) 2005-2012 Patrick McHardy <kaber@trash.net> |
9fb9cbb1 YK |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
13 | */ |
14 | ||
9fb9cbb1 YK |
15 | #include <linux/types.h> |
16 | #include <linux/netfilter.h> | |
17 | #include <linux/module.h> | |
d43c36dc | 18 | #include <linux/sched.h> |
9fb9cbb1 YK |
19 | #include <linux/skbuff.h> |
20 | #include <linux/proc_fs.h> | |
21 | #include <linux/vmalloc.h> | |
22 | #include <linux/stddef.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/random.h> | |
25 | #include <linux/jhash.h> | |
26 | #include <linux/err.h> | |
27 | #include <linux/percpu.h> | |
28 | #include <linux/moduleparam.h> | |
29 | #include <linux/notifier.h> | |
30 | #include <linux/kernel.h> | |
31 | #include <linux/netdevice.h> | |
32 | #include <linux/socket.h> | |
d7fe0f24 | 33 | #include <linux/mm.h> |
d696c7bd | 34 | #include <linux/nsproxy.h> |
ea781f19 | 35 | #include <linux/rculist_nulls.h> |
9fb9cbb1 | 36 | |
9fb9cbb1 YK |
37 | #include <net/netfilter/nf_conntrack.h> |
38 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 39 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 40 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 | 41 | #include <net/netfilter/nf_conntrack_helper.h> |
41d73ec0 | 42 | #include <net/netfilter/nf_conntrack_seqadj.h> |
9fb9cbb1 | 43 | #include <net/netfilter/nf_conntrack_core.h> |
ecfab2c9 | 44 | #include <net/netfilter/nf_conntrack_extend.h> |
58401572 | 45 | #include <net/netfilter/nf_conntrack_acct.h> |
a0891aa6 | 46 | #include <net/netfilter/nf_conntrack_ecache.h> |
5d0aa2cc | 47 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 48 | #include <net/netfilter/nf_conntrack_timestamp.h> |
dd705072 | 49 | #include <net/netfilter/nf_conntrack_timeout.h> |
c539f017 | 50 | #include <net/netfilter/nf_conntrack_labels.h> |
48b1de4c | 51 | #include <net/netfilter/nf_conntrack_synproxy.h> |
e6a7d3c0 | 52 | #include <net/netfilter/nf_nat.h> |
e17b666a | 53 | #include <net/netfilter/nf_nat_core.h> |
49376368 | 54 | #include <net/netfilter/nf_nat_helper.h> |
9fb9cbb1 | 55 | |
dc808fe2 | 56 | #define NF_CONNTRACK_VERSION "0.5.0" |
9fb9cbb1 | 57 | |
e17b666a PM |
58 | int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct, |
59 | enum nf_nat_manip_type manip, | |
39938324 | 60 | const struct nlattr *attr) __read_mostly; |
e6a7d3c0 PNA |
61 | EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook); |
62 | ||
93bb0ceb JDB |
63 | __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS]; |
64 | EXPORT_SYMBOL_GPL(nf_conntrack_locks); | |
9fb9cbb1 | 65 | |
ca7433df JDB |
66 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock); |
67 | EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock); | |
68 | ||
70d72b7e | 69 | static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock); |
b16c2919 SL |
70 | static __read_mostly bool nf_conntrack_locks_all; |
71 | ||
72 | void nf_conntrack_lock(spinlock_t *lock) __acquires(lock) | |
73 | { | |
74 | spin_lock(lock); | |
75 | while (unlikely(nf_conntrack_locks_all)) { | |
76 | spin_unlock(lock); | |
e39365be | 77 | spin_unlock_wait(&nf_conntrack_locks_all_lock); |
b16c2919 SL |
78 | spin_lock(lock); |
79 | } | |
80 | } | |
81 | EXPORT_SYMBOL_GPL(nf_conntrack_lock); | |
82 | ||
93bb0ceb JDB |
83 | static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2) |
84 | { | |
85 | h1 %= CONNTRACK_LOCKS; | |
86 | h2 %= CONNTRACK_LOCKS; | |
87 | spin_unlock(&nf_conntrack_locks[h1]); | |
88 | if (h1 != h2) | |
89 | spin_unlock(&nf_conntrack_locks[h2]); | |
90 | } | |
91 | ||
92 | /* return true if we need to recompute hashes (in case hash table was resized) */ | |
93 | static bool nf_conntrack_double_lock(struct net *net, unsigned int h1, | |
94 | unsigned int h2, unsigned int sequence) | |
95 | { | |
96 | h1 %= CONNTRACK_LOCKS; | |
97 | h2 %= CONNTRACK_LOCKS; | |
98 | if (h1 <= h2) { | |
b16c2919 | 99 | nf_conntrack_lock(&nf_conntrack_locks[h1]); |
93bb0ceb JDB |
100 | if (h1 != h2) |
101 | spin_lock_nested(&nf_conntrack_locks[h2], | |
102 | SINGLE_DEPTH_NESTING); | |
103 | } else { | |
b16c2919 | 104 | nf_conntrack_lock(&nf_conntrack_locks[h2]); |
93bb0ceb JDB |
105 | spin_lock_nested(&nf_conntrack_locks[h1], |
106 | SINGLE_DEPTH_NESTING); | |
107 | } | |
108 | if (read_seqcount_retry(&net->ct.generation, sequence)) { | |
109 | nf_conntrack_double_unlock(h1, h2); | |
110 | return true; | |
111 | } | |
112 | return false; | |
113 | } | |
114 | ||
115 | static void nf_conntrack_all_lock(void) | |
116 | { | |
117 | int i; | |
118 | ||
b16c2919 SL |
119 | spin_lock(&nf_conntrack_locks_all_lock); |
120 | nf_conntrack_locks_all = true; | |
121 | ||
122 | for (i = 0; i < CONNTRACK_LOCKS; i++) { | |
e39365be | 123 | spin_unlock_wait(&nf_conntrack_locks[i]); |
b16c2919 | 124 | } |
93bb0ceb JDB |
125 | } |
126 | ||
127 | static void nf_conntrack_all_unlock(void) | |
128 | { | |
b16c2919 SL |
129 | nf_conntrack_locks_all = false; |
130 | spin_unlock(&nf_conntrack_locks_all_lock); | |
93bb0ceb JDB |
131 | } |
132 | ||
e2b7606c | 133 | unsigned int nf_conntrack_htable_size __read_mostly; |
13b18339 PM |
134 | EXPORT_SYMBOL_GPL(nf_conntrack_htable_size); |
135 | ||
e478075c | 136 | unsigned int nf_conntrack_max __read_mostly; |
a999e683 | 137 | EXPORT_SYMBOL_GPL(nf_conntrack_max); |
13b18339 | 138 | |
b3c5163f ED |
139 | DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked); |
140 | EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked); | |
13b18339 | 141 | |
f682cefa | 142 | unsigned int nf_conntrack_hash_rnd __read_mostly; |
4d4e61c6 | 143 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_rnd); |
9fb9cbb1 | 144 | |
deedb590 | 145 | static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple) |
9fb9cbb1 | 146 | { |
0794935e | 147 | unsigned int n; |
0794935e PM |
148 | |
149 | /* The direction must be ignored, so we hash everything up to the | |
150 | * destination ports (which is a multiple of 4) and treat the last | |
151 | * three bytes manually. | |
152 | */ | |
153 | n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32); | |
deedb590 | 154 | return jhash2((u32 *)tuple, n, nf_conntrack_hash_rnd ^ |
99f07e91 CG |
155 | (((__force __u16)tuple->dst.u.all << 16) | |
156 | tuple->dst.protonum)); | |
157 | } | |
158 | ||
159 | static u32 __hash_bucket(u32 hash, unsigned int size) | |
160 | { | |
8fc54f68 | 161 | return reciprocal_scale(hash, size); |
99f07e91 CG |
162 | } |
163 | ||
164 | static u32 hash_bucket(u32 hash, const struct net *net) | |
165 | { | |
166 | return __hash_bucket(hash, net->ct.htable_size); | |
167 | } | |
0794935e | 168 | |
99f07e91 | 169 | static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple, |
308ac914 | 170 | unsigned int size) |
99f07e91 | 171 | { |
deedb590 | 172 | return __hash_bucket(hash_conntrack_raw(tuple), size); |
9fb9cbb1 YK |
173 | } |
174 | ||
308ac914 | 175 | static inline u_int32_t hash_conntrack(const struct net *net, |
d696c7bd | 176 | const struct nf_conntrack_tuple *tuple) |
9fb9cbb1 | 177 | { |
deedb590 | 178 | return __hash_conntrack(tuple, net->ct.htable_size); |
9fb9cbb1 YK |
179 | } |
180 | ||
5f2b4c90 | 181 | bool |
9fb9cbb1 YK |
182 | nf_ct_get_tuple(const struct sk_buff *skb, |
183 | unsigned int nhoff, | |
184 | unsigned int dataoff, | |
185 | u_int16_t l3num, | |
186 | u_int8_t protonum, | |
a31f1adc | 187 | struct net *net, |
9fb9cbb1 YK |
188 | struct nf_conntrack_tuple *tuple, |
189 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 190 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 | 191 | { |
443a70d5 | 192 | memset(tuple, 0, sizeof(*tuple)); |
9fb9cbb1 YK |
193 | |
194 | tuple->src.l3num = l3num; | |
195 | if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0) | |
5f2b4c90 | 196 | return false; |
9fb9cbb1 YK |
197 | |
198 | tuple->dst.protonum = protonum; | |
199 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
200 | ||
a31f1adc | 201 | return l4proto->pkt_to_tuple(skb, dataoff, net, tuple); |
9fb9cbb1 | 202 | } |
13b18339 | 203 | EXPORT_SYMBOL_GPL(nf_ct_get_tuple); |
9fb9cbb1 | 204 | |
5f2b4c90 | 205 | bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff, |
a31f1adc EB |
206 | u_int16_t l3num, |
207 | struct net *net, struct nf_conntrack_tuple *tuple) | |
e2a3123f YK |
208 | { |
209 | struct nf_conntrack_l3proto *l3proto; | |
210 | struct nf_conntrack_l4proto *l4proto; | |
211 | unsigned int protoff; | |
212 | u_int8_t protonum; | |
213 | int ret; | |
214 | ||
215 | rcu_read_lock(); | |
216 | ||
217 | l3proto = __nf_ct_l3proto_find(l3num); | |
218 | ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum); | |
219 | if (ret != NF_ACCEPT) { | |
220 | rcu_read_unlock(); | |
5f2b4c90 | 221 | return false; |
e2a3123f YK |
222 | } |
223 | ||
224 | l4proto = __nf_ct_l4proto_find(l3num, protonum); | |
225 | ||
a31f1adc | 226 | ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple, |
e2a3123f YK |
227 | l3proto, l4proto); |
228 | ||
229 | rcu_read_unlock(); | |
230 | return ret; | |
231 | } | |
232 | EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr); | |
233 | ||
5f2b4c90 | 234 | bool |
9fb9cbb1 YK |
235 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, |
236 | const struct nf_conntrack_tuple *orig, | |
237 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 238 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 | 239 | { |
443a70d5 | 240 | memset(inverse, 0, sizeof(*inverse)); |
9fb9cbb1 YK |
241 | |
242 | inverse->src.l3num = orig->src.l3num; | |
243 | if (l3proto->invert_tuple(inverse, orig) == 0) | |
5f2b4c90 | 244 | return false; |
9fb9cbb1 YK |
245 | |
246 | inverse->dst.dir = !orig->dst.dir; | |
247 | ||
248 | inverse->dst.protonum = orig->dst.protonum; | |
605dcad6 | 249 | return l4proto->invert_tuple(inverse, orig); |
9fb9cbb1 | 250 | } |
13b18339 | 251 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuple); |
9fb9cbb1 | 252 | |
9fb9cbb1 YK |
253 | static void |
254 | clean_from_lists(struct nf_conn *ct) | |
255 | { | |
0d53778e | 256 | pr_debug("clean_from_lists(%p)\n", ct); |
ea781f19 ED |
257 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); |
258 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode); | |
9fb9cbb1 YK |
259 | |
260 | /* Destroy all pending expectations */ | |
c1d10adb | 261 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
262 | } |
263 | ||
b7779d06 JDB |
264 | /* must be called with local_bh_disable */ |
265 | static void nf_ct_add_to_dying_list(struct nf_conn *ct) | |
266 | { | |
267 | struct ct_pcpu *pcpu; | |
268 | ||
269 | /* add this conntrack to the (per cpu) dying list */ | |
270 | ct->cpu = smp_processor_id(); | |
271 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
272 | ||
273 | spin_lock(&pcpu->lock); | |
274 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, | |
275 | &pcpu->dying); | |
276 | spin_unlock(&pcpu->lock); | |
277 | } | |
278 | ||
279 | /* must be called with local_bh_disable */ | |
280 | static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct) | |
281 | { | |
282 | struct ct_pcpu *pcpu; | |
283 | ||
284 | /* add this conntrack to the (per cpu) unconfirmed list */ | |
285 | ct->cpu = smp_processor_id(); | |
286 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
287 | ||
288 | spin_lock(&pcpu->lock); | |
289 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, | |
290 | &pcpu->unconfirmed); | |
291 | spin_unlock(&pcpu->lock); | |
292 | } | |
293 | ||
294 | /* must be called with local_bh_disable */ | |
295 | static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct) | |
296 | { | |
297 | struct ct_pcpu *pcpu; | |
298 | ||
299 | /* We overload first tuple to link into unconfirmed or dying list.*/ | |
300 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
301 | ||
302 | spin_lock(&pcpu->lock); | |
303 | BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode)); | |
304 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); | |
305 | spin_unlock(&pcpu->lock); | |
306 | } | |
307 | ||
0838aa7f | 308 | /* Released via destroy_conntrack() */ |
308ac914 DB |
309 | struct nf_conn *nf_ct_tmpl_alloc(struct net *net, |
310 | const struct nf_conntrack_zone *zone, | |
311 | gfp_t flags) | |
0838aa7f PNA |
312 | { |
313 | struct nf_conn *tmpl; | |
314 | ||
f58e5aa7 | 315 | tmpl = kzalloc(sizeof(*tmpl), flags); |
0838aa7f PNA |
316 | if (tmpl == NULL) |
317 | return NULL; | |
318 | ||
319 | tmpl->status = IPS_TEMPLATE; | |
320 | write_pnet(&tmpl->ct_net, net); | |
321 | ||
5e8018fc DB |
322 | if (nf_ct_zone_add(tmpl, flags, zone) < 0) |
323 | goto out_free; | |
324 | ||
0838aa7f PNA |
325 | atomic_set(&tmpl->ct_general.use, 0); |
326 | ||
327 | return tmpl; | |
0838aa7f PNA |
328 | out_free: |
329 | kfree(tmpl); | |
330 | return NULL; | |
0838aa7f PNA |
331 | } |
332 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc); | |
333 | ||
9cf94eab | 334 | void nf_ct_tmpl_free(struct nf_conn *tmpl) |
0838aa7f PNA |
335 | { |
336 | nf_ct_ext_destroy(tmpl); | |
337 | nf_ct_ext_free(tmpl); | |
338 | kfree(tmpl); | |
339 | } | |
9cf94eab | 340 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_free); |
0838aa7f | 341 | |
9fb9cbb1 YK |
342 | static void |
343 | destroy_conntrack(struct nf_conntrack *nfct) | |
344 | { | |
345 | struct nf_conn *ct = (struct nf_conn *)nfct; | |
0d55af87 | 346 | struct net *net = nf_ct_net(ct); |
605dcad6 | 347 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 | 348 | |
0d53778e | 349 | pr_debug("destroy_conntrack(%p)\n", ct); |
9fb9cbb1 YK |
350 | NF_CT_ASSERT(atomic_read(&nfct->use) == 0); |
351 | NF_CT_ASSERT(!timer_pending(&ct->timeout)); | |
352 | ||
0838aa7f PNA |
353 | if (unlikely(nf_ct_is_template(ct))) { |
354 | nf_ct_tmpl_free(ct); | |
355 | return; | |
356 | } | |
923f4902 | 357 | rcu_read_lock(); |
5e8fbe2a | 358 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
605dcad6 MJ |
359 | if (l4proto && l4proto->destroy) |
360 | l4proto->destroy(ct); | |
9fb9cbb1 | 361 | |
982d9a9c | 362 | rcu_read_unlock(); |
9fb9cbb1 | 363 | |
ca7433df | 364 | local_bh_disable(); |
9fb9cbb1 YK |
365 | /* Expectations will have been removed in clean_from_lists, |
366 | * except TFTP can create an expectation on the first packet, | |
367 | * before connection is in the list, so we need to clean here, | |
ca7433df JDB |
368 | * too. |
369 | */ | |
c1d10adb | 370 | nf_ct_remove_expectations(ct); |
9fb9cbb1 | 371 | |
b7779d06 | 372 | nf_ct_del_from_dying_or_unconfirmed_list(ct); |
9fb9cbb1 | 373 | |
0d55af87 | 374 | NF_CT_STAT_INC(net, delete); |
ca7433df | 375 | local_bh_enable(); |
9fb9cbb1 YK |
376 | |
377 | if (ct->master) | |
378 | nf_ct_put(ct->master); | |
379 | ||
0d53778e | 380 | pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct); |
9fb9cbb1 YK |
381 | nf_conntrack_free(ct); |
382 | } | |
383 | ||
02982c27 | 384 | static void nf_ct_delete_from_lists(struct nf_conn *ct) |
9fb9cbb1 | 385 | { |
0d55af87 | 386 | struct net *net = nf_ct_net(ct); |
93bb0ceb | 387 | unsigned int hash, reply_hash; |
93bb0ceb | 388 | unsigned int sequence; |
9fb9cbb1 | 389 | |
9858a3ae | 390 | nf_ct_helper_destroy(ct); |
93bb0ceb JDB |
391 | |
392 | local_bh_disable(); | |
393 | do { | |
394 | sequence = read_seqcount_begin(&net->ct.generation); | |
deedb590 | 395 | hash = hash_conntrack(net, |
93bb0ceb | 396 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
deedb590 | 397 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
398 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
399 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
400 | ||
9fb9cbb1 | 401 | clean_from_lists(ct); |
93bb0ceb JDB |
402 | nf_conntrack_double_unlock(hash, reply_hash); |
403 | ||
b7779d06 | 404 | nf_ct_add_to_dying_list(ct); |
93bb0ceb JDB |
405 | |
406 | NF_CT_STAT_INC(net, delete_list); | |
407 | local_bh_enable(); | |
dd7669a9 | 408 | } |
dd7669a9 | 409 | |
02982c27 | 410 | bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report) |
dd7669a9 | 411 | { |
a992ca2a PNA |
412 | struct nf_conn_tstamp *tstamp; |
413 | ||
414 | tstamp = nf_conn_tstamp_find(ct); | |
415 | if (tstamp && tstamp->stop == 0) | |
d2de875c | 416 | tstamp->stop = ktime_get_real_ns(); |
dd7669a9 | 417 | |
9500507c FW |
418 | if (nf_ct_is_dying(ct)) |
419 | goto delete; | |
420 | ||
421 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, | |
422 | portid, report) < 0) { | |
dd7669a9 PNA |
423 | /* destroy event was not delivered */ |
424 | nf_ct_delete_from_lists(ct); | |
9500507c | 425 | nf_conntrack_ecache_delayed_work(nf_ct_net(ct)); |
02982c27 | 426 | return false; |
dd7669a9 | 427 | } |
9500507c FW |
428 | |
429 | nf_conntrack_ecache_work(nf_ct_net(ct)); | |
dd7669a9 | 430 | set_bit(IPS_DYING_BIT, &ct->status); |
9500507c | 431 | delete: |
dd7669a9 | 432 | nf_ct_delete_from_lists(ct); |
9fb9cbb1 | 433 | nf_ct_put(ct); |
02982c27 FW |
434 | return true; |
435 | } | |
436 | EXPORT_SYMBOL_GPL(nf_ct_delete); | |
437 | ||
438 | static void death_by_timeout(unsigned long ul_conntrack) | |
439 | { | |
440 | nf_ct_delete((struct nf_conn *)ul_conntrack, 0, 0); | |
9fb9cbb1 YK |
441 | } |
442 | ||
c6825c09 AV |
443 | static inline bool |
444 | nf_ct_key_equal(struct nf_conntrack_tuple_hash *h, | |
308ac914 DB |
445 | const struct nf_conntrack_tuple *tuple, |
446 | const struct nf_conntrack_zone *zone) | |
c6825c09 AV |
447 | { |
448 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
449 | ||
450 | /* A conntrack can be recreated with the equal tuple, | |
451 | * so we need to check that the conntrack is confirmed | |
452 | */ | |
453 | return nf_ct_tuple_equal(tuple, &h->tuple) && | |
deedb590 | 454 | nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) && |
308ac914 | 455 | nf_ct_is_confirmed(ct); |
c6825c09 AV |
456 | } |
457 | ||
ea781f19 ED |
458 | /* |
459 | * Warning : | |
460 | * - Caller must take a reference on returned object | |
461 | * and recheck nf_ct_tuple_equal(tuple, &h->tuple) | |
ea781f19 | 462 | */ |
99f07e91 | 463 | static struct nf_conntrack_tuple_hash * |
308ac914 | 464 | ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 | 465 | const struct nf_conntrack_tuple *tuple, u32 hash) |
9fb9cbb1 YK |
466 | { |
467 | struct nf_conntrack_tuple_hash *h; | |
ea781f19 | 468 | struct hlist_nulls_node *n; |
99f07e91 | 469 | unsigned int bucket = hash_bucket(hash, net); |
9fb9cbb1 | 470 | |
4e29e9ec PM |
471 | /* Disable BHs the entire time since we normally need to disable them |
472 | * at least once for the stats anyway. | |
473 | */ | |
474 | local_bh_disable(); | |
ea781f19 | 475 | begin: |
99f07e91 | 476 | hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[bucket], hnnode) { |
c6825c09 | 477 | if (nf_ct_key_equal(h, tuple, zone)) { |
0d55af87 | 478 | NF_CT_STAT_INC(net, found); |
4e29e9ec | 479 | local_bh_enable(); |
9fb9cbb1 YK |
480 | return h; |
481 | } | |
0d55af87 | 482 | NF_CT_STAT_INC(net, searched); |
9fb9cbb1 | 483 | } |
ea781f19 ED |
484 | /* |
485 | * if the nulls value we got at the end of this lookup is | |
486 | * not the expected one, we must restart lookup. | |
487 | * We probably met an item that was moved to another chain. | |
488 | */ | |
99f07e91 | 489 | if (get_nulls_value(n) != bucket) { |
af740b2c | 490 | NF_CT_STAT_INC(net, search_restart); |
ea781f19 | 491 | goto begin; |
af740b2c | 492 | } |
4e29e9ec | 493 | local_bh_enable(); |
9fb9cbb1 YK |
494 | |
495 | return NULL; | |
496 | } | |
99f07e91 | 497 | |
9fb9cbb1 | 498 | /* Find a connection corresponding to a tuple. */ |
99f07e91 | 499 | static struct nf_conntrack_tuple_hash * |
308ac914 | 500 | __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 | 501 | const struct nf_conntrack_tuple *tuple, u32 hash) |
9fb9cbb1 YK |
502 | { |
503 | struct nf_conntrack_tuple_hash *h; | |
76507f69 | 504 | struct nf_conn *ct; |
9fb9cbb1 | 505 | |
76507f69 | 506 | rcu_read_lock(); |
ea781f19 | 507 | begin: |
99f07e91 | 508 | h = ____nf_conntrack_find(net, zone, tuple, hash); |
76507f69 PM |
509 | if (h) { |
510 | ct = nf_ct_tuplehash_to_ctrack(h); | |
8d8890b7 PM |
511 | if (unlikely(nf_ct_is_dying(ct) || |
512 | !atomic_inc_not_zero(&ct->ct_general.use))) | |
76507f69 | 513 | h = NULL; |
ea781f19 | 514 | else { |
c6825c09 | 515 | if (unlikely(!nf_ct_key_equal(h, tuple, zone))) { |
ea781f19 ED |
516 | nf_ct_put(ct); |
517 | goto begin; | |
518 | } | |
519 | } | |
76507f69 PM |
520 | } |
521 | rcu_read_unlock(); | |
9fb9cbb1 YK |
522 | |
523 | return h; | |
524 | } | |
99f07e91 CG |
525 | |
526 | struct nf_conntrack_tuple_hash * | |
308ac914 | 527 | nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 CG |
528 | const struct nf_conntrack_tuple *tuple) |
529 | { | |
530 | return __nf_conntrack_find_get(net, zone, tuple, | |
deedb590 | 531 | hash_conntrack_raw(tuple)); |
99f07e91 | 532 | } |
13b18339 | 533 | EXPORT_SYMBOL_GPL(nf_conntrack_find_get); |
9fb9cbb1 | 534 | |
c1d10adb PNA |
535 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
536 | unsigned int hash, | |
b476b72a | 537 | unsigned int reply_hash) |
c1d10adb | 538 | { |
400dad39 AD |
539 | struct net *net = nf_ct_net(ct); |
540 | ||
ea781f19 | 541 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, |
400dad39 | 542 | &net->ct.hash[hash]); |
ea781f19 | 543 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode, |
b476b72a | 544 | &net->ct.hash[reply_hash]); |
c1d10adb PNA |
545 | } |
546 | ||
7d367e06 JK |
547 | int |
548 | nf_conntrack_hash_check_insert(struct nf_conn *ct) | |
c1d10adb | 549 | { |
308ac914 | 550 | const struct nf_conntrack_zone *zone; |
d696c7bd | 551 | struct net *net = nf_ct_net(ct); |
b476b72a | 552 | unsigned int hash, reply_hash; |
7d367e06 JK |
553 | struct nf_conntrack_tuple_hash *h; |
554 | struct hlist_nulls_node *n; | |
93bb0ceb | 555 | unsigned int sequence; |
c1d10adb | 556 | |
5d0aa2cc | 557 | zone = nf_ct_zone(ct); |
7d367e06 | 558 | |
93bb0ceb JDB |
559 | local_bh_disable(); |
560 | do { | |
561 | sequence = read_seqcount_begin(&net->ct.generation); | |
deedb590 | 562 | hash = hash_conntrack(net, |
93bb0ceb | 563 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
deedb590 | 564 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
565 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
566 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
7d367e06 JK |
567 | |
568 | /* See if there's one in the list already, including reverse */ | |
569 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[hash], hnnode) | |
570 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
571 | &h->tuple) && | |
deedb590 DB |
572 | nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h), zone, |
573 | NF_CT_DIRECTION(h))) | |
7d367e06 | 574 | goto out; |
b476b72a | 575 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[reply_hash], hnnode) |
7d367e06 JK |
576 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
577 | &h->tuple) && | |
deedb590 DB |
578 | nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h), zone, |
579 | NF_CT_DIRECTION(h))) | |
7d367e06 | 580 | goto out; |
c1d10adb | 581 | |
7d367e06 | 582 | add_timer(&ct->timeout); |
e53376be PNA |
583 | smp_wmb(); |
584 | /* The caller holds a reference to this object */ | |
585 | atomic_set(&ct->ct_general.use, 2); | |
b476b72a | 586 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
93bb0ceb | 587 | nf_conntrack_double_unlock(hash, reply_hash); |
7d367e06 | 588 | NF_CT_STAT_INC(net, insert); |
93bb0ceb | 589 | local_bh_enable(); |
7d367e06 JK |
590 | return 0; |
591 | ||
592 | out: | |
93bb0ceb | 593 | nf_conntrack_double_unlock(hash, reply_hash); |
7d367e06 | 594 | NF_CT_STAT_INC(net, insert_failed); |
93bb0ceb | 595 | local_bh_enable(); |
7d367e06 | 596 | return -EEXIST; |
c1d10adb | 597 | } |
7d367e06 | 598 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert); |
c1d10adb | 599 | |
9fb9cbb1 YK |
600 | /* Confirm a connection given skb; places it in hash table */ |
601 | int | |
3db05fea | 602 | __nf_conntrack_confirm(struct sk_buff *skb) |
9fb9cbb1 | 603 | { |
308ac914 | 604 | const struct nf_conntrack_zone *zone; |
b476b72a | 605 | unsigned int hash, reply_hash; |
df0933dc | 606 | struct nf_conntrack_tuple_hash *h; |
9fb9cbb1 | 607 | struct nf_conn *ct; |
df0933dc | 608 | struct nf_conn_help *help; |
a992ca2a | 609 | struct nf_conn_tstamp *tstamp; |
ea781f19 | 610 | struct hlist_nulls_node *n; |
9fb9cbb1 | 611 | enum ip_conntrack_info ctinfo; |
400dad39 | 612 | struct net *net; |
93bb0ceb | 613 | unsigned int sequence; |
9fb9cbb1 | 614 | |
3db05fea | 615 | ct = nf_ct_get(skb, &ctinfo); |
400dad39 | 616 | net = nf_ct_net(ct); |
9fb9cbb1 YK |
617 | |
618 | /* ipt_REJECT uses nf_conntrack_attach to attach related | |
619 | ICMP/TCP RST packets in other direction. Actual packet | |
620 | which created connection will be IP_CT_NEW or for an | |
621 | expected connection, IP_CT_RELATED. */ | |
622 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) | |
623 | return NF_ACCEPT; | |
624 | ||
5d0aa2cc | 625 | zone = nf_ct_zone(ct); |
93bb0ceb JDB |
626 | local_bh_disable(); |
627 | ||
628 | do { | |
629 | sequence = read_seqcount_begin(&net->ct.generation); | |
630 | /* reuse the hash saved before */ | |
631 | hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev; | |
632 | hash = hash_bucket(hash, net); | |
deedb590 | 633 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
634 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
635 | ||
636 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
9fb9cbb1 YK |
637 | |
638 | /* We're not in hash table, and we refuse to set up related | |
93bb0ceb JDB |
639 | * connections for unconfirmed conns. But packet copies and |
640 | * REJECT will give spurious warnings here. | |
641 | */ | |
9fb9cbb1 YK |
642 | /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */ |
643 | ||
25985edc | 644 | /* No external references means no one else could have |
93bb0ceb JDB |
645 | * confirmed us. |
646 | */ | |
9fb9cbb1 | 647 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
0d53778e | 648 | pr_debug("Confirming conntrack %p\n", ct); |
8ca3f5e9 PNA |
649 | /* We have to check the DYING flag after unlink to prevent |
650 | * a race against nf_ct_get_next_corpse() possibly called from | |
651 | * user context, else we insert an already 'dead' hash, blocking | |
652 | * further use of that particular connection -JM. | |
653 | */ | |
654 | nf_ct_del_from_dying_or_unconfirmed_list(ct); | |
655 | ||
656 | if (unlikely(nf_ct_is_dying(ct))) | |
657 | goto out; | |
fc350777 | 658 | |
9fb9cbb1 YK |
659 | /* See if there's one in the list already, including reverse: |
660 | NAT could have grabbed it without realizing, since we're | |
661 | not in the hash. If there is, we lost race. */ | |
ea781f19 | 662 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[hash], hnnode) |
df0933dc | 663 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
5d0aa2cc | 664 | &h->tuple) && |
deedb590 DB |
665 | nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h), zone, |
666 | NF_CT_DIRECTION(h))) | |
df0933dc | 667 | goto out; |
b476b72a | 668 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[reply_hash], hnnode) |
df0933dc | 669 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
5d0aa2cc | 670 | &h->tuple) && |
deedb590 DB |
671 | nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h), zone, |
672 | NF_CT_DIRECTION(h))) | |
df0933dc | 673 | goto out; |
9fb9cbb1 | 674 | |
df0933dc PM |
675 | /* Timer relative to confirmation time, not original |
676 | setting time, otherwise we'd get timer wrap in | |
677 | weird delay cases. */ | |
678 | ct->timeout.expires += jiffies; | |
679 | add_timer(&ct->timeout); | |
680 | atomic_inc(&ct->ct_general.use); | |
45eec341 | 681 | ct->status |= IPS_CONFIRMED; |
5c8ec910 | 682 | |
a992ca2a PNA |
683 | /* set conntrack timestamp, if enabled. */ |
684 | tstamp = nf_conn_tstamp_find(ct); | |
685 | if (tstamp) { | |
686 | if (skb->tstamp.tv64 == 0) | |
e3192690 | 687 | __net_timestamp(skb); |
a992ca2a PNA |
688 | |
689 | tstamp->start = ktime_to_ns(skb->tstamp); | |
690 | } | |
5c8ec910 PM |
691 | /* Since the lookup is lockless, hash insertion must be done after |
692 | * starting the timer and setting the CONFIRMED bit. The RCU barriers | |
693 | * guarantee that no other CPU can find the conntrack before the above | |
694 | * stores are visible. | |
695 | */ | |
b476b72a | 696 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
93bb0ceb | 697 | nf_conntrack_double_unlock(hash, reply_hash); |
0d55af87 | 698 | NF_CT_STAT_INC(net, insert); |
93bb0ceb | 699 | local_bh_enable(); |
5c8ec910 | 700 | |
df0933dc PM |
701 | help = nfct_help(ct); |
702 | if (help && help->helper) | |
a71996fc | 703 | nf_conntrack_event_cache(IPCT_HELPER, ct); |
17e6e4ea | 704 | |
df0933dc | 705 | nf_conntrack_event_cache(master_ct(ct) ? |
a71996fc | 706 | IPCT_RELATED : IPCT_NEW, ct); |
df0933dc | 707 | return NF_ACCEPT; |
9fb9cbb1 | 708 | |
df0933dc | 709 | out: |
8ca3f5e9 | 710 | nf_ct_add_to_dying_list(ct); |
93bb0ceb | 711 | nf_conntrack_double_unlock(hash, reply_hash); |
0d55af87 | 712 | NF_CT_STAT_INC(net, insert_failed); |
93bb0ceb | 713 | local_bh_enable(); |
9fb9cbb1 YK |
714 | return NF_DROP; |
715 | } | |
13b18339 | 716 | EXPORT_SYMBOL_GPL(__nf_conntrack_confirm); |
9fb9cbb1 YK |
717 | |
718 | /* Returns true if a connection correspondings to the tuple (required | |
719 | for NAT). */ | |
720 | int | |
721 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, | |
722 | const struct nf_conn *ignored_conntrack) | |
723 | { | |
400dad39 | 724 | struct net *net = nf_ct_net(ignored_conntrack); |
308ac914 | 725 | const struct nf_conntrack_zone *zone; |
9fb9cbb1 | 726 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 727 | struct hlist_nulls_node *n; |
5d0aa2cc | 728 | struct nf_conn *ct; |
308ac914 DB |
729 | unsigned int hash; |
730 | ||
731 | zone = nf_ct_zone(ignored_conntrack); | |
deedb590 | 732 | hash = hash_conntrack(net, tuple); |
9fb9cbb1 | 733 | |
4e29e9ec PM |
734 | /* Disable BHs the entire time since we need to disable them at |
735 | * least once for the stats anyway. | |
736 | */ | |
737 | rcu_read_lock_bh(); | |
ea781f19 | 738 | hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnnode) { |
5d0aa2cc PM |
739 | ct = nf_ct_tuplehash_to_ctrack(h); |
740 | if (ct != ignored_conntrack && | |
741 | nf_ct_tuple_equal(tuple, &h->tuple) && | |
deedb590 | 742 | nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h))) { |
0d55af87 | 743 | NF_CT_STAT_INC(net, found); |
4e29e9ec | 744 | rcu_read_unlock_bh(); |
ba419aff PM |
745 | return 1; |
746 | } | |
0d55af87 | 747 | NF_CT_STAT_INC(net, searched); |
ba419aff | 748 | } |
4e29e9ec | 749 | rcu_read_unlock_bh(); |
9fb9cbb1 | 750 | |
ba419aff | 751 | return 0; |
9fb9cbb1 | 752 | } |
13b18339 | 753 | EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken); |
9fb9cbb1 | 754 | |
7ae7730f PM |
755 | #define NF_CT_EVICTION_RANGE 8 |
756 | ||
9fb9cbb1 YK |
757 | /* There's a small race here where we may free a just-assured |
758 | connection. Too bad: we're in trouble anyway. */ | |
93bb0ceb | 759 | static noinline int early_drop(struct net *net, unsigned int _hash) |
9fb9cbb1 | 760 | { |
f205c5e0 | 761 | /* Use oldest entry, which is roughly LRU */ |
9fb9cbb1 | 762 | struct nf_conntrack_tuple_hash *h; |
df0933dc | 763 | struct nf_conn *ct = NULL, *tmp; |
ea781f19 | 764 | struct hlist_nulls_node *n; |
93bb0ceb | 765 | unsigned int i = 0, cnt = 0; |
9fb9cbb1 | 766 | int dropped = 0; |
93bb0ceb JDB |
767 | unsigned int hash, sequence; |
768 | spinlock_t *lockp; | |
9fb9cbb1 | 769 | |
93bb0ceb JDB |
770 | local_bh_disable(); |
771 | restart: | |
772 | sequence = read_seqcount_begin(&net->ct.generation); | |
773 | hash = hash_bucket(_hash, net); | |
774 | for (; i < net->ct.htable_size; i++) { | |
775 | lockp = &nf_conntrack_locks[hash % CONNTRACK_LOCKS]; | |
b16c2919 | 776 | nf_conntrack_lock(lockp); |
93bb0ceb JDB |
777 | if (read_seqcount_retry(&net->ct.generation, sequence)) { |
778 | spin_unlock(lockp); | |
779 | goto restart; | |
780 | } | |
ea781f19 ED |
781 | hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash], |
782 | hnnode) { | |
7ae7730f | 783 | tmp = nf_ct_tuplehash_to_ctrack(h); |
93bb0ceb JDB |
784 | if (!test_bit(IPS_ASSURED_BIT, &tmp->status) && |
785 | !nf_ct_is_dying(tmp) && | |
786 | atomic_inc_not_zero(&tmp->ct_general.use)) { | |
7ae7730f | 787 | ct = tmp; |
93bb0ceb JDB |
788 | break; |
789 | } | |
7ae7730f PM |
790 | cnt++; |
791 | } | |
76507f69 | 792 | |
93bb0ceb JDB |
793 | hash = (hash + 1) % net->ct.htable_size; |
794 | spin_unlock(lockp); | |
5ae27aa2 | 795 | |
93bb0ceb | 796 | if (ct || cnt >= NF_CT_EVICTION_RANGE) |
7ae7730f | 797 | break; |
5ae27aa2 | 798 | |
9fb9cbb1 | 799 | } |
93bb0ceb | 800 | local_bh_enable(); |
9fb9cbb1 YK |
801 | |
802 | if (!ct) | |
803 | return dropped; | |
804 | ||
805 | if (del_timer(&ct->timeout)) { | |
02982c27 | 806 | if (nf_ct_delete(ct, 0, 0)) { |
74138511 PNA |
807 | dropped = 1; |
808 | NF_CT_STAT_INC_ATOMIC(net, early_drop); | |
809 | } | |
9fb9cbb1 YK |
810 | } |
811 | nf_ct_put(ct); | |
812 | return dropped; | |
813 | } | |
814 | ||
f682cefa CG |
815 | void init_nf_conntrack_hash_rnd(void) |
816 | { | |
817 | unsigned int rand; | |
818 | ||
819 | /* | |
820 | * Why not initialize nf_conntrack_rnd in a "init()" function ? | |
821 | * Because there isn't enough entropy when system initializing, | |
822 | * and we initialize it as late as possible. | |
823 | */ | |
824 | do { | |
825 | get_random_bytes(&rand, sizeof(rand)); | |
826 | } while (!rand); | |
827 | cmpxchg(&nf_conntrack_hash_rnd, 0, rand); | |
828 | } | |
829 | ||
99f07e91 | 830 | static struct nf_conn * |
308ac914 DB |
831 | __nf_conntrack_alloc(struct net *net, |
832 | const struct nf_conntrack_zone *zone, | |
99f07e91 CG |
833 | const struct nf_conntrack_tuple *orig, |
834 | const struct nf_conntrack_tuple *repl, | |
835 | gfp_t gfp, u32 hash) | |
9fb9cbb1 | 836 | { |
cd7fcbf1 | 837 | struct nf_conn *ct; |
9fb9cbb1 | 838 | |
b2390969 | 839 | if (unlikely(!nf_conntrack_hash_rnd)) { |
f682cefa | 840 | init_nf_conntrack_hash_rnd(); |
99f07e91 | 841 | /* recompute the hash as nf_conntrack_hash_rnd is initialized */ |
deedb590 | 842 | hash = hash_conntrack_raw(orig); |
9fb9cbb1 YK |
843 | } |
844 | ||
5251e2d2 | 845 | /* We don't want any race condition at early drop stage */ |
49ac8713 | 846 | atomic_inc(&net->ct.count); |
5251e2d2 | 847 | |
76eb9460 | 848 | if (nf_conntrack_max && |
49ac8713 | 849 | unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) { |
93bb0ceb | 850 | if (!early_drop(net, hash)) { |
49ac8713 | 851 | atomic_dec(&net->ct.count); |
e87cc472 | 852 | net_warn_ratelimited("nf_conntrack: table full, dropping packet\n"); |
9fb9cbb1 YK |
853 | return ERR_PTR(-ENOMEM); |
854 | } | |
855 | } | |
856 | ||
941297f4 ED |
857 | /* |
858 | * Do not use kmem_cache_zalloc(), as this cache uses | |
859 | * SLAB_DESTROY_BY_RCU. | |
860 | */ | |
5b3501fa | 861 | ct = kmem_cache_alloc(net->ct.nf_conntrack_cachep, gfp); |
5e8018fc DB |
862 | if (ct == NULL) |
863 | goto out; | |
864 | ||
440f0d58 | 865 | spin_lock_init(&ct->lock); |
c88130bc | 866 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
941297f4 | 867 | ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL; |
c88130bc | 868 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; |
99f07e91 CG |
869 | /* save hash for reusing when confirming */ |
870 | *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash; | |
c41884ce | 871 | ct->status = 0; |
9fb9cbb1 | 872 | /* Don't set timer yet: wait for confirmation */ |
c88130bc | 873 | setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct); |
c2d9ba9b | 874 | write_pnet(&ct->ct_net, net); |
c41884ce FW |
875 | memset(&ct->__nfct_init_offset[0], 0, |
876 | offsetof(struct nf_conn, proto) - | |
877 | offsetof(struct nf_conn, __nfct_init_offset[0])); | |
5e8018fc DB |
878 | |
879 | if (zone && nf_ct_zone_add(ct, GFP_ATOMIC, zone) < 0) | |
880 | goto out_free; | |
881 | ||
e53376be PNA |
882 | /* Because we use RCU lookups, we set ct_general.use to zero before |
883 | * this is inserted in any list. | |
941297f4 | 884 | */ |
e53376be | 885 | atomic_set(&ct->ct_general.use, 0); |
c88130bc | 886 | return ct; |
5d0aa2cc PM |
887 | out_free: |
888 | kmem_cache_free(net->ct.nf_conntrack_cachep, ct); | |
5e8018fc DB |
889 | out: |
890 | atomic_dec(&net->ct.count); | |
5d0aa2cc | 891 | return ERR_PTR(-ENOMEM); |
9fb9cbb1 | 892 | } |
99f07e91 | 893 | |
308ac914 DB |
894 | struct nf_conn *nf_conntrack_alloc(struct net *net, |
895 | const struct nf_conntrack_zone *zone, | |
99f07e91 CG |
896 | const struct nf_conntrack_tuple *orig, |
897 | const struct nf_conntrack_tuple *repl, | |
898 | gfp_t gfp) | |
899 | { | |
900 | return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0); | |
901 | } | |
13b18339 | 902 | EXPORT_SYMBOL_GPL(nf_conntrack_alloc); |
9fb9cbb1 | 903 | |
c88130bc | 904 | void nf_conntrack_free(struct nf_conn *ct) |
76507f69 | 905 | { |
1d45209d ED |
906 | struct net *net = nf_ct_net(ct); |
907 | ||
e53376be PNA |
908 | /* A freed object has refcnt == 0, that's |
909 | * the golden rule for SLAB_DESTROY_BY_RCU | |
910 | */ | |
911 | NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0); | |
912 | ||
ceeff754 | 913 | nf_ct_ext_destroy(ct); |
ea781f19 | 914 | nf_ct_ext_free(ct); |
5b3501fa | 915 | kmem_cache_free(net->ct.nf_conntrack_cachep, ct); |
4e857c58 | 916 | smp_mb__before_atomic(); |
0c3c6c00 | 917 | atomic_dec(&net->ct.count); |
76507f69 | 918 | } |
13b18339 | 919 | EXPORT_SYMBOL_GPL(nf_conntrack_free); |
9fb9cbb1 | 920 | |
c539f017 | 921 | |
9fb9cbb1 YK |
922 | /* Allocate a new conntrack: we return -ENOMEM if classification |
923 | failed due to stress. Otherwise it really is unclassifiable. */ | |
924 | static struct nf_conntrack_tuple_hash * | |
b2a15a60 | 925 | init_conntrack(struct net *net, struct nf_conn *tmpl, |
5a1fb391 | 926 | const struct nf_conntrack_tuple *tuple, |
9fb9cbb1 | 927 | struct nf_conntrack_l3proto *l3proto, |
605dcad6 | 928 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 | 929 | struct sk_buff *skb, |
60b5f8f7 | 930 | unsigned int dataoff, u32 hash) |
9fb9cbb1 | 931 | { |
c88130bc | 932 | struct nf_conn *ct; |
3c158f7f | 933 | struct nf_conn_help *help; |
9fb9cbb1 | 934 | struct nf_conntrack_tuple repl_tuple; |
b2a15a60 | 935 | struct nf_conntrack_ecache *ecache; |
ca7433df | 936 | struct nf_conntrack_expect *exp = NULL; |
308ac914 | 937 | const struct nf_conntrack_zone *zone; |
60b5f8f7 | 938 | struct nf_conn_timeout *timeout_ext; |
5e8018fc | 939 | struct nf_conntrack_zone tmp; |
60b5f8f7 | 940 | unsigned int *timeouts; |
9fb9cbb1 | 941 | |
605dcad6 | 942 | if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) { |
0d53778e | 943 | pr_debug("Can't invert tuple.\n"); |
9fb9cbb1 YK |
944 | return NULL; |
945 | } | |
946 | ||
5e8018fc | 947 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
99f07e91 CG |
948 | ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC, |
949 | hash); | |
0a9ee813 | 950 | if (IS_ERR(ct)) |
c88130bc | 951 | return (struct nf_conntrack_tuple_hash *)ct; |
9fb9cbb1 | 952 | |
48b1de4c PM |
953 | if (tmpl && nfct_synproxy(tmpl)) { |
954 | nfct_seqadj_ext_add(ct); | |
955 | nfct_synproxy_ext_add(ct); | |
956 | } | |
957 | ||
60b5f8f7 | 958 | timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL; |
ae2d708e PNA |
959 | if (timeout_ext) { |
960 | timeouts = nf_ct_timeout_data(timeout_ext); | |
961 | if (unlikely(!timeouts)) | |
962 | timeouts = l4proto->get_timeouts(net); | |
963 | } else { | |
60b5f8f7 | 964 | timeouts = l4proto->get_timeouts(net); |
ae2d708e | 965 | } |
60b5f8f7 | 966 | |
2c8503f5 | 967 | if (!l4proto->new(ct, skb, dataoff, timeouts)) { |
c88130bc | 968 | nf_conntrack_free(ct); |
0d53778e | 969 | pr_debug("init conntrack: can't track with proto module\n"); |
9fb9cbb1 YK |
970 | return NULL; |
971 | } | |
972 | ||
60b5f8f7 | 973 | if (timeout_ext) |
ae2d708e PNA |
974 | nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout), |
975 | GFP_ATOMIC); | |
60b5f8f7 | 976 | |
58401572 | 977 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 978 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
c539f017 | 979 | nf_ct_labels_ext_add(ct); |
b2a15a60 PM |
980 | |
981 | ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL; | |
982 | nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0, | |
983 | ecache ? ecache->expmask : 0, | |
984 | GFP_ATOMIC); | |
58401572 | 985 | |
ca7433df JDB |
986 | local_bh_disable(); |
987 | if (net->ct.expect_count) { | |
988 | spin_lock(&nf_conntrack_expect_lock); | |
989 | exp = nf_ct_find_expectation(net, zone, tuple); | |
990 | if (exp) { | |
991 | pr_debug("conntrack: expectation arrives ct=%p exp=%p\n", | |
992 | ct, exp); | |
993 | /* Welcome, Mr. Bond. We've been expecting you... */ | |
994 | __set_bit(IPS_EXPECTED_BIT, &ct->status); | |
995 | /* exp->master safe, refcnt bumped in nf_ct_find_expectation */ | |
996 | ct->master = exp->master; | |
997 | if (exp->helper) { | |
998 | help = nf_ct_helper_ext_add(ct, exp->helper, | |
999 | GFP_ATOMIC); | |
1000 | if (help) | |
1001 | rcu_assign_pointer(help->helper, exp->helper); | |
1002 | } | |
ceceae1b | 1003 | |
9fb9cbb1 | 1004 | #ifdef CONFIG_NF_CONNTRACK_MARK |
ca7433df | 1005 | ct->mark = exp->master->mark; |
7c9728c3 JM |
1006 | #endif |
1007 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
ca7433df | 1008 | ct->secmark = exp->master->secmark; |
9fb9cbb1 | 1009 | #endif |
ca7433df JDB |
1010 | NF_CT_STAT_INC(net, expect_new); |
1011 | } | |
1012 | spin_unlock(&nf_conntrack_expect_lock); | |
1013 | } | |
1014 | if (!exp) { | |
b2a15a60 | 1015 | __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC); |
0d55af87 | 1016 | NF_CT_STAT_INC(net, new); |
22e7410b | 1017 | } |
9fb9cbb1 | 1018 | |
e53376be PNA |
1019 | /* Now it is inserted into the unconfirmed list, bump refcount */ |
1020 | nf_conntrack_get(&ct->ct_general); | |
b7779d06 | 1021 | nf_ct_add_to_unconfirmed_list(ct); |
9fb9cbb1 | 1022 | |
ca7433df | 1023 | local_bh_enable(); |
9fb9cbb1 YK |
1024 | |
1025 | if (exp) { | |
1026 | if (exp->expectfn) | |
c88130bc | 1027 | exp->expectfn(ct, exp); |
6823645d | 1028 | nf_ct_expect_put(exp); |
9fb9cbb1 YK |
1029 | } |
1030 | ||
c88130bc | 1031 | return &ct->tuplehash[IP_CT_DIR_ORIGINAL]; |
9fb9cbb1 YK |
1032 | } |
1033 | ||
1034 | /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ | |
1035 | static inline struct nf_conn * | |
b2a15a60 | 1036 | resolve_normal_ct(struct net *net, struct nf_conn *tmpl, |
a702a65f | 1037 | struct sk_buff *skb, |
9fb9cbb1 YK |
1038 | unsigned int dataoff, |
1039 | u_int16_t l3num, | |
1040 | u_int8_t protonum, | |
1041 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 1042 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 | 1043 | int *set_reply, |
60b5f8f7 | 1044 | enum ip_conntrack_info *ctinfo) |
9fb9cbb1 | 1045 | { |
308ac914 | 1046 | const struct nf_conntrack_zone *zone; |
9fb9cbb1 YK |
1047 | struct nf_conntrack_tuple tuple; |
1048 | struct nf_conntrack_tuple_hash *h; | |
5e8018fc | 1049 | struct nf_conntrack_zone tmp; |
9fb9cbb1 | 1050 | struct nf_conn *ct; |
99f07e91 | 1051 | u32 hash; |
9fb9cbb1 | 1052 | |
bbe735e4 | 1053 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), |
a31f1adc | 1054 | dataoff, l3num, protonum, net, &tuple, l3proto, |
605dcad6 | 1055 | l4proto)) { |
0d53778e | 1056 | pr_debug("resolve_normal_ct: Can't get tuple\n"); |
9fb9cbb1 YK |
1057 | return NULL; |
1058 | } | |
1059 | ||
1060 | /* look for tuple match */ | |
5e8018fc | 1061 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
deedb590 | 1062 | hash = hash_conntrack_raw(&tuple); |
99f07e91 | 1063 | h = __nf_conntrack_find_get(net, zone, &tuple, hash); |
9fb9cbb1 | 1064 | if (!h) { |
b2a15a60 | 1065 | h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto, |
60b5f8f7 | 1066 | skb, dataoff, hash); |
9fb9cbb1 YK |
1067 | if (!h) |
1068 | return NULL; | |
1069 | if (IS_ERR(h)) | |
1070 | return (void *)h; | |
1071 | } | |
1072 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1073 | ||
1074 | /* It exists; we have (non-exclusive) reference. */ | |
1075 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { | |
fb048833 | 1076 | *ctinfo = IP_CT_ESTABLISHED_REPLY; |
9fb9cbb1 YK |
1077 | /* Please set reply bit if this packet OK */ |
1078 | *set_reply = 1; | |
1079 | } else { | |
1080 | /* Once we've had two way comms, always ESTABLISHED. */ | |
1081 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { | |
0d53778e | 1082 | pr_debug("nf_conntrack_in: normal packet for %p\n", ct); |
9fb9cbb1 YK |
1083 | *ctinfo = IP_CT_ESTABLISHED; |
1084 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { | |
0d53778e PM |
1085 | pr_debug("nf_conntrack_in: related packet for %p\n", |
1086 | ct); | |
9fb9cbb1 YK |
1087 | *ctinfo = IP_CT_RELATED; |
1088 | } else { | |
0d53778e | 1089 | pr_debug("nf_conntrack_in: new packet for %p\n", ct); |
9fb9cbb1 YK |
1090 | *ctinfo = IP_CT_NEW; |
1091 | } | |
1092 | *set_reply = 0; | |
1093 | } | |
1094 | skb->nfct = &ct->ct_general; | |
1095 | skb->nfctinfo = *ctinfo; | |
1096 | return ct; | |
1097 | } | |
1098 | ||
1099 | unsigned int | |
a702a65f AD |
1100 | nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum, |
1101 | struct sk_buff *skb) | |
9fb9cbb1 | 1102 | { |
b2a15a60 | 1103 | struct nf_conn *ct, *tmpl = NULL; |
9fb9cbb1 YK |
1104 | enum ip_conntrack_info ctinfo; |
1105 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 1106 | struct nf_conntrack_l4proto *l4proto; |
2c8503f5 | 1107 | unsigned int *timeouts; |
9fb9cbb1 YK |
1108 | unsigned int dataoff; |
1109 | u_int8_t protonum; | |
1110 | int set_reply = 0; | |
1111 | int ret; | |
1112 | ||
3db05fea | 1113 | if (skb->nfct) { |
b2a15a60 PM |
1114 | /* Previously seen (loopback or untracked)? Ignore. */ |
1115 | tmpl = (struct nf_conn *)skb->nfct; | |
1116 | if (!nf_ct_is_template(tmpl)) { | |
1117 | NF_CT_STAT_INC_ATOMIC(net, ignore); | |
1118 | return NF_ACCEPT; | |
1119 | } | |
1120 | skb->nfct = NULL; | |
9fb9cbb1 YK |
1121 | } |
1122 | ||
923f4902 | 1123 | /* rcu_read_lock()ed by nf_hook_slow */ |
76108cea | 1124 | l3proto = __nf_ct_l3proto_find(pf); |
3db05fea | 1125 | ret = l3proto->get_l4proto(skb, skb_network_offset(skb), |
ffc30690 YK |
1126 | &dataoff, &protonum); |
1127 | if (ret <= 0) { | |
25985edc | 1128 | pr_debug("not prepared to track yet or error occurred\n"); |
0d55af87 AD |
1129 | NF_CT_STAT_INC_ATOMIC(net, error); |
1130 | NF_CT_STAT_INC_ATOMIC(net, invalid); | |
b2a15a60 PM |
1131 | ret = -ret; |
1132 | goto out; | |
9fb9cbb1 YK |
1133 | } |
1134 | ||
76108cea | 1135 | l4proto = __nf_ct_l4proto_find(pf, protonum); |
9fb9cbb1 YK |
1136 | |
1137 | /* It may be an special packet, error, unclean... | |
1138 | * inverse of the return code tells to the netfilter | |
1139 | * core what to do with the packet. */ | |
74c51a14 | 1140 | if (l4proto->error != NULL) { |
8fea97ec PM |
1141 | ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo, |
1142 | pf, hooknum); | |
74c51a14 | 1143 | if (ret <= 0) { |
0d55af87 AD |
1144 | NF_CT_STAT_INC_ATOMIC(net, error); |
1145 | NF_CT_STAT_INC_ATOMIC(net, invalid); | |
b2a15a60 PM |
1146 | ret = -ret; |
1147 | goto out; | |
74c51a14 | 1148 | } |
88ed01d1 PNA |
1149 | /* ICMP[v6] protocol trackers may assign one conntrack. */ |
1150 | if (skb->nfct) | |
1151 | goto out; | |
9fb9cbb1 YK |
1152 | } |
1153 | ||
b2a15a60 | 1154 | ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum, |
60b5f8f7 | 1155 | l3proto, l4proto, &set_reply, &ctinfo); |
9fb9cbb1 YK |
1156 | if (!ct) { |
1157 | /* Not valid part of a connection */ | |
0d55af87 | 1158 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
b2a15a60 PM |
1159 | ret = NF_ACCEPT; |
1160 | goto out; | |
9fb9cbb1 YK |
1161 | } |
1162 | ||
1163 | if (IS_ERR(ct)) { | |
1164 | /* Too stressed to deal. */ | |
0d55af87 | 1165 | NF_CT_STAT_INC_ATOMIC(net, drop); |
b2a15a60 PM |
1166 | ret = NF_DROP; |
1167 | goto out; | |
9fb9cbb1 YK |
1168 | } |
1169 | ||
3db05fea | 1170 | NF_CT_ASSERT(skb->nfct); |
9fb9cbb1 | 1171 | |
60b5f8f7 | 1172 | /* Decide what timeout policy we want to apply to this flow. */ |
84b5ee93 | 1173 | timeouts = nf_ct_timeout_lookup(net, ct, l4proto); |
60b5f8f7 | 1174 | |
2c8503f5 | 1175 | ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts); |
ec8d5409 | 1176 | if (ret <= 0) { |
9fb9cbb1 YK |
1177 | /* Invalid: inverse of the return code tells |
1178 | * the netfilter core what to do */ | |
0d53778e | 1179 | pr_debug("nf_conntrack_in: Can't track with proto module\n"); |
3db05fea HX |
1180 | nf_conntrack_put(skb->nfct); |
1181 | skb->nfct = NULL; | |
0d55af87 | 1182 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
7d1e0459 PNA |
1183 | if (ret == -NF_DROP) |
1184 | NF_CT_STAT_INC_ATOMIC(net, drop); | |
b2a15a60 PM |
1185 | ret = -ret; |
1186 | goto out; | |
9fb9cbb1 YK |
1187 | } |
1188 | ||
1189 | if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
858b3133 | 1190 | nf_conntrack_event_cache(IPCT_REPLY, ct); |
b2a15a60 | 1191 | out: |
c3174286 PNA |
1192 | if (tmpl) { |
1193 | /* Special case: we have to repeat this hook, assign the | |
1194 | * template again to this packet. We assume that this packet | |
1195 | * has no conntrack assigned. This is used by nf_ct_tcp. */ | |
1196 | if (ret == NF_REPEAT) | |
1197 | skb->nfct = (struct nf_conntrack *)tmpl; | |
1198 | else | |
1199 | nf_ct_put(tmpl); | |
1200 | } | |
9fb9cbb1 YK |
1201 | |
1202 | return ret; | |
1203 | } | |
13b18339 | 1204 | EXPORT_SYMBOL_GPL(nf_conntrack_in); |
9fb9cbb1 | 1205 | |
5f2b4c90 JE |
1206 | bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, |
1207 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 | 1208 | { |
5f2b4c90 | 1209 | bool ret; |
923f4902 PM |
1210 | |
1211 | rcu_read_lock(); | |
1212 | ret = nf_ct_invert_tuple(inverse, orig, | |
1213 | __nf_ct_l3proto_find(orig->src.l3num), | |
1214 | __nf_ct_l4proto_find(orig->src.l3num, | |
1215 | orig->dst.protonum)); | |
1216 | rcu_read_unlock(); | |
1217 | return ret; | |
9fb9cbb1 | 1218 | } |
13b18339 | 1219 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr); |
9fb9cbb1 | 1220 | |
5b1158e9 JK |
1221 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
1222 | implicitly racy: see __nf_conntrack_confirm */ | |
1223 | void nf_conntrack_alter_reply(struct nf_conn *ct, | |
1224 | const struct nf_conntrack_tuple *newreply) | |
1225 | { | |
1226 | struct nf_conn_help *help = nfct_help(ct); | |
1227 | ||
5b1158e9 JK |
1228 | /* Should be unconfirmed, so not in hash table yet */ |
1229 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); | |
1230 | ||
0d53778e | 1231 | pr_debug("Altering reply tuple of %p to ", ct); |
3c9fba65 | 1232 | nf_ct_dump_tuple(newreply); |
5b1158e9 JK |
1233 | |
1234 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; | |
ef1a5a50 | 1235 | if (ct->master || (help && !hlist_empty(&help->expectations))) |
c52fbb41 | 1236 | return; |
ceceae1b | 1237 | |
c52fbb41 | 1238 | rcu_read_lock(); |
b2a15a60 | 1239 | __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
c52fbb41 | 1240 | rcu_read_unlock(); |
5b1158e9 | 1241 | } |
13b18339 | 1242 | EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply); |
5b1158e9 | 1243 | |
9fb9cbb1 YK |
1244 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
1245 | void __nf_ct_refresh_acct(struct nf_conn *ct, | |
1246 | enum ip_conntrack_info ctinfo, | |
1247 | const struct sk_buff *skb, | |
1248 | unsigned long extra_jiffies, | |
1249 | int do_acct) | |
1250 | { | |
9fb9cbb1 YK |
1251 | NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct); |
1252 | NF_CT_ASSERT(skb); | |
1253 | ||
997ae831 | 1254 | /* Only update if this is not a fixed timeout */ |
47d95045 PM |
1255 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) |
1256 | goto acct; | |
997ae831 | 1257 | |
9fb9cbb1 YK |
1258 | /* If not in hash table, timer will not be active yet */ |
1259 | if (!nf_ct_is_confirmed(ct)) { | |
1260 | ct->timeout.expires = extra_jiffies; | |
9fb9cbb1 | 1261 | } else { |
be00c8e4 MJ |
1262 | unsigned long newtime = jiffies + extra_jiffies; |
1263 | ||
1264 | /* Only update the timeout if the new timeout is at least | |
1265 | HZ jiffies from the old timeout. Need del_timer for race | |
1266 | avoidance (may already be dying). */ | |
65cb9fda PM |
1267 | if (newtime - ct->timeout.expires >= HZ) |
1268 | mod_timer_pending(&ct->timeout, newtime); | |
9fb9cbb1 YK |
1269 | } |
1270 | ||
47d95045 | 1271 | acct: |
9fb9cbb1 | 1272 | if (do_acct) { |
f7b13e43 | 1273 | struct nf_conn_acct *acct; |
3ffd5eeb | 1274 | |
58401572 KPO |
1275 | acct = nf_conn_acct_find(ct); |
1276 | if (acct) { | |
f7b13e43 HE |
1277 | struct nf_conn_counter *counter = acct->counter; |
1278 | ||
1279 | atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets); | |
1280 | atomic64_add(skb->len, &counter[CTINFO2DIR(ctinfo)].bytes); | |
58401572 | 1281 | } |
9fb9cbb1 | 1282 | } |
9fb9cbb1 | 1283 | } |
13b18339 | 1284 | EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct); |
9fb9cbb1 | 1285 | |
4c889498 DM |
1286 | bool __nf_ct_kill_acct(struct nf_conn *ct, |
1287 | enum ip_conntrack_info ctinfo, | |
1288 | const struct sk_buff *skb, | |
1289 | int do_acct) | |
51091764 | 1290 | { |
718d4ad9 | 1291 | if (do_acct) { |
f7b13e43 | 1292 | struct nf_conn_acct *acct; |
58401572 | 1293 | |
58401572 KPO |
1294 | acct = nf_conn_acct_find(ct); |
1295 | if (acct) { | |
f7b13e43 HE |
1296 | struct nf_conn_counter *counter = acct->counter; |
1297 | ||
1298 | atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets); | |
b3e0bfa7 | 1299 | atomic64_add(skb->len - skb_network_offset(skb), |
f7b13e43 | 1300 | &counter[CTINFO2DIR(ctinfo)].bytes); |
58401572 | 1301 | } |
718d4ad9 | 1302 | } |
58401572 | 1303 | |
4c889498 | 1304 | if (del_timer(&ct->timeout)) { |
51091764 | 1305 | ct->timeout.function((unsigned long)ct); |
4c889498 DM |
1306 | return true; |
1307 | } | |
1308 | return false; | |
51091764 | 1309 | } |
718d4ad9 | 1310 | EXPORT_SYMBOL_GPL(__nf_ct_kill_acct); |
51091764 | 1311 | |
5d0aa2cc PM |
1312 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
1313 | static struct nf_ct_ext_type nf_ct_zone_extend __read_mostly = { | |
1314 | .len = sizeof(struct nf_conntrack_zone), | |
1315 | .align = __alignof__(struct nf_conntrack_zone), | |
1316 | .id = NF_CT_EXT_ZONE, | |
1317 | }; | |
1318 | #endif | |
1319 | ||
c0cd1156 | 1320 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c1d10adb PNA |
1321 | |
1322 | #include <linux/netfilter/nfnetlink.h> | |
1323 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
57b47a53 IM |
1324 | #include <linux/mutex.h> |
1325 | ||
c1d10adb PNA |
1326 | /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be |
1327 | * in ip_conntrack_core, since we don't want the protocols to autoload | |
1328 | * or depend on ctnetlink */ | |
fdf70832 | 1329 | int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
1330 | const struct nf_conntrack_tuple *tuple) |
1331 | { | |
bae65be8 DM |
1332 | if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) || |
1333 | nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port)) | |
1334 | goto nla_put_failure; | |
c1d10adb PNA |
1335 | return 0; |
1336 | ||
df6fb868 | 1337 | nla_put_failure: |
c1d10adb PNA |
1338 | return -1; |
1339 | } | |
fdf70832 | 1340 | EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr); |
c1d10adb | 1341 | |
f73e924c PM |
1342 | const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = { |
1343 | [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 }, | |
1344 | [CTA_PROTO_DST_PORT] = { .type = NLA_U16 }, | |
c1d10adb | 1345 | }; |
f73e924c | 1346 | EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy); |
c1d10adb | 1347 | |
fdf70832 | 1348 | int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
1349 | struct nf_conntrack_tuple *t) |
1350 | { | |
df6fb868 | 1351 | if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT]) |
c1d10adb PNA |
1352 | return -EINVAL; |
1353 | ||
77236b6e PM |
1354 | t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]); |
1355 | t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]); | |
c1d10adb PNA |
1356 | |
1357 | return 0; | |
1358 | } | |
fdf70832 | 1359 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple); |
5c0de29d HE |
1360 | |
1361 | int nf_ct_port_nlattr_tuple_size(void) | |
1362 | { | |
1363 | return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1); | |
1364 | } | |
1365 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size); | |
c1d10adb PNA |
1366 | #endif |
1367 | ||
9fb9cbb1 | 1368 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
312a0c16 | 1369 | static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb) |
9fb9cbb1 YK |
1370 | { |
1371 | struct nf_conn *ct; | |
1372 | enum ip_conntrack_info ctinfo; | |
1373 | ||
1374 | /* This ICMP is in reverse direction to the packet which caused it */ | |
1375 | ct = nf_ct_get(skb, &ctinfo); | |
1376 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) | |
fb048833 | 1377 | ctinfo = IP_CT_RELATED_REPLY; |
9fb9cbb1 YK |
1378 | else |
1379 | ctinfo = IP_CT_RELATED; | |
1380 | ||
1381 | /* Attach to new skbuff, and increment count */ | |
1382 | nskb->nfct = &ct->ct_general; | |
1383 | nskb->nfctinfo = ctinfo; | |
1384 | nf_conntrack_get(nskb->nfct); | |
1385 | } | |
1386 | ||
9fb9cbb1 | 1387 | /* Bring out ya dead! */ |
df0933dc | 1388 | static struct nf_conn * |
400dad39 | 1389 | get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data), |
9fb9cbb1 YK |
1390 | void *data, unsigned int *bucket) |
1391 | { | |
df0933dc PM |
1392 | struct nf_conntrack_tuple_hash *h; |
1393 | struct nf_conn *ct; | |
ea781f19 | 1394 | struct hlist_nulls_node *n; |
b7779d06 | 1395 | int cpu; |
93bb0ceb | 1396 | spinlock_t *lockp; |
9fb9cbb1 | 1397 | |
d696c7bd | 1398 | for (; *bucket < net->ct.htable_size; (*bucket)++) { |
93bb0ceb JDB |
1399 | lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS]; |
1400 | local_bh_disable(); | |
b16c2919 | 1401 | nf_conntrack_lock(lockp); |
93bb0ceb JDB |
1402 | if (*bucket < net->ct.htable_size) { |
1403 | hlist_nulls_for_each_entry(h, n, &net->ct.hash[*bucket], hnnode) { | |
1404 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) | |
1405 | continue; | |
1406 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1407 | if (iter(ct, data)) | |
1408 | goto found; | |
1409 | } | |
df0933dc | 1410 | } |
93bb0ceb JDB |
1411 | spin_unlock(lockp); |
1412 | local_bh_enable(); | |
d93c6258 | 1413 | cond_resched(); |
601e68e1 | 1414 | } |
b7779d06 JDB |
1415 | |
1416 | for_each_possible_cpu(cpu) { | |
1417 | struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
1418 | ||
1419 | spin_lock_bh(&pcpu->lock); | |
1420 | hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) { | |
1421 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1422 | if (iter(ct, data)) | |
1423 | set_bit(IPS_DYING_BIT, &ct->status); | |
1424 | } | |
1425 | spin_unlock_bh(&pcpu->lock); | |
d93c6258 | 1426 | cond_resched(); |
b7779d06 | 1427 | } |
df0933dc PM |
1428 | return NULL; |
1429 | found: | |
c073e3fa | 1430 | atomic_inc(&ct->ct_general.use); |
93bb0ceb JDB |
1431 | spin_unlock(lockp); |
1432 | local_bh_enable(); | |
df0933dc | 1433 | return ct; |
9fb9cbb1 YK |
1434 | } |
1435 | ||
400dad39 AD |
1436 | void nf_ct_iterate_cleanup(struct net *net, |
1437 | int (*iter)(struct nf_conn *i, void *data), | |
c655bc68 | 1438 | void *data, u32 portid, int report) |
9fb9cbb1 | 1439 | { |
df0933dc | 1440 | struct nf_conn *ct; |
9fb9cbb1 YK |
1441 | unsigned int bucket = 0; |
1442 | ||
d93c6258 FW |
1443 | might_sleep(); |
1444 | ||
400dad39 | 1445 | while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) { |
9fb9cbb1 YK |
1446 | /* Time to push up daises... */ |
1447 | if (del_timer(&ct->timeout)) | |
c655bc68 | 1448 | nf_ct_delete(ct, portid, report); |
02982c27 | 1449 | |
9fb9cbb1 YK |
1450 | /* ... else the timer will get him soon. */ |
1451 | ||
1452 | nf_ct_put(ct); | |
d93c6258 | 1453 | cond_resched(); |
9fb9cbb1 YK |
1454 | } |
1455 | } | |
13b18339 | 1456 | EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup); |
9fb9cbb1 | 1457 | |
274d383b PNA |
1458 | static int kill_all(struct nf_conn *i, void *data) |
1459 | { | |
1460 | return 1; | |
1461 | } | |
1462 | ||
d862a662 | 1463 | void nf_ct_free_hashtable(void *hash, unsigned int size) |
9fb9cbb1 | 1464 | { |
d862a662 | 1465 | if (is_vmalloc_addr(hash)) |
9fb9cbb1 YK |
1466 | vfree(hash); |
1467 | else | |
601e68e1 | 1468 | free_pages((unsigned long)hash, |
f205c5e0 | 1469 | get_order(sizeof(struct hlist_head) * size)); |
9fb9cbb1 | 1470 | } |
ac565e5f | 1471 | EXPORT_SYMBOL_GPL(nf_ct_free_hashtable); |
9fb9cbb1 | 1472 | |
b3c5163f ED |
1473 | static int untrack_refs(void) |
1474 | { | |
1475 | int cnt = 0, cpu; | |
1476 | ||
1477 | for_each_possible_cpu(cpu) { | |
1478 | struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu); | |
1479 | ||
1480 | cnt += atomic_read(&ct->ct_general.use) - 1; | |
1481 | } | |
1482 | return cnt; | |
1483 | } | |
1484 | ||
f94161c1 | 1485 | void nf_conntrack_cleanup_start(void) |
9fb9cbb1 | 1486 | { |
f94161c1 G |
1487 | RCU_INIT_POINTER(ip_ct_attach, NULL); |
1488 | } | |
1489 | ||
1490 | void nf_conntrack_cleanup_end(void) | |
1491 | { | |
1492 | RCU_INIT_POINTER(nf_ct_destroy, NULL); | |
b3c5163f | 1493 | while (untrack_refs() > 0) |
9edd7ca0 PM |
1494 | schedule(); |
1495 | ||
5d0aa2cc PM |
1496 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
1497 | nf_ct_extend_unregister(&nf_ct_zone_extend); | |
1498 | #endif | |
04d87001 | 1499 | nf_conntrack_proto_fini(); |
41d73ec0 | 1500 | nf_conntrack_seqadj_fini(); |
5f69b8f5 | 1501 | nf_conntrack_labels_fini(); |
5e615b22 | 1502 | nf_conntrack_helper_fini(); |
8684094c | 1503 | nf_conntrack_timeout_fini(); |
3fe0f943 | 1504 | nf_conntrack_ecache_fini(); |
73f4001a | 1505 | nf_conntrack_tstamp_fini(); |
b7ff3a1f | 1506 | nf_conntrack_acct_fini(); |
83b4dbe1 | 1507 | nf_conntrack_expect_fini(); |
08f6547d | 1508 | } |
9fb9cbb1 | 1509 | |
f94161c1 G |
1510 | /* |
1511 | * Mishearing the voices in his head, our hero wonders how he's | |
1512 | * supposed to kill the mall. | |
1513 | */ | |
1514 | void nf_conntrack_cleanup_net(struct net *net) | |
08f6547d | 1515 | { |
dece40e8 VD |
1516 | LIST_HEAD(single); |
1517 | ||
1518 | list_add(&net->exit_list, &single); | |
1519 | nf_conntrack_cleanup_net_list(&single); | |
1520 | } | |
1521 | ||
1522 | void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list) | |
1523 | { | |
1524 | int busy; | |
1525 | struct net *net; | |
1526 | ||
f94161c1 G |
1527 | /* |
1528 | * This makes sure all current packets have passed through | |
1529 | * netfilter framework. Roll on, two-stage module | |
1530 | * delete... | |
1531 | */ | |
1532 | synchronize_net(); | |
dece40e8 VD |
1533 | i_see_dead_people: |
1534 | busy = 0; | |
1535 | list_for_each_entry(net, net_exit_list, exit_list) { | |
c655bc68 | 1536 | nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0); |
dece40e8 VD |
1537 | if (atomic_read(&net->ct.count) != 0) |
1538 | busy = 1; | |
1539 | } | |
1540 | if (busy) { | |
9fb9cbb1 YK |
1541 | schedule(); |
1542 | goto i_see_dead_people; | |
1543 | } | |
1544 | ||
dece40e8 VD |
1545 | list_for_each_entry(net, net_exit_list, exit_list) { |
1546 | nf_ct_free_hashtable(net->ct.hash, net->ct.htable_size); | |
1547 | nf_conntrack_proto_pernet_fini(net); | |
1548 | nf_conntrack_helper_pernet_fini(net); | |
1549 | nf_conntrack_ecache_pernet_fini(net); | |
1550 | nf_conntrack_tstamp_pernet_fini(net); | |
1551 | nf_conntrack_acct_pernet_fini(net); | |
1552 | nf_conntrack_expect_pernet_fini(net); | |
1553 | kmem_cache_destroy(net->ct.nf_conntrack_cachep); | |
1554 | kfree(net->ct.slabname); | |
1555 | free_percpu(net->ct.stat); | |
b7779d06 | 1556 | free_percpu(net->ct.pcpu_lists); |
dece40e8 | 1557 | } |
08f6547d AD |
1558 | } |
1559 | ||
d862a662 | 1560 | void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) |
9fb9cbb1 | 1561 | { |
ea781f19 ED |
1562 | struct hlist_nulls_head *hash; |
1563 | unsigned int nr_slots, i; | |
1564 | size_t sz; | |
9fb9cbb1 | 1565 | |
ea781f19 ED |
1566 | BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head)); |
1567 | nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head)); | |
1568 | sz = nr_slots * sizeof(struct hlist_nulls_head); | |
1569 | hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, | |
1570 | get_order(sz)); | |
f0ad4621 | 1571 | if (!hash) |
966567b7 | 1572 | hash = vzalloc(sz); |
9fb9cbb1 | 1573 | |
ea781f19 ED |
1574 | if (hash && nulls) |
1575 | for (i = 0; i < nr_slots; i++) | |
1576 | INIT_HLIST_NULLS_HEAD(&hash[i], i); | |
9fb9cbb1 YK |
1577 | |
1578 | return hash; | |
1579 | } | |
ac565e5f | 1580 | EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable); |
9fb9cbb1 | 1581 | |
fae718dd | 1582 | int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) |
9fb9cbb1 | 1583 | { |
4b5511eb | 1584 | int i, bucket, rc; |
96eb24d7 | 1585 | unsigned int hashsize, old_size; |
ea781f19 | 1586 | struct hlist_nulls_head *hash, *old_hash; |
9fb9cbb1 | 1587 | struct nf_conntrack_tuple_hash *h; |
5d0aa2cc | 1588 | struct nf_conn *ct; |
9fb9cbb1 | 1589 | |
d696c7bd PM |
1590 | if (current->nsproxy->net_ns != &init_net) |
1591 | return -EOPNOTSUPP; | |
1592 | ||
9fb9cbb1 YK |
1593 | /* On boot, we can set this without any fancy locking. */ |
1594 | if (!nf_conntrack_htable_size) | |
1595 | return param_set_uint(val, kp); | |
1596 | ||
4b5511eb AP |
1597 | rc = kstrtouint(val, 0, &hashsize); |
1598 | if (rc) | |
1599 | return rc; | |
9fb9cbb1 YK |
1600 | if (!hashsize) |
1601 | return -EINVAL; | |
1602 | ||
d862a662 | 1603 | hash = nf_ct_alloc_hashtable(&hashsize, 1); |
9fb9cbb1 YK |
1604 | if (!hash) |
1605 | return -ENOMEM; | |
1606 | ||
93bb0ceb JDB |
1607 | local_bh_disable(); |
1608 | nf_conntrack_all_lock(); | |
1609 | write_seqcount_begin(&init_net.ct.generation); | |
1610 | ||
76507f69 PM |
1611 | /* Lookups in the old hash might happen in parallel, which means we |
1612 | * might get false negatives during connection lookup. New connections | |
1613 | * created because of a false negative won't make it into the hash | |
93bb0ceb | 1614 | * though since that required taking the locks. |
76507f69 | 1615 | */ |
93bb0ceb | 1616 | |
d696c7bd | 1617 | for (i = 0; i < init_net.ct.htable_size; i++) { |
ea781f19 ED |
1618 | while (!hlist_nulls_empty(&init_net.ct.hash[i])) { |
1619 | h = hlist_nulls_entry(init_net.ct.hash[i].first, | |
1620 | struct nf_conntrack_tuple_hash, hnnode); | |
5d0aa2cc | 1621 | ct = nf_ct_tuplehash_to_ctrack(h); |
ea781f19 | 1622 | hlist_nulls_del_rcu(&h->hnnode); |
deedb590 | 1623 | bucket = __hash_conntrack(&h->tuple, hashsize); |
ea781f19 | 1624 | hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]); |
9fb9cbb1 YK |
1625 | } |
1626 | } | |
d696c7bd | 1627 | old_size = init_net.ct.htable_size; |
400dad39 | 1628 | old_hash = init_net.ct.hash; |
9fb9cbb1 | 1629 | |
d696c7bd | 1630 | init_net.ct.htable_size = nf_conntrack_htable_size = hashsize; |
400dad39 | 1631 | init_net.ct.hash = hash; |
93bb0ceb JDB |
1632 | |
1633 | write_seqcount_end(&init_net.ct.generation); | |
1634 | nf_conntrack_all_unlock(); | |
1635 | local_bh_enable(); | |
9fb9cbb1 | 1636 | |
d862a662 | 1637 | nf_ct_free_hashtable(old_hash, old_size); |
9fb9cbb1 YK |
1638 | return 0; |
1639 | } | |
fae718dd | 1640 | EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize); |
9fb9cbb1 | 1641 | |
fae718dd | 1642 | module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint, |
9fb9cbb1 YK |
1643 | &nf_conntrack_htable_size, 0600); |
1644 | ||
5bfddbd4 ED |
1645 | void nf_ct_untracked_status_or(unsigned long bits) |
1646 | { | |
b3c5163f ED |
1647 | int cpu; |
1648 | ||
1649 | for_each_possible_cpu(cpu) | |
1650 | per_cpu(nf_conntrack_untracked, cpu).status |= bits; | |
5bfddbd4 ED |
1651 | } |
1652 | EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or); | |
1653 | ||
f94161c1 | 1654 | int nf_conntrack_init_start(void) |
9fb9cbb1 | 1655 | { |
f205c5e0 | 1656 | int max_factor = 8; |
93bb0ceb JDB |
1657 | int i, ret, cpu; |
1658 | ||
d5d20912 | 1659 | for (i = 0; i < CONNTRACK_LOCKS; i++) |
93bb0ceb | 1660 | spin_lock_init(&nf_conntrack_locks[i]); |
9fb9cbb1 | 1661 | |
9fb9cbb1 | 1662 | if (!nf_conntrack_htable_size) { |
88eab472 ML |
1663 | /* Idea from tcp.c: use 1/16384 of memory. |
1664 | * On i386: 32MB machine has 512 buckets. | |
1665 | * >= 1GB machines have 16384 buckets. | |
1666 | * >= 4GB machines have 65536 buckets. | |
1667 | */ | |
9fb9cbb1 | 1668 | nf_conntrack_htable_size |
4481374c | 1669 | = (((totalram_pages << PAGE_SHIFT) / 16384) |
f205c5e0 | 1670 | / sizeof(struct hlist_head)); |
88eab472 ML |
1671 | if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE))) |
1672 | nf_conntrack_htable_size = 65536; | |
1673 | else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE)) | |
f205c5e0 PM |
1674 | nf_conntrack_htable_size = 16384; |
1675 | if (nf_conntrack_htable_size < 32) | |
1676 | nf_conntrack_htable_size = 32; | |
1677 | ||
1678 | /* Use a max. factor of four by default to get the same max as | |
1679 | * with the old struct list_heads. When a table size is given | |
1680 | * we use the old value of 8 to avoid reducing the max. | |
1681 | * entries. */ | |
1682 | max_factor = 4; | |
9fb9cbb1 | 1683 | } |
f205c5e0 | 1684 | nf_conntrack_max = max_factor * nf_conntrack_htable_size; |
8e5105a0 | 1685 | |
654d0fbd | 1686 | printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n", |
8e5105a0 PM |
1687 | NF_CONNTRACK_VERSION, nf_conntrack_htable_size, |
1688 | nf_conntrack_max); | |
83b4dbe1 G |
1689 | |
1690 | ret = nf_conntrack_expect_init(); | |
1691 | if (ret < 0) | |
1692 | goto err_expect; | |
1693 | ||
b7ff3a1f G |
1694 | ret = nf_conntrack_acct_init(); |
1695 | if (ret < 0) | |
1696 | goto err_acct; | |
1697 | ||
73f4001a G |
1698 | ret = nf_conntrack_tstamp_init(); |
1699 | if (ret < 0) | |
1700 | goto err_tstamp; | |
1701 | ||
3fe0f943 G |
1702 | ret = nf_conntrack_ecache_init(); |
1703 | if (ret < 0) | |
1704 | goto err_ecache; | |
1705 | ||
8684094c G |
1706 | ret = nf_conntrack_timeout_init(); |
1707 | if (ret < 0) | |
1708 | goto err_timeout; | |
1709 | ||
5e615b22 G |
1710 | ret = nf_conntrack_helper_init(); |
1711 | if (ret < 0) | |
1712 | goto err_helper; | |
1713 | ||
5f69b8f5 G |
1714 | ret = nf_conntrack_labels_init(); |
1715 | if (ret < 0) | |
1716 | goto err_labels; | |
1717 | ||
41d73ec0 PM |
1718 | ret = nf_conntrack_seqadj_init(); |
1719 | if (ret < 0) | |
1720 | goto err_seqadj; | |
1721 | ||
5d0aa2cc PM |
1722 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
1723 | ret = nf_ct_extend_register(&nf_ct_zone_extend); | |
1724 | if (ret < 0) | |
1725 | goto err_extend; | |
1726 | #endif | |
04d87001 G |
1727 | ret = nf_conntrack_proto_init(); |
1728 | if (ret < 0) | |
1729 | goto err_proto; | |
1730 | ||
9edd7ca0 | 1731 | /* Set up fake conntrack: to never be deleted, not in any hashes */ |
b3c5163f ED |
1732 | for_each_possible_cpu(cpu) { |
1733 | struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu); | |
b3c5163f ED |
1734 | write_pnet(&ct->ct_net, &init_net); |
1735 | atomic_set(&ct->ct_general.use, 1); | |
1736 | } | |
9edd7ca0 | 1737 | /* - and look it like as a confirmed connection */ |
5bfddbd4 | 1738 | nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED); |
08f6547d AD |
1739 | return 0; |
1740 | ||
04d87001 | 1741 | err_proto: |
5d0aa2cc | 1742 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
04d87001 | 1743 | nf_ct_extend_unregister(&nf_ct_zone_extend); |
5d0aa2cc | 1744 | err_extend: |
a9006892 | 1745 | #endif |
41d73ec0 PM |
1746 | nf_conntrack_seqadj_fini(); |
1747 | err_seqadj: | |
04d87001 | 1748 | nf_conntrack_labels_fini(); |
5f69b8f5 G |
1749 | err_labels: |
1750 | nf_conntrack_helper_fini(); | |
5e615b22 G |
1751 | err_helper: |
1752 | nf_conntrack_timeout_fini(); | |
8684094c G |
1753 | err_timeout: |
1754 | nf_conntrack_ecache_fini(); | |
3fe0f943 G |
1755 | err_ecache: |
1756 | nf_conntrack_tstamp_fini(); | |
73f4001a G |
1757 | err_tstamp: |
1758 | nf_conntrack_acct_fini(); | |
b7ff3a1f G |
1759 | err_acct: |
1760 | nf_conntrack_expect_fini(); | |
83b4dbe1 | 1761 | err_expect: |
08f6547d AD |
1762 | return ret; |
1763 | } | |
1764 | ||
f94161c1 G |
1765 | void nf_conntrack_init_end(void) |
1766 | { | |
1767 | /* For use by REJECT target */ | |
1768 | RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach); | |
1769 | RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack); | |
f94161c1 G |
1770 | } |
1771 | ||
8cc20198 ED |
1772 | /* |
1773 | * We need to use special "null" values, not used in hash table | |
1774 | */ | |
1775 | #define UNCONFIRMED_NULLS_VAL ((1<<30)+0) | |
1776 | #define DYING_NULLS_VAL ((1<<30)+1) | |
252b3e8c | 1777 | #define TEMPLATE_NULLS_VAL ((1<<30)+2) |
8cc20198 | 1778 | |
f94161c1 | 1779 | int nf_conntrack_init_net(struct net *net) |
08f6547d | 1780 | { |
31b0b385 | 1781 | static atomic64_t unique_id; |
b7779d06 JDB |
1782 | int ret = -ENOMEM; |
1783 | int cpu; | |
ceceae1b | 1784 | |
08f6547d | 1785 | atomic_set(&net->ct.count, 0); |
ee214d54 | 1786 | seqcount_init(&net->ct.generation); |
b7779d06 JDB |
1787 | |
1788 | net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu); | |
1789 | if (!net->ct.pcpu_lists) | |
08f6547d | 1790 | goto err_stat; |
b7779d06 JDB |
1791 | |
1792 | for_each_possible_cpu(cpu) { | |
1793 | struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
1794 | ||
1795 | spin_lock_init(&pcpu->lock); | |
1796 | INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL); | |
1797 | INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL); | |
08f6547d | 1798 | } |
5b3501fa | 1799 | |
b7779d06 JDB |
1800 | net->ct.stat = alloc_percpu(struct ip_conntrack_stat); |
1801 | if (!net->ct.stat) | |
1802 | goto err_pcpu_lists; | |
1803 | ||
31b0b385 LT |
1804 | net->ct.slabname = kasprintf(GFP_KERNEL, "nf_conntrack_%llu", |
1805 | (u64)atomic64_inc_return(&unique_id)); | |
b7779d06 | 1806 | if (!net->ct.slabname) |
5b3501fa | 1807 | goto err_slabname; |
5b3501fa ED |
1808 | |
1809 | net->ct.nf_conntrack_cachep = kmem_cache_create(net->ct.slabname, | |
1810 | sizeof(struct nf_conn), 0, | |
1811 | SLAB_DESTROY_BY_RCU, NULL); | |
1812 | if (!net->ct.nf_conntrack_cachep) { | |
1813 | printk(KERN_ERR "Unable to create nf_conn slab cache\n"); | |
5b3501fa ED |
1814 | goto err_cache; |
1815 | } | |
d696c7bd PM |
1816 | |
1817 | net->ct.htable_size = nf_conntrack_htable_size; | |
d862a662 | 1818 | net->ct.hash = nf_ct_alloc_hashtable(&net->ct.htable_size, 1); |
08f6547d | 1819 | if (!net->ct.hash) { |
08f6547d AD |
1820 | printk(KERN_ERR "Unable to create nf_conntrack_hash\n"); |
1821 | goto err_hash; | |
1822 | } | |
83b4dbe1 | 1823 | ret = nf_conntrack_expect_pernet_init(net); |
08f6547d AD |
1824 | if (ret < 0) |
1825 | goto err_expect; | |
b7ff3a1f | 1826 | ret = nf_conntrack_acct_pernet_init(net); |
58401572 | 1827 | if (ret < 0) |
08f6547d | 1828 | goto err_acct; |
73f4001a | 1829 | ret = nf_conntrack_tstamp_pernet_init(net); |
a992ca2a PNA |
1830 | if (ret < 0) |
1831 | goto err_tstamp; | |
3fe0f943 | 1832 | ret = nf_conntrack_ecache_pernet_init(net); |
a0891aa6 PNA |
1833 | if (ret < 0) |
1834 | goto err_ecache; | |
5e615b22 | 1835 | ret = nf_conntrack_helper_pernet_init(net); |
a9006892 EL |
1836 | if (ret < 0) |
1837 | goto err_helper; | |
04d87001 | 1838 | ret = nf_conntrack_proto_pernet_init(net); |
f94161c1 G |
1839 | if (ret < 0) |
1840 | goto err_proto; | |
08f6547d | 1841 | return 0; |
c539f017 | 1842 | |
f94161c1 | 1843 | err_proto: |
5e615b22 | 1844 | nf_conntrack_helper_pernet_fini(net); |
a9006892 | 1845 | err_helper: |
3fe0f943 | 1846 | nf_conntrack_ecache_pernet_fini(net); |
a0891aa6 | 1847 | err_ecache: |
73f4001a | 1848 | nf_conntrack_tstamp_pernet_fini(net); |
a992ca2a | 1849 | err_tstamp: |
b7ff3a1f | 1850 | nf_conntrack_acct_pernet_fini(net); |
08f6547d | 1851 | err_acct: |
83b4dbe1 | 1852 | nf_conntrack_expect_pernet_fini(net); |
08f6547d | 1853 | err_expect: |
d862a662 | 1854 | nf_ct_free_hashtable(net->ct.hash, net->ct.htable_size); |
6058fa6b | 1855 | err_hash: |
5b3501fa ED |
1856 | kmem_cache_destroy(net->ct.nf_conntrack_cachep); |
1857 | err_cache: | |
1858 | kfree(net->ct.slabname); | |
1859 | err_slabname: | |
0d55af87 | 1860 | free_percpu(net->ct.stat); |
b7779d06 JDB |
1861 | err_pcpu_lists: |
1862 | free_percpu(net->ct.pcpu_lists); | |
0d55af87 | 1863 | err_stat: |
08f6547d AD |
1864 | return ret; |
1865 | } |