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