]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9fb9cbb1 YK |
2 | /* Connection state tracking for netfilter. This is separated from, |
3 | but required by, the NAT layer; it can also be used by an iptables | |
4 | extension. */ | |
5 | ||
6 | /* (C) 1999-2001 Paul `Rusty' Russell | |
dc808fe2 | 7 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
9fb9cbb1 | 8 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
f229f6ce | 9 | * (C) 2005-2012 Patrick McHardy <kaber@trash.net> |
9fb9cbb1 YK |
10 | */ |
11 | ||
ccd63c20 WJ |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
9fb9cbb1 YK |
14 | #include <linux/types.h> |
15 | #include <linux/netfilter.h> | |
16 | #include <linux/module.h> | |
d43c36dc | 17 | #include <linux/sched.h> |
9fb9cbb1 YK |
18 | #include <linux/skbuff.h> |
19 | #include <linux/proc_fs.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/stddef.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/random.h> | |
24 | #include <linux/jhash.h> | |
3c791076 | 25 | #include <linux/siphash.h> |
9fb9cbb1 YK |
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 | 37 | #include <net/netfilter/nf_conntrack.h> |
605dcad6 | 38 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 39 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 | 40 | #include <net/netfilter/nf_conntrack_helper.h> |
41d73ec0 | 41 | #include <net/netfilter/nf_conntrack_seqadj.h> |
9fb9cbb1 | 42 | #include <net/netfilter/nf_conntrack_core.h> |
ecfab2c9 | 43 | #include <net/netfilter/nf_conntrack_extend.h> |
58401572 | 44 | #include <net/netfilter/nf_conntrack_acct.h> |
a0891aa6 | 45 | #include <net/netfilter/nf_conntrack_ecache.h> |
5d0aa2cc | 46 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 47 | #include <net/netfilter/nf_conntrack_timestamp.h> |
dd705072 | 48 | #include <net/netfilter/nf_conntrack_timeout.h> |
c539f017 | 49 | #include <net/netfilter/nf_conntrack_labels.h> |
48b1de4c | 50 | #include <net/netfilter/nf_conntrack_synproxy.h> |
e6a7d3c0 | 51 | #include <net/netfilter/nf_nat.h> |
49376368 | 52 | #include <net/netfilter/nf_nat_helper.h> |
1b8c8a9f | 53 | #include <net/netns/hash.h> |
6816d931 | 54 | #include <net/ip.h> |
9fb9cbb1 | 55 | |
e2a75007 FW |
56 | #include "nf_internals.h" |
57 | ||
93bb0ceb JDB |
58 | __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS]; |
59 | EXPORT_SYMBOL_GPL(nf_conntrack_locks); | |
9fb9cbb1 | 60 | |
ca7433df JDB |
61 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock); |
62 | EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock); | |
63 | ||
56d52d48 FW |
64 | struct hlist_nulls_head *nf_conntrack_hash __read_mostly; |
65 | EXPORT_SYMBOL_GPL(nf_conntrack_hash); | |
66 | ||
b87a2f91 FW |
67 | struct conntrack_gc_work { |
68 | struct delayed_work dwork; | |
69 | u32 last_bucket; | |
70 | bool exiting; | |
c6dd940b | 71 | bool early_drop; |
e0df8cae | 72 | long next_gc_run; |
b87a2f91 FW |
73 | }; |
74 | ||
0c5366b3 | 75 | static __read_mostly struct kmem_cache *nf_conntrack_cachep; |
44b63b0a | 76 | static DEFINE_SPINLOCK(nf_conntrack_locks_all_lock); |
b16c2919 SL |
77 | static __read_mostly bool nf_conntrack_locks_all; |
78 | ||
e0df8cae | 79 | /* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */ |
e5072053 FW |
80 | #define GC_MAX_BUCKETS_DIV 128u |
81 | /* upper bound of full table scan */ | |
82 | #define GC_MAX_SCAN_JIFFIES (16u * HZ) | |
83 | /* desired ratio of entries found to be expired */ | |
84 | #define GC_EVICT_RATIO 50u | |
b87a2f91 FW |
85 | |
86 | static struct conntrack_gc_work conntrack_gc_work; | |
87 | ||
b16c2919 SL |
88 | void nf_conntrack_lock(spinlock_t *lock) __acquires(lock) |
89 | { | |
3ef0c7a7 | 90 | /* 1) Acquire the lock */ |
b16c2919 | 91 | spin_lock(lock); |
b316ff78 | 92 | |
3ef0c7a7 MS |
93 | /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics |
94 | * It pairs with the smp_store_release() in nf_conntrack_all_unlock() | |
95 | */ | |
96 | if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false)) | |
97 | return; | |
98 | ||
99 | /* fast path failed, unlock */ | |
100 | spin_unlock(lock); | |
101 | ||
102 | /* Slow path 1) get global lock */ | |
103 | spin_lock(&nf_conntrack_locks_all_lock); | |
104 | ||
105 | /* Slow path 2) get the lock we want */ | |
106 | spin_lock(lock); | |
107 | ||
108 | /* Slow path 3) release the global lock */ | |
109 | spin_unlock(&nf_conntrack_locks_all_lock); | |
b16c2919 SL |
110 | } |
111 | EXPORT_SYMBOL_GPL(nf_conntrack_lock); | |
112 | ||
93bb0ceb JDB |
113 | static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2) |
114 | { | |
115 | h1 %= CONNTRACK_LOCKS; | |
116 | h2 %= CONNTRACK_LOCKS; | |
117 | spin_unlock(&nf_conntrack_locks[h1]); | |
118 | if (h1 != h2) | |
119 | spin_unlock(&nf_conntrack_locks[h2]); | |
120 | } | |
121 | ||
122 | /* return true if we need to recompute hashes (in case hash table was resized) */ | |
123 | static bool nf_conntrack_double_lock(struct net *net, unsigned int h1, | |
124 | unsigned int h2, unsigned int sequence) | |
125 | { | |
126 | h1 %= CONNTRACK_LOCKS; | |
127 | h2 %= CONNTRACK_LOCKS; | |
128 | if (h1 <= h2) { | |
b16c2919 | 129 | nf_conntrack_lock(&nf_conntrack_locks[h1]); |
93bb0ceb JDB |
130 | if (h1 != h2) |
131 | spin_lock_nested(&nf_conntrack_locks[h2], | |
132 | SINGLE_DEPTH_NESTING); | |
133 | } else { | |
b16c2919 | 134 | nf_conntrack_lock(&nf_conntrack_locks[h2]); |
93bb0ceb JDB |
135 | spin_lock_nested(&nf_conntrack_locks[h1], |
136 | SINGLE_DEPTH_NESTING); | |
137 | } | |
a3efd812 | 138 | if (read_seqcount_retry(&nf_conntrack_generation, sequence)) { |
93bb0ceb JDB |
139 | nf_conntrack_double_unlock(h1, h2); |
140 | return true; | |
141 | } | |
142 | return false; | |
143 | } | |
144 | ||
145 | static void nf_conntrack_all_lock(void) | |
6b36d482 | 146 | __acquires(&nf_conntrack_locks_all_lock) |
93bb0ceb JDB |
147 | { |
148 | int i; | |
149 | ||
b16c2919 | 150 | spin_lock(&nf_conntrack_locks_all_lock); |
b16c2919 | 151 | |
3ef0c7a7 | 152 | nf_conntrack_locks_all = true; |
b316ff78 | 153 | |
b16c2919 | 154 | for (i = 0; i < CONNTRACK_LOCKS; i++) { |
3ef0c7a7 MS |
155 | spin_lock(&nf_conntrack_locks[i]); |
156 | ||
157 | /* This spin_unlock provides the "release" to ensure that | |
158 | * nf_conntrack_locks_all==true is visible to everyone that | |
159 | * acquired spin_lock(&nf_conntrack_locks[]). | |
160 | */ | |
161 | spin_unlock(&nf_conntrack_locks[i]); | |
b16c2919 | 162 | } |
93bb0ceb JDB |
163 | } |
164 | ||
165 | static void nf_conntrack_all_unlock(void) | |
6b36d482 | 166 | __releases(&nf_conntrack_locks_all_lock) |
93bb0ceb | 167 | { |
3ef0c7a7 | 168 | /* All prior stores must be complete before we clear |
b316ff78 PZ |
169 | * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock() |
170 | * might observe the false value but not the entire | |
3ef0c7a7 MS |
171 | * critical section. |
172 | * It pairs with the smp_load_acquire() in nf_conntrack_lock() | |
b316ff78 PZ |
173 | */ |
174 | smp_store_release(&nf_conntrack_locks_all, false); | |
b16c2919 | 175 | spin_unlock(&nf_conntrack_locks_all_lock); |
93bb0ceb JDB |
176 | } |
177 | ||
e2b7606c | 178 | unsigned int nf_conntrack_htable_size __read_mostly; |
2567c4ea PNA |
179 | EXPORT_SYMBOL_GPL(nf_conntrack_htable_size); |
180 | ||
e478075c | 181 | unsigned int nf_conntrack_max __read_mostly; |
538c5672 | 182 | EXPORT_SYMBOL_GPL(nf_conntrack_max); |
92e47ba8 | 183 | seqcount_t nf_conntrack_generation __read_mostly; |
141658fb | 184 | static unsigned int nf_conntrack_hash_rnd __read_mostly; |
9fb9cbb1 | 185 | |
1b8c8a9f FW |
186 | static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple, |
187 | const struct net *net) | |
9fb9cbb1 | 188 | { |
0794935e | 189 | unsigned int n; |
1b8c8a9f | 190 | u32 seed; |
0794935e | 191 | |
141658fb FW |
192 | get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd)); |
193 | ||
0794935e PM |
194 | /* The direction must be ignored, so we hash everything up to the |
195 | * destination ports (which is a multiple of 4) and treat the last | |
196 | * three bytes manually. | |
197 | */ | |
1b8c8a9f | 198 | seed = nf_conntrack_hash_rnd ^ net_hash_mix(net); |
0794935e | 199 | n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32); |
1b8c8a9f | 200 | return jhash2((u32 *)tuple, n, seed ^ |
99f07e91 CG |
201 | (((__force __u16)tuple->dst.u.all << 16) | |
202 | tuple->dst.protonum)); | |
203 | } | |
204 | ||
56d52d48 | 205 | static u32 scale_hash(u32 hash) |
99f07e91 | 206 | { |
56d52d48 | 207 | return reciprocal_scale(hash, nf_conntrack_htable_size); |
99f07e91 | 208 | } |
0794935e | 209 | |
1b8c8a9f FW |
210 | static u32 __hash_conntrack(const struct net *net, |
211 | const struct nf_conntrack_tuple *tuple, | |
212 | unsigned int size) | |
99f07e91 | 213 | { |
1b8c8a9f | 214 | return reciprocal_scale(hash_conntrack_raw(tuple, net), size); |
9fb9cbb1 YK |
215 | } |
216 | ||
1b8c8a9f FW |
217 | static u32 hash_conntrack(const struct net *net, |
218 | const struct nf_conntrack_tuple *tuple) | |
9fb9cbb1 | 219 | { |
56d52d48 | 220 | return scale_hash(hash_conntrack_raw(tuple, net)); |
9fb9cbb1 YK |
221 | } |
222 | ||
e2f7cc72 FW |
223 | static bool nf_ct_get_tuple_ports(const struct sk_buff *skb, |
224 | unsigned int dataoff, | |
225 | struct nf_conntrack_tuple *tuple) | |
226 | { struct { | |
227 | __be16 sport; | |
228 | __be16 dport; | |
229 | } _inet_hdr, *inet_hdr; | |
230 | ||
231 | /* Actually only need first 4 bytes to get ports. */ | |
232 | inet_hdr = skb_header_pointer(skb, dataoff, sizeof(_inet_hdr), &_inet_hdr); | |
233 | if (!inet_hdr) | |
234 | return false; | |
235 | ||
236 | tuple->src.u.udp.port = inet_hdr->sport; | |
237 | tuple->dst.u.udp.port = inet_hdr->dport; | |
238 | return true; | |
239 | } | |
240 | ||
60e3be94 | 241 | static bool |
9fb9cbb1 YK |
242 | nf_ct_get_tuple(const struct sk_buff *skb, |
243 | unsigned int nhoff, | |
244 | unsigned int dataoff, | |
245 | u_int16_t l3num, | |
246 | u_int8_t protonum, | |
a31f1adc | 247 | struct net *net, |
303e0c55 | 248 | struct nf_conntrack_tuple *tuple) |
9fb9cbb1 | 249 | { |
47a91b14 FW |
250 | unsigned int size; |
251 | const __be32 *ap; | |
252 | __be32 _addrs[8]; | |
253 | ||
443a70d5 | 254 | memset(tuple, 0, sizeof(*tuple)); |
9fb9cbb1 YK |
255 | |
256 | tuple->src.l3num = l3num; | |
47a91b14 FW |
257 | switch (l3num) { |
258 | case NFPROTO_IPV4: | |
259 | nhoff += offsetof(struct iphdr, saddr); | |
260 | size = 2 * sizeof(__be32); | |
261 | break; | |
262 | case NFPROTO_IPV6: | |
263 | nhoff += offsetof(struct ipv6hdr, saddr); | |
264 | size = sizeof(_addrs); | |
265 | break; | |
266 | default: | |
267 | return true; | |
268 | } | |
269 | ||
270 | ap = skb_header_pointer(skb, nhoff, size, _addrs); | |
271 | if (!ap) | |
5f2b4c90 | 272 | return false; |
9fb9cbb1 | 273 | |
47a91b14 FW |
274 | switch (l3num) { |
275 | case NFPROTO_IPV4: | |
276 | tuple->src.u3.ip = ap[0]; | |
277 | tuple->dst.u3.ip = ap[1]; | |
278 | break; | |
279 | case NFPROTO_IPV6: | |
280 | memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6)); | |
281 | memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6)); | |
282 | break; | |
283 | } | |
284 | ||
9fb9cbb1 YK |
285 | tuple->dst.protonum = protonum; |
286 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
287 | ||
e2e48b47 | 288 | switch (protonum) { |
81e01647 | 289 | #if IS_ENABLED(CONFIG_IPV6) |
e2e48b47 FW |
290 | case IPPROTO_ICMPV6: |
291 | return icmpv6_pkt_to_tuple(skb, dataoff, net, tuple); | |
81e01647 | 292 | #endif |
e2e48b47 FW |
293 | case IPPROTO_ICMP: |
294 | return icmp_pkt_to_tuple(skb, dataoff, net, tuple); | |
df5e1629 FW |
295 | #ifdef CONFIG_NF_CT_PROTO_GRE |
296 | case IPPROTO_GRE: | |
297 | return gre_pkt_to_tuple(skb, dataoff, net, tuple); | |
298 | #endif | |
e2f7cc72 FW |
299 | case IPPROTO_TCP: |
300 | case IPPROTO_UDP: /* fallthrough */ | |
301 | return nf_ct_get_tuple_ports(skb, dataoff, tuple); | |
302 | #ifdef CONFIG_NF_CT_PROTO_UDPLITE | |
303 | case IPPROTO_UDPLITE: | |
304 | return nf_ct_get_tuple_ports(skb, dataoff, tuple); | |
305 | #endif | |
306 | #ifdef CONFIG_NF_CT_PROTO_SCTP | |
307 | case IPPROTO_SCTP: | |
308 | return nf_ct_get_tuple_ports(skb, dataoff, tuple); | |
309 | #endif | |
310 | #ifdef CONFIG_NF_CT_PROTO_DCCP | |
311 | case IPPROTO_DCCP: | |
312 | return nf_ct_get_tuple_ports(skb, dataoff, tuple); | |
313 | #endif | |
314 | default: | |
315 | break; | |
e2e48b47 | 316 | } |
97e08cae | 317 | |
97e08cae | 318 | return true; |
9fb9cbb1 YK |
319 | } |
320 | ||
6816d931 FW |
321 | static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
322 | u_int8_t *protonum) | |
323 | { | |
324 | int dataoff = -1; | |
6816d931 FW |
325 | const struct iphdr *iph; |
326 | struct iphdr _iph; | |
327 | ||
328 | iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); | |
329 | if (!iph) | |
330 | return -1; | |
331 | ||
332 | /* Conntrack defragments packets, we might still see fragments | |
333 | * inside ICMP packets though. | |
334 | */ | |
335 | if (iph->frag_off & htons(IP_OFFSET)) | |
336 | return -1; | |
337 | ||
338 | dataoff = nhoff + (iph->ihl << 2); | |
339 | *protonum = iph->protocol; | |
340 | ||
341 | /* Check bogus IP headers */ | |
342 | if (dataoff > skb->len) { | |
343 | pr_debug("bogus IPv4 packet: nhoff %u, ihl %u, skblen %u\n", | |
344 | nhoff, iph->ihl << 2, skb->len); | |
345 | return -1; | |
346 | } | |
6816d931 FW |
347 | return dataoff; |
348 | } | |
349 | ||
a0ae2562 | 350 | #if IS_ENABLED(CONFIG_IPV6) |
6816d931 FW |
351 | static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
352 | u8 *protonum) | |
353 | { | |
354 | int protoff = -1; | |
6816d931 FW |
355 | unsigned int extoff = nhoff + sizeof(struct ipv6hdr); |
356 | __be16 frag_off; | |
357 | u8 nexthdr; | |
358 | ||
359 | if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr), | |
360 | &nexthdr, sizeof(nexthdr)) != 0) { | |
361 | pr_debug("can't get nexthdr\n"); | |
362 | return -1; | |
363 | } | |
364 | protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off); | |
365 | /* | |
366 | * (protoff == skb->len) means the packet has not data, just | |
367 | * IPv6 and possibly extensions headers, but it is tracked anyway | |
368 | */ | |
369 | if (protoff < 0 || (frag_off & htons(~0x7)) != 0) { | |
370 | pr_debug("can't find proto in pkt\n"); | |
371 | return -1; | |
372 | } | |
373 | ||
374 | *protonum = nexthdr; | |
6816d931 FW |
375 | return protoff; |
376 | } | |
a0ae2562 | 377 | #endif |
6816d931 FW |
378 | |
379 | static int get_l4proto(const struct sk_buff *skb, | |
380 | unsigned int nhoff, u8 pf, u8 *l4num) | |
381 | { | |
382 | switch (pf) { | |
383 | case NFPROTO_IPV4: | |
384 | return ipv4_get_l4proto(skb, nhoff, l4num); | |
a0ae2562 | 385 | #if IS_ENABLED(CONFIG_IPV6) |
6816d931 FW |
386 | case NFPROTO_IPV6: |
387 | return ipv6_get_l4proto(skb, nhoff, l4num); | |
a0ae2562 | 388 | #endif |
6816d931 FW |
389 | default: |
390 | *l4num = 0; | |
391 | break; | |
392 | } | |
393 | return -1; | |
9fb9cbb1 YK |
394 | } |
395 | ||
5f2b4c90 | 396 | bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff, |
a31f1adc EB |
397 | u_int16_t l3num, |
398 | struct net *net, struct nf_conntrack_tuple *tuple) | |
e2a3123f | 399 | { |
6816d931 FW |
400 | u8 protonum; |
401 | int protoff; | |
e2a3123f | 402 | |
6816d931 | 403 | protoff = get_l4proto(skb, nhoff, l3num, &protonum); |
303e0c55 | 404 | if (protoff <= 0) |
5f2b4c90 | 405 | return false; |
e2a3123f | 406 | |
303e0c55 | 407 | return nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple); |
e2a3123f YK |
408 | } |
409 | EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr); | |
410 | ||
5f2b4c90 | 411 | bool |
9fb9cbb1 | 412 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, |
303e0c55 | 413 | const struct nf_conntrack_tuple *orig) |
9fb9cbb1 | 414 | { |
443a70d5 | 415 | memset(inverse, 0, sizeof(*inverse)); |
9fb9cbb1 YK |
416 | |
417 | inverse->src.l3num = orig->src.l3num; | |
d1b6fe94 FW |
418 | |
419 | switch (orig->src.l3num) { | |
420 | case NFPROTO_IPV4: | |
421 | inverse->src.u3.ip = orig->dst.u3.ip; | |
422 | inverse->dst.u3.ip = orig->src.u3.ip; | |
423 | break; | |
424 | case NFPROTO_IPV6: | |
425 | inverse->src.u3.in6 = orig->dst.u3.in6; | |
426 | inverse->dst.u3.in6 = orig->src.u3.in6; | |
427 | break; | |
428 | default: | |
429 | break; | |
430 | } | |
9fb9cbb1 YK |
431 | |
432 | inverse->dst.dir = !orig->dst.dir; | |
433 | ||
434 | inverse->dst.protonum = orig->dst.protonum; | |
8b3892ea | 435 | |
197c4300 FW |
436 | switch (orig->dst.protonum) { |
437 | case IPPROTO_ICMP: | |
438 | return nf_conntrack_invert_icmp_tuple(inverse, orig); | |
81e01647 | 439 | #if IS_ENABLED(CONFIG_IPV6) |
197c4300 FW |
440 | case IPPROTO_ICMPV6: |
441 | return nf_conntrack_invert_icmpv6_tuple(inverse, orig); | |
81e01647 | 442 | #endif |
197c4300 | 443 | } |
8b3892ea FW |
444 | |
445 | inverse->src.u.all = orig->dst.u.all; | |
446 | inverse->dst.u.all = orig->src.u.all; | |
447 | return true; | |
9fb9cbb1 | 448 | } |
13b18339 | 449 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuple); |
9fb9cbb1 | 450 | |
3c791076 FW |
451 | /* Generate a almost-unique pseudo-id for a given conntrack. |
452 | * | |
453 | * intentionally doesn't re-use any of the seeds used for hash | |
454 | * table location, we assume id gets exposed to userspace. | |
455 | * | |
456 | * Following nf_conn items do not change throughout lifetime | |
656c8e9c | 457 | * of the nf_conn: |
3c791076 FW |
458 | * |
459 | * 1. nf_conn address | |
656c8e9c DM |
460 | * 2. nf_conn->master address (normally NULL) |
461 | * 3. the associated net namespace | |
462 | * 4. the original direction tuple | |
3c791076 FW |
463 | */ |
464 | u32 nf_ct_get_id(const struct nf_conn *ct) | |
465 | { | |
466 | static __read_mostly siphash_key_t ct_id_seed; | |
467 | unsigned long a, b, c, d; | |
468 | ||
469 | net_get_random_once(&ct_id_seed, sizeof(ct_id_seed)); | |
470 | ||
471 | a = (unsigned long)ct; | |
656c8e9c DM |
472 | b = (unsigned long)ct->master; |
473 | c = (unsigned long)nf_ct_net(ct); | |
474 | d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
475 | sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple), | |
3c791076 FW |
476 | &ct_id_seed); |
477 | #ifdef CONFIG_64BIT | |
478 | return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed); | |
479 | #else | |
480 | return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed); | |
481 | #endif | |
482 | } | |
483 | EXPORT_SYMBOL_GPL(nf_ct_get_id); | |
484 | ||
9fb9cbb1 YK |
485 | static void |
486 | clean_from_lists(struct nf_conn *ct) | |
487 | { | |
0d53778e | 488 | pr_debug("clean_from_lists(%p)\n", ct); |
ea781f19 ED |
489 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); |
490 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode); | |
9fb9cbb1 YK |
491 | |
492 | /* Destroy all pending expectations */ | |
c1d10adb | 493 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
494 | } |
495 | ||
b7779d06 JDB |
496 | /* must be called with local_bh_disable */ |
497 | static void nf_ct_add_to_dying_list(struct nf_conn *ct) | |
498 | { | |
499 | struct ct_pcpu *pcpu; | |
500 | ||
501 | /* add this conntrack to the (per cpu) dying list */ | |
502 | ct->cpu = smp_processor_id(); | |
503 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
504 | ||
505 | spin_lock(&pcpu->lock); | |
506 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, | |
507 | &pcpu->dying); | |
508 | spin_unlock(&pcpu->lock); | |
509 | } | |
510 | ||
511 | /* must be called with local_bh_disable */ | |
512 | static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct) | |
513 | { | |
514 | struct ct_pcpu *pcpu; | |
515 | ||
516 | /* add this conntrack to the (per cpu) unconfirmed list */ | |
517 | ct->cpu = smp_processor_id(); | |
518 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
519 | ||
520 | spin_lock(&pcpu->lock); | |
521 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, | |
522 | &pcpu->unconfirmed); | |
523 | spin_unlock(&pcpu->lock); | |
524 | } | |
525 | ||
526 | /* must be called with local_bh_disable */ | |
527 | static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct) | |
528 | { | |
529 | struct ct_pcpu *pcpu; | |
530 | ||
531 | /* We overload first tuple to link into unconfirmed or dying list.*/ | |
532 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); | |
533 | ||
534 | spin_lock(&pcpu->lock); | |
535 | BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode)); | |
536 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); | |
537 | spin_unlock(&pcpu->lock); | |
538 | } | |
539 | ||
30322309 FW |
540 | #define NFCT_ALIGN(len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK) |
541 | ||
0838aa7f | 542 | /* Released via destroy_conntrack() */ |
308ac914 DB |
543 | struct nf_conn *nf_ct_tmpl_alloc(struct net *net, |
544 | const struct nf_conntrack_zone *zone, | |
545 | gfp_t flags) | |
0838aa7f | 546 | { |
30322309 | 547 | struct nf_conn *tmpl, *p; |
0838aa7f | 548 | |
30322309 FW |
549 | if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK) { |
550 | tmpl = kzalloc(sizeof(*tmpl) + NFCT_INFOMASK, flags); | |
551 | if (!tmpl) | |
552 | return NULL; | |
553 | ||
554 | p = tmpl; | |
555 | tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p); | |
556 | if (tmpl != p) { | |
557 | tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p); | |
558 | tmpl->proto.tmpl_padto = (char *)tmpl - (char *)p; | |
559 | } | |
560 | } else { | |
561 | tmpl = kzalloc(sizeof(*tmpl), flags); | |
562 | if (!tmpl) | |
563 | return NULL; | |
564 | } | |
0838aa7f PNA |
565 | |
566 | tmpl->status = IPS_TEMPLATE; | |
567 | write_pnet(&tmpl->ct_net, net); | |
6c8dee98 | 568 | nf_ct_zone_add(tmpl, zone); |
0838aa7f PNA |
569 | atomic_set(&tmpl->ct_general.use, 0); |
570 | ||
571 | return tmpl; | |
0838aa7f PNA |
572 | } |
573 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc); | |
574 | ||
9cf94eab | 575 | void nf_ct_tmpl_free(struct nf_conn *tmpl) |
0838aa7f PNA |
576 | { |
577 | nf_ct_ext_destroy(tmpl); | |
30322309 FW |
578 | |
579 | if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK) | |
580 | kfree((char *)tmpl - tmpl->proto.tmpl_padto); | |
581 | else | |
582 | kfree(tmpl); | |
0838aa7f | 583 | } |
9cf94eab | 584 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_free); |
0838aa7f | 585 | |
e5689435 FW |
586 | static void destroy_gre_conntrack(struct nf_conn *ct) |
587 | { | |
81e01647 | 588 | #ifdef CONFIG_NF_CT_PROTO_GRE |
e5689435 FW |
589 | struct nf_conn *master = ct->master; |
590 | ||
591 | if (master) | |
592 | nf_ct_gre_keymap_destroy(master); | |
81e01647 | 593 | #endif |
e5689435 FW |
594 | } |
595 | ||
9fb9cbb1 YK |
596 | static void |
597 | destroy_conntrack(struct nf_conntrack *nfct) | |
598 | { | |
599 | struct nf_conn *ct = (struct nf_conn *)nfct; | |
9fb9cbb1 | 600 | |
0d53778e | 601 | pr_debug("destroy_conntrack(%p)\n", ct); |
44d6e2f2 | 602 | WARN_ON(atomic_read(&nfct->use) != 0); |
9fb9cbb1 | 603 | |
0838aa7f PNA |
604 | if (unlikely(nf_ct_is_template(ct))) { |
605 | nf_ct_tmpl_free(ct); | |
606 | return; | |
607 | } | |
e5689435 FW |
608 | |
609 | if (unlikely(nf_ct_protonum(ct) == IPPROTO_GRE)) | |
610 | destroy_gre_conntrack(ct); | |
9fb9cbb1 | 611 | |
ca7433df | 612 | local_bh_disable(); |
9fb9cbb1 YK |
613 | /* Expectations will have been removed in clean_from_lists, |
614 | * except TFTP can create an expectation on the first packet, | |
615 | * before connection is in the list, so we need to clean here, | |
ca7433df JDB |
616 | * too. |
617 | */ | |
c1d10adb | 618 | nf_ct_remove_expectations(ct); |
9fb9cbb1 | 619 | |
b7779d06 | 620 | nf_ct_del_from_dying_or_unconfirmed_list(ct); |
9fb9cbb1 | 621 | |
ca7433df | 622 | local_bh_enable(); |
9fb9cbb1 YK |
623 | |
624 | if (ct->master) | |
625 | nf_ct_put(ct->master); | |
626 | ||
0d53778e | 627 | pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct); |
9fb9cbb1 YK |
628 | nf_conntrack_free(ct); |
629 | } | |
630 | ||
02982c27 | 631 | static void nf_ct_delete_from_lists(struct nf_conn *ct) |
9fb9cbb1 | 632 | { |
0d55af87 | 633 | struct net *net = nf_ct_net(ct); |
93bb0ceb | 634 | unsigned int hash, reply_hash; |
93bb0ceb | 635 | unsigned int sequence; |
9fb9cbb1 | 636 | |
9858a3ae | 637 | nf_ct_helper_destroy(ct); |
93bb0ceb JDB |
638 | |
639 | local_bh_disable(); | |
640 | do { | |
a3efd812 | 641 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
deedb590 | 642 | hash = hash_conntrack(net, |
93bb0ceb | 643 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
deedb590 | 644 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
645 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
646 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
647 | ||
9fb9cbb1 | 648 | clean_from_lists(ct); |
93bb0ceb JDB |
649 | nf_conntrack_double_unlock(hash, reply_hash); |
650 | ||
b7779d06 | 651 | nf_ct_add_to_dying_list(ct); |
93bb0ceb | 652 | |
93bb0ceb | 653 | local_bh_enable(); |
dd7669a9 | 654 | } |
dd7669a9 | 655 | |
02982c27 | 656 | bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report) |
dd7669a9 | 657 | { |
a992ca2a PNA |
658 | struct nf_conn_tstamp *tstamp; |
659 | ||
f330a7fd FW |
660 | if (test_and_set_bit(IPS_DYING_BIT, &ct->status)) |
661 | return false; | |
662 | ||
a992ca2a PNA |
663 | tstamp = nf_conn_tstamp_find(ct); |
664 | if (tstamp && tstamp->stop == 0) | |
d2de875c | 665 | tstamp->stop = ktime_get_real_ns(); |
dd7669a9 | 666 | |
9500507c FW |
667 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
668 | portid, report) < 0) { | |
f330a7fd FW |
669 | /* destroy event was not delivered. nf_ct_put will |
670 | * be done by event cache worker on redelivery. | |
671 | */ | |
dd7669a9 | 672 | nf_ct_delete_from_lists(ct); |
9500507c | 673 | nf_conntrack_ecache_delayed_work(nf_ct_net(ct)); |
02982c27 | 674 | return false; |
dd7669a9 | 675 | } |
9500507c FW |
676 | |
677 | nf_conntrack_ecache_work(nf_ct_net(ct)); | |
dd7669a9 | 678 | nf_ct_delete_from_lists(ct); |
9fb9cbb1 | 679 | nf_ct_put(ct); |
02982c27 FW |
680 | return true; |
681 | } | |
682 | EXPORT_SYMBOL_GPL(nf_ct_delete); | |
683 | ||
c6825c09 AV |
684 | static inline bool |
685 | nf_ct_key_equal(struct nf_conntrack_tuple_hash *h, | |
308ac914 | 686 | const struct nf_conntrack_tuple *tuple, |
e0c7d472 FW |
687 | const struct nf_conntrack_zone *zone, |
688 | const struct net *net) | |
c6825c09 AV |
689 | { |
690 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
691 | ||
692 | /* A conntrack can be recreated with the equal tuple, | |
693 | * so we need to check that the conntrack is confirmed | |
694 | */ | |
695 | return nf_ct_tuple_equal(tuple, &h->tuple) && | |
deedb590 | 696 | nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) && |
e0c7d472 FW |
697 | nf_ct_is_confirmed(ct) && |
698 | net_eq(net, nf_ct_net(ct)); | |
c6825c09 AV |
699 | } |
700 | ||
ed07d9a0 MP |
701 | static inline bool |
702 | nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2) | |
703 | { | |
704 | return nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
705 | &ct2->tuplehash[IP_CT_DIR_ORIGINAL].tuple) && | |
706 | nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_REPLY].tuple, | |
707 | &ct2->tuplehash[IP_CT_DIR_REPLY].tuple) && | |
708 | nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_ORIGINAL) && | |
709 | nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_REPLY) && | |
710 | net_eq(nf_ct_net(ct1), nf_ct_net(ct2)); | |
711 | } | |
712 | ||
f330a7fd FW |
713 | /* caller must hold rcu readlock and none of the nf_conntrack_locks */ |
714 | static void nf_ct_gc_expired(struct nf_conn *ct) | |
715 | { | |
716 | if (!atomic_inc_not_zero(&ct->ct_general.use)) | |
717 | return; | |
718 | ||
719 | if (nf_ct_should_gc(ct)) | |
720 | nf_ct_kill(ct); | |
721 | ||
722 | nf_ct_put(ct); | |
723 | } | |
724 | ||
ea781f19 ED |
725 | /* |
726 | * Warning : | |
727 | * - Caller must take a reference on returned object | |
728 | * and recheck nf_ct_tuple_equal(tuple, &h->tuple) | |
ea781f19 | 729 | */ |
99f07e91 | 730 | static struct nf_conntrack_tuple_hash * |
308ac914 | 731 | ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 | 732 | const struct nf_conntrack_tuple *tuple, u32 hash) |
9fb9cbb1 YK |
733 | { |
734 | struct nf_conntrack_tuple_hash *h; | |
5e3c61f9 | 735 | struct hlist_nulls_head *ct_hash; |
ea781f19 | 736 | struct hlist_nulls_node *n; |
92e47ba8 | 737 | unsigned int bucket, hsize; |
9fb9cbb1 | 738 | |
ea781f19 | 739 | begin: |
92e47ba8 LZ |
740 | nf_conntrack_get_ht(&ct_hash, &hsize); |
741 | bucket = reciprocal_scale(hash, hsize); | |
5e3c61f9 FW |
742 | |
743 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) { | |
f330a7fd FW |
744 | struct nf_conn *ct; |
745 | ||
746 | ct = nf_ct_tuplehash_to_ctrack(h); | |
747 | if (nf_ct_is_expired(ct)) { | |
748 | nf_ct_gc_expired(ct); | |
749 | continue; | |
750 | } | |
751 | ||
8e8118f8 | 752 | if (nf_ct_key_equal(h, tuple, zone, net)) |
9fb9cbb1 | 753 | return h; |
9fb9cbb1 | 754 | } |
ea781f19 ED |
755 | /* |
756 | * if the nulls value we got at the end of this lookup is | |
757 | * not the expected one, we must restart lookup. | |
758 | * We probably met an item that was moved to another chain. | |
759 | */ | |
99f07e91 | 760 | if (get_nulls_value(n) != bucket) { |
2cf12348 | 761 | NF_CT_STAT_INC_ATOMIC(net, search_restart); |
ea781f19 | 762 | goto begin; |
af740b2c | 763 | } |
9fb9cbb1 YK |
764 | |
765 | return NULL; | |
766 | } | |
99f07e91 | 767 | |
9fb9cbb1 | 768 | /* Find a connection corresponding to a tuple. */ |
99f07e91 | 769 | static struct nf_conntrack_tuple_hash * |
308ac914 | 770 | __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 | 771 | const struct nf_conntrack_tuple *tuple, u32 hash) |
9fb9cbb1 YK |
772 | { |
773 | struct nf_conntrack_tuple_hash *h; | |
76507f69 | 774 | struct nf_conn *ct; |
9fb9cbb1 | 775 | |
76507f69 | 776 | rcu_read_lock(); |
87e389b4 | 777 | |
99f07e91 | 778 | h = ____nf_conntrack_find(net, zone, tuple, hash); |
76507f69 | 779 | if (h) { |
87e389b4 FW |
780 | /* We have a candidate that matches the tuple we're interested |
781 | * in, try to obtain a reference and re-check tuple | |
782 | */ | |
76507f69 | 783 | ct = nf_ct_tuplehash_to_ctrack(h); |
87e389b4 FW |
784 | if (likely(atomic_inc_not_zero(&ct->ct_general.use))) { |
785 | if (likely(nf_ct_key_equal(h, tuple, zone, net))) | |
786 | goto found; | |
787 | ||
788 | /* TYPESAFE_BY_RCU recycled the candidate */ | |
789 | nf_ct_put(ct); | |
ea781f19 | 790 | } |
87e389b4 FW |
791 | |
792 | h = NULL; | |
76507f69 | 793 | } |
87e389b4 | 794 | found: |
76507f69 | 795 | rcu_read_unlock(); |
9fb9cbb1 YK |
796 | |
797 | return h; | |
798 | } | |
99f07e91 CG |
799 | |
800 | struct nf_conntrack_tuple_hash * | |
308ac914 | 801 | nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
99f07e91 CG |
802 | const struct nf_conntrack_tuple *tuple) |
803 | { | |
804 | return __nf_conntrack_find_get(net, zone, tuple, | |
1b8c8a9f | 805 | hash_conntrack_raw(tuple, net)); |
99f07e91 | 806 | } |
13b18339 | 807 | EXPORT_SYMBOL_GPL(nf_conntrack_find_get); |
9fb9cbb1 | 808 | |
c1d10adb PNA |
809 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
810 | unsigned int hash, | |
b476b72a | 811 | unsigned int reply_hash) |
c1d10adb | 812 | { |
ea781f19 | 813 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, |
56d52d48 | 814 | &nf_conntrack_hash[hash]); |
ea781f19 | 815 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode, |
56d52d48 | 816 | &nf_conntrack_hash[reply_hash]); |
c1d10adb PNA |
817 | } |
818 | ||
7d367e06 JK |
819 | int |
820 | nf_conntrack_hash_check_insert(struct nf_conn *ct) | |
c1d10adb | 821 | { |
308ac914 | 822 | const struct nf_conntrack_zone *zone; |
d696c7bd | 823 | struct net *net = nf_ct_net(ct); |
b476b72a | 824 | unsigned int hash, reply_hash; |
7d367e06 JK |
825 | struct nf_conntrack_tuple_hash *h; |
826 | struct hlist_nulls_node *n; | |
93bb0ceb | 827 | unsigned int sequence; |
c1d10adb | 828 | |
5d0aa2cc | 829 | zone = nf_ct_zone(ct); |
7d367e06 | 830 | |
93bb0ceb JDB |
831 | local_bh_disable(); |
832 | do { | |
a3efd812 | 833 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
deedb590 | 834 | hash = hash_conntrack(net, |
93bb0ceb | 835 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
deedb590 | 836 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
837 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
838 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
7d367e06 JK |
839 | |
840 | /* See if there's one in the list already, including reverse */ | |
56d52d48 | 841 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) |
86804348 | 842 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
e0c7d472 | 843 | zone, net)) |
7d367e06 | 844 | goto out; |
86804348 | 845 | |
56d52d48 | 846 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) |
86804348 | 847 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
e0c7d472 | 848 | zone, net)) |
7d367e06 | 849 | goto out; |
c1d10adb | 850 | |
e53376be PNA |
851 | smp_wmb(); |
852 | /* The caller holds a reference to this object */ | |
853 | atomic_set(&ct->ct_general.use, 2); | |
b476b72a | 854 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
93bb0ceb | 855 | nf_conntrack_double_unlock(hash, reply_hash); |
7d367e06 | 856 | NF_CT_STAT_INC(net, insert); |
93bb0ceb | 857 | local_bh_enable(); |
7d367e06 JK |
858 | return 0; |
859 | ||
860 | out: | |
93bb0ceb | 861 | nf_conntrack_double_unlock(hash, reply_hash); |
7d367e06 | 862 | NF_CT_STAT_INC(net, insert_failed); |
93bb0ceb | 863 | local_bh_enable(); |
7d367e06 | 864 | return -EEXIST; |
c1d10adb | 865 | } |
7d367e06 | 866 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert); |
c1d10adb | 867 | |
9312eaba | 868 | void nf_ct_acct_add(struct nf_conn *ct, u32 dir, unsigned int packets, |
869 | unsigned int bytes) | |
ba76738c PNA |
870 | { |
871 | struct nf_conn_acct *acct; | |
872 | ||
873 | acct = nf_conn_acct_find(ct); | |
874 | if (acct) { | |
875 | struct nf_conn_counter *counter = acct->counter; | |
876 | ||
9312eaba | 877 | atomic64_add(packets, &counter[dir].packets); |
8ac2bd35 | 878 | atomic64_add(bytes, &counter[dir].bytes); |
ba76738c PNA |
879 | } |
880 | } | |
9312eaba | 881 | EXPORT_SYMBOL_GPL(nf_ct_acct_add); |
ba76738c | 882 | |
71d8c47f PNA |
883 | static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
884 | const struct nf_conn *loser_ct) | |
885 | { | |
886 | struct nf_conn_acct *acct; | |
887 | ||
888 | acct = nf_conn_acct_find(loser_ct); | |
889 | if (acct) { | |
890 | struct nf_conn_counter *counter = acct->counter; | |
71d8c47f PNA |
891 | unsigned int bytes; |
892 | ||
893 | /* u32 should be fine since we must have seen one packet. */ | |
894 | bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes); | |
8ac2bd35 | 895 | nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), bytes); |
71d8c47f PNA |
896 | } |
897 | } | |
898 | ||
b1b32552 FW |
899 | static void __nf_conntrack_insert_prepare(struct nf_conn *ct) |
900 | { | |
901 | struct nf_conn_tstamp *tstamp; | |
902 | ||
903 | atomic_inc(&ct->ct_general.use); | |
904 | ct->status |= IPS_CONFIRMED; | |
905 | ||
906 | /* set conntrack timestamp, if enabled. */ | |
907 | tstamp = nf_conn_tstamp_find(ct); | |
908 | if (tstamp) | |
909 | tstamp->start = ktime_get_real_ns(); | |
910 | } | |
911 | ||
bb89abe5 FW |
912 | static int __nf_ct_resolve_clash(struct sk_buff *skb, |
913 | struct nf_conntrack_tuple_hash *h) | |
914 | { | |
915 | /* This is the conntrack entry already in hashes that won race. */ | |
916 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
917 | enum ip_conntrack_info ctinfo; | |
918 | struct nf_conn *loser_ct; | |
919 | ||
920 | loser_ct = nf_ct_get(skb, &ctinfo); | |
921 | ||
922 | if (nf_ct_is_dying(ct)) | |
923 | return NF_DROP; | |
924 | ||
925 | if (!atomic_inc_not_zero(&ct->ct_general.use)) | |
926 | return NF_DROP; | |
927 | ||
928 | if (((ct->status & IPS_NAT_DONE_MASK) == 0) || | |
929 | nf_ct_match(ct, loser_ct)) { | |
930 | struct net *net = nf_ct_net(ct); | |
931 | ||
932 | nf_ct_acct_merge(ct, ctinfo, loser_ct); | |
933 | nf_ct_add_to_dying_list(loser_ct); | |
934 | nf_conntrack_put(&loser_ct->ct_general); | |
935 | nf_ct_set(skb, ct, ctinfo); | |
936 | ||
937 | NF_CT_STAT_INC(net, insert_failed); | |
938 | return NF_ACCEPT; | |
939 | } | |
940 | ||
941 | nf_ct_put(ct); | |
942 | return NF_DROP; | |
943 | } | |
944 | ||
6a757c07 FW |
945 | /** |
946 | * nf_ct_resolve_clash_harder - attempt to insert clashing conntrack entry | |
947 | * | |
948 | * @skb: skb that causes the collision | |
949 | * @repl_idx: hash slot for reply direction | |
950 | * | |
951 | * Called when origin or reply direction had a clash. | |
952 | * The skb can be handled without packet drop provided the reply direction | |
953 | * is unique or there the existing entry has the identical tuple in both | |
954 | * directions. | |
955 | * | |
956 | * Caller must hold conntrack table locks to prevent concurrent updates. | |
957 | * | |
958 | * Returns NF_DROP if the clash could not be handled. | |
959 | */ | |
960 | static int nf_ct_resolve_clash_harder(struct sk_buff *skb, u32 repl_idx) | |
961 | { | |
962 | struct nf_conn *loser_ct = (struct nf_conn *)skb_nfct(skb); | |
963 | const struct nf_conntrack_zone *zone; | |
964 | struct nf_conntrack_tuple_hash *h; | |
965 | struct hlist_nulls_node *n; | |
966 | struct net *net; | |
967 | ||
968 | zone = nf_ct_zone(loser_ct); | |
969 | net = nf_ct_net(loser_ct); | |
970 | ||
971 | /* Reply direction must never result in a clash, unless both origin | |
972 | * and reply tuples are identical. | |
973 | */ | |
974 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[repl_idx], hnnode) { | |
975 | if (nf_ct_key_equal(h, | |
976 | &loser_ct->tuplehash[IP_CT_DIR_REPLY].tuple, | |
977 | zone, net)) | |
978 | return __nf_ct_resolve_clash(skb, h); | |
979 | } | |
980 | ||
981 | /* We want the clashing entry to go away real soon: 1 second timeout. */ | |
982 | loser_ct->timeout = nfct_time_stamp + HZ; | |
983 | ||
984 | /* IPS_NAT_CLASH removes the entry automatically on the first | |
985 | * reply. Also prevents UDP tracker from moving the entry to | |
986 | * ASSURED state, i.e. the entry can always be evicted under | |
987 | * pressure. | |
988 | */ | |
989 | loser_ct->status |= IPS_FIXED_TIMEOUT | IPS_NAT_CLASH; | |
990 | ||
991 | __nf_conntrack_insert_prepare(loser_ct); | |
992 | ||
993 | /* fake add for ORIGINAL dir: we want lookups to only find the entry | |
994 | * already in the table. This also hides the clashing entry from | |
995 | * ctnetlink iteration, i.e. conntrack -L won't show them. | |
996 | */ | |
997 | hlist_nulls_add_fake(&loser_ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); | |
998 | ||
999 | hlist_nulls_add_head_rcu(&loser_ct->tuplehash[IP_CT_DIR_REPLY].hnnode, | |
1000 | &nf_conntrack_hash[repl_idx]); | |
1001 | return NF_ACCEPT; | |
1002 | } | |
1003 | ||
3d1e0b40 FW |
1004 | /** |
1005 | * nf_ct_resolve_clash - attempt to handle clash without packet drop | |
1006 | * | |
1007 | * @skb: skb that causes the clash | |
1008 | * @h: tuplehash of the clashing entry already in table | |
6a757c07 | 1009 | * @hash_reply: hash slot for reply direction |
3d1e0b40 FW |
1010 | * |
1011 | * A conntrack entry can be inserted to the connection tracking table | |
1012 | * if there is no existing entry with an identical tuple. | |
1013 | * | |
1014 | * If there is one, @skb (and the assocated, unconfirmed conntrack) has | |
1015 | * to be dropped. In case @skb is retransmitted, next conntrack lookup | |
1016 | * will find the already-existing entry. | |
1017 | * | |
1018 | * The major problem with such packet drop is the extra delay added by | |
1019 | * the packet loss -- it will take some time for a retransmit to occur | |
1020 | * (or the sender to time out when waiting for a reply). | |
1021 | * | |
1022 | * This function attempts to handle the situation without packet drop. | |
1023 | * | |
1024 | * If @skb has no NAT transformation or if the colliding entries are | |
1025 | * exactly the same, only the to-be-confirmed conntrack entry is discarded | |
1026 | * and @skb is associated with the conntrack entry already in the table. | |
1027 | * | |
6a757c07 FW |
1028 | * Failing that, the new, unconfirmed conntrack is still added to the table |
1029 | * provided that the collision only occurs in the ORIGINAL direction. | |
1030 | * The new entry will be added after the existing one in the hash list, | |
1031 | * so packets in the ORIGINAL direction will continue to match the existing | |
1032 | * entry. The new entry will also have a fixed timeout so it expires -- | |
1033 | * due to the collision, it will not see bidirectional traffic. | |
1034 | * | |
3d1e0b40 FW |
1035 | * Returns NF_DROP if the clash could not be resolved. |
1036 | */ | |
c7c17e6a | 1037 | static __cold noinline int |
6a757c07 FW |
1038 | nf_ct_resolve_clash(struct sk_buff *skb, struct nf_conntrack_tuple_hash *h, |
1039 | u32 reply_hash) | |
71d8c47f PNA |
1040 | { |
1041 | /* This is the conntrack entry already in hashes that won race. */ | |
1042 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
b3480fe0 | 1043 | const struct nf_conntrack_l4proto *l4proto; |
3d1e0b40 FW |
1044 | enum ip_conntrack_info ctinfo; |
1045 | struct nf_conn *loser_ct; | |
1046 | struct net *net; | |
bb89abe5 | 1047 | int ret; |
3d1e0b40 FW |
1048 | |
1049 | loser_ct = nf_ct_get(skb, &ctinfo); | |
bb89abe5 | 1050 | net = nf_ct_net(loser_ct); |
71d8c47f | 1051 | |
4a60dc74 | 1052 | l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); |
3d1e0b40 FW |
1053 | if (!l4proto->allow_clash) |
1054 | goto drop; | |
1055 | ||
bb89abe5 FW |
1056 | ret = __nf_ct_resolve_clash(skb, h); |
1057 | if (ret == NF_ACCEPT) | |
1058 | return ret; | |
3d1e0b40 | 1059 | |
6a757c07 FW |
1060 | ret = nf_ct_resolve_clash_harder(skb, reply_hash); |
1061 | if (ret == NF_ACCEPT) | |
1062 | return ret; | |
1063 | ||
3d1e0b40 | 1064 | drop: |
bb89abe5 | 1065 | nf_ct_add_to_dying_list(loser_ct); |
71d8c47f | 1066 | NF_CT_STAT_INC(net, drop); |
bb89abe5 | 1067 | NF_CT_STAT_INC(net, insert_failed); |
71d8c47f PNA |
1068 | return NF_DROP; |
1069 | } | |
1070 | ||
9fb9cbb1 YK |
1071 | /* Confirm a connection given skb; places it in hash table */ |
1072 | int | |
3db05fea | 1073 | __nf_conntrack_confirm(struct sk_buff *skb) |
9fb9cbb1 | 1074 | { |
308ac914 | 1075 | const struct nf_conntrack_zone *zone; |
b476b72a | 1076 | unsigned int hash, reply_hash; |
df0933dc | 1077 | struct nf_conntrack_tuple_hash *h; |
9fb9cbb1 | 1078 | struct nf_conn *ct; |
df0933dc | 1079 | struct nf_conn_help *help; |
ea781f19 | 1080 | struct hlist_nulls_node *n; |
9fb9cbb1 | 1081 | enum ip_conntrack_info ctinfo; |
400dad39 | 1082 | struct net *net; |
93bb0ceb | 1083 | unsigned int sequence; |
71d8c47f | 1084 | int ret = NF_DROP; |
9fb9cbb1 | 1085 | |
3db05fea | 1086 | ct = nf_ct_get(skb, &ctinfo); |
400dad39 | 1087 | net = nf_ct_net(ct); |
9fb9cbb1 YK |
1088 | |
1089 | /* ipt_REJECT uses nf_conntrack_attach to attach related | |
1090 | ICMP/TCP RST packets in other direction. Actual packet | |
1091 | which created connection will be IP_CT_NEW or for an | |
1092 | expected connection, IP_CT_RELATED. */ | |
1093 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) | |
1094 | return NF_ACCEPT; | |
1095 | ||
5d0aa2cc | 1096 | zone = nf_ct_zone(ct); |
93bb0ceb JDB |
1097 | local_bh_disable(); |
1098 | ||
1099 | do { | |
a3efd812 | 1100 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
93bb0ceb JDB |
1101 | /* reuse the hash saved before */ |
1102 | hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev; | |
56d52d48 | 1103 | hash = scale_hash(hash); |
deedb590 | 1104 | reply_hash = hash_conntrack(net, |
93bb0ceb JDB |
1105 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
1106 | ||
1107 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); | |
9fb9cbb1 YK |
1108 | |
1109 | /* We're not in hash table, and we refuse to set up related | |
93bb0ceb JDB |
1110 | * connections for unconfirmed conns. But packet copies and |
1111 | * REJECT will give spurious warnings here. | |
1112 | */ | |
9fb9cbb1 | 1113 | |
13f5251f CMW |
1114 | /* Another skb with the same unconfirmed conntrack may |
1115 | * win the race. This may happen for bridge(br_flood) | |
1116 | * or broadcast/multicast packets do skb_clone with | |
1117 | * unconfirmed conntrack. | |
93bb0ceb | 1118 | */ |
13f5251f CMW |
1119 | if (unlikely(nf_ct_is_confirmed(ct))) { |
1120 | WARN_ON_ONCE(1); | |
1121 | nf_conntrack_double_unlock(hash, reply_hash); | |
1122 | local_bh_enable(); | |
1123 | return NF_DROP; | |
1124 | } | |
1125 | ||
0d53778e | 1126 | pr_debug("Confirming conntrack %p\n", ct); |
8ca3f5e9 PNA |
1127 | /* We have to check the DYING flag after unlink to prevent |
1128 | * a race against nf_ct_get_next_corpse() possibly called from | |
1129 | * user context, else we insert an already 'dead' hash, blocking | |
1130 | * further use of that particular connection -JM. | |
1131 | */ | |
1132 | nf_ct_del_from_dying_or_unconfirmed_list(ct); | |
1133 | ||
71d8c47f PNA |
1134 | if (unlikely(nf_ct_is_dying(ct))) { |
1135 | nf_ct_add_to_dying_list(ct); | |
bb89abe5 | 1136 | NF_CT_STAT_INC(net, insert_failed); |
71d8c47f PNA |
1137 | goto dying; |
1138 | } | |
fc350777 | 1139 | |
9fb9cbb1 YK |
1140 | /* See if there's one in the list already, including reverse: |
1141 | NAT could have grabbed it without realizing, since we're | |
1142 | not in the hash. If there is, we lost race. */ | |
56d52d48 | 1143 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) |
86804348 | 1144 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
e0c7d472 | 1145 | zone, net)) |
df0933dc | 1146 | goto out; |
86804348 | 1147 | |
56d52d48 | 1148 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) |
86804348 | 1149 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
e0c7d472 | 1150 | zone, net)) |
df0933dc | 1151 | goto out; |
9fb9cbb1 | 1152 | |
df0933dc PM |
1153 | /* Timer relative to confirmation time, not original |
1154 | setting time, otherwise we'd get timer wrap in | |
1155 | weird delay cases. */ | |
f330a7fd | 1156 | ct->timeout += nfct_time_stamp; |
5c8ec910 | 1157 | |
b1b32552 | 1158 | __nf_conntrack_insert_prepare(ct); |
a992ca2a | 1159 | |
5c8ec910 PM |
1160 | /* Since the lookup is lockless, hash insertion must be done after |
1161 | * starting the timer and setting the CONFIRMED bit. The RCU barriers | |
1162 | * guarantee that no other CPU can find the conntrack before the above | |
1163 | * stores are visible. | |
1164 | */ | |
b476b72a | 1165 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
93bb0ceb | 1166 | nf_conntrack_double_unlock(hash, reply_hash); |
93bb0ceb | 1167 | local_bh_enable(); |
5c8ec910 | 1168 | |
df0933dc PM |
1169 | help = nfct_help(ct); |
1170 | if (help && help->helper) | |
a71996fc | 1171 | nf_conntrack_event_cache(IPCT_HELPER, ct); |
17e6e4ea | 1172 | |
df0933dc | 1173 | nf_conntrack_event_cache(master_ct(ct) ? |
a71996fc | 1174 | IPCT_RELATED : IPCT_NEW, ct); |
df0933dc | 1175 | return NF_ACCEPT; |
9fb9cbb1 | 1176 | |
df0933dc | 1177 | out: |
6a757c07 | 1178 | ret = nf_ct_resolve_clash(skb, h, reply_hash); |
71d8c47f | 1179 | dying: |
93bb0ceb | 1180 | nf_conntrack_double_unlock(hash, reply_hash); |
93bb0ceb | 1181 | local_bh_enable(); |
71d8c47f | 1182 | return ret; |
9fb9cbb1 | 1183 | } |
13b18339 | 1184 | EXPORT_SYMBOL_GPL(__nf_conntrack_confirm); |
9fb9cbb1 YK |
1185 | |
1186 | /* Returns true if a connection correspondings to the tuple (required | |
1187 | for NAT). */ | |
1188 | int | |
1189 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, | |
1190 | const struct nf_conn *ignored_conntrack) | |
1191 | { | |
400dad39 | 1192 | struct net *net = nf_ct_net(ignored_conntrack); |
308ac914 | 1193 | const struct nf_conntrack_zone *zone; |
9fb9cbb1 | 1194 | struct nf_conntrack_tuple_hash *h; |
5e3c61f9 | 1195 | struct hlist_nulls_head *ct_hash; |
92e47ba8 | 1196 | unsigned int hash, hsize; |
ea781f19 | 1197 | struct hlist_nulls_node *n; |
5d0aa2cc | 1198 | struct nf_conn *ct; |
308ac914 DB |
1199 | |
1200 | zone = nf_ct_zone(ignored_conntrack); | |
9fb9cbb1 | 1201 | |
2cf12348 | 1202 | rcu_read_lock(); |
95a8d19f | 1203 | begin: |
92e47ba8 LZ |
1204 | nf_conntrack_get_ht(&ct_hash, &hsize); |
1205 | hash = __hash_conntrack(net, tuple, hsize); | |
5e3c61f9 FW |
1206 | |
1207 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) { | |
5d0aa2cc | 1208 | ct = nf_ct_tuplehash_to_ctrack(h); |
f330a7fd FW |
1209 | |
1210 | if (ct == ignored_conntrack) | |
1211 | continue; | |
1212 | ||
1213 | if (nf_ct_is_expired(ct)) { | |
1214 | nf_ct_gc_expired(ct); | |
1215 | continue; | |
1216 | } | |
1217 | ||
1218 | if (nf_ct_key_equal(h, tuple, zone, net)) { | |
4e35c1cb MP |
1219 | /* Tuple is taken already, so caller will need to find |
1220 | * a new source port to use. | |
1221 | * | |
1222 | * Only exception: | |
1223 | * If the *original tuples* are identical, then both | |
1224 | * conntracks refer to the same flow. | |
1225 | * This is a rare situation, it can occur e.g. when | |
1226 | * more than one UDP packet is sent from same socket | |
1227 | * in different threads. | |
1228 | * | |
1229 | * Let nf_ct_resolve_clash() deal with this later. | |
1230 | */ | |
1231 | if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1232 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)) | |
1233 | continue; | |
1234 | ||
2cf12348 FW |
1235 | NF_CT_STAT_INC_ATOMIC(net, found); |
1236 | rcu_read_unlock(); | |
ba419aff PM |
1237 | return 1; |
1238 | } | |
ba419aff | 1239 | } |
95a8d19f FW |
1240 | |
1241 | if (get_nulls_value(n) != hash) { | |
1242 | NF_CT_STAT_INC_ATOMIC(net, search_restart); | |
1243 | goto begin; | |
1244 | } | |
1245 | ||
2cf12348 | 1246 | rcu_read_unlock(); |
9fb9cbb1 | 1247 | |
ba419aff | 1248 | return 0; |
9fb9cbb1 | 1249 | } |
13b18339 | 1250 | EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken); |
9fb9cbb1 | 1251 | |
7ae7730f PM |
1252 | #define NF_CT_EVICTION_RANGE 8 |
1253 | ||
9fb9cbb1 YK |
1254 | /* There's a small race here where we may free a just-assured |
1255 | connection. Too bad: we're in trouble anyway. */ | |
242922a0 FW |
1256 | static unsigned int early_drop_list(struct net *net, |
1257 | struct hlist_nulls_head *head) | |
9fb9cbb1 | 1258 | { |
9fb9cbb1 | 1259 | struct nf_conntrack_tuple_hash *h; |
ea781f19 | 1260 | struct hlist_nulls_node *n; |
242922a0 FW |
1261 | unsigned int drops = 0; |
1262 | struct nf_conn *tmp; | |
3e86638e | 1263 | |
242922a0 FW |
1264 | hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) { |
1265 | tmp = nf_ct_tuplehash_to_ctrack(h); | |
9fb9cbb1 | 1266 | |
90964016 PNA |
1267 | if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) |
1268 | continue; | |
1269 | ||
f330a7fd FW |
1270 | if (nf_ct_is_expired(tmp)) { |
1271 | nf_ct_gc_expired(tmp); | |
1272 | continue; | |
1273 | } | |
1274 | ||
242922a0 FW |
1275 | if (test_bit(IPS_ASSURED_BIT, &tmp->status) || |
1276 | !net_eq(nf_ct_net(tmp), net) || | |
1277 | nf_ct_is_dying(tmp)) | |
1278 | continue; | |
76507f69 | 1279 | |
242922a0 FW |
1280 | if (!atomic_inc_not_zero(&tmp->ct_general.use)) |
1281 | continue; | |
76507f69 | 1282 | |
242922a0 | 1283 | /* kill only if still in same netns -- might have moved due to |
5f0d5a3a | 1284 | * SLAB_TYPESAFE_BY_RCU rules. |
242922a0 FW |
1285 | * |
1286 | * We steal the timer reference. If that fails timer has | |
1287 | * already fired or someone else deleted it. Just drop ref | |
1288 | * and move to next entry. | |
1289 | */ | |
1290 | if (net_eq(nf_ct_net(tmp), net) && | |
1291 | nf_ct_is_confirmed(tmp) && | |
242922a0 FW |
1292 | nf_ct_delete(tmp, 0, 0)) |
1293 | drops++; | |
1294 | ||
1295 | nf_ct_put(tmp); | |
9fb9cbb1 | 1296 | } |
3e86638e | 1297 | |
242922a0 FW |
1298 | return drops; |
1299 | } | |
9fb9cbb1 | 1300 | |
f393808d | 1301 | static noinline int early_drop(struct net *net, unsigned int hash) |
242922a0 | 1302 | { |
f393808d | 1303 | unsigned int i, bucket; |
9fb9cbb1 | 1304 | |
242922a0 FW |
1305 | for (i = 0; i < NF_CT_EVICTION_RANGE; i++) { |
1306 | struct hlist_nulls_head *ct_hash; | |
f393808d | 1307 | unsigned int hsize, drops; |
242922a0 | 1308 | |
3101e0fc | 1309 | rcu_read_lock(); |
92e47ba8 | 1310 | nf_conntrack_get_ht(&ct_hash, &hsize); |
f393808d VK |
1311 | if (!i) |
1312 | bucket = reciprocal_scale(hash, hsize); | |
1313 | else | |
1314 | bucket = (bucket + 1) % hsize; | |
242922a0 | 1315 | |
f393808d | 1316 | drops = early_drop_list(net, &ct_hash[bucket]); |
3101e0fc LZ |
1317 | rcu_read_unlock(); |
1318 | ||
242922a0 FW |
1319 | if (drops) { |
1320 | NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops); | |
1321 | return true; | |
74138511 | 1322 | } |
9fb9cbb1 | 1323 | } |
3e86638e | 1324 | |
242922a0 | 1325 | return false; |
9fb9cbb1 YK |
1326 | } |
1327 | ||
c6dd940b FW |
1328 | static bool gc_worker_skip_ct(const struct nf_conn *ct) |
1329 | { | |
1330 | return !nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct); | |
1331 | } | |
1332 | ||
1333 | static bool gc_worker_can_early_drop(const struct nf_conn *ct) | |
1334 | { | |
1335 | const struct nf_conntrack_l4proto *l4proto; | |
1336 | ||
1337 | if (!test_bit(IPS_ASSURED_BIT, &ct->status)) | |
1338 | return true; | |
1339 | ||
4a60dc74 | 1340 | l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); |
c6dd940b FW |
1341 | if (l4proto->can_early_drop && l4proto->can_early_drop(ct)) |
1342 | return true; | |
1343 | ||
1344 | return false; | |
1345 | } | |
1346 | ||
90964016 PNA |
1347 | #define DAY (86400 * HZ) |
1348 | ||
1349 | /* Set an arbitrary timeout large enough not to ever expire, this save | |
1350 | * us a check for the IPS_OFFLOAD_BIT from the packet path via | |
1351 | * nf_ct_is_expired(). | |
1352 | */ | |
1353 | static void nf_ct_offload_timeout(struct nf_conn *ct) | |
1354 | { | |
1355 | if (nf_ct_expires(ct) < DAY / 2) | |
1356 | ct->timeout = nfct_time_stamp + DAY; | |
1357 | } | |
1358 | ||
b87a2f91 FW |
1359 | static void gc_worker(struct work_struct *work) |
1360 | { | |
e5072053 | 1361 | unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u); |
b87a2f91 | 1362 | unsigned int i, goal, buckets = 0, expired_count = 0; |
c6dd940b | 1363 | unsigned int nf_conntrack_max95 = 0; |
b87a2f91 | 1364 | struct conntrack_gc_work *gc_work; |
e0df8cae FW |
1365 | unsigned int ratio, scanned = 0; |
1366 | unsigned long next_run; | |
b87a2f91 FW |
1367 | |
1368 | gc_work = container_of(work, struct conntrack_gc_work, dwork.work); | |
1369 | ||
e0df8cae | 1370 | goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV; |
b87a2f91 | 1371 | i = gc_work->last_bucket; |
c6dd940b FW |
1372 | if (gc_work->early_drop) |
1373 | nf_conntrack_max95 = nf_conntrack_max / 100u * 95u; | |
b87a2f91 FW |
1374 | |
1375 | do { | |
1376 | struct nf_conntrack_tuple_hash *h; | |
1377 | struct hlist_nulls_head *ct_hash; | |
1378 | struct hlist_nulls_node *n; | |
1379 | unsigned int hashsz; | |
1380 | struct nf_conn *tmp; | |
1381 | ||
1382 | i++; | |
1383 | rcu_read_lock(); | |
1384 | ||
1385 | nf_conntrack_get_ht(&ct_hash, &hashsz); | |
1386 | if (i >= hashsz) | |
1387 | i = 0; | |
1388 | ||
1389 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) { | |
c6dd940b FW |
1390 | struct net *net; |
1391 | ||
b87a2f91 FW |
1392 | tmp = nf_ct_tuplehash_to_ctrack(h); |
1393 | ||
c023c0e4 | 1394 | scanned++; |
90964016 PNA |
1395 | if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) { |
1396 | nf_ct_offload_timeout(tmp); | |
1397 | continue; | |
1398 | } | |
1399 | ||
b87a2f91 FW |
1400 | if (nf_ct_is_expired(tmp)) { |
1401 | nf_ct_gc_expired(tmp); | |
1402 | expired_count++; | |
1403 | continue; | |
1404 | } | |
c6dd940b FW |
1405 | |
1406 | if (nf_conntrack_max95 == 0 || gc_worker_skip_ct(tmp)) | |
1407 | continue; | |
1408 | ||
1409 | net = nf_ct_net(tmp); | |
1410 | if (atomic_read(&net->ct.count) < nf_conntrack_max95) | |
1411 | continue; | |
1412 | ||
1413 | /* need to take reference to avoid possible races */ | |
1414 | if (!atomic_inc_not_zero(&tmp->ct_general.use)) | |
1415 | continue; | |
1416 | ||
1417 | if (gc_worker_skip_ct(tmp)) { | |
1418 | nf_ct_put(tmp); | |
1419 | continue; | |
1420 | } | |
1421 | ||
1422 | if (gc_worker_can_early_drop(tmp)) | |
1423 | nf_ct_kill(tmp); | |
1424 | ||
1425 | nf_ct_put(tmp); | |
b87a2f91 FW |
1426 | } |
1427 | ||
1428 | /* could check get_nulls_value() here and restart if ct | |
1429 | * was moved to another chain. But given gc is best-effort | |
1430 | * we will just continue with next hash slot. | |
1431 | */ | |
1432 | rcu_read_unlock(); | |
ffa53c58 | 1433 | cond_resched(); |
524b698d | 1434 | } while (++buckets < goal); |
b87a2f91 FW |
1435 | |
1436 | if (gc_work->exiting) | |
1437 | return; | |
1438 | ||
e0df8cae FW |
1439 | /* |
1440 | * Eviction will normally happen from the packet path, and not | |
1441 | * from this gc worker. | |
1442 | * | |
1443 | * This worker is only here to reap expired entries when system went | |
1444 | * idle after a busy period. | |
1445 | * | |
1446 | * The heuristics below are supposed to balance conflicting goals: | |
1447 | * | |
1448 | * 1. Minimize time until we notice a stale entry | |
1449 | * 2. Maximize scan intervals to not waste cycles | |
1450 | * | |
e5072053 | 1451 | * Normally, expire ratio will be close to 0. |
e0df8cae | 1452 | * |
e5072053 FW |
1453 | * As soon as a sizeable fraction of the entries have expired |
1454 | * increase scan frequency. | |
e0df8cae | 1455 | */ |
c023c0e4 | 1456 | ratio = scanned ? expired_count * 100 / scanned : 0; |
e5072053 FW |
1457 | if (ratio > GC_EVICT_RATIO) { |
1458 | gc_work->next_gc_run = min_interval; | |
e0df8cae | 1459 | } else { |
e5072053 | 1460 | unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV; |
e0df8cae | 1461 | |
e5072053 FW |
1462 | BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0); |
1463 | ||
1464 | gc_work->next_gc_run += min_interval; | |
1465 | if (gc_work->next_gc_run > max) | |
1466 | gc_work->next_gc_run = max; | |
e0df8cae | 1467 | } |
c023c0e4 | 1468 | |
e5072053 | 1469 | next_run = gc_work->next_gc_run; |
b87a2f91 | 1470 | gc_work->last_bucket = i; |
c6dd940b | 1471 | gc_work->early_drop = false; |
0984d427 | 1472 | queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run); |
b87a2f91 FW |
1473 | } |
1474 | ||
1475 | static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work) | |
1476 | { | |
a232cd0e | 1477 | INIT_DEFERRABLE_WORK(&gc_work->dwork, gc_worker); |
e5072053 | 1478 | gc_work->next_gc_run = HZ; |
b87a2f91 FW |
1479 | gc_work->exiting = false; |
1480 | } | |
1481 | ||
99f07e91 | 1482 | static struct nf_conn * |
308ac914 DB |
1483 | __nf_conntrack_alloc(struct net *net, |
1484 | const struct nf_conntrack_zone *zone, | |
99f07e91 CG |
1485 | const struct nf_conntrack_tuple *orig, |
1486 | const struct nf_conntrack_tuple *repl, | |
1487 | gfp_t gfp, u32 hash) | |
9fb9cbb1 | 1488 | { |
cd7fcbf1 | 1489 | struct nf_conn *ct; |
9fb9cbb1 | 1490 | |
5251e2d2 | 1491 | /* We don't want any race condition at early drop stage */ |
49ac8713 | 1492 | atomic_inc(&net->ct.count); |
5251e2d2 | 1493 | |
76eb9460 | 1494 | if (nf_conntrack_max && |
49ac8713 | 1495 | unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) { |
93bb0ceb | 1496 | if (!early_drop(net, hash)) { |
c6dd940b FW |
1497 | if (!conntrack_gc_work.early_drop) |
1498 | conntrack_gc_work.early_drop = true; | |
49ac8713 | 1499 | atomic_dec(&net->ct.count); |
e87cc472 | 1500 | net_warn_ratelimited("nf_conntrack: table full, dropping packet\n"); |
9fb9cbb1 YK |
1501 | return ERR_PTR(-ENOMEM); |
1502 | } | |
1503 | } | |
1504 | ||
941297f4 ED |
1505 | /* |
1506 | * Do not use kmem_cache_zalloc(), as this cache uses | |
5f0d5a3a | 1507 | * SLAB_TYPESAFE_BY_RCU. |
941297f4 | 1508 | */ |
0c5366b3 | 1509 | ct = kmem_cache_alloc(nf_conntrack_cachep, gfp); |
5e8018fc DB |
1510 | if (ct == NULL) |
1511 | goto out; | |
1512 | ||
440f0d58 | 1513 | spin_lock_init(&ct->lock); |
c88130bc | 1514 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
941297f4 | 1515 | ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL; |
c88130bc | 1516 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; |
99f07e91 CG |
1517 | /* save hash for reusing when confirming */ |
1518 | *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash; | |
c41884ce | 1519 | ct->status = 0; |
8176c833 | 1520 | ct->timeout = 0; |
c2d9ba9b | 1521 | write_pnet(&ct->ct_net, net); |
c41884ce FW |
1522 | memset(&ct->__nfct_init_offset[0], 0, |
1523 | offsetof(struct nf_conn, proto) - | |
1524 | offsetof(struct nf_conn, __nfct_init_offset[0])); | |
5e8018fc | 1525 | |
6c8dee98 | 1526 | nf_ct_zone_add(ct, zone); |
5e8018fc | 1527 | |
e53376be PNA |
1528 | /* Because we use RCU lookups, we set ct_general.use to zero before |
1529 | * this is inserted in any list. | |
941297f4 | 1530 | */ |
e53376be | 1531 | atomic_set(&ct->ct_general.use, 0); |
c88130bc | 1532 | return ct; |
5e8018fc DB |
1533 | out: |
1534 | atomic_dec(&net->ct.count); | |
5d0aa2cc | 1535 | return ERR_PTR(-ENOMEM); |
9fb9cbb1 | 1536 | } |
99f07e91 | 1537 | |
308ac914 DB |
1538 | struct nf_conn *nf_conntrack_alloc(struct net *net, |
1539 | const struct nf_conntrack_zone *zone, | |
99f07e91 CG |
1540 | const struct nf_conntrack_tuple *orig, |
1541 | const struct nf_conntrack_tuple *repl, | |
1542 | gfp_t gfp) | |
1543 | { | |
1544 | return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0); | |
1545 | } | |
13b18339 | 1546 | EXPORT_SYMBOL_GPL(nf_conntrack_alloc); |
9fb9cbb1 | 1547 | |
c88130bc | 1548 | void nf_conntrack_free(struct nf_conn *ct) |
76507f69 | 1549 | { |
1d45209d ED |
1550 | struct net *net = nf_ct_net(ct); |
1551 | ||
e53376be | 1552 | /* A freed object has refcnt == 0, that's |
5f0d5a3a | 1553 | * the golden rule for SLAB_TYPESAFE_BY_RCU |
e53376be | 1554 | */ |
44d6e2f2 | 1555 | WARN_ON(atomic_read(&ct->ct_general.use) != 0); |
e53376be | 1556 | |
ceeff754 | 1557 | nf_ct_ext_destroy(ct); |
0c5366b3 | 1558 | kmem_cache_free(nf_conntrack_cachep, ct); |
4e857c58 | 1559 | smp_mb__before_atomic(); |
0c3c6c00 | 1560 | atomic_dec(&net->ct.count); |
76507f69 | 1561 | } |
13b18339 | 1562 | EXPORT_SYMBOL_GPL(nf_conntrack_free); |
9fb9cbb1 | 1563 | |
c539f017 | 1564 | |
9fb9cbb1 YK |
1565 | /* Allocate a new conntrack: we return -ENOMEM if classification |
1566 | failed due to stress. Otherwise it really is unclassifiable. */ | |
fc09e4a7 | 1567 | static noinline struct nf_conntrack_tuple_hash * |
b2a15a60 | 1568 | init_conntrack(struct net *net, struct nf_conn *tmpl, |
5a1fb391 | 1569 | const struct nf_conntrack_tuple *tuple, |
9fb9cbb1 | 1570 | struct sk_buff *skb, |
60b5f8f7 | 1571 | unsigned int dataoff, u32 hash) |
9fb9cbb1 | 1572 | { |
c88130bc | 1573 | struct nf_conn *ct; |
3c158f7f | 1574 | struct nf_conn_help *help; |
9fb9cbb1 | 1575 | struct nf_conntrack_tuple repl_tuple; |
b2a15a60 | 1576 | struct nf_conntrack_ecache *ecache; |
ca7433df | 1577 | struct nf_conntrack_expect *exp = NULL; |
308ac914 | 1578 | const struct nf_conntrack_zone *zone; |
60b5f8f7 | 1579 | struct nf_conn_timeout *timeout_ext; |
5e8018fc | 1580 | struct nf_conntrack_zone tmp; |
9fb9cbb1 | 1581 | |
303e0c55 | 1582 | if (!nf_ct_invert_tuple(&repl_tuple, tuple)) { |
0d53778e | 1583 | pr_debug("Can't invert tuple.\n"); |
9fb9cbb1 YK |
1584 | return NULL; |
1585 | } | |
1586 | ||
5e8018fc | 1587 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
99f07e91 CG |
1588 | ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC, |
1589 | hash); | |
0a9ee813 | 1590 | if (IS_ERR(ct)) |
c88130bc | 1591 | return (struct nf_conntrack_tuple_hash *)ct; |
9fb9cbb1 | 1592 | |
4440a2ab GF |
1593 | if (!nf_ct_add_synproxy(ct, tmpl)) { |
1594 | nf_conntrack_free(ct); | |
1595 | return ERR_PTR(-ENOMEM); | |
48b1de4c PM |
1596 | } |
1597 | ||
60b5f8f7 | 1598 | timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL; |
60b5f8f7 | 1599 | |
60b5f8f7 | 1600 | if (timeout_ext) |
ae2d708e PNA |
1601 | nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout), |
1602 | GFP_ATOMIC); | |
60b5f8f7 | 1603 | |
58401572 | 1604 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
a992ca2a | 1605 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
c539f017 | 1606 | nf_ct_labels_ext_add(ct); |
b2a15a60 PM |
1607 | |
1608 | ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL; | |
1609 | nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0, | |
1610 | ecache ? ecache->expmask : 0, | |
1611 | GFP_ATOMIC); | |
58401572 | 1612 | |
ca7433df JDB |
1613 | local_bh_disable(); |
1614 | if (net->ct.expect_count) { | |
1615 | spin_lock(&nf_conntrack_expect_lock); | |
1616 | exp = nf_ct_find_expectation(net, zone, tuple); | |
1617 | if (exp) { | |
ccd63c20 | 1618 | pr_debug("expectation arrives ct=%p exp=%p\n", |
ca7433df JDB |
1619 | ct, exp); |
1620 | /* Welcome, Mr. Bond. We've been expecting you... */ | |
1621 | __set_bit(IPS_EXPECTED_BIT, &ct->status); | |
1622 | /* exp->master safe, refcnt bumped in nf_ct_find_expectation */ | |
1623 | ct->master = exp->master; | |
1624 | if (exp->helper) { | |
440534d3 | 1625 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
ca7433df JDB |
1626 | if (help) |
1627 | rcu_assign_pointer(help->helper, exp->helper); | |
1628 | } | |
ceceae1b | 1629 | |
9fb9cbb1 | 1630 | #ifdef CONFIG_NF_CONNTRACK_MARK |
ca7433df | 1631 | ct->mark = exp->master->mark; |
7c9728c3 JM |
1632 | #endif |
1633 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
ca7433df | 1634 | ct->secmark = exp->master->secmark; |
9fb9cbb1 | 1635 | #endif |
ca7433df JDB |
1636 | NF_CT_STAT_INC(net, expect_new); |
1637 | } | |
1638 | spin_unlock(&nf_conntrack_expect_lock); | |
1639 | } | |
8e8118f8 | 1640 | if (!exp) |
b2a15a60 | 1641 | __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC); |
9fb9cbb1 | 1642 | |
e53376be PNA |
1643 | /* Now it is inserted into the unconfirmed list, bump refcount */ |
1644 | nf_conntrack_get(&ct->ct_general); | |
b7779d06 | 1645 | nf_ct_add_to_unconfirmed_list(ct); |
9fb9cbb1 | 1646 | |
ca7433df | 1647 | local_bh_enable(); |
9fb9cbb1 YK |
1648 | |
1649 | if (exp) { | |
1650 | if (exp->expectfn) | |
c88130bc | 1651 | exp->expectfn(ct, exp); |
6823645d | 1652 | nf_ct_expect_put(exp); |
9fb9cbb1 YK |
1653 | } |
1654 | ||
c88130bc | 1655 | return &ct->tuplehash[IP_CT_DIR_ORIGINAL]; |
9fb9cbb1 YK |
1656 | } |
1657 | ||
fc09e4a7 FW |
1658 | /* On success, returns 0, sets skb->_nfct | ctinfo */ |
1659 | static int | |
93e66024 | 1660 | resolve_normal_ct(struct nf_conn *tmpl, |
a702a65f | 1661 | struct sk_buff *skb, |
9fb9cbb1 | 1662 | unsigned int dataoff, |
9fb9cbb1 | 1663 | u_int8_t protonum, |
93e66024 | 1664 | const struct nf_hook_state *state) |
9fb9cbb1 | 1665 | { |
308ac914 | 1666 | const struct nf_conntrack_zone *zone; |
9fb9cbb1 YK |
1667 | struct nf_conntrack_tuple tuple; |
1668 | struct nf_conntrack_tuple_hash *h; | |
fc09e4a7 | 1669 | enum ip_conntrack_info ctinfo; |
5e8018fc | 1670 | struct nf_conntrack_zone tmp; |
9fb9cbb1 | 1671 | struct nf_conn *ct; |
99f07e91 | 1672 | u32 hash; |
9fb9cbb1 | 1673 | |
bbe735e4 | 1674 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), |
93e66024 | 1675 | dataoff, state->pf, protonum, state->net, |
303e0c55 | 1676 | &tuple)) { |
ccd63c20 | 1677 | pr_debug("Can't get tuple\n"); |
fc09e4a7 | 1678 | return 0; |
9fb9cbb1 YK |
1679 | } |
1680 | ||
1681 | /* look for tuple match */ | |
5e8018fc | 1682 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
93e66024 FW |
1683 | hash = hash_conntrack_raw(&tuple, state->net); |
1684 | h = __nf_conntrack_find_get(state->net, zone, &tuple, hash); | |
9fb9cbb1 | 1685 | if (!h) { |
303e0c55 | 1686 | h = init_conntrack(state->net, tmpl, &tuple, |
60b5f8f7 | 1687 | skb, dataoff, hash); |
9fb9cbb1 | 1688 | if (!h) |
fc09e4a7 | 1689 | return 0; |
9fb9cbb1 | 1690 | if (IS_ERR(h)) |
fc09e4a7 | 1691 | return PTR_ERR(h); |
9fb9cbb1 YK |
1692 | } |
1693 | ct = nf_ct_tuplehash_to_ctrack(h); | |
1694 | ||
1695 | /* It exists; we have (non-exclusive) reference. */ | |
1696 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { | |
fc09e4a7 | 1697 | ctinfo = IP_CT_ESTABLISHED_REPLY; |
9fb9cbb1 YK |
1698 | } else { |
1699 | /* Once we've had two way comms, always ESTABLISHED. */ | |
1700 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { | |
ccd63c20 | 1701 | pr_debug("normal packet for %p\n", ct); |
fc09e4a7 | 1702 | ctinfo = IP_CT_ESTABLISHED; |
9fb9cbb1 | 1703 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { |
ccd63c20 | 1704 | pr_debug("related packet for %p\n", ct); |
fc09e4a7 | 1705 | ctinfo = IP_CT_RELATED; |
9fb9cbb1 | 1706 | } else { |
ccd63c20 | 1707 | pr_debug("new packet for %p\n", ct); |
fc09e4a7 | 1708 | ctinfo = IP_CT_NEW; |
9fb9cbb1 | 1709 | } |
9fb9cbb1 | 1710 | } |
fc09e4a7 FW |
1711 | nf_ct_set(skb, ct, ctinfo); |
1712 | return 0; | |
9fb9cbb1 YK |
1713 | } |
1714 | ||
6fe78fa4 FW |
1715 | /* |
1716 | * icmp packets need special treatment to handle error messages that are | |
1717 | * related to a connection. | |
1718 | * | |
1719 | * Callers need to check if skb has a conntrack assigned when this | |
1720 | * helper returns; in such case skb belongs to an already known connection. | |
1721 | */ | |
1722 | static unsigned int __cold | |
1723 | nf_conntrack_handle_icmp(struct nf_conn *tmpl, | |
1724 | struct sk_buff *skb, | |
1725 | unsigned int dataoff, | |
1726 | u8 protonum, | |
1727 | const struct nf_hook_state *state) | |
1728 | { | |
1729 | int ret; | |
1730 | ||
1731 | if (state->pf == NFPROTO_IPV4 && protonum == IPPROTO_ICMP) | |
1732 | ret = nf_conntrack_icmpv4_error(tmpl, skb, dataoff, state); | |
1733 | #if IS_ENABLED(CONFIG_IPV6) | |
1734 | else if (state->pf == NFPROTO_IPV6 && protonum == IPPROTO_ICMPV6) | |
1735 | ret = nf_conntrack_icmpv6_error(tmpl, skb, dataoff, state); | |
1736 | #endif | |
1737 | else | |
1738 | return NF_ACCEPT; | |
1739 | ||
1740 | if (ret <= 0) { | |
1741 | NF_CT_STAT_INC_ATOMIC(state->net, error); | |
1742 | NF_CT_STAT_INC_ATOMIC(state->net, invalid); | |
1743 | } | |
1744 | ||
1745 | return ret; | |
1746 | } | |
1747 | ||
44fb87f6 FW |
1748 | static int generic_packet(struct nf_conn *ct, struct sk_buff *skb, |
1749 | enum ip_conntrack_info ctinfo) | |
1750 | { | |
1751 | const unsigned int *timeout = nf_ct_timeout_lookup(ct); | |
1752 | ||
1753 | if (!timeout) | |
1754 | timeout = &nf_generic_pernet(nf_ct_net(ct))->timeout; | |
1755 | ||
1756 | nf_ct_refresh_acct(ct, ctinfo, skb, *timeout); | |
1757 | return NF_ACCEPT; | |
1758 | } | |
1759 | ||
a47c5404 FW |
1760 | /* Returns verdict for packet, or -1 for invalid. */ |
1761 | static int nf_conntrack_handle_packet(struct nf_conn *ct, | |
1762 | struct sk_buff *skb, | |
1763 | unsigned int dataoff, | |
1764 | enum ip_conntrack_info ctinfo, | |
1765 | const struct nf_hook_state *state) | |
1766 | { | |
1767 | switch (nf_ct_protonum(ct)) { | |
1768 | case IPPROTO_TCP: | |
1769 | return nf_conntrack_tcp_packet(ct, skb, dataoff, | |
1770 | ctinfo, state); | |
1771 | case IPPROTO_UDP: | |
1772 | return nf_conntrack_udp_packet(ct, skb, dataoff, | |
1773 | ctinfo, state); | |
1774 | case IPPROTO_ICMP: | |
1775 | return nf_conntrack_icmp_packet(ct, skb, ctinfo, state); | |
81e01647 | 1776 | #if IS_ENABLED(CONFIG_IPV6) |
a47c5404 FW |
1777 | case IPPROTO_ICMPV6: |
1778 | return nf_conntrack_icmpv6_packet(ct, skb, ctinfo, state); | |
81e01647 | 1779 | #endif |
a47c5404 FW |
1780 | #ifdef CONFIG_NF_CT_PROTO_UDPLITE |
1781 | case IPPROTO_UDPLITE: | |
1782 | return nf_conntrack_udplite_packet(ct, skb, dataoff, | |
1783 | ctinfo, state); | |
1784 | #endif | |
1785 | #ifdef CONFIG_NF_CT_PROTO_SCTP | |
1786 | case IPPROTO_SCTP: | |
1787 | return nf_conntrack_sctp_packet(ct, skb, dataoff, | |
1788 | ctinfo, state); | |
1789 | #endif | |
1790 | #ifdef CONFIG_NF_CT_PROTO_DCCP | |
1791 | case IPPROTO_DCCP: | |
1792 | return nf_conntrack_dccp_packet(ct, skb, dataoff, | |
1793 | ctinfo, state); | |
44fb87f6 FW |
1794 | #endif |
1795 | #ifdef CONFIG_NF_CT_PROTO_GRE | |
1796 | case IPPROTO_GRE: | |
1797 | return nf_conntrack_gre_packet(ct, skb, dataoff, | |
1798 | ctinfo, state); | |
a47c5404 FW |
1799 | #endif |
1800 | } | |
1801 | ||
44fb87f6 | 1802 | return generic_packet(ct, skb, ctinfo); |
a47c5404 FW |
1803 | } |
1804 | ||
9fb9cbb1 | 1805 | unsigned int |
93e66024 | 1806 | nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state) |
9fb9cbb1 | 1807 | { |
9fb9cbb1 | 1808 | enum ip_conntrack_info ctinfo; |
93e66024 | 1809 | struct nf_conn *ct, *tmpl; |
9fb9cbb1 | 1810 | u_int8_t protonum; |
6816d931 | 1811 | int dataoff, ret; |
9fb9cbb1 | 1812 | |
97a6ad13 | 1813 | tmpl = nf_ct_get(skb, &ctinfo); |
cc41c84b | 1814 | if (tmpl || ctinfo == IP_CT_UNTRACKED) { |
b2a15a60 | 1815 | /* Previously seen (loopback or untracked)? Ignore. */ |
cc41c84b FW |
1816 | if ((tmpl && !nf_ct_is_template(tmpl)) || |
1817 | ctinfo == IP_CT_UNTRACKED) { | |
93e66024 | 1818 | NF_CT_STAT_INC_ATOMIC(state->net, ignore); |
b2a15a60 PM |
1819 | return NF_ACCEPT; |
1820 | } | |
a9e419dc | 1821 | skb->_nfct = 0; |
9fb9cbb1 YK |
1822 | } |
1823 | ||
e2361cb9 | 1824 | /* rcu_read_lock()ed by nf_hook_thresh */ |
93e66024 | 1825 | dataoff = get_l4proto(skb, skb_network_offset(skb), state->pf, &protonum); |
6816d931 | 1826 | if (dataoff <= 0) { |
25985edc | 1827 | pr_debug("not prepared to track yet or error occurred\n"); |
93e66024 FW |
1828 | NF_CT_STAT_INC_ATOMIC(state->net, error); |
1829 | NF_CT_STAT_INC_ATOMIC(state->net, invalid); | |
6816d931 | 1830 | ret = NF_ACCEPT; |
b2a15a60 | 1831 | goto out; |
9fb9cbb1 YK |
1832 | } |
1833 | ||
6fe78fa4 FW |
1834 | if (protonum == IPPROTO_ICMP || protonum == IPPROTO_ICMPV6) { |
1835 | ret = nf_conntrack_handle_icmp(tmpl, skb, dataoff, | |
1836 | protonum, state); | |
74c51a14 | 1837 | if (ret <= 0) { |
b2a15a60 PM |
1838 | ret = -ret; |
1839 | goto out; | |
74c51a14 | 1840 | } |
88ed01d1 | 1841 | /* ICMP[v6] protocol trackers may assign one conntrack. */ |
a9e419dc | 1842 | if (skb->_nfct) |
88ed01d1 | 1843 | goto out; |
9fb9cbb1 | 1844 | } |
08733a0c | 1845 | repeat: |
93e66024 | 1846 | ret = resolve_normal_ct(tmpl, skb, dataoff, |
303e0c55 | 1847 | protonum, state); |
fc09e4a7 | 1848 | if (ret < 0) { |
9fb9cbb1 | 1849 | /* Too stressed to deal. */ |
93e66024 | 1850 | NF_CT_STAT_INC_ATOMIC(state->net, drop); |
b2a15a60 PM |
1851 | ret = NF_DROP; |
1852 | goto out; | |
9fb9cbb1 YK |
1853 | } |
1854 | ||
fc09e4a7 FW |
1855 | ct = nf_ct_get(skb, &ctinfo); |
1856 | if (!ct) { | |
1857 | /* Not valid part of a connection */ | |
93e66024 | 1858 | NF_CT_STAT_INC_ATOMIC(state->net, invalid); |
fc09e4a7 FW |
1859 | ret = NF_ACCEPT; |
1860 | goto out; | |
1861 | } | |
9fb9cbb1 | 1862 | |
44fb87f6 | 1863 | ret = nf_conntrack_handle_packet(ct, skb, dataoff, ctinfo, state); |
ec8d5409 | 1864 | if (ret <= 0) { |
9fb9cbb1 YK |
1865 | /* Invalid: inverse of the return code tells |
1866 | * the netfilter core what to do */ | |
0d53778e | 1867 | pr_debug("nf_conntrack_in: Can't track with proto module\n"); |
97a6ad13 | 1868 | nf_conntrack_put(&ct->ct_general); |
a9e419dc | 1869 | skb->_nfct = 0; |
93e66024 | 1870 | NF_CT_STAT_INC_ATOMIC(state->net, invalid); |
7d1e0459 | 1871 | if (ret == -NF_DROP) |
93e66024 | 1872 | NF_CT_STAT_INC_ATOMIC(state->net, drop); |
56a62e22 AB |
1873 | /* Special case: TCP tracker reports an attempt to reopen a |
1874 | * closed/aborted connection. We have to go back and create a | |
1875 | * fresh conntrack. | |
1876 | */ | |
1877 | if (ret == -NF_REPEAT) | |
1878 | goto repeat; | |
b2a15a60 PM |
1879 | ret = -ret; |
1880 | goto out; | |
9fb9cbb1 YK |
1881 | } |
1882 | ||
fc09e4a7 FW |
1883 | if (ctinfo == IP_CT_ESTABLISHED_REPLY && |
1884 | !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
858b3133 | 1885 | nf_conntrack_event_cache(IPCT_REPLY, ct); |
b2a15a60 | 1886 | out: |
56a62e22 AB |
1887 | if (tmpl) |
1888 | nf_ct_put(tmpl); | |
9fb9cbb1 YK |
1889 | |
1890 | return ret; | |
1891 | } | |
13b18339 | 1892 | EXPORT_SYMBOL_GPL(nf_conntrack_in); |
9fb9cbb1 | 1893 | |
5b1158e9 JK |
1894 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
1895 | implicitly racy: see __nf_conntrack_confirm */ | |
1896 | void nf_conntrack_alter_reply(struct nf_conn *ct, | |
1897 | const struct nf_conntrack_tuple *newreply) | |
1898 | { | |
1899 | struct nf_conn_help *help = nfct_help(ct); | |
1900 | ||
5b1158e9 | 1901 | /* Should be unconfirmed, so not in hash table yet */ |
44d6e2f2 | 1902 | WARN_ON(nf_ct_is_confirmed(ct)); |
5b1158e9 | 1903 | |
0d53778e | 1904 | pr_debug("Altering reply tuple of %p to ", ct); |
3c9fba65 | 1905 | nf_ct_dump_tuple(newreply); |
5b1158e9 JK |
1906 | |
1907 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; | |
ef1a5a50 | 1908 | if (ct->master || (help && !hlist_empty(&help->expectations))) |
c52fbb41 | 1909 | return; |
ceceae1b | 1910 | |
c52fbb41 | 1911 | rcu_read_lock(); |
b2a15a60 | 1912 | __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
c52fbb41 | 1913 | rcu_read_unlock(); |
5b1158e9 | 1914 | } |
13b18339 | 1915 | EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply); |
5b1158e9 | 1916 | |
9fb9cbb1 YK |
1917 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
1918 | void __nf_ct_refresh_acct(struct nf_conn *ct, | |
1919 | enum ip_conntrack_info ctinfo, | |
1920 | const struct sk_buff *skb, | |
cc169213 FW |
1921 | u32 extra_jiffies, |
1922 | bool do_acct) | |
9fb9cbb1 | 1923 | { |
997ae831 | 1924 | /* Only update if this is not a fixed timeout */ |
47d95045 PM |
1925 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) |
1926 | goto acct; | |
997ae831 | 1927 | |
9fb9cbb1 | 1928 | /* If not in hash table, timer will not be active yet */ |
f330a7fd FW |
1929 | if (nf_ct_is_confirmed(ct)) |
1930 | extra_jiffies += nfct_time_stamp; | |
9fb9cbb1 | 1931 | |
e37542ba ED |
1932 | if (READ_ONCE(ct->timeout) != extra_jiffies) |
1933 | WRITE_ONCE(ct->timeout, extra_jiffies); | |
47d95045 | 1934 | acct: |
ba76738c | 1935 | if (do_acct) |
8ac2bd35 | 1936 | nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len); |
9fb9cbb1 | 1937 | } |
13b18339 | 1938 | EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct); |
9fb9cbb1 | 1939 | |
ad66713f FW |
1940 | bool nf_ct_kill_acct(struct nf_conn *ct, |
1941 | enum ip_conntrack_info ctinfo, | |
1942 | const struct sk_buff *skb) | |
51091764 | 1943 | { |
8ac2bd35 | 1944 | nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len); |
58401572 | 1945 | |
f330a7fd | 1946 | return nf_ct_delete(ct, 0, 0); |
51091764 | 1947 | } |
ad66713f | 1948 | EXPORT_SYMBOL_GPL(nf_ct_kill_acct); |
51091764 | 1949 | |
c0cd1156 | 1950 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c1d10adb PNA |
1951 | |
1952 | #include <linux/netfilter/nfnetlink.h> | |
1953 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
57b47a53 IM |
1954 | #include <linux/mutex.h> |
1955 | ||
05ba4c89 | 1956 | /* Generic function for tcp/udp/sctp/dccp and alike. */ |
fdf70832 | 1957 | int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
1958 | const struct nf_conntrack_tuple *tuple) |
1959 | { | |
bae65be8 DM |
1960 | if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) || |
1961 | nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port)) | |
1962 | goto nla_put_failure; | |
c1d10adb PNA |
1963 | return 0; |
1964 | ||
df6fb868 | 1965 | nla_put_failure: |
c1d10adb PNA |
1966 | return -1; |
1967 | } | |
fdf70832 | 1968 | EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr); |
c1d10adb | 1969 | |
f73e924c PM |
1970 | const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = { |
1971 | [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 }, | |
1972 | [CTA_PROTO_DST_PORT] = { .type = NLA_U16 }, | |
c1d10adb | 1973 | }; |
f73e924c | 1974 | EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy); |
c1d10adb | 1975 | |
fdf70832 | 1976 | int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
1977 | struct nf_conntrack_tuple *t) |
1978 | { | |
df6fb868 | 1979 | if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT]) |
c1d10adb PNA |
1980 | return -EINVAL; |
1981 | ||
77236b6e PM |
1982 | t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]); |
1983 | t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]); | |
c1d10adb PNA |
1984 | |
1985 | return 0; | |
1986 | } | |
fdf70832 | 1987 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple); |
5c0de29d | 1988 | |
5caaed15 | 1989 | unsigned int nf_ct_port_nlattr_tuple_size(void) |
5c0de29d | 1990 | { |
5caaed15 FW |
1991 | static unsigned int size __read_mostly; |
1992 | ||
1993 | if (!size) | |
1994 | size = nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1); | |
1995 | ||
1996 | return size; | |
5c0de29d HE |
1997 | } |
1998 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size); | |
c1d10adb PNA |
1999 | #endif |
2000 | ||
9fb9cbb1 | 2001 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
312a0c16 | 2002 | static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb) |
9fb9cbb1 YK |
2003 | { |
2004 | struct nf_conn *ct; | |
2005 | enum ip_conntrack_info ctinfo; | |
2006 | ||
2007 | /* This ICMP is in reverse direction to the packet which caused it */ | |
2008 | ct = nf_ct_get(skb, &ctinfo); | |
2009 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) | |
fb048833 | 2010 | ctinfo = IP_CT_RELATED_REPLY; |
9fb9cbb1 YK |
2011 | else |
2012 | ctinfo = IP_CT_RELATED; | |
2013 | ||
2014 | /* Attach to new skbuff, and increment count */ | |
c74454fa | 2015 | nf_ct_set(nskb, ct, ctinfo); |
cb9c6836 | 2016 | nf_conntrack_get(skb_nfct(nskb)); |
9fb9cbb1 YK |
2017 | } |
2018 | ||
368982cd PNA |
2019 | static int nf_conntrack_update(struct net *net, struct sk_buff *skb) |
2020 | { | |
368982cd PNA |
2021 | struct nf_conntrack_tuple_hash *h; |
2022 | struct nf_conntrack_tuple tuple; | |
2023 | enum ip_conntrack_info ctinfo; | |
2024 | struct nf_nat_hook *nat_hook; | |
6816d931 | 2025 | unsigned int status; |
368982cd | 2026 | struct nf_conn *ct; |
6816d931 | 2027 | int dataoff; |
368982cd PNA |
2028 | u16 l3num; |
2029 | u8 l4num; | |
2030 | ||
2031 | ct = nf_ct_get(skb, &ctinfo); | |
2032 | if (!ct || nf_ct_is_confirmed(ct)) | |
2033 | return 0; | |
2034 | ||
2035 | l3num = nf_ct_l3num(ct); | |
368982cd | 2036 | |
6816d931 FW |
2037 | dataoff = get_l4proto(skb, skb_network_offset(skb), l3num, &l4num); |
2038 | if (dataoff <= 0) | |
368982cd PNA |
2039 | return -1; |
2040 | ||
368982cd | 2041 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num, |
303e0c55 | 2042 | l4num, net, &tuple)) |
368982cd PNA |
2043 | return -1; |
2044 | ||
2045 | if (ct->status & IPS_SRC_NAT) { | |
2046 | memcpy(tuple.src.u3.all, | |
2047 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.all, | |
2048 | sizeof(tuple.src.u3.all)); | |
2049 | tuple.src.u.all = | |
2050 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all; | |
2051 | } | |
2052 | ||
2053 | if (ct->status & IPS_DST_NAT) { | |
2054 | memcpy(tuple.dst.u3.all, | |
2055 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.all, | |
2056 | sizeof(tuple.dst.u3.all)); | |
2057 | tuple.dst.u.all = | |
2058 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all; | |
2059 | } | |
2060 | ||
2061 | h = nf_conntrack_find_get(net, nf_ct_zone(ct), &tuple); | |
2062 | if (!h) | |
2063 | return 0; | |
2064 | ||
2065 | /* Store status bits of the conntrack that is clashing to re-do NAT | |
2066 | * mangling according to what it has been done already to this packet. | |
2067 | */ | |
2068 | status = ct->status; | |
2069 | ||
2070 | nf_ct_put(ct); | |
2071 | ct = nf_ct_tuplehash_to_ctrack(h); | |
2072 | nf_ct_set(skb, ct, ctinfo); | |
2073 | ||
2074 | nat_hook = rcu_dereference(nf_nat_hook); | |
2075 | if (!nat_hook) | |
2076 | return 0; | |
2077 | ||
2078 | if (status & IPS_SRC_NAT && | |
2079 | nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_SRC, | |
2080 | IP_CT_DIR_ORIGINAL) == NF_DROP) | |
2081 | return -1; | |
2082 | ||
2083 | if (status & IPS_DST_NAT && | |
2084 | nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_DST, | |
2085 | IP_CT_DIR_ORIGINAL) == NF_DROP) | |
2086 | return -1; | |
2087 | ||
2088 | return 0; | |
2089 | } | |
2090 | ||
b60a6040 THJ |
2091 | static bool nf_conntrack_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple, |
2092 | const struct sk_buff *skb) | |
2093 | { | |
2094 | const struct nf_conntrack_tuple *src_tuple; | |
2095 | const struct nf_conntrack_tuple_hash *hash; | |
2096 | struct nf_conntrack_tuple srctuple; | |
2097 | enum ip_conntrack_info ctinfo; | |
2098 | struct nf_conn *ct; | |
2099 | ||
2100 | ct = nf_ct_get(skb, &ctinfo); | |
2101 | if (ct) { | |
2102 | src_tuple = nf_ct_tuple(ct, CTINFO2DIR(ctinfo)); | |
2103 | memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple)); | |
2104 | return true; | |
2105 | } | |
2106 | ||
2107 | if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), | |
2108 | NFPROTO_IPV4, dev_net(skb->dev), | |
2109 | &srctuple)) | |
2110 | return false; | |
2111 | ||
2112 | hash = nf_conntrack_find_get(dev_net(skb->dev), | |
2113 | &nf_ct_zone_dflt, | |
2114 | &srctuple); | |
2115 | if (!hash) | |
2116 | return false; | |
2117 | ||
2118 | ct = nf_ct_tuplehash_to_ctrack(hash); | |
2119 | src_tuple = nf_ct_tuple(ct, !hash->tuple.dst.dir); | |
2120 | memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple)); | |
2121 | nf_ct_put(ct); | |
2122 | ||
2123 | return true; | |
2124 | } | |
2125 | ||
9fb9cbb1 | 2126 | /* Bring out ya dead! */ |
df0933dc | 2127 | static struct nf_conn * |
2843fb69 | 2128 | get_next_corpse(int (*iter)(struct nf_conn *i, void *data), |
9fb9cbb1 YK |
2129 | void *data, unsigned int *bucket) |
2130 | { | |
df0933dc PM |
2131 | struct nf_conntrack_tuple_hash *h; |
2132 | struct nf_conn *ct; | |
ea781f19 | 2133 | struct hlist_nulls_node *n; |
93bb0ceb | 2134 | spinlock_t *lockp; |
9fb9cbb1 | 2135 | |
56d52d48 | 2136 | for (; *bucket < nf_conntrack_htable_size; (*bucket)++) { |
93bb0ceb JDB |
2137 | lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS]; |
2138 | local_bh_disable(); | |
b16c2919 | 2139 | nf_conntrack_lock(lockp); |
56d52d48 FW |
2140 | if (*bucket < nf_conntrack_htable_size) { |
2141 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) { | |
93bb0ceb JDB |
2142 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
2143 | continue; | |
2144 | ct = nf_ct_tuplehash_to_ctrack(h); | |
2843fb69 | 2145 | if (iter(ct, data)) |
93bb0ceb JDB |
2146 | goto found; |
2147 | } | |
df0933dc | 2148 | } |
93bb0ceb JDB |
2149 | spin_unlock(lockp); |
2150 | local_bh_enable(); | |
d93c6258 | 2151 | cond_resched(); |
601e68e1 | 2152 | } |
b7779d06 | 2153 | |
b0feacaa FW |
2154 | return NULL; |
2155 | found: | |
2156 | atomic_inc(&ct->ct_general.use); | |
2157 | spin_unlock(lockp); | |
2158 | local_bh_enable(); | |
2159 | return ct; | |
2160 | } | |
2161 | ||
2843fb69 FW |
2162 | static void nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), |
2163 | void *data, u32 portid, int report) | |
2164 | { | |
0d02d564 | 2165 | unsigned int bucket = 0, sequence; |
2843fb69 | 2166 | struct nf_conn *ct; |
2843fb69 FW |
2167 | |
2168 | might_sleep(); | |
2169 | ||
0d02d564 FW |
2170 | for (;;) { |
2171 | sequence = read_seqcount_begin(&nf_conntrack_generation); | |
2843fb69 | 2172 | |
0d02d564 FW |
2173 | while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) { |
2174 | /* Time to push up daises... */ | |
2175 | ||
2176 | nf_ct_delete(ct, portid, report); | |
2177 | nf_ct_put(ct); | |
2178 | cond_resched(); | |
2179 | } | |
2180 | ||
2181 | if (!read_seqcount_retry(&nf_conntrack_generation, sequence)) | |
2182 | break; | |
2183 | bucket = 0; | |
2843fb69 FW |
2184 | } |
2185 | } | |
2186 | ||
2187 | struct iter_data { | |
2188 | int (*iter)(struct nf_conn *i, void *data); | |
2189 | void *data; | |
2190 | struct net *net; | |
2191 | }; | |
2192 | ||
2193 | static int iter_net_only(struct nf_conn *i, void *data) | |
2194 | { | |
2195 | struct iter_data *d = data; | |
2196 | ||
2197 | if (!net_eq(d->net, nf_ct_net(i))) | |
2198 | return 0; | |
2199 | ||
2200 | return d->iter(i, d->data); | |
2201 | } | |
2202 | ||
b0feacaa FW |
2203 | static void |
2204 | __nf_ct_unconfirmed_destroy(struct net *net) | |
2205 | { | |
2206 | int cpu; | |
2207 | ||
b7779d06 | 2208 | for_each_possible_cpu(cpu) { |
b0feacaa FW |
2209 | struct nf_conntrack_tuple_hash *h; |
2210 | struct hlist_nulls_node *n; | |
2211 | struct ct_pcpu *pcpu; | |
2212 | ||
2213 | pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
b7779d06 JDB |
2214 | |
2215 | spin_lock_bh(&pcpu->lock); | |
2216 | hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) { | |
b0feacaa FW |
2217 | struct nf_conn *ct; |
2218 | ||
b7779d06 | 2219 | ct = nf_ct_tuplehash_to_ctrack(h); |
b0feacaa FW |
2220 | |
2221 | /* we cannot call iter() on unconfirmed list, the | |
2222 | * owning cpu can reallocate ct->ext at any time. | |
2223 | */ | |
2224 | set_bit(IPS_DYING_BIT, &ct->status); | |
b7779d06 JDB |
2225 | } |
2226 | spin_unlock_bh(&pcpu->lock); | |
d93c6258 | 2227 | cond_resched(); |
b7779d06 | 2228 | } |
9fb9cbb1 YK |
2229 | } |
2230 | ||
84657984 FW |
2231 | void nf_ct_unconfirmed_destroy(struct net *net) |
2232 | { | |
2233 | might_sleep(); | |
2234 | ||
2235 | if (atomic_read(&net->ct.count) > 0) { | |
2236 | __nf_ct_unconfirmed_destroy(net); | |
e2a75007 | 2237 | nf_queue_nf_hook_drop(net); |
84657984 FW |
2238 | synchronize_net(); |
2239 | } | |
2240 | } | |
2241 | EXPORT_SYMBOL_GPL(nf_ct_unconfirmed_destroy); | |
2242 | ||
9fd6452d FW |
2243 | void nf_ct_iterate_cleanup_net(struct net *net, |
2244 | int (*iter)(struct nf_conn *i, void *data), | |
2245 | void *data, u32 portid, int report) | |
9fb9cbb1 | 2246 | { |
2843fb69 | 2247 | struct iter_data d; |
9fb9cbb1 | 2248 | |
d93c6258 FW |
2249 | might_sleep(); |
2250 | ||
88b68bc5 FW |
2251 | if (atomic_read(&net->ct.count) == 0) |
2252 | return; | |
2253 | ||
2843fb69 FW |
2254 | d.iter = iter; |
2255 | d.data = data; | |
2256 | d.net = net; | |
2257 | ||
2843fb69 FW |
2258 | nf_ct_iterate_cleanup(iter_net_only, &d, portid, report); |
2259 | } | |
2260 | EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup_net); | |
9fb9cbb1 | 2261 | |
2843fb69 FW |
2262 | /** |
2263 | * nf_ct_iterate_destroy - destroy unconfirmed conntracks and iterate table | |
2264 | * @iter: callback to invoke for each conntrack | |
2265 | * @data: data to pass to @iter | |
2266 | * | |
2267 | * Like nf_ct_iterate_cleanup, but first marks conntracks on the | |
2268 | * unconfirmed list as dying (so they will not be inserted into | |
2269 | * main table). | |
7866cc57 FW |
2270 | * |
2271 | * Can only be called in module exit path. | |
2843fb69 FW |
2272 | */ |
2273 | void | |
2274 | nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data) | |
2275 | { | |
2276 | struct net *net; | |
2277 | ||
f0b07bb1 | 2278 | down_read(&net_rwsem); |
2843fb69 FW |
2279 | for_each_net(net) { |
2280 | if (atomic_read(&net->ct.count) == 0) | |
2281 | continue; | |
2282 | __nf_ct_unconfirmed_destroy(net); | |
e2a75007 | 2283 | nf_queue_nf_hook_drop(net); |
9fb9cbb1 | 2284 | } |
f0b07bb1 | 2285 | up_read(&net_rwsem); |
2843fb69 | 2286 | |
7866cc57 FW |
2287 | /* Need to wait for netns cleanup worker to finish, if its |
2288 | * running -- it might have deleted a net namespace from | |
2289 | * the global list, so our __nf_ct_unconfirmed_destroy() might | |
2290 | * not have affected all namespaces. | |
2291 | */ | |
2292 | net_ns_barrier(); | |
2293 | ||
2843fb69 FW |
2294 | /* a conntrack could have been unlinked from unconfirmed list |
2295 | * before we grabbed pcpu lock in __nf_ct_unconfirmed_destroy(). | |
2296 | * This makes sure its inserted into conntrack table. | |
2297 | */ | |
2298 | synchronize_net(); | |
2299 | ||
2300 | nf_ct_iterate_cleanup(iter, data, 0, 0); | |
9fb9cbb1 | 2301 | } |
2843fb69 | 2302 | EXPORT_SYMBOL_GPL(nf_ct_iterate_destroy); |
9fb9cbb1 | 2303 | |
274d383b PNA |
2304 | static int kill_all(struct nf_conn *i, void *data) |
2305 | { | |
2843fb69 | 2306 | return net_eq(nf_ct_net(i), data); |
274d383b PNA |
2307 | } |
2308 | ||
f94161c1 | 2309 | void nf_conntrack_cleanup_start(void) |
9fb9cbb1 | 2310 | { |
b87a2f91 | 2311 | conntrack_gc_work.exiting = true; |
f94161c1 G |
2312 | RCU_INIT_POINTER(ip_ct_attach, NULL); |
2313 | } | |
2314 | ||
2315 | void nf_conntrack_cleanup_end(void) | |
2316 | { | |
1f4b2439 | 2317 | RCU_INIT_POINTER(nf_ct_hook, NULL); |
b87a2f91 | 2318 | cancel_delayed_work_sync(&conntrack_gc_work.dwork); |
285189c7 | 2319 | kvfree(nf_conntrack_hash); |
56d52d48 | 2320 | |
04d87001 | 2321 | nf_conntrack_proto_fini(); |
41d73ec0 | 2322 | nf_conntrack_seqadj_fini(); |
5f69b8f5 | 2323 | nf_conntrack_labels_fini(); |
5e615b22 | 2324 | nf_conntrack_helper_fini(); |
8684094c | 2325 | nf_conntrack_timeout_fini(); |
3fe0f943 | 2326 | nf_conntrack_ecache_fini(); |
73f4001a | 2327 | nf_conntrack_tstamp_fini(); |
b7ff3a1f | 2328 | nf_conntrack_acct_fini(); |
83b4dbe1 | 2329 | nf_conntrack_expect_fini(); |
77571149 FW |
2330 | |
2331 | kmem_cache_destroy(nf_conntrack_cachep); | |
08f6547d | 2332 | } |
9fb9cbb1 | 2333 | |
f94161c1 G |
2334 | /* |
2335 | * Mishearing the voices in his head, our hero wonders how he's | |
2336 | * supposed to kill the mall. | |
2337 | */ | |
2338 | void nf_conntrack_cleanup_net(struct net *net) | |
08f6547d | 2339 | { |
dece40e8 VD |
2340 | LIST_HEAD(single); |
2341 | ||
2342 | list_add(&net->exit_list, &single); | |
2343 | nf_conntrack_cleanup_net_list(&single); | |
2344 | } | |
2345 | ||
2346 | void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list) | |
2347 | { | |
2348 | int busy; | |
2349 | struct net *net; | |
2350 | ||
f94161c1 G |
2351 | /* |
2352 | * This makes sure all current packets have passed through | |
2353 | * netfilter framework. Roll on, two-stage module | |
2354 | * delete... | |
2355 | */ | |
2356 | synchronize_net(); | |
dece40e8 VD |
2357 | i_see_dead_people: |
2358 | busy = 0; | |
2359 | list_for_each_entry(net, net_exit_list, exit_list) { | |
2843fb69 | 2360 | nf_ct_iterate_cleanup(kill_all, net, 0, 0); |
dece40e8 VD |
2361 | if (atomic_read(&net->ct.count) != 0) |
2362 | busy = 1; | |
2363 | } | |
2364 | if (busy) { | |
9fb9cbb1 YK |
2365 | schedule(); |
2366 | goto i_see_dead_people; | |
2367 | } | |
2368 | ||
dece40e8 | 2369 | list_for_each_entry(net, net_exit_list, exit_list) { |
dece40e8 | 2370 | nf_conntrack_proto_pernet_fini(net); |
dece40e8 | 2371 | nf_conntrack_ecache_pernet_fini(net); |
dece40e8 | 2372 | nf_conntrack_expect_pernet_fini(net); |
dece40e8 | 2373 | free_percpu(net->ct.stat); |
b7779d06 | 2374 | free_percpu(net->ct.pcpu_lists); |
dece40e8 | 2375 | } |
08f6547d AD |
2376 | } |
2377 | ||
d862a662 | 2378 | void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) |
9fb9cbb1 | 2379 | { |
ea781f19 ED |
2380 | struct hlist_nulls_head *hash; |
2381 | unsigned int nr_slots, i; | |
9fb9cbb1 | 2382 | |
9cc1c73a FW |
2383 | if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head))) |
2384 | return NULL; | |
2385 | ||
ea781f19 ED |
2386 | BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head)); |
2387 | nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head)); | |
9cc1c73a | 2388 | |
b9e0102a | 2389 | hash = kvcalloc(nr_slots, sizeof(struct hlist_nulls_head), GFP_KERNEL); |
9fb9cbb1 | 2390 | |
ea781f19 ED |
2391 | if (hash && nulls) |
2392 | for (i = 0; i < nr_slots; i++) | |
2393 | INIT_HLIST_NULLS_HEAD(&hash[i], i); | |
9fb9cbb1 YK |
2394 | |
2395 | return hash; | |
2396 | } | |
ac565e5f | 2397 | EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable); |
9fb9cbb1 | 2398 | |
3183ab89 | 2399 | int nf_conntrack_hash_resize(unsigned int hashsize) |
9fb9cbb1 | 2400 | { |
3183ab89 FW |
2401 | int i, bucket; |
2402 | unsigned int old_size; | |
ea781f19 | 2403 | struct hlist_nulls_head *hash, *old_hash; |
9fb9cbb1 | 2404 | struct nf_conntrack_tuple_hash *h; |
5d0aa2cc | 2405 | struct nf_conn *ct; |
9fb9cbb1 | 2406 | |
9fb9cbb1 YK |
2407 | if (!hashsize) |
2408 | return -EINVAL; | |
2409 | ||
d862a662 | 2410 | hash = nf_ct_alloc_hashtable(&hashsize, 1); |
9fb9cbb1 YK |
2411 | if (!hash) |
2412 | return -ENOMEM; | |
2413 | ||
3183ab89 FW |
2414 | old_size = nf_conntrack_htable_size; |
2415 | if (old_size == hashsize) { | |
285189c7 | 2416 | kvfree(hash); |
3183ab89 FW |
2417 | return 0; |
2418 | } | |
2419 | ||
93bb0ceb JDB |
2420 | local_bh_disable(); |
2421 | nf_conntrack_all_lock(); | |
a3efd812 | 2422 | write_seqcount_begin(&nf_conntrack_generation); |
93bb0ceb | 2423 | |
76507f69 PM |
2424 | /* Lookups in the old hash might happen in parallel, which means we |
2425 | * might get false negatives during connection lookup. New connections | |
2426 | * created because of a false negative won't make it into the hash | |
93bb0ceb | 2427 | * though since that required taking the locks. |
76507f69 | 2428 | */ |
93bb0ceb | 2429 | |
56d52d48 FW |
2430 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
2431 | while (!hlist_nulls_empty(&nf_conntrack_hash[i])) { | |
2432 | h = hlist_nulls_entry(nf_conntrack_hash[i].first, | |
2433 | struct nf_conntrack_tuple_hash, hnnode); | |
5d0aa2cc | 2434 | ct = nf_ct_tuplehash_to_ctrack(h); |
ea781f19 | 2435 | hlist_nulls_del_rcu(&h->hnnode); |
1b8c8a9f FW |
2436 | bucket = __hash_conntrack(nf_ct_net(ct), |
2437 | &h->tuple, hashsize); | |
ea781f19 | 2438 | hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]); |
9fb9cbb1 YK |
2439 | } |
2440 | } | |
56d52d48 FW |
2441 | old_size = nf_conntrack_htable_size; |
2442 | old_hash = nf_conntrack_hash; | |
9fb9cbb1 | 2443 | |
56d52d48 FW |
2444 | nf_conntrack_hash = hash; |
2445 | nf_conntrack_htable_size = hashsize; | |
93bb0ceb | 2446 | |
a3efd812 | 2447 | write_seqcount_end(&nf_conntrack_generation); |
93bb0ceb JDB |
2448 | nf_conntrack_all_unlock(); |
2449 | local_bh_enable(); | |
9fb9cbb1 | 2450 | |
5e3c61f9 | 2451 | synchronize_net(); |
285189c7 | 2452 | kvfree(old_hash); |
9fb9cbb1 YK |
2453 | return 0; |
2454 | } | |
3183ab89 | 2455 | |
e4dca7b7 | 2456 | int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp) |
3183ab89 FW |
2457 | { |
2458 | unsigned int hashsize; | |
2459 | int rc; | |
2460 | ||
2461 | if (current->nsproxy->net_ns != &init_net) | |
2462 | return -EOPNOTSUPP; | |
2463 | ||
2464 | /* On boot, we can set this without any fancy locking. */ | |
2045cdfa | 2465 | if (!nf_conntrack_hash) |
3183ab89 FW |
2466 | return param_set_uint(val, kp); |
2467 | ||
2468 | rc = kstrtouint(val, 0, &hashsize); | |
2469 | if (rc) | |
2470 | return rc; | |
2471 | ||
2472 | return nf_conntrack_hash_resize(hashsize); | |
2473 | } | |
9fb9cbb1 | 2474 | |
ab71632c | 2475 | static __always_inline unsigned int total_extension_size(void) |
b3a5db10 FW |
2476 | { |
2477 | /* remember to add new extensions below */ | |
2478 | BUILD_BUG_ON(NF_CT_EXT_NUM > 9); | |
2479 | ||
2480 | return sizeof(struct nf_ct_ext) + | |
2481 | sizeof(struct nf_conn_help) | |
2482 | #if IS_ENABLED(CONFIG_NF_NAT) | |
2483 | + sizeof(struct nf_conn_nat) | |
2484 | #endif | |
2485 | + sizeof(struct nf_conn_seqadj) | |
2486 | + sizeof(struct nf_conn_acct) | |
2487 | #ifdef CONFIG_NF_CONNTRACK_EVENTS | |
2488 | + sizeof(struct nf_conntrack_ecache) | |
2489 | #endif | |
2490 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
2491 | + sizeof(struct nf_conn_tstamp) | |
2492 | #endif | |
2493 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT | |
2494 | + sizeof(struct nf_conn_timeout) | |
2495 | #endif | |
2496 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
2497 | + sizeof(struct nf_conn_labels) | |
2498 | #endif | |
2499 | #if IS_ENABLED(CONFIG_NETFILTER_SYNPROXY) | |
2500 | + sizeof(struct nf_conn_synproxy) | |
2501 | #endif | |
2502 | ; | |
2503 | }; | |
2504 | ||
f94161c1 | 2505 | int nf_conntrack_init_start(void) |
9fb9cbb1 | 2506 | { |
ca79b0c2 | 2507 | unsigned long nr_pages = totalram_pages(); |
f205c5e0 | 2508 | int max_factor = 8; |
0c5366b3 | 2509 | int ret = -ENOMEM; |
cc41c84b | 2510 | int i; |
93bb0ceb | 2511 | |
b3a5db10 FW |
2512 | /* struct nf_ct_ext uses u8 to store offsets/size */ |
2513 | BUILD_BUG_ON(total_extension_size() > 255u); | |
2514 | ||
a3efd812 FW |
2515 | seqcount_init(&nf_conntrack_generation); |
2516 | ||
d5d20912 | 2517 | for (i = 0; i < CONNTRACK_LOCKS; i++) |
93bb0ceb | 2518 | spin_lock_init(&nf_conntrack_locks[i]); |
9fb9cbb1 | 2519 | |
9fb9cbb1 | 2520 | if (!nf_conntrack_htable_size) { |
88eab472 ML |
2521 | /* Idea from tcp.c: use 1/16384 of memory. |
2522 | * On i386: 32MB machine has 512 buckets. | |
2523 | * >= 1GB machines have 16384 buckets. | |
2524 | * >= 4GB machines have 65536 buckets. | |
2525 | */ | |
9fb9cbb1 | 2526 | nf_conntrack_htable_size |
3d6357de | 2527 | = (((nr_pages << PAGE_SHIFT) / 16384) |
f205c5e0 | 2528 | / sizeof(struct hlist_head)); |
3d6357de | 2529 | if (nr_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE))) |
88eab472 | 2530 | nf_conntrack_htable_size = 65536; |
3d6357de | 2531 | else if (nr_pages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
f205c5e0 PM |
2532 | nf_conntrack_htable_size = 16384; |
2533 | if (nf_conntrack_htable_size < 32) | |
2534 | nf_conntrack_htable_size = 32; | |
2535 | ||
2536 | /* Use a max. factor of four by default to get the same max as | |
2537 | * with the old struct list_heads. When a table size is given | |
2538 | * we use the old value of 8 to avoid reducing the max. | |
2539 | * entries. */ | |
2540 | max_factor = 4; | |
9fb9cbb1 | 2541 | } |
56d52d48 FW |
2542 | |
2543 | nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1); | |
2544 | if (!nf_conntrack_hash) | |
2545 | return -ENOMEM; | |
2546 | ||
f205c5e0 | 2547 | nf_conntrack_max = max_factor * nf_conntrack_htable_size; |
8e5105a0 | 2548 | |
0c5366b3 | 2549 | nf_conntrack_cachep = kmem_cache_create("nf_conntrack", |
a9e419dc FW |
2550 | sizeof(struct nf_conn), |
2551 | NFCT_INFOMASK + 1, | |
5f0d5a3a | 2552 | SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN, NULL); |
0c5366b3 FW |
2553 | if (!nf_conntrack_cachep) |
2554 | goto err_cachep; | |
2555 | ||
83b4dbe1 G |
2556 | ret = nf_conntrack_expect_init(); |
2557 | if (ret < 0) | |
2558 | goto err_expect; | |
2559 | ||
b7ff3a1f G |
2560 | ret = nf_conntrack_acct_init(); |
2561 | if (ret < 0) | |
2562 | goto err_acct; | |
2563 | ||
73f4001a G |
2564 | ret = nf_conntrack_tstamp_init(); |
2565 | if (ret < 0) | |
2566 | goto err_tstamp; | |
2567 | ||
3fe0f943 G |
2568 | ret = nf_conntrack_ecache_init(); |
2569 | if (ret < 0) | |
2570 | goto err_ecache; | |
2571 | ||
8684094c G |
2572 | ret = nf_conntrack_timeout_init(); |
2573 | if (ret < 0) | |
2574 | goto err_timeout; | |
2575 | ||
5e615b22 G |
2576 | ret = nf_conntrack_helper_init(); |
2577 | if (ret < 0) | |
2578 | goto err_helper; | |
2579 | ||
5f69b8f5 G |
2580 | ret = nf_conntrack_labels_init(); |
2581 | if (ret < 0) | |
2582 | goto err_labels; | |
2583 | ||
41d73ec0 PM |
2584 | ret = nf_conntrack_seqadj_init(); |
2585 | if (ret < 0) | |
2586 | goto err_seqadj; | |
2587 | ||
04d87001 G |
2588 | ret = nf_conntrack_proto_init(); |
2589 | if (ret < 0) | |
2590 | goto err_proto; | |
2591 | ||
b87a2f91 | 2592 | conntrack_gc_work_init(&conntrack_gc_work); |
0984d427 | 2593 | queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ); |
b87a2f91 | 2594 | |
08f6547d AD |
2595 | return 0; |
2596 | ||
04d87001 | 2597 | err_proto: |
41d73ec0 PM |
2598 | nf_conntrack_seqadj_fini(); |
2599 | err_seqadj: | |
04d87001 | 2600 | nf_conntrack_labels_fini(); |
5f69b8f5 G |
2601 | err_labels: |
2602 | nf_conntrack_helper_fini(); | |
5e615b22 G |
2603 | err_helper: |
2604 | nf_conntrack_timeout_fini(); | |
8684094c G |
2605 | err_timeout: |
2606 | nf_conntrack_ecache_fini(); | |
3fe0f943 G |
2607 | err_ecache: |
2608 | nf_conntrack_tstamp_fini(); | |
73f4001a G |
2609 | err_tstamp: |
2610 | nf_conntrack_acct_fini(); | |
b7ff3a1f G |
2611 | err_acct: |
2612 | nf_conntrack_expect_fini(); | |
83b4dbe1 | 2613 | err_expect: |
0c5366b3 FW |
2614 | kmem_cache_destroy(nf_conntrack_cachep); |
2615 | err_cachep: | |
285189c7 | 2616 | kvfree(nf_conntrack_hash); |
08f6547d AD |
2617 | return ret; |
2618 | } | |
2619 | ||
1f4b2439 | 2620 | static struct nf_ct_hook nf_conntrack_hook = { |
368982cd | 2621 | .update = nf_conntrack_update, |
1f4b2439 | 2622 | .destroy = destroy_conntrack, |
b60a6040 | 2623 | .get_tuple_skb = nf_conntrack_get_tuple_skb, |
1f4b2439 PNA |
2624 | }; |
2625 | ||
f94161c1 G |
2626 | void nf_conntrack_init_end(void) |
2627 | { | |
2628 | /* For use by REJECT target */ | |
2629 | RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach); | |
1f4b2439 | 2630 | RCU_INIT_POINTER(nf_ct_hook, &nf_conntrack_hook); |
f94161c1 G |
2631 | } |
2632 | ||
8cc20198 ED |
2633 | /* |
2634 | * We need to use special "null" values, not used in hash table | |
2635 | */ | |
2636 | #define UNCONFIRMED_NULLS_VAL ((1<<30)+0) | |
2637 | #define DYING_NULLS_VAL ((1<<30)+1) | |
2638 | ||
f94161c1 | 2639 | int nf_conntrack_init_net(struct net *net) |
08f6547d | 2640 | { |
b7779d06 JDB |
2641 | int ret = -ENOMEM; |
2642 | int cpu; | |
ceceae1b | 2643 | |
cc41c84b | 2644 | BUILD_BUG_ON(IP_CT_UNTRACKED == IP_CT_NUMBER); |
2e7b162c | 2645 | BUILD_BUG_ON_NOT_POWER_OF_2(CONNTRACK_LOCKS); |
08f6547d | 2646 | atomic_set(&net->ct.count, 0); |
b7779d06 JDB |
2647 | |
2648 | net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu); | |
2649 | if (!net->ct.pcpu_lists) | |
08f6547d | 2650 | goto err_stat; |
b7779d06 JDB |
2651 | |
2652 | for_each_possible_cpu(cpu) { | |
2653 | struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); | |
2654 | ||
2655 | spin_lock_init(&pcpu->lock); | |
2656 | INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL); | |
2657 | INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL); | |
08f6547d | 2658 | } |
5b3501fa | 2659 | |
b7779d06 JDB |
2660 | net->ct.stat = alloc_percpu(struct ip_conntrack_stat); |
2661 | if (!net->ct.stat) | |
2662 | goto err_pcpu_lists; | |
2663 | ||
83b4dbe1 | 2664 | ret = nf_conntrack_expect_pernet_init(net); |
08f6547d AD |
2665 | if (ret < 0) |
2666 | goto err_expect; | |
fc3893fd FW |
2667 | |
2668 | nf_conntrack_acct_pernet_init(net); | |
2669 | nf_conntrack_tstamp_pernet_init(net); | |
2670 | nf_conntrack_ecache_pernet_init(net); | |
2671 | nf_conntrack_helper_pernet_init(net); | |
4a60dc74 | 2672 | nf_conntrack_proto_pernet_init(net); |
fc3893fd | 2673 | |
08f6547d | 2674 | return 0; |
c539f017 | 2675 | |
08f6547d | 2676 | err_expect: |
0d55af87 | 2677 | free_percpu(net->ct.stat); |
b7779d06 JDB |
2678 | err_pcpu_lists: |
2679 | free_percpu(net->ct.pcpu_lists); | |
0d55af87 | 2680 | err_stat: |
08f6547d AD |
2681 | return ret; |
2682 | } |