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