blob: 9bf061cd2b574d5c73109770fc7c43aebf53bbd2 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* Connection state tracking for netfilter. This is separated from,
2 but required by, the NAT layer; it can also be used by an iptables
3 extension. */
4
5/* (C) 1999-2001 Paul `Rusty' Russell
Harald Weltedc808fe2006-03-20 17:56:32 -08006 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02008 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013 */
14
Weongyo Jeongccd63c22016-03-15 10:57:44 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <linux/types.h>
18#include <linux/netfilter.h>
19#include <linux/module.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021#include <linux/skbuff.h>
22#include <linux/proc_fs.h>
23#include <linux/vmalloc.h>
24#include <linux/stddef.h>
25#include <linux/slab.h>
26#include <linux/random.h>
27#include <linux/jhash.h>
Florian Westphal19224762019-08-17 00:01:34 +010028#include <linux/siphash.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029#include <linux/err.h>
30#include <linux/percpu.h>
31#include <linux/moduleparam.h>
32#include <linux/notifier.h>
33#include <linux/kernel.h>
34#include <linux/netdevice.h>
35#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050036#include <linux/mm.h>
Patrick McHardyd696c7b2010-02-08 11:18:07 -080037#include <linux/nsproxy.h>
Eric Dumazetea781f12009-03-25 21:05:46 +010038#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040#include <net/netfilter/nf_conntrack.h>
41#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010042#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010043#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy41d73ec2013-08-27 08:50:12 +020045#include <net/netfilter/nf_conntrack_seqadj.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070047#include <net/netfilter/nf_conntrack_extend.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070048#include <net/netfilter/nf_conntrack_acct.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020049#include <net/netfilter/nf_conntrack_ecache.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010050#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010051#include <net/netfilter/nf_conntrack_timestamp.h>
Pablo Neira Ayusodd705072012-02-28 23:36:48 +010052#include <net/netfilter/nf_conntrack_timeout.h>
Florian Westphalc539f012013-01-11 06:30:44 +000053#include <net/netfilter/nf_conntrack_labels.h>
Patrick McHardy48b1de42013-08-27 08:50:14 +020054#include <net/netfilter/nf_conntrack_synproxy.h>
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070055#include <net/netfilter/nf_nat.h>
Patrick McHardye17b6662008-11-18 12:24:17 +010056#include <net/netfilter/nf_nat_core.h>
stephen hemminger49376362013-03-16 07:00:28 +000057#include <net/netfilter/nf_nat_helper.h>
Florian Westphal1b8c8a92016-05-03 00:25:58 +020058#include <net/netns/hash.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059
Harald Weltedc808fe2006-03-20 17:56:32 -080060#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061
Patrick McHardye17b6662008-11-18 12:24:17 +010062int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
63 enum nf_nat_manip_type manip,
Patrick McHardy39938322009-08-25 16:07:58 +020064 const struct nlattr *attr) __read_mostly;
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070065EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
66
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +010067__cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
68EXPORT_SYMBOL_GPL(nf_conntrack_locks);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +010070__cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
71EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
72
Florian Westphal56d52d42016-05-02 18:39:55 +020073struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
74EXPORT_SYMBOL_GPL(nf_conntrack_hash);
75
Mohammed Javide6d2be82017-11-24 15:40:26 +053076bool (*nattype_refresh_timer)
77 (unsigned long nattype,
78 unsigned long timeout_value)
79 __rcu __read_mostly;
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +053080EXPORT_SYMBOL(nattype_refresh_timer);
81
Florian Westphalb87a2f92016-08-25 15:33:33 +020082struct conntrack_gc_work {
83 struct delayed_work dwork;
84 u32 last_bucket;
85 bool exiting;
Florian Westphale0df8ca2016-11-04 16:54:58 +010086 long next_gc_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +020087};
88
Florian Westphal0c5366b2016-05-09 16:24:32 +020089static __read_mostly struct kmem_cache *nf_conntrack_cachep;
Sasha Levinb16c2912016-01-18 19:23:51 -050090static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
Florian Westphal70d72b72016-04-24 01:17:14 +020091static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -050092static __read_mostly bool nf_conntrack_locks_all;
93
Florian Westphale0df8ca2016-11-04 16:54:58 +010094/* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */
Florian Westphal371d0342017-01-18 02:01:22 +010095#define GC_MAX_BUCKETS_DIV 128u
96/* upper bound of full table scan */
97#define GC_MAX_SCAN_JIFFIES (16u * HZ)
98/* desired ratio of entries found to be expired */
99#define GC_EVICT_RATIO 50u
Florian Westphalb87a2f92016-08-25 15:33:33 +0200100
101static struct conntrack_gc_work conntrack_gc_work;
102
Sasha Levinb16c2912016-01-18 19:23:51 -0500103void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
104{
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200105 /* 1) Acquire the lock */
Sasha Levinb16c2912016-01-18 19:23:51 -0500106 spin_lock(lock);
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200107
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200108 /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
109 * It pairs with the smp_store_release() in nf_conntrack_all_unlock()
110 */
111 if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
112 return;
113
114 /* fast path failed, unlock */
115 spin_unlock(lock);
116
117 /* Slow path 1) get global lock */
118 spin_lock(&nf_conntrack_locks_all_lock);
119
120 /* Slow path 2) get the lock we want */
121 spin_lock(lock);
122
123 /* Slow path 3) release the global lock */
124 spin_unlock(&nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -0500125}
126EXPORT_SYMBOL_GPL(nf_conntrack_lock);
127
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100128static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
129{
130 h1 %= CONNTRACK_LOCKS;
131 h2 %= CONNTRACK_LOCKS;
132 spin_unlock(&nf_conntrack_locks[h1]);
133 if (h1 != h2)
134 spin_unlock(&nf_conntrack_locks[h2]);
135}
136
137/* return true if we need to recompute hashes (in case hash table was resized) */
138static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
139 unsigned int h2, unsigned int sequence)
140{
141 h1 %= CONNTRACK_LOCKS;
142 h2 %= CONNTRACK_LOCKS;
143 if (h1 <= h2) {
Sasha Levinb16c2912016-01-18 19:23:51 -0500144 nf_conntrack_lock(&nf_conntrack_locks[h1]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100145 if (h1 != h2)
146 spin_lock_nested(&nf_conntrack_locks[h2],
147 SINGLE_DEPTH_NESTING);
148 } else {
Sasha Levinb16c2912016-01-18 19:23:51 -0500149 nf_conntrack_lock(&nf_conntrack_locks[h2]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100150 spin_lock_nested(&nf_conntrack_locks[h1],
151 SINGLE_DEPTH_NESTING);
152 }
Florian Westphala3efd812016-04-18 16:16:59 +0200153 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100154 nf_conntrack_double_unlock(h1, h2);
155 return true;
156 }
157 return false;
158}
159
160static void nf_conntrack_all_lock(void)
161{
162 int i;
163
Sasha Levinb16c2912016-01-18 19:23:51 -0500164 spin_lock(&nf_conntrack_locks_all_lock);
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200165
Sasha Levinb16c2912016-01-18 19:23:51 -0500166 nf_conntrack_locks_all = true;
167
168 for (i = 0; i < CONNTRACK_LOCKS; i++) {
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200169 spin_lock(&nf_conntrack_locks[i]);
170
171 /* This spin_unlock provides the "release" to ensure that
172 * nf_conntrack_locks_all==true is visible to everyone that
173 * acquired spin_lock(&nf_conntrack_locks[]).
174 */
175 spin_unlock(&nf_conntrack_locks[i]);
Sasha Levinb16c2912016-01-18 19:23:51 -0500176 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100177}
178
179static void nf_conntrack_all_unlock(void)
180{
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200181 /* All prior stores must be complete before we clear
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200182 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
183 * might observe the false value but not the entire
Manfred Spraul4c7f54a2017-07-06 20:45:59 +0200184 * critical section.
185 * It pairs with the smp_load_acquire() in nf_conntrack_lock()
Peter Zijlstrab316ff72016-05-24 15:00:38 +0200186 */
187 smp_store_release(&nf_conntrack_locks_all, false);
Sasha Levinb16c2912016-01-18 19:23:51 -0500188 spin_unlock(&nf_conntrack_locks_all_lock);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100189}
190
Martin Josefssone2b76062006-11-29 02:35:04 +0100191unsigned int nf_conntrack_htable_size __read_mostly;
Pablo Neira Ayuso2567c4e2016-08-18 11:15:12 +0200192EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
193
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100194unsigned int nf_conntrack_max __read_mostly;
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530195
Liping Zhang92e47ba2016-08-13 22:35:36 +0800196seqcount_t nf_conntrack_generation __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -0800197
Mohammed Javid53d20ee2016-02-10 17:29:57 +0530198unsigned int nf_conntrack_pkt_threshold __read_mostly;
199EXPORT_SYMBOL(nf_conntrack_pkt_threshold);
200
Eric Dumazetb3c51632010-06-09 14:43:38 +0200201DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
202EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
Patrick McHardy13b18332006-12-02 22:11:25 -0800203
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530204unsigned int nf_conntrack_hash_rnd __read_mostly;
205EXPORT_SYMBOL(nf_conntrack_hash_rnd);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800206
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200207static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
208 const struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209{
Patrick McHardy07949352008-01-31 04:40:52 -0800210 unsigned int n;
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200211 u32 seed;
Sami Farin9b887902007-03-14 16:43:00 -0700212
Florian Westphal141658f2016-04-18 16:17:01 +0200213 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
214
Patrick McHardy07949352008-01-31 04:40:52 -0800215 /* The direction must be ignored, so we hash everything up to the
216 * destination ports (which is a multiple of 4) and treat the last
217 * three bytes manually.
218 */
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200219 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
Patrick McHardy07949352008-01-31 04:40:52 -0800220 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200221 return jhash2((u32 *)tuple, n, seed ^
Changli Gao99f07e92010-09-21 17:49:20 +0200222 (((__force __u16)tuple->dst.u.all << 16) |
223 tuple->dst.protonum));
224}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225
Florian Westphal56d52d42016-05-02 18:39:55 +0200226static u32 scale_hash(u32 hash)
Changli Gao99f07e92010-09-21 17:49:20 +0200227{
Florian Westphal56d52d42016-05-02 18:39:55 +0200228 return reciprocal_scale(hash, nf_conntrack_htable_size);
Changli Gao99f07e92010-09-21 17:49:20 +0200229}
230
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200231static u32 __hash_conntrack(const struct net *net,
232 const struct nf_conntrack_tuple *tuple,
233 unsigned int size)
Changli Gao99f07e92010-09-21 17:49:20 +0200234{
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200235 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236}
237
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200238static u32 hash_conntrack(const struct net *net,
239 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800240{
Florian Westphal56d52d42016-05-02 18:39:55 +0200241 return scale_hash(hash_conntrack_raw(tuple, net));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242}
243
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200244bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245nf_ct_get_tuple(const struct sk_buff *skb,
246 unsigned int nhoff,
247 unsigned int dataoff,
248 u_int16_t l3num,
249 u_int8_t protonum,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500250 struct net *net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800251 struct nf_conntrack_tuple *tuple,
252 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100253 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254{
Philip Craig443a70d2008-04-29 03:35:10 -0700255 memset(tuple, 0, sizeof(*tuple));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800256
257 tuple->src.l3num = l3num;
258 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200259 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260
261 tuple->dst.protonum = protonum;
262 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
263
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500264 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800265}
Patrick McHardy13b18332006-12-02 22:11:25 -0800266EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200268bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500269 u_int16_t l3num,
270 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700271{
272 struct nf_conntrack_l3proto *l3proto;
273 struct nf_conntrack_l4proto *l4proto;
274 unsigned int protoff;
275 u_int8_t protonum;
276 int ret;
277
278 rcu_read_lock();
279
280 l3proto = __nf_ct_l3proto_find(l3num);
281 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
282 if (ret != NF_ACCEPT) {
283 rcu_read_unlock();
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200284 return false;
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700285 }
286
287 l4proto = __nf_ct_l4proto_find(l3num, protonum);
288
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500289 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700290 l3proto, l4proto);
291
292 rcu_read_unlock();
293 return ret;
294}
295EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
296
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200297bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800298nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
299 const struct nf_conntrack_tuple *orig,
300 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100301 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302{
Philip Craig443a70d2008-04-29 03:35:10 -0700303 memset(inverse, 0, sizeof(*inverse));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304
305 inverse->src.l3num = orig->src.l3num;
306 if (l3proto->invert_tuple(inverse, orig) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200307 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800308
309 inverse->dst.dir = !orig->dst.dir;
310
311 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100312 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313}
Patrick McHardy13b18332006-12-02 22:11:25 -0800314EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800315
Florian Westphal19224762019-08-17 00:01:34 +0100316/* Generate a almost-unique pseudo-id for a given conntrack.
317 *
318 * intentionally doesn't re-use any of the seeds used for hash
319 * table location, we assume id gets exposed to userspace.
320 *
321 * Following nf_conn items do not change throughout lifetime
Dirk Morris62b08632019-08-08 13:57:51 -0700322 * of the nf_conn:
Florian Westphal19224762019-08-17 00:01:34 +0100323 *
324 * 1. nf_conn address
Dirk Morris62b08632019-08-08 13:57:51 -0700325 * 2. nf_conn->master address (normally NULL)
326 * 3. the associated net namespace
327 * 4. the original direction tuple
Florian Westphal19224762019-08-17 00:01:34 +0100328 */
329u32 nf_ct_get_id(const struct nf_conn *ct)
330{
331 static __read_mostly siphash_key_t ct_id_seed;
332 unsigned long a, b, c, d;
333
334 net_get_random_once(&ct_id_seed, sizeof(ct_id_seed));
335
336 a = (unsigned long)ct;
Dirk Morris62b08632019-08-08 13:57:51 -0700337 b = (unsigned long)ct->master;
338 c = (unsigned long)nf_ct_net(ct);
339 d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
340 sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple),
Florian Westphal19224762019-08-17 00:01:34 +0100341 &ct_id_seed);
342#ifdef CONFIG_64BIT
343 return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed);
344#else
345 return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed);
346#endif
347}
348EXPORT_SYMBOL_GPL(nf_ct_get_id);
349
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800350static void
351clean_from_lists(struct nf_conn *ct)
352{
Min Chong81ebc7e2016-10-13 17:18:40 -0700353 pr_debug("clean_from_lists(%pK)\n", ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100354 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
355 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356
357 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800358 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800359}
360
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100361/* must be called with local_bh_disable */
362static void nf_ct_add_to_dying_list(struct nf_conn *ct)
363{
364 struct ct_pcpu *pcpu;
365
366 /* add this conntrack to the (per cpu) dying list */
367 ct->cpu = smp_processor_id();
368 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
369
370 spin_lock(&pcpu->lock);
371 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
372 &pcpu->dying);
373 spin_unlock(&pcpu->lock);
374}
375
376/* must be called with local_bh_disable */
377static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
378{
379 struct ct_pcpu *pcpu;
380
381 /* add this conntrack to the (per cpu) unconfirmed list */
382 ct->cpu = smp_processor_id();
383 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
384
385 spin_lock(&pcpu->lock);
386 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
387 &pcpu->unconfirmed);
388 spin_unlock(&pcpu->lock);
389}
390
391/* must be called with local_bh_disable */
392static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
393{
394 struct ct_pcpu *pcpu;
395
396 /* We overload first tuple to link into unconfirmed or dying list.*/
397 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
398
399 spin_lock(&pcpu->lock);
400 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
401 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
402 spin_unlock(&pcpu->lock);
403}
404
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200405/* Released via destroy_conntrack() */
Daniel Borkmann308ac912015-08-08 21:40:01 +0200406struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
407 const struct nf_conntrack_zone *zone,
408 gfp_t flags)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200409{
410 struct nf_conn *tmpl;
411
Joe Stringerf58e5aa2015-08-04 18:34:00 -0700412 tmpl = kzalloc(sizeof(*tmpl), flags);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200413 if (tmpl == NULL)
414 return NULL;
415
416 tmpl->status = IPS_TEMPLATE;
417 write_pnet(&tmpl->ct_net, net);
Florian Westphal6c8dee92016-06-11 21:57:35 +0200418 nf_ct_zone_add(tmpl, zone);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200419 atomic_set(&tmpl->ct_general.use, 0);
420
421 return tmpl;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200422}
423EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
424
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200425void nf_ct_tmpl_free(struct nf_conn *tmpl)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200426{
427 nf_ct_ext_destroy(tmpl);
428 nf_ct_ext_free(tmpl);
429 kfree(tmpl);
430}
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200431EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200432
Mohammed Javid9c843412015-07-16 22:43:04 +0530433void (*delete_sfe_entry)(struct nf_conn *ct) __rcu __read_mostly;
434EXPORT_SYMBOL(delete_sfe_entry);
435
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800436static void
437destroy_conntrack(struct nf_conntrack *nfct)
438{
439 struct nf_conn *ct = (struct nf_conn *)nfct;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100440 struct nf_conntrack_l4proto *l4proto;
Mohammed Javid9c843412015-07-16 22:43:04 +0530441 void (*delete_entry)(struct nf_conn *ct);
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530442 struct sip_list *sip_node = NULL;
443 struct list_head *sip_node_list;
444 struct list_head *sip_node_save_list;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445
Min Chong81ebc7e2016-10-13 17:18:40 -0700446 pr_debug("destroy_conntrack(%pK)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800448
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200449 if (unlikely(nf_ct_is_template(ct))) {
450 nf_ct_tmpl_free(ct);
451 return;
452 }
Mohammed Javid9c843412015-07-16 22:43:04 +0530453
454 if (ct->sfe_entry) {
455 delete_entry = rcu_dereference(delete_sfe_entry);
456 if (delete_entry)
457 delete_entry(ct);
458 }
459
460 /* To make sure we don't get any weird locking issues here:
461 * destroy_conntrack() MUST NOT be called with a write lock
462 * to nf_conntrack_lock!!! -HW
463 */
Patrick McHardy923f4902007-02-12 11:12:57 -0800464 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200465 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Pablo Neira Ayuso4b4ceb92016-05-01 00:34:37 +0200466 if (l4proto->destroy)
Martin Josefsson605dcad2006-11-29 02:35:06 +0100467 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468
Patrick McHardy982d9a92007-02-12 11:14:11 -0800469 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800470
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100471 local_bh_disable();
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530472
473 pr_debug("freeing item in the SIP list\n");
Utkarsh Saxena55000b52017-01-11 16:30:00 +0530474 if (ct->sip_segment_list.next)
475 list_for_each_safe(sip_node_list, sip_node_save_list,
476 &ct->sip_segment_list) {
477 sip_node = list_entry(sip_node_list,
478 struct sip_list, list);
479 list_del(&sip_node->list);
480 kfree(sip_node);
481 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482 /* Expectations will have been removed in clean_from_lists,
483 * except TFTP can create an expectation on the first packet,
484 * before connection is in the list, so we need to clean here,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100485 * too.
486 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800487 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100489 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800490
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100491 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800492
493 if (ct->master)
494 nf_ct_put(ct->master);
495
Min Chong81ebc7e2016-10-13 17:18:40 -0700496 pr_debug("destroy_conntrack: returning ct=%pK to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497 nf_conntrack_free(ct);
498}
499
Florian Westphal02982c22013-07-29 15:41:54 +0200500static void nf_ct_delete_from_lists(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800501{
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200502 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100503 unsigned int hash, reply_hash;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100504 unsigned int sequence;
Patrick McHardy5397e972007-05-19 14:23:52 -0700505
Pablo Neira Ayuso9858a3a2009-06-13 12:28:22 +0200506 nf_ct_helper_destroy(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100507
508 local_bh_disable();
509 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200510 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200511 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100512 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200513 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100514 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
515 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
516
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800517 clean_from_lists(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100518 nf_conntrack_double_unlock(hash, reply_hash);
519
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100520 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100521
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100522 local_bh_enable();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200523}
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200524
Florian Westphal02982c22013-07-29 15:41:54 +0200525bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200526{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100527 struct nf_conn_tstamp *tstamp;
528
Florian Westphalf330a7f2016-08-25 15:33:31 +0200529 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
530 return false;
531
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100532 tstamp = nf_conn_tstamp_find(ct);
533 if (tstamp && tstamp->stop == 0)
Eric Dumazetd2de8752014-08-22 18:32:09 -0700534 tstamp->stop = ktime_get_real_ns();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200535
Florian Westphal95005072014-06-10 23:12:56 +0200536 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
537 portid, report) < 0) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200538 /* destroy event was not delivered. nf_ct_put will
539 * be done by event cache worker on redelivery.
540 */
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200541 nf_ct_delete_from_lists(ct);
Florian Westphal95005072014-06-10 23:12:56 +0200542 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
Florian Westphal02982c22013-07-29 15:41:54 +0200543 return false;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200544 }
Florian Westphal95005072014-06-10 23:12:56 +0200545
546 nf_conntrack_ecache_work(nf_ct_net(ct));
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200547 nf_ct_delete_from_lists(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800548 nf_ct_put(ct);
Florian Westphal02982c22013-07-29 15:41:54 +0200549 return true;
550}
551EXPORT_SYMBOL_GPL(nf_ct_delete);
552
Andrey Vaginc6825c02014-01-29 19:34:14 +0100553static inline bool
554nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
Daniel Borkmann308ac912015-08-08 21:40:01 +0200555 const struct nf_conntrack_tuple *tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200556 const struct nf_conntrack_zone *zone,
557 const struct net *net)
Andrey Vaginc6825c02014-01-29 19:34:14 +0100558{
559 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
560
561 /* A conntrack can be recreated with the equal tuple,
562 * so we need to check that the conntrack is confirmed
563 */
564 return nf_ct_tuple_equal(tuple, &h->tuple) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200565 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200566 nf_ct_is_confirmed(ct) &&
567 net_eq(net, nf_ct_net(ct));
Andrey Vaginc6825c02014-01-29 19:34:14 +0100568}
569
Florian Westphalf330a7f2016-08-25 15:33:31 +0200570/* caller must hold rcu readlock and none of the nf_conntrack_locks */
571static void nf_ct_gc_expired(struct nf_conn *ct)
572{
573 if (!atomic_inc_not_zero(&ct->ct_general.use))
574 return;
575
576 if (nf_ct_should_gc(ct))
577 nf_ct_kill(ct);
578
579 nf_ct_put(ct);
580}
581
Eric Dumazetea781f12009-03-25 21:05:46 +0100582/*
583 * Warning :
584 * - Caller must take a reference on returned object
585 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
Eric Dumazetea781f12009-03-25 21:05:46 +0100586 */
Changli Gao99f07e92010-09-21 17:49:20 +0200587static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200588____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200589 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800590{
591 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200592 struct hlist_nulls_head *ct_hash;
Eric Dumazetea781f12009-03-25 21:05:46 +0100593 struct hlist_nulls_node *n;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800594 unsigned int bucket, hsize;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800595
Eric Dumazetea781f12009-03-25 21:05:46 +0100596begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800597 nf_conntrack_get_ht(&ct_hash, &hsize);
598 bucket = reciprocal_scale(hash, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200599
600 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
Florian Westphalf330a7f2016-08-25 15:33:31 +0200601 struct nf_conn *ct;
602
603 ct = nf_ct_tuplehash_to_ctrack(h);
604 if (nf_ct_is_expired(ct)) {
605 nf_ct_gc_expired(ct);
606 continue;
607 }
608
609 if (nf_ct_is_dying(ct))
610 continue;
611
Florian Westphal8e8118f2016-09-11 22:55:53 +0200612 if (nf_ct_key_equal(h, tuple, zone, net))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800613 return h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800614 }
Eric Dumazetea781f12009-03-25 21:05:46 +0100615 /*
616 * if the nulls value we got at the end of this lookup is
617 * not the expected one, we must restart lookup.
618 * We probably met an item that was moved to another chain.
619 */
Changli Gao99f07e92010-09-21 17:49:20 +0200620 if (get_nulls_value(n) != bucket) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200621 NF_CT_STAT_INC_ATOMIC(net, search_restart);
Eric Dumazetea781f12009-03-25 21:05:46 +0100622 goto begin;
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200623 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800624
625 return NULL;
626}
Changli Gao99f07e92010-09-21 17:49:20 +0200627
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800628/* Find a connection corresponding to a tuple. */
Changli Gao99f07e92010-09-21 17:49:20 +0200629static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200630__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200631 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800632{
633 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800634 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800635
Patrick McHardy76507f62008-01-31 04:38:38 -0800636 rcu_read_lock();
Eric Dumazetea781f12009-03-25 21:05:46 +0100637begin:
Changli Gao99f07e92010-09-21 17:49:20 +0200638 h = ____nf_conntrack_find(net, zone, tuple, hash);
Patrick McHardy76507f62008-01-31 04:38:38 -0800639 if (h) {
640 ct = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy8d8890b72009-06-22 14:14:41 +0200641 if (unlikely(nf_ct_is_dying(ct) ||
642 !atomic_inc_not_zero(&ct->ct_general.use)))
Patrick McHardy76507f62008-01-31 04:38:38 -0800643 h = NULL;
Eric Dumazetea781f12009-03-25 21:05:46 +0100644 else {
Florian Westphale0c7d472016-04-28 19:13:45 +0200645 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
Eric Dumazetea781f12009-03-25 21:05:46 +0100646 nf_ct_put(ct);
647 goto begin;
648 }
649 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800650 }
651 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800652
653 return h;
654}
Changli Gao99f07e92010-09-21 17:49:20 +0200655
656struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200657nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200658 const struct nf_conntrack_tuple *tuple)
659{
660 return __nf_conntrack_find_get(net, zone, tuple,
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200661 hash_conntrack_raw(tuple, net));
Changli Gao99f07e92010-09-21 17:49:20 +0200662}
Patrick McHardy13b18332006-12-02 22:11:25 -0800663EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800664
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800665static void __nf_conntrack_hash_insert(struct nf_conn *ct,
666 unsigned int hash,
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100667 unsigned int reply_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800668{
Eric Dumazetea781f12009-03-25 21:05:46 +0100669 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200670 &nf_conntrack_hash[hash]);
Eric Dumazetea781f12009-03-25 21:05:46 +0100671 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200672 &nf_conntrack_hash[reply_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800673}
674
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100675int
676nf_conntrack_hash_check_insert(struct nf_conn *ct)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800677{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200678 const struct nf_conntrack_zone *zone;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800679 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100680 unsigned int hash, reply_hash;
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100681 struct nf_conntrack_tuple_hash *h;
682 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100683 unsigned int sequence;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800684
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100685 zone = nf_ct_zone(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800686
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100687 local_bh_disable();
688 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200689 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200690 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100691 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200692 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100693 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
694 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100695
696 /* See if there's one in the list already, including reverse */
Florian Westphal56d52d42016-05-02 18:39:55 +0200697 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200698 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200699 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100700 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200701
Florian Westphal56d52d42016-05-02 18:39:55 +0200702 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200703 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200704 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100705 goto out;
706
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100707 smp_wmb();
708 /* The caller holds a reference to this object */
709 atomic_set(&ct->ct_general.use, 2);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100710 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100711 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100712 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100713 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100714 return 0;
715
716out:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100717 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100718 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100719 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100720 return -EEXIST;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800721}
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100722EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800723
Pablo Neira Ayusoba767382016-05-02 21:28:57 +0200724static inline void nf_ct_acct_update(struct nf_conn *ct,
725 enum ip_conntrack_info ctinfo,
726 unsigned int len)
727{
728 struct nf_conn_acct *acct;
729
730 acct = nf_conn_acct_find(ct);
731 if (acct) {
732 struct nf_conn_counter *counter = acct->counter;
733
734 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
735 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
736 }
737}
738
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200739static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
740 const struct nf_conn *loser_ct)
741{
742 struct nf_conn_acct *acct;
743
744 acct = nf_conn_acct_find(loser_ct);
745 if (acct) {
746 struct nf_conn_counter *counter = acct->counter;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200747 unsigned int bytes;
748
749 /* u32 should be fine since we must have seen one packet. */
750 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
751 nf_ct_acct_update(ct, ctinfo, bytes);
752 }
753}
754
755/* Resolve race on insertion if this protocol allows this. */
756static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
757 enum ip_conntrack_info ctinfo,
758 struct nf_conntrack_tuple_hash *h)
759{
760 /* This is the conntrack entry already in hashes that won race. */
761 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
762 struct nf_conntrack_l4proto *l4proto;
763
764 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
765 if (l4proto->allow_clash &&
Florian Westphal25db12f2017-03-28 10:31:03 +0200766 ((ct->status & IPS_NAT_DONE_MASK) == 0) &&
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200767 !nf_ct_is_dying(ct) &&
768 atomic_inc_not_zero(&ct->ct_general.use)) {
769 nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct);
770 nf_conntrack_put(skb->nfct);
771 /* Assign conntrack already in hashes to this skbuff. Don't
772 * modify skb->nfctinfo to ensure consistent stateful filtering.
773 */
774 skb->nfct = &ct->ct_general;
775 return NF_ACCEPT;
776 }
777 NF_CT_STAT_INC(net, drop);
778 return NF_DROP;
779}
780
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800781/* Confirm a connection given skb; places it in hash table */
782int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700783__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800784{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200785 const struct nf_conntrack_zone *zone;
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100786 unsigned int hash, reply_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700787 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800788 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700789 struct nf_conn_help *help;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100790 struct nf_conn_tstamp *tstamp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100791 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800792 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200793 struct net *net;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100794 unsigned int sequence;
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200795 int ret = NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800796
Herbert Xu3db05fe2007-10-15 00:53:15 -0700797 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200798 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800799
800 /* ipt_REJECT uses nf_conntrack_attach to attach related
801 ICMP/TCP RST packets in other direction. Actual packet
802 which created connection will be IP_CT_NEW or for an
803 expected connection, IP_CT_RELATED. */
804 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
805 return NF_ACCEPT;
806
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100807 zone = nf_ct_zone(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100808 local_bh_disable();
809
810 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200811 sequence = read_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100812 /* reuse the hash saved before */
813 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
Florian Westphal56d52d42016-05-02 18:39:55 +0200814 hash = scale_hash(hash);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200815 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100816 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
817
818 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800819
820 /* We're not in hash table, and we refuse to set up related
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100821 * connections for unconfirmed conns. But packet copies and
822 * REJECT will give spurious warnings here.
823 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800824 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
825
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300826 /* No external references means no one else could have
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100827 * confirmed us.
828 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800829 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Min Chong81ebc7e2016-10-13 17:18:40 -0700830 pr_debug("Confirming conntrack %pK\n", ct);
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100831 /* We have to check the DYING flag after unlink to prevent
832 * a race against nf_ct_get_next_corpse() possibly called from
833 * user context, else we insert an already 'dead' hash, blocking
834 * further use of that particular connection -JM.
835 */
836 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Joerg Marxfc350772010-05-20 15:55:30 +0200837
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200838 if (unlikely(nf_ct_is_dying(ct))) {
839 nf_ct_add_to_dying_list(ct);
840 goto dying;
841 }
Joerg Marxfc350772010-05-20 15:55:30 +0200842
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800843 /* See if there's one in the list already, including reverse:
844 NAT could have grabbed it without realizing, since we're
845 not in the hash. If there is, we lost race. */
Florian Westphal56d52d42016-05-02 18:39:55 +0200846 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200847 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200848 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700849 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200850
Florian Westphal56d52d42016-05-02 18:39:55 +0200851 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200852 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200853 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700854 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800855
Patrick McHardydf0933d2006-09-20 11:57:53 -0700856 /* Timer relative to confirmation time, not original
857 setting time, otherwise we'd get timer wrap in
858 weird delay cases. */
Florian Westphalf330a7f2016-08-25 15:33:31 +0200859 ct->timeout += nfct_time_stamp;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700860 atomic_inc(&ct->ct_general.use);
Changli Gao45eec342011-01-18 15:08:13 +0100861 ct->status |= IPS_CONFIRMED;
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200862
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100863 /* set conntrack timestamp, if enabled. */
864 tstamp = nf_conn_tstamp_find(ct);
865 if (tstamp) {
866 if (skb->tstamp.tv64 == 0)
Joe Perchese3192692012-06-03 17:41:40 +0000867 __net_timestamp(skb);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100868
869 tstamp->start = ktime_to_ns(skb->tstamp);
870 }
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200871 /* Since the lookup is lockless, hash insertion must be done after
872 * starting the timer and setting the CONFIRMED bit. The RCU barriers
873 * guarantee that no other CPU can find the conntrack before the above
874 * stores are visible.
875 */
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100876 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100877 nf_conntrack_double_unlock(hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100878 local_bh_enable();
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200879
Patrick McHardydf0933d2006-09-20 11:57:53 -0700880 help = nfct_help(ct);
881 if (help && help->helper)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200882 nf_conntrack_event_cache(IPCT_HELPER, ct);
Pablo Neira Ayuso17e6e4e2009-06-02 20:08:46 +0200883
Patrick McHardydf0933d2006-09-20 11:57:53 -0700884 nf_conntrack_event_cache(master_ct(ct) ?
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200885 IPCT_RELATED : IPCT_NEW, ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700886 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800887
Patrick McHardydf0933d2006-09-20 11:57:53 -0700888out:
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100889 nf_ct_add_to_dying_list(ct);
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200890 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
891dying:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100892 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200893 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100894 local_bh_enable();
Pablo Neira Ayuso71d8c472016-05-01 00:28:40 +0200895 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800896}
Patrick McHardy13b18332006-12-02 22:11:25 -0800897EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800898
899/* Returns true if a connection correspondings to the tuple (required
900 for NAT). */
901int
902nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
903 const struct nf_conn *ignored_conntrack)
904{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200905 struct net *net = nf_ct_net(ignored_conntrack);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200906 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800907 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200908 struct hlist_nulls_head *ct_hash;
Liping Zhang92e47ba2016-08-13 22:35:36 +0800909 unsigned int hash, hsize;
Eric Dumazetea781f12009-03-25 21:05:46 +0100910 struct hlist_nulls_node *n;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100911 struct nf_conn *ct;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200912
913 zone = nf_ct_zone(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800914
Florian Westphal2cf12342016-04-28 19:13:40 +0200915 rcu_read_lock();
Florian Westphal95a8d192016-08-25 15:33:29 +0200916 begin:
Liping Zhang92e47ba2016-08-13 22:35:36 +0800917 nf_conntrack_get_ht(&ct_hash, &hsize);
918 hash = __hash_conntrack(net, tuple, hsize);
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200919
920 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100921 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphalf330a7f2016-08-25 15:33:31 +0200922
923 if (ct == ignored_conntrack)
924 continue;
925
926 if (nf_ct_is_expired(ct)) {
927 nf_ct_gc_expired(ct);
928 continue;
929 }
930
931 if (nf_ct_key_equal(h, tuple, zone, net)) {
Martynas Pumputis92044922019-01-29 15:51:42 +0100932 /* Tuple is taken already, so caller will need to find
933 * a new source port to use.
934 *
935 * Only exception:
936 * If the *original tuples* are identical, then both
937 * conntracks refer to the same flow.
938 * This is a rare situation, it can occur e.g. when
939 * more than one UDP packet is sent from same socket
940 * in different threads.
941 *
942 * Let nf_ct_resolve_clash() deal with this later.
943 */
944 if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
945 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple))
946 continue;
947
Florian Westphal2cf12342016-04-28 19:13:40 +0200948 NF_CT_STAT_INC_ATOMIC(net, found);
949 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800950 return 1;
951 }
Patrick McHardyba419af2008-01-31 04:39:23 -0800952 }
Florian Westphal95a8d192016-08-25 15:33:29 +0200953
954 if (get_nulls_value(n) != hash) {
955 NF_CT_STAT_INC_ATOMIC(net, search_restart);
956 goto begin;
957 }
958
Florian Westphal2cf12342016-04-28 19:13:40 +0200959 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800960
Patrick McHardyba419af2008-01-31 04:39:23 -0800961 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800962}
Patrick McHardy13b18332006-12-02 22:11:25 -0800963EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800964
Patrick McHardy7ae77302007-07-07 22:37:38 -0700965#define NF_CT_EVICTION_RANGE 8
966
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800967/* There's a small race here where we may free a just-assured
968 connection. Too bad: we're in trouble anyway. */
Florian Westphal242922a2016-07-03 20:44:01 +0200969static unsigned int early_drop_list(struct net *net,
970 struct hlist_nulls_head *head)
971{
972 struct nf_conntrack_tuple_hash *h;
973 struct hlist_nulls_node *n;
974 unsigned int drops = 0;
975 struct nf_conn *tmp;
976
977 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
978 tmp = nf_ct_tuplehash_to_ctrack(h);
979
Florian Westphalf330a7f2016-08-25 15:33:31 +0200980 if (nf_ct_is_expired(tmp)) {
981 nf_ct_gc_expired(tmp);
982 continue;
983 }
984
Florian Westphal242922a2016-07-03 20:44:01 +0200985 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
986 !net_eq(nf_ct_net(tmp), net) ||
987 nf_ct_is_dying(tmp))
988 continue;
989
990 if (!atomic_inc_not_zero(&tmp->ct_general.use))
991 continue;
992
993 /* kill only if still in same netns -- might have moved due to
994 * SLAB_DESTROY_BY_RCU rules.
995 *
996 * We steal the timer reference. If that fails timer has
997 * already fired or someone else deleted it. Just drop ref
998 * and move to next entry.
999 */
1000 if (net_eq(nf_ct_net(tmp), net) &&
1001 nf_ct_is_confirmed(tmp) &&
Florian Westphal242922a2016-07-03 20:44:01 +02001002 nf_ct_delete(tmp, 0, 0))
1003 drops++;
1004
1005 nf_ct_put(tmp);
1006 }
1007
1008 return drops;
1009}
1010
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -07001011static noinline int early_drop(struct net *net, unsigned int hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001012{
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -07001013 unsigned int i, bucket;
Florian Westphal3e866382016-05-02 18:40:14 +02001014
Florian Westphal242922a2016-07-03 20:44:01 +02001015 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
1016 struct hlist_nulls_head *ct_hash;
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -07001017 unsigned int hsize, drops;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001018
Liping Zhang3101e0f2016-07-12 19:45:00 +08001019 rcu_read_lock();
Liping Zhang92e47ba2016-08-13 22:35:36 +08001020 nf_conntrack_get_ht(&ct_hash, &hsize);
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -07001021 if (!i)
1022 bucket = reciprocal_scale(hash, hsize);
1023 else
1024 bucket = (bucket + 1) % hsize;
Florian Westphal3e866382016-05-02 18:40:14 +02001025
Vasily Khoruzhick7e1e1952018-10-25 12:15:43 -07001026 drops = early_drop_list(net, &ct_hash[bucket]);
Liping Zhang3101e0f2016-07-12 19:45:00 +08001027 rcu_read_unlock();
Florian Westphal3e866382016-05-02 18:40:14 +02001028
Florian Westphal242922a2016-07-03 20:44:01 +02001029 if (drops) {
1030 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
1031 return true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +00001032 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001033 }
Florian Westphal3e866382016-05-02 18:40:14 +02001034
Florian Westphal242922a2016-07-03 20:44:01 +02001035 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001036}
1037
Florian Westphalb87a2f92016-08-25 15:33:33 +02001038static void gc_worker(struct work_struct *work)
1039{
Florian Westphal371d0342017-01-18 02:01:22 +01001040 unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001041 unsigned int i, goal, buckets = 0, expired_count = 0;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001042 struct conntrack_gc_work *gc_work;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001043 unsigned int ratio, scanned = 0;
1044 unsigned long next_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001045
1046 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
1047
Florian Westphale0df8ca2016-11-04 16:54:58 +01001048 goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001049 i = gc_work->last_bucket;
1050
1051 do {
1052 struct nf_conntrack_tuple_hash *h;
1053 struct hlist_nulls_head *ct_hash;
1054 struct hlist_nulls_node *n;
1055 unsigned int hashsz;
1056 struct nf_conn *tmp;
1057
1058 i++;
1059 rcu_read_lock();
1060
1061 nf_conntrack_get_ht(&ct_hash, &hashsz);
1062 if (i >= hashsz)
1063 i = 0;
1064
1065 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
1066 tmp = nf_ct_tuplehash_to_ctrack(h);
1067
Florian Westphalc023c0e2016-08-25 15:33:34 +02001068 scanned++;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001069 if (nf_ct_is_expired(tmp)) {
1070 nf_ct_gc_expired(tmp);
1071 expired_count++;
1072 continue;
1073 }
1074 }
1075
1076 /* could check get_nulls_value() here and restart if ct
1077 * was moved to another chain. But given gc is best-effort
1078 * we will just continue with next hash slot.
1079 */
1080 rcu_read_unlock();
1081 cond_resched_rcu_qs();
Florian Westphal5f7ff592017-01-16 18:24:56 +01001082 } while (++buckets < goal);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001083
1084 if (gc_work->exiting)
1085 return;
1086
Florian Westphale0df8ca2016-11-04 16:54:58 +01001087 /*
1088 * Eviction will normally happen from the packet path, and not
1089 * from this gc worker.
1090 *
1091 * This worker is only here to reap expired entries when system went
1092 * idle after a busy period.
1093 *
1094 * The heuristics below are supposed to balance conflicting goals:
1095 *
1096 * 1. Minimize time until we notice a stale entry
1097 * 2. Maximize scan intervals to not waste cycles
1098 *
Florian Westphal371d0342017-01-18 02:01:22 +01001099 * Normally, expire ratio will be close to 0.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001100 *
Florian Westphal371d0342017-01-18 02:01:22 +01001101 * As soon as a sizeable fraction of the entries have expired
1102 * increase scan frequency.
Florian Westphale0df8ca2016-11-04 16:54:58 +01001103 */
Florian Westphalc023c0e2016-08-25 15:33:34 +02001104 ratio = scanned ? expired_count * 100 / scanned : 0;
Florian Westphal371d0342017-01-18 02:01:22 +01001105 if (ratio > GC_EVICT_RATIO) {
1106 gc_work->next_gc_run = min_interval;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001107 } else {
Florian Westphal371d0342017-01-18 02:01:22 +01001108 unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001109
Florian Westphal371d0342017-01-18 02:01:22 +01001110 BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1111
1112 gc_work->next_gc_run += min_interval;
1113 if (gc_work->next_gc_run > max)
1114 gc_work->next_gc_run = max;
Florian Westphale0df8ca2016-11-04 16:54:58 +01001115 }
Florian Westphalc023c0e2016-08-25 15:33:34 +02001116
Florian Westphal371d0342017-01-18 02:01:22 +01001117 next_run = gc_work->next_gc_run;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001118 gc_work->last_bucket = i;
Vincent Guittot44066712017-11-02 16:16:07 +01001119 queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
Florian Westphalb87a2f92016-08-25 15:33:33 +02001120}
1121
1122static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1123{
Subash Abhinov Kasiviswanathan19d74202017-07-20 18:41:09 -06001124 INIT_DEFERRABLE_WORK(&gc_work->dwork, gc_worker);
Florian Westphal371d0342017-01-18 02:01:22 +01001125 gc_work->next_gc_run = HZ;
Florian Westphalb87a2f92016-08-25 15:33:33 +02001126 gc_work->exiting = false;
1127}
1128
Changli Gao99f07e92010-09-21 17:49:20 +02001129static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +02001130__nf_conntrack_alloc(struct net *net,
1131 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001132 const struct nf_conntrack_tuple *orig,
1133 const struct nf_conntrack_tuple *repl,
1134 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001135{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001136 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001137
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001138 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001139 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -07001140
Patrick McHardy76eb9462008-01-31 04:41:44 -08001141 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001142 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001143 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +02001144 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +00001145 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001146 return ERR_PTR(-ENOMEM);
1147 }
1148 }
1149
Eric Dumazet941297f2009-07-16 14:03:40 +02001150 /*
1151 * Do not use kmem_cache_zalloc(), as this cache uses
1152 * SLAB_DESTROY_BY_RCU.
1153 */
Florian Westphal0c5366b2016-05-09 16:24:32 +02001154 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001155 if (ct == NULL)
1156 goto out;
1157
Patrick McHardy440f0d52009-06-10 14:32:47 +02001158 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001159 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +02001160 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -08001161 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +02001162 /* save hash for reusing when confirming */
1163 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +01001164 ct->status = 0;
Eric Dumazetc2d9ba92010-06-01 06:51:19 +00001165 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +01001166 memset(&ct->__nfct_init_offset[0], 0,
1167 offsetof(struct nf_conn, proto) -
1168 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001169
Florian Westphal6c8dee92016-06-11 21:57:35 +02001170 nf_ct_zone_add(ct, zone);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001171
Mohammed Javide6d2be82017-11-24 15:40:26 +05301172#if defined(CONFIG_IP_NF_TARGET_NATTYPE_MODULE)
1173 ct->nattype_entry = 0;
1174#endif
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001175 /* Because we use RCU lookups, we set ct_general.use to zero before
1176 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +02001177 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001178 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -08001179 return ct;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001180out:
1181 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001182 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001183}
Changli Gao99f07e92010-09-21 17:49:20 +02001184
Daniel Borkmann308ac912015-08-08 21:40:01 +02001185struct nf_conn *nf_conntrack_alloc(struct net *net,
1186 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +02001187 const struct nf_conntrack_tuple *orig,
1188 const struct nf_conntrack_tuple *repl,
1189 gfp_t gfp)
1190{
1191 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1192}
Patrick McHardy13b18332006-12-02 22:11:25 -08001193EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001194
Patrick McHardyc88130b2008-01-31 04:42:11 -08001195void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001196{
Eric Dumazet1d452092009-03-24 14:26:50 +01001197 struct net *net = nf_ct_net(ct);
1198
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001199 /* A freed object has refcnt == 0, that's
1200 * the golden rule for SLAB_DESTROY_BY_RCU
1201 */
1202 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1203
Patrick McHardyceeff752008-06-11 17:51:10 -07001204 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +01001205 nf_ct_ext_free(ct);
Florian Westphal0c5366b2016-05-09 16:24:32 +02001206 kmem_cache_free(nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001207 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +01001208 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001209}
Patrick McHardy13b18332006-12-02 22:11:25 -08001210EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001211
Florian Westphalc539f012013-01-11 06:30:44 +00001212
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001213/* Allocate a new conntrack: we return -ENOMEM if classification
1214 failed due to stress. Otherwise it really is unclassifiable. */
1215static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001216init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +02001217 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001218 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001219 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001220 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001221 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001222{
Patrick McHardyc88130b2008-01-31 04:42:11 -08001223 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001224 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001225 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001226 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001227 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +02001228 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001229 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001230 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001231 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001232
Martin Josefsson605dcad2006-11-29 02:35:06 +01001233 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001234 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001235 return NULL;
1236 }
1237
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001238 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +02001239 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1240 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -07001241 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -08001242 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001243
Gao Feng4440a2a2016-09-13 08:49:18 +08001244 if (!nf_ct_add_synproxy(ct, tmpl)) {
1245 nf_conntrack_free(ct);
1246 return ERR_PTR(-ENOMEM);
Patrick McHardy48b1de42013-08-27 08:50:14 +02001247 }
1248
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001249 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001250 if (timeout_ext) {
1251 timeouts = nf_ct_timeout_data(timeout_ext);
1252 if (unlikely(!timeouts))
1253 timeouts = l4proto->get_timeouts(net);
1254 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001255 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001256 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001257
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001258 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -08001259 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001260 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001261 return NULL;
1262 }
1263
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001264 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +02001265 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1266 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001267
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001268 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001269 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +00001270 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001271
1272 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1273 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1274 ecache ? ecache->expmask : 0,
1275 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001276
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001277 local_bh_disable();
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301278 INIT_LIST_HEAD(&ct->sip_segment_list);
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001279 if (net->ct.expect_count) {
1280 spin_lock(&nf_conntrack_expect_lock);
1281 exp = nf_ct_find_expectation(net, zone, tuple);
1282 if (exp) {
Min Chong81ebc7e2016-10-13 17:18:40 -07001283 pr_debug("expectation arrives ct=%pK exp=%pK\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001284 ct, exp);
1285 /* Welcome, Mr. Bond. We've been expecting you... */
1286 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1287 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1288 ct->master = exp->master;
1289 if (exp->helper) {
1290 help = nf_ct_helper_ext_add(ct, exp->helper,
1291 GFP_ATOMIC);
1292 if (help)
1293 rcu_assign_pointer(help->helper, exp->helper);
1294 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001295
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001296#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001297 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001298#endif
James Morris7c9728c2006-06-09 00:31:46 -07001299#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001300 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001301#endif
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +05301302/* Initialize the NAT type entry. */
1303#if defined(CONFIG_IP_NF_TARGET_NATTYPE_MODULE)
1304 ct->nattype_entry = 0;
1305#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001306 NF_CT_STAT_INC(net, expect_new);
1307 }
1308 spin_unlock(&nf_conntrack_expect_lock);
1309 }
Florian Westphal8e8118f2016-09-11 22:55:53 +02001310 if (!exp)
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001311 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001312
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001313 /* Now it is inserted into the unconfirmed list, bump refcount */
1314 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001315 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001316
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001317 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001318
1319 if (exp) {
1320 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001321 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001322 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001323 }
1324
Patrick McHardyc88130b2008-01-31 04:42:11 -08001325 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001326}
1327
1328/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1329static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001330resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001331 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001332 unsigned int dataoff,
1333 u_int16_t l3num,
1334 u_int8_t protonum,
1335 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001336 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001337 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001338 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001339{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001340 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001341 struct nf_conntrack_tuple tuple;
1342 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001343 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001344 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001345 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001346
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001347 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001348 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001349 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001350 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001351 return NULL;
1352 }
1353
1354 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001355 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001356 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001357 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001358 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001359 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001360 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001361 if (!h)
1362 return NULL;
1363 if (IS_ERR(h))
1364 return (void *)h;
1365 }
1366 ct = nf_ct_tuplehash_to_ctrack(h);
1367
1368 /* It exists; we have (non-exclusive) reference. */
1369 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001370 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001371 /* Please set reply bit if this packet OK */
1372 *set_reply = 1;
1373 } else {
1374 /* Once we've had two way comms, always ESTABLISHED. */
1375 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Min Chong81ebc7e2016-10-13 17:18:40 -07001376 pr_debug("normal packet for %pK\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001377 *ctinfo = IP_CT_ESTABLISHED;
1378 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Min Chong81ebc7e2016-10-13 17:18:40 -07001379 pr_debug("related packet for %pK\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001380 *ctinfo = IP_CT_RELATED;
1381 } else {
Min Chong81ebc7e2016-10-13 17:18:40 -07001382 pr_debug("new packet for %pK\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001383 *ctinfo = IP_CT_NEW;
1384 }
1385 *set_reply = 0;
1386 }
1387 skb->nfct = &ct->ct_general;
1388 skb->nfctinfo = *ctinfo;
1389 return ct;
1390}
1391
1392unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001393nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1394 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001395{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001396 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001397 enum ip_conntrack_info ctinfo;
1398 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001399 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001400 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001401 unsigned int dataoff;
1402 u_int8_t protonum;
1403 int set_reply = 0;
1404 int ret;
1405
Herbert Xu3db05fe2007-10-15 00:53:15 -07001406 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001407 /* Previously seen (loopback or untracked)? Ignore. */
1408 tmpl = (struct nf_conn *)skb->nfct;
1409 if (!nf_ct_is_template(tmpl)) {
1410 NF_CT_STAT_INC_ATOMIC(net, ignore);
1411 return NF_ACCEPT;
1412 }
1413 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001414 }
1415
Aaron Conolee2361cb2016-09-21 11:35:04 -04001416 /* rcu_read_lock()ed by nf_hook_thresh */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001417 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001418 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001419 &dataoff, &protonum);
1420 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001421 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001422 NF_CT_STAT_INC_ATOMIC(net, error);
1423 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001424 ret = -ret;
1425 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001426 }
1427
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001428 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001429
1430 /* It may be an special packet, error, unclean...
1431 * inverse of the return code tells to the netfilter
1432 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001433 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001434 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1435 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001436 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001437 NF_CT_STAT_INC_ATOMIC(net, error);
1438 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001439 ret = -ret;
1440 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001441 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001442 /* ICMP[v6] protocol trackers may assign one conntrack. */
1443 if (skb->nfct)
1444 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001445 }
1446
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001447 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001448 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001449 if (!ct) {
1450 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001451 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001452 ret = NF_ACCEPT;
1453 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001454 }
1455
1456 if (IS_ERR(ct)) {
1457 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001458 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001459 ret = NF_DROP;
1460 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001461 }
1462
Herbert Xu3db05fe2007-10-15 00:53:15 -07001463 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001464
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001465 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001466 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001467
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001468 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001469 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001470 /* Invalid: inverse of the return code tells
1471 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001472 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001473 nf_conntrack_put(skb->nfct);
1474 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001475 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001476 if (ret == -NF_DROP)
1477 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001478 ret = -ret;
1479 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001480 }
1481
1482 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001483 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001484out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001485 if (tmpl) {
1486 /* Special case: we have to repeat this hook, assign the
1487 * template again to this packet. We assume that this packet
1488 * has no conntrack assigned. This is used by nf_ct_tcp. */
1489 if (ret == NF_REPEAT)
1490 skb->nfct = (struct nf_conntrack *)tmpl;
1491 else
1492 nf_ct_put(tmpl);
1493 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001494
1495 return ret;
1496}
Patrick McHardy13b18332006-12-02 22:11:25 -08001497EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001498
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001499bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1500 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001501{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001502 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001503
1504 rcu_read_lock();
1505 ret = nf_ct_invert_tuple(inverse, orig,
1506 __nf_ct_l3proto_find(orig->src.l3num),
1507 __nf_ct_l4proto_find(orig->src.l3num,
1508 orig->dst.protonum));
1509 rcu_read_unlock();
1510 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001511}
Patrick McHardy13b18332006-12-02 22:11:25 -08001512EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001513
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001514/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1515 implicitly racy: see __nf_conntrack_confirm */
1516void nf_conntrack_alter_reply(struct nf_conn *ct,
1517 const struct nf_conntrack_tuple *newreply)
1518{
1519 struct nf_conn_help *help = nfct_help(ct);
1520
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001521 /* Should be unconfirmed, so not in hash table yet */
1522 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1523
Min Chong81ebc7e2016-10-13 17:18:40 -07001524 pr_debug("Altering reply tuple of %pK to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001525 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001526
1527 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001528 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001529 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001530
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001531 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001532 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001533 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001534}
Patrick McHardy13b18332006-12-02 22:11:25 -08001535EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001536
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001537/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1538void __nf_ct_refresh_acct(struct nf_conn *ct,
1539 enum ip_conntrack_info ctinfo,
1540 const struct sk_buff *skb,
1541 unsigned long extra_jiffies,
1542 int do_acct)
1543{
Mohammed Javid53d20ee2016-02-10 17:29:57 +05301544 struct nf_conn_acct *acct;
1545 u64 pkts;
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +05301546#if defined(CONFIG_IP_NF_TARGET_NATTYPE_MODULE)
Mohammed Javide6d2be82017-11-24 15:40:26 +05301547 bool (*nattype_ref_timer)
1548 (unsigned long nattype,
1549 unsigned long timeout_value);
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +05301550#endif
Mohammed Javid53d20ee2016-02-10 17:29:57 +05301551
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001552 NF_CT_ASSERT(skb);
1553
Eric Leblond997ae832006-05-29 18:24:20 -07001554 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001555 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1556 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001557
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001558 /* If not in hash table, timer will not be active yet */
Florian Westphalf330a7f2016-08-25 15:33:31 +02001559 if (nf_ct_is_confirmed(ct))
1560 extra_jiffies += nfct_time_stamp;
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001561
Florian Westphalf330a7f2016-08-25 15:33:31 +02001562 ct->timeout = extra_jiffies;
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +05301563/* Refresh the NAT type entry. */
1564#if defined(CONFIG_IP_NF_TARGET_NATTYPE_MODULE)
1565 nattype_ref_timer = rcu_dereference(nattype_refresh_timer);
1566 if (nattype_ref_timer)
Justin Teed3083bd2017-12-13 10:00:16 -08001567 nattype_ref_timer(ct->nattype_entry, ct->timeout);
Niranjan Reddy Dumbala410988b2013-01-30 17:34:45 +05301568#endif
1569
Patrick McHardy47d95042008-01-31 04:36:31 -08001570acct:
Mohammed Javid53d20ee2016-02-10 17:29:57 +05301571 if (do_acct) {
1572 acct = nf_conn_acct_find(ct);
1573 if (acct) {
1574 struct nf_conn_counter *counter = acct->counter;
1575
1576 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1577 atomic64_add(skb->len, &counter
1578 [CTINFO2DIR(ctinfo)].bytes);
1579
1580 pkts =
1581 atomic64_read(&counter[CTINFO2DIR(ctinfo)].packets) +
1582 atomic64_read(&counter[!CTINFO2DIR(ctinfo)].packets);
1583 /* Report if the packet threshold is reached. */
1584 if ((nf_conntrack_pkt_threshold > 0) &&
1585 (pkts == nf_conntrack_pkt_threshold)) {
1586 nf_conntrack_event_cache(IPCT_COUNTER, ct);
1587 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
1588 nf_ct_deliver_cached_events(ct);
1589 }
1590 }
1591 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001592}
Patrick McHardy13b18332006-12-02 22:11:25 -08001593EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001594
Florian Westphalad667132016-08-25 15:33:35 +02001595bool nf_ct_kill_acct(struct nf_conn *ct,
1596 enum ip_conntrack_info ctinfo,
1597 const struct sk_buff *skb)
Patrick McHardy51091762008-06-09 15:59:06 -07001598{
Florian Westphalad667132016-08-25 15:33:35 +02001599 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001600
Florian Westphalf330a7f2016-08-25 15:33:31 +02001601 return nf_ct_delete(ct, 0, 0);
Patrick McHardy51091762008-06-09 15:59:06 -07001602}
Florian Westphalad667132016-08-25 15:33:35 +02001603EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001604
Igor Maravićc0cd1152011-12-12 02:58:24 +00001605#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001606
1607#include <linux/netfilter/nfnetlink.h>
1608#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001609#include <linux/mutex.h>
1610
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001611/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1612 * in ip_conntrack_core, since we don't want the protocols to autoload
1613 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001614int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001615 const struct nf_conntrack_tuple *tuple)
1616{
David S. Millerbae65be2012-04-01 18:58:28 -04001617 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1618 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1619 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001620 return 0;
1621
Patrick McHardydf6fb862007-09-28 14:37:03 -07001622nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001623 return -1;
1624}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001625EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001626
Patrick McHardyf73e9242007-09-28 14:39:55 -07001627const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1628 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1629 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001630};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001631EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001632
Patrick McHardyfdf70832007-09-28 14:37:41 -07001633int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001634 struct nf_conntrack_tuple *t)
1635{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001636 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001637 return -EINVAL;
1638
Patrick McHardy77236b62007-12-17 22:29:45 -08001639 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1640 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001641
1642 return 0;
1643}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001644EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001645
1646int nf_ct_port_nlattr_tuple_size(void)
1647{
1648 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1649}
1650EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001651#endif
1652
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001653/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001654static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001655{
1656 struct nf_conn *ct;
1657 enum ip_conntrack_info ctinfo;
1658
1659 /* This ICMP is in reverse direction to the packet which caused it */
1660 ct = nf_ct_get(skb, &ctinfo);
1661 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001662 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001663 else
1664 ctinfo = IP_CT_RELATED;
1665
1666 /* Attach to new skbuff, and increment count */
1667 nskb->nfct = &ct->ct_general;
1668 nskb->nfctinfo = ctinfo;
1669 nf_conntrack_get(nskb->nfct);
1670}
1671
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001672/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001673static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001674get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001675 void *data, unsigned int *bucket)
1676{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001677 struct nf_conntrack_tuple_hash *h;
1678 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001679 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001680 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001681
Florian Westphal56d52d42016-05-02 18:39:55 +02001682 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001683 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1684 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001685 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001686 if (*bucket < nf_conntrack_htable_size) {
1687 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001688 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1689 continue;
1690 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001691 if (net_eq(nf_ct_net(ct), net) &&
1692 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001693 goto found;
1694 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001695 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001696 spin_unlock(lockp);
1697 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001698 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001699 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001700
Patrick McHardydf0933d2006-09-20 11:57:53 -07001701 return NULL;
1702found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001703 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001704 spin_unlock(lockp);
1705 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001706 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001707}
1708
Florian Westphalf51c4542017-05-21 12:52:56 +02001709static void
1710__nf_ct_unconfirmed_destroy(struct net *net)
1711{
1712 int cpu;
1713
1714 for_each_possible_cpu(cpu) {
1715 struct nf_conntrack_tuple_hash *h;
1716 struct hlist_nulls_node *n;
1717 struct ct_pcpu *pcpu;
1718
1719 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1720
1721 spin_lock_bh(&pcpu->lock);
1722 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1723 struct nf_conn *ct;
1724
1725 ct = nf_ct_tuplehash_to_ctrack(h);
1726
1727 /* we cannot call iter() on unconfirmed list, the
1728 * owning cpu can reallocate ct->ext at any time.
1729 */
1730 set_bit(IPS_DYING_BIT, &ct->status);
1731 }
1732 spin_unlock_bh(&pcpu->lock);
1733 cond_resched();
1734 }
1735}
1736
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001737void nf_ct_iterate_cleanup(struct net *net,
1738 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001739 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001740{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001741 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001742 unsigned int bucket = 0;
1743
Florian Westphald93c6252016-01-20 11:16:43 +01001744 might_sleep();
1745
Florian Westphal88b68bc2016-04-28 19:13:42 +02001746 if (atomic_read(&net->ct.count) == 0)
1747 return;
1748
Florian Westphalf51c4542017-05-21 12:52:56 +02001749 __nf_ct_unconfirmed_destroy(net);
1750
1751 synchronize_net();
1752
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001753 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001754 /* Time to push up daises... */
Florian Westphal02982c22013-07-29 15:41:54 +02001755
Florian Westphalf330a7f2016-08-25 15:33:31 +02001756 nf_ct_delete(ct, portid, report);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001757 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001758 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001759 }
1760}
Patrick McHardy13b18332006-12-02 22:11:25 -08001761EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001762
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001763static int kill_all(struct nf_conn *i, void *data)
1764{
1765 return 1;
1766}
1767
Patrick McHardyd862a662011-01-14 15:45:56 +01001768void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001769{
Patrick McHardyd862a662011-01-14 15:45:56 +01001770 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001771 vfree(hash);
1772 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001773 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001774 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001775}
Patrick McHardyac565e52007-07-07 22:30:08 -07001776EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001777
Eric Dumazetb3c51632010-06-09 14:43:38 +02001778static int untrack_refs(void)
1779{
1780 int cnt = 0, cpu;
1781
1782 for_each_possible_cpu(cpu) {
1783 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1784
1785 cnt += atomic_read(&ct->ct_general.use) - 1;
1786 }
1787 return cnt;
1788}
1789
Gao fengf94161c2013-01-21 22:10:24 +00001790void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001791{
Florian Westphalb87a2f92016-08-25 15:33:33 +02001792 conntrack_gc_work.exiting = true;
Gao fengf94161c2013-01-21 22:10:24 +00001793 RCU_INIT_POINTER(ip_ct_attach, NULL);
1794}
1795
1796void nf_conntrack_cleanup_end(void)
1797{
1798 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001799 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001800 schedule();
1801
Florian Westphalb87a2f92016-08-25 15:33:33 +02001802 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
Florian Westphal56d52d42016-05-02 18:39:55 +02001803 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1804
Gao feng04d87002013-01-21 22:10:32 +00001805 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001806 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001807 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001808 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001809 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001810 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001811 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001812 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001813 nf_conntrack_expect_fini();
Florian Westphal77571142016-06-10 17:25:19 +02001814
1815 kmem_cache_destroy(nf_conntrack_cachep);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001816}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001817
Gao fengf94161c2013-01-21 22:10:24 +00001818/*
1819 * Mishearing the voices in his head, our hero wonders how he's
1820 * supposed to kill the mall.
1821 */
1822void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001823{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001824 LIST_HEAD(single);
1825
1826 list_add(&net->exit_list, &single);
1827 nf_conntrack_cleanup_net_list(&single);
1828}
1829
1830void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1831{
1832 int busy;
1833 struct net *net;
1834
Gao fengf94161c2013-01-21 22:10:24 +00001835 /*
1836 * This makes sure all current packets have passed through
1837 * netfilter framework. Roll on, two-stage module
1838 * delete...
1839 */
1840 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001841i_see_dead_people:
1842 busy = 0;
1843 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001844 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001845 if (atomic_read(&net->ct.count) != 0)
1846 busy = 1;
1847 }
1848 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001849 schedule();
1850 goto i_see_dead_people;
1851 }
1852
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001853 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001854 nf_conntrack_proto_pernet_fini(net);
1855 nf_conntrack_helper_pernet_fini(net);
1856 nf_conntrack_ecache_pernet_fini(net);
1857 nf_conntrack_tstamp_pernet_fini(net);
1858 nf_conntrack_acct_pernet_fini(net);
1859 nf_conntrack_expect_pernet_fini(net);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001860 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001861 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001862 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001863}
1864
Patrick McHardyd862a662011-01-14 15:45:56 +01001865void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001866{
Eric Dumazetea781f12009-03-25 21:05:46 +01001867 struct hlist_nulls_head *hash;
1868 unsigned int nr_slots, i;
1869 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001870
Florian Westphal9cc1c732016-04-24 01:18:21 +02001871 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1872 return NULL;
1873
Eric Dumazetea781f12009-03-25 21:05:46 +01001874 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1875 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
Florian Westphal9cc1c732016-04-24 01:18:21 +02001876
1877 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1878 return NULL;
1879
Eric Dumazetea781f12009-03-25 21:05:46 +01001880 sz = nr_slots * sizeof(struct hlist_nulls_head);
1881 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1882 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001883 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001884 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001885
Eric Dumazetea781f12009-03-25 21:05:46 +01001886 if (hash && nulls)
1887 for (i = 0; i < nr_slots; i++)
1888 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001889
1890 return hash;
1891}
Patrick McHardyac565e52007-07-07 22:30:08 -07001892EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001893
Florian Westphal3183ab82016-06-22 13:26:10 +02001894int nf_conntrack_hash_resize(unsigned int hashsize)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001895{
Florian Westphal3183ab82016-06-22 13:26:10 +02001896 int i, bucket;
1897 unsigned int old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001898 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001899 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001900 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001901
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001902 if (!hashsize)
1903 return -EINVAL;
1904
Patrick McHardyd862a662011-01-14 15:45:56 +01001905 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001906 if (!hash)
1907 return -ENOMEM;
1908
Florian Westphal3183ab82016-06-22 13:26:10 +02001909 old_size = nf_conntrack_htable_size;
1910 if (old_size == hashsize) {
1911 nf_ct_free_hashtable(hash, hashsize);
1912 return 0;
1913 }
1914
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001915 local_bh_disable();
1916 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001917 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001918
Patrick McHardy76507f62008-01-31 04:38:38 -08001919 /* Lookups in the old hash might happen in parallel, which means we
1920 * might get false negatives during connection lookup. New connections
1921 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001922 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001923 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001924
Florian Westphal56d52d42016-05-02 18:39:55 +02001925 for (i = 0; i < nf_conntrack_htable_size; i++) {
1926 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1927 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1928 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001929 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001930 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001931 bucket = __hash_conntrack(nf_ct_net(ct),
1932 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001933 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001934 }
1935 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001936 old_size = nf_conntrack_htable_size;
1937 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001938
Florian Westphal56d52d42016-05-02 18:39:55 +02001939 nf_conntrack_hash = hash;
1940 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001941
Florian Westphala3efd812016-04-18 16:16:59 +02001942 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001943 nf_conntrack_all_unlock();
1944 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001945
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001946 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001947 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001948 return 0;
1949}
Florian Westphal3183ab82016-06-22 13:26:10 +02001950
Kees Cook24da2c82017-10-17 19:04:42 -07001951int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp)
Florian Westphal3183ab82016-06-22 13:26:10 +02001952{
1953 unsigned int hashsize;
1954 int rc;
1955
1956 if (current->nsproxy->net_ns != &init_net)
1957 return -EOPNOTSUPP;
1958
1959 /* On boot, we can set this without any fancy locking. */
Andrey Ryabinind8a77d12018-07-06 16:38:53 +03001960 if (!nf_conntrack_hash)
Florian Westphal3183ab82016-06-22 13:26:10 +02001961 return param_set_uint(val, kp);
1962
1963 rc = kstrtouint(val, 0, &hashsize);
1964 if (rc)
1965 return rc;
1966
1967 return nf_conntrack_hash_resize(hashsize);
1968}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001969EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001970
Patrick McHardyfae718d2007-12-24 21:09:10 -08001971module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001972 &nf_conntrack_htable_size, 0600);
1973
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001974void nf_ct_untracked_status_or(unsigned long bits)
1975{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001976 int cpu;
1977
1978 for_each_possible_cpu(cpu)
1979 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001980}
1981EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1982
Gao fengf94161c2013-01-21 22:10:24 +00001983int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001984{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001985 int max_factor = 8;
Florian Westphal0c5366b2016-05-09 16:24:32 +02001986 int ret = -ENOMEM;
1987 int i, cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001988
Florian Westphala3efd812016-04-18 16:16:59 +02001989 seqcount_init(&nf_conntrack_generation);
1990
Eric Dumazetd5d20912014-03-17 13:37:53 -07001991 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001992 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001993
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001994 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001995 /* Idea from tcp.c: use 1/16384 of memory.
1996 * On i386: 32MB machine has 512 buckets.
1997 * >= 1GB machines have 16384 buckets.
1998 * >= 4GB machines have 65536 buckets.
1999 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08002000 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07002001 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07002002 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02002003 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
2004 nf_conntrack_htable_size = 65536;
2005 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07002006 nf_conntrack_htable_size = 16384;
2007 if (nf_conntrack_htable_size < 32)
2008 nf_conntrack_htable_size = 32;
2009
2010 /* Use a max. factor of four by default to get the same max as
2011 * with the old struct list_heads. When a table size is given
2012 * we use the old value of 8 to avoid reducing the max.
2013 * entries. */
2014 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08002015 }
Florian Westphal56d52d42016-05-02 18:39:55 +02002016
2017 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
2018 if (!nf_conntrack_hash)
2019 return -ENOMEM;
2020
Patrick McHardyf205c5e2007-07-07 22:28:14 -07002021 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07002022
Florian Westphal0c5366b2016-05-09 16:24:32 +02002023 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
2024 sizeof(struct nf_conn), 0,
Florian Westphal5a75cde2016-06-10 22:25:22 +02002025 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
Florian Westphal0c5366b2016-05-09 16:24:32 +02002026 if (!nf_conntrack_cachep)
2027 goto err_cachep;
2028
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02002029 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07002030 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
2031 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00002032
2033 ret = nf_conntrack_expect_init();
2034 if (ret < 0)
2035 goto err_expect;
2036
Gao fengb7ff3a12013-01-21 22:10:26 +00002037 ret = nf_conntrack_acct_init();
2038 if (ret < 0)
2039 goto err_acct;
2040
Gao feng73f40012013-01-21 22:10:27 +00002041 ret = nf_conntrack_tstamp_init();
2042 if (ret < 0)
2043 goto err_tstamp;
2044
Gao feng3fe0f942013-01-21 22:10:28 +00002045 ret = nf_conntrack_ecache_init();
2046 if (ret < 0)
2047 goto err_ecache;
2048
Gao feng86840942013-01-21 22:10:29 +00002049 ret = nf_conntrack_timeout_init();
2050 if (ret < 0)
2051 goto err_timeout;
2052
Gao feng5e615b22013-01-21 22:10:30 +00002053 ret = nf_conntrack_helper_init();
2054 if (ret < 0)
2055 goto err_helper;
2056
Gao feng5f69b8f2013-01-21 22:10:31 +00002057 ret = nf_conntrack_labels_init();
2058 if (ret < 0)
2059 goto err_labels;
2060
Patrick McHardy41d73ec2013-08-27 08:50:12 +02002061 ret = nf_conntrack_seqadj_init();
2062 if (ret < 0)
2063 goto err_seqadj;
2064
Gao feng04d87002013-01-21 22:10:32 +00002065 ret = nf_conntrack_proto_init();
2066 if (ret < 0)
2067 goto err_proto;
2068
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08002069 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02002070 for_each_possible_cpu(cpu) {
2071 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02002072 write_pnet(&ct->ct_net, &init_net);
2073 atomic_set(&ct->ct_general.use, 1);
2074 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08002075 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02002076 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Florian Westphalb87a2f92016-08-25 15:33:33 +02002077
2078 conntrack_gc_work_init(&conntrack_gc_work);
Vincent Guittot44066712017-11-02 16:16:07 +01002079 queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ);
Florian Westphalb87a2f92016-08-25 15:33:33 +02002080
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002081 return 0;
2082
Gao feng04d87002013-01-21 22:10:32 +00002083err_proto:
Patrick McHardy41d73ec2013-08-27 08:50:12 +02002084 nf_conntrack_seqadj_fini();
2085err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00002086 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00002087err_labels:
2088 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00002089err_helper:
2090 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00002091err_timeout:
2092 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00002093err_ecache:
2094 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00002095err_tstamp:
2096 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00002097err_acct:
2098 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00002099err_expect:
Florian Westphal0c5366b2016-05-09 16:24:32 +02002100 kmem_cache_destroy(nf_conntrack_cachep);
2101err_cachep:
Florian Westphal56d52d42016-05-02 18:39:55 +02002102 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002103 return ret;
2104}
2105
Gao fengf94161c2013-01-21 22:10:24 +00002106void nf_conntrack_init_end(void)
2107{
2108 /* For use by REJECT target */
2109 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
2110 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00002111}
2112
Eric Dumazet8cc20192009-06-22 14:13:55 +02002113/*
2114 * We need to use special "null" values, not used in hash table
2115 */
2116#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
2117#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00002118#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02002119
Gao fengf94161c2013-01-21 22:10:24 +00002120int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002121{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002122 int ret = -ENOMEM;
2123 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002124
2125 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002126
2127 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
2128 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002129 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002130
2131 for_each_possible_cpu(cpu) {
2132 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
2133
2134 spin_lock_init(&pcpu->lock);
2135 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
2136 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002137 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08002138
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002139 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2140 if (!net->ct.stat)
2141 goto err_pcpu_lists;
2142
Gao feng83b4dbe2013-01-21 22:10:25 +00002143 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002144 if (ret < 0)
2145 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00002146 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07002147 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002148 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00002149 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002150 if (ret < 0)
2151 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00002152 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002153 if (ret < 0)
2154 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00002155 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002156 if (ret < 0)
2157 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00002158 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00002159 if (ret < 0)
2160 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002161 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00002162
Gao fengf94161c2013-01-21 22:10:24 +00002163err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00002164 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02002165err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00002166 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02002167err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00002168 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01002169err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00002170 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002171err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00002172 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002173err_expect:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002174 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01002175err_pcpu_lists:
2176 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02002177err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02002178 return ret;
2179}