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