blob: 25e0c2677a12612021bb33815eb2515cacfe106e [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>
28#include <linux/err.h>
29#include <linux/percpu.h>
30#include <linux/moduleparam.h>
31#include <linux/notifier.h>
32#include <linux/kernel.h>
33#include <linux/netdevice.h>
34#include <linux/socket.h>
Al Virod7fe0f22006-12-03 23:15:30 -050035#include <linux/mm.h>
Patrick McHardyd696c7b2010-02-08 11:18:07 -080036#include <linux/nsproxy.h>
Eric Dumazetea781f12009-03-25 21:05:46 +010037#include <linux/rculist_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039#include <net/netfilter/nf_conntrack.h>
40#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010041#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010042#include <net/netfilter/nf_conntrack_expect.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy41d73ec2013-08-27 08:50:12 +020044#include <net/netfilter/nf_conntrack_seqadj.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045#include <net/netfilter/nf_conntrack_core.h>
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -070046#include <net/netfilter/nf_conntrack_extend.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070047#include <net/netfilter/nf_conntrack_acct.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020048#include <net/netfilter/nf_conntrack_ecache.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010049#include <net/netfilter/nf_conntrack_zones.h>
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +010050#include <net/netfilter/nf_conntrack_timestamp.h>
Pablo Neira Ayusodd705072012-02-28 23:36:48 +010051#include <net/netfilter/nf_conntrack_timeout.h>
Florian Westphalc539f012013-01-11 06:30:44 +000052#include <net/netfilter/nf_conntrack_labels.h>
Patrick McHardy48b1de42013-08-27 08:50:14 +020053#include <net/netfilter/nf_conntrack_synproxy.h>
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070054#include <net/netfilter/nf_nat.h>
Patrick McHardye17b6662008-11-18 12:24:17 +010055#include <net/netfilter/nf_nat_core.h>
stephen hemminger49376362013-03-16 07:00:28 +000056#include <net/netfilter/nf_nat_helper.h>
Florian Westphal1b8c8a92016-05-03 00:25:58 +020057#include <net/netns/hash.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058
Harald Weltedc808fe2006-03-20 17:56:32 -080059#define NF_CONNTRACK_VERSION "0.5.0"
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060
Patrick McHardye17b6662008-11-18 12:24:17 +010061int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
62 enum nf_nat_manip_type manip,
Patrick McHardy39938322009-08-25 16:07:58 +020063 const struct nlattr *attr) __read_mostly;
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070064EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
65
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +010066__cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
67EXPORT_SYMBOL_GPL(nf_conntrack_locks);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +010069__cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
70EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
71
Florian Westphal56d52d42016-05-02 18:39:55 +020072struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
73EXPORT_SYMBOL_GPL(nf_conntrack_hash);
74
Sasha Levinb16c2912016-01-18 19:23:51 -050075static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
Florian Westphala3efd812016-04-18 16:16:59 +020076static __read_mostly seqcount_t nf_conntrack_generation;
Sasha Levinb16c2912016-01-18 19:23:51 -050077static __read_mostly bool nf_conntrack_locks_all;
78
79void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
80{
81 spin_lock(lock);
82 while (unlikely(nf_conntrack_locks_all)) {
83 spin_unlock(lock);
Nicholas Mc Guiree39365b2016-03-14 12:39:02 +010084 spin_unlock_wait(&nf_conntrack_locks_all_lock);
Sasha Levinb16c2912016-01-18 19:23:51 -050085 spin_lock(lock);
86 }
87}
88EXPORT_SYMBOL_GPL(nf_conntrack_lock);
89
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +010090static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
91{
92 h1 %= CONNTRACK_LOCKS;
93 h2 %= CONNTRACK_LOCKS;
94 spin_unlock(&nf_conntrack_locks[h1]);
95 if (h1 != h2)
96 spin_unlock(&nf_conntrack_locks[h2]);
97}
98
99/* return true if we need to recompute hashes (in case hash table was resized) */
100static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
101 unsigned int h2, unsigned int sequence)
102{
103 h1 %= CONNTRACK_LOCKS;
104 h2 %= CONNTRACK_LOCKS;
105 if (h1 <= h2) {
Sasha Levinb16c2912016-01-18 19:23:51 -0500106 nf_conntrack_lock(&nf_conntrack_locks[h1]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100107 if (h1 != h2)
108 spin_lock_nested(&nf_conntrack_locks[h2],
109 SINGLE_DEPTH_NESTING);
110 } else {
Sasha Levinb16c2912016-01-18 19:23:51 -0500111 nf_conntrack_lock(&nf_conntrack_locks[h2]);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100112 spin_lock_nested(&nf_conntrack_locks[h1],
113 SINGLE_DEPTH_NESTING);
114 }
Florian Westphala3efd812016-04-18 16:16:59 +0200115 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100116 nf_conntrack_double_unlock(h1, h2);
117 return true;
118 }
119 return false;
120}
121
122static void nf_conntrack_all_lock(void)
123{
124 int i;
125
Sasha Levinb16c2912016-01-18 19:23:51 -0500126 spin_lock(&nf_conntrack_locks_all_lock);
127 nf_conntrack_locks_all = true;
128
129 for (i = 0; i < CONNTRACK_LOCKS; i++) {
Nicholas Mc Guiree39365b2016-03-14 12:39:02 +0100130 spin_unlock_wait(&nf_conntrack_locks[i]);
Sasha Levinb16c2912016-01-18 19:23:51 -0500131 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100132}
133
134static void nf_conntrack_all_unlock(void)
135{
Sasha Levinb16c2912016-01-18 19:23:51 -0500136 nf_conntrack_locks_all = false;
137 spin_unlock(&nf_conntrack_locks_all_lock);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100138}
139
Martin Josefssone2b76062006-11-29 02:35:04 +0100140unsigned int nf_conntrack_htable_size __read_mostly;
Patrick McHardy13b18332006-12-02 22:11:25 -0800141EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
142
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100143unsigned int nf_conntrack_max __read_mostly;
Patrick McHardya999e682006-11-29 02:35:20 +0100144EXPORT_SYMBOL_GPL(nf_conntrack_max);
Patrick McHardy13b18332006-12-02 22:11:25 -0800145
Eric Dumazetb3c51632010-06-09 14:43:38 +0200146DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
147EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
Patrick McHardy13b18332006-12-02 22:11:25 -0800148
Florian Westphal141658f2016-04-18 16:17:01 +0200149static unsigned int nf_conntrack_hash_rnd __read_mostly;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200151static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
152 const struct net *net)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153{
Patrick McHardy07949352008-01-31 04:40:52 -0800154 unsigned int n;
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200155 u32 seed;
Sami Farin9b887902007-03-14 16:43:00 -0700156
Florian Westphal141658f2016-04-18 16:17:01 +0200157 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
158
Patrick McHardy07949352008-01-31 04:40:52 -0800159 /* The direction must be ignored, so we hash everything up to the
160 * destination ports (which is a multiple of 4) and treat the last
161 * three bytes manually.
162 */
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200163 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
Patrick McHardy07949352008-01-31 04:40:52 -0800164 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200165 return jhash2((u32 *)tuple, n, seed ^
Changli Gao99f07e92010-09-21 17:49:20 +0200166 (((__force __u16)tuple->dst.u.all << 16) |
167 tuple->dst.protonum));
168}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169
Florian Westphal56d52d42016-05-02 18:39:55 +0200170static u32 scale_hash(u32 hash)
Changli Gao99f07e92010-09-21 17:49:20 +0200171{
Florian Westphal56d52d42016-05-02 18:39:55 +0200172 return reciprocal_scale(hash, nf_conntrack_htable_size);
Changli Gao99f07e92010-09-21 17:49:20 +0200173}
174
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200175static u32 __hash_conntrack(const struct net *net,
176 const struct nf_conntrack_tuple *tuple,
177 unsigned int size)
Changli Gao99f07e92010-09-21 17:49:20 +0200178{
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200179 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180}
181
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200182static u32 hash_conntrack(const struct net *net,
183 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184{
Florian Westphal56d52d42016-05-02 18:39:55 +0200185 return scale_hash(hash_conntrack_raw(tuple, net));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186}
187
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200188bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189nf_ct_get_tuple(const struct sk_buff *skb,
190 unsigned int nhoff,
191 unsigned int dataoff,
192 u_int16_t l3num,
193 u_int8_t protonum,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500194 struct net *net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195 struct nf_conntrack_tuple *tuple,
196 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100197 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198{
Philip Craig443a70d2008-04-29 03:35:10 -0700199 memset(tuple, 0, sizeof(*tuple));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200
201 tuple->src.l3num = l3num;
202 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200203 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204
205 tuple->dst.protonum = protonum;
206 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
207
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500208 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209}
Patrick McHardy13b18332006-12-02 22:11:25 -0800210EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200212bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500213 u_int16_t l3num,
214 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700215{
216 struct nf_conntrack_l3proto *l3proto;
217 struct nf_conntrack_l4proto *l4proto;
218 unsigned int protoff;
219 u_int8_t protonum;
220 int ret;
221
222 rcu_read_lock();
223
224 l3proto = __nf_ct_l3proto_find(l3num);
225 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
226 if (ret != NF_ACCEPT) {
227 rcu_read_unlock();
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200228 return false;
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700229 }
230
231 l4proto = __nf_ct_l4proto_find(l3num, protonum);
232
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500233 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700234 l3proto, l4proto);
235
236 rcu_read_unlock();
237 return ret;
238}
239EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
240
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200241bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800242nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
243 const struct nf_conntrack_tuple *orig,
244 const struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100245 const struct nf_conntrack_l4proto *l4proto)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246{
Philip Craig443a70d2008-04-29 03:35:10 -0700247 memset(inverse, 0, sizeof(*inverse));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248
249 inverse->src.l3num = orig->src.l3num;
250 if (l3proto->invert_tuple(inverse, orig) == 0)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200251 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800252
253 inverse->dst.dir = !orig->dst.dir;
254
255 inverse->dst.protonum = orig->dst.protonum;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100256 return l4proto->invert_tuple(inverse, orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257}
Patrick McHardy13b18332006-12-02 22:11:25 -0800258EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800259
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800260static void
261clean_from_lists(struct nf_conn *ct)
262{
Patrick McHardy0d537782007-07-07 22:39:38 -0700263 pr_debug("clean_from_lists(%p)\n", ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100264 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
265 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800266
267 /* Destroy all pending expectations */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800268 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800269}
270
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100271/* must be called with local_bh_disable */
272static void nf_ct_add_to_dying_list(struct nf_conn *ct)
273{
274 struct ct_pcpu *pcpu;
275
276 /* add this conntrack to the (per cpu) dying list */
277 ct->cpu = smp_processor_id();
278 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
279
280 spin_lock(&pcpu->lock);
281 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
282 &pcpu->dying);
283 spin_unlock(&pcpu->lock);
284}
285
286/* must be called with local_bh_disable */
287static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
288{
289 struct ct_pcpu *pcpu;
290
291 /* add this conntrack to the (per cpu) unconfirmed list */
292 ct->cpu = smp_processor_id();
293 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
294
295 spin_lock(&pcpu->lock);
296 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
297 &pcpu->unconfirmed);
298 spin_unlock(&pcpu->lock);
299}
300
301/* must be called with local_bh_disable */
302static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
303{
304 struct ct_pcpu *pcpu;
305
306 /* We overload first tuple to link into unconfirmed or dying list.*/
307 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
308
309 spin_lock(&pcpu->lock);
310 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
311 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
312 spin_unlock(&pcpu->lock);
313}
314
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200315/* Released via destroy_conntrack() */
Daniel Borkmann308ac912015-08-08 21:40:01 +0200316struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
317 const struct nf_conntrack_zone *zone,
318 gfp_t flags)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200319{
320 struct nf_conn *tmpl;
321
Joe Stringerf58e5aa2015-08-04 18:34:00 -0700322 tmpl = kzalloc(sizeof(*tmpl), flags);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200323 if (tmpl == NULL)
324 return NULL;
325
326 tmpl->status = IPS_TEMPLATE;
327 write_pnet(&tmpl->ct_net, net);
328
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200329 if (nf_ct_zone_add(tmpl, flags, zone) < 0)
330 goto out_free;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200331
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200332 atomic_set(&tmpl->ct_general.use, 0);
333
334 return tmpl;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200335out_free:
336 kfree(tmpl);
337 return NULL;
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200338}
339EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
340
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200341void nf_ct_tmpl_free(struct nf_conn *tmpl)
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200342{
343 nf_ct_ext_destroy(tmpl);
344 nf_ct_ext_free(tmpl);
345 kfree(tmpl);
346}
Daniel Borkmann9cf94ea2015-08-31 19:11:02 +0200347EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200348
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349static void
350destroy_conntrack(struct nf_conntrack *nfct)
351{
352 struct nf_conn *ct = (struct nf_conn *)nfct;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200353 struct net *net = nf_ct_net(ct);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100354 struct nf_conntrack_l4proto *l4proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355
Patrick McHardy0d537782007-07-07 22:39:38 -0700356 pr_debug("destroy_conntrack(%p)\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800357 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
358 NF_CT_ASSERT(!timer_pending(&ct->timeout));
359
Pablo Neira Ayuso0838aa72015-07-13 15:11:48 +0200360 if (unlikely(nf_ct_is_template(ct))) {
361 nf_ct_tmpl_free(ct);
362 return;
363 }
Patrick McHardy923f4902007-02-12 11:12:57 -0800364 rcu_read_lock();
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200365 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Pablo Neira Ayuso4b4ceb92016-05-01 00:34:37 +0200366 if (l4proto->destroy)
Martin Josefsson605dcad2006-11-29 02:35:06 +0100367 l4proto->destroy(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368
Patrick McHardy982d9a92007-02-12 11:14:11 -0800369 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800370
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100371 local_bh_disable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800372 /* Expectations will have been removed in clean_from_lists,
373 * except TFTP can create an expectation on the first packet,
374 * before connection is in the list, so we need to clean here,
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100375 * too.
376 */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800377 nf_ct_remove_expectations(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800378
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100379 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800380
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200381 NF_CT_STAT_INC(net, delete);
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100382 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383
384 if (ct->master)
385 nf_ct_put(ct->master);
386
Patrick McHardy0d537782007-07-07 22:39:38 -0700387 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800388 nf_conntrack_free(ct);
389}
390
Florian Westphal02982c22013-07-29 15:41:54 +0200391static void nf_ct_delete_from_lists(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392{
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200393 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100394 unsigned int hash, reply_hash;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100395 unsigned int sequence;
Patrick McHardy5397e972007-05-19 14:23:52 -0700396
Pablo Neira Ayuso9858a3a2009-06-13 12:28:22 +0200397 nf_ct_helper_destroy(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100398
399 local_bh_disable();
400 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200401 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200402 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100403 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200404 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100405 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
406 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
407
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800408 clean_from_lists(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100409 nf_conntrack_double_unlock(hash, reply_hash);
410
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +0100411 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100412
413 NF_CT_STAT_INC(net, delete_list);
414 local_bh_enable();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200415}
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200416
Florian Westphal02982c22013-07-29 15:41:54 +0200417bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200418{
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100419 struct nf_conn_tstamp *tstamp;
420
421 tstamp = nf_conn_tstamp_find(ct);
422 if (tstamp && tstamp->stop == 0)
Eric Dumazetd2de8752014-08-22 18:32:09 -0700423 tstamp->stop = ktime_get_real_ns();
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200424
Florian Westphal95005072014-06-10 23:12:56 +0200425 if (nf_ct_is_dying(ct))
426 goto delete;
427
428 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
429 portid, report) < 0) {
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200430 /* destroy event was not delivered */
431 nf_ct_delete_from_lists(ct);
Florian Westphal95005072014-06-10 23:12:56 +0200432 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
Florian Westphal02982c22013-07-29 15:41:54 +0200433 return false;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200434 }
Florian Westphal95005072014-06-10 23:12:56 +0200435
436 nf_conntrack_ecache_work(nf_ct_net(ct));
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200437 set_bit(IPS_DYING_BIT, &ct->status);
Florian Westphal95005072014-06-10 23:12:56 +0200438 delete:
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200439 nf_ct_delete_from_lists(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440 nf_ct_put(ct);
Florian Westphal02982c22013-07-29 15:41:54 +0200441 return true;
442}
443EXPORT_SYMBOL_GPL(nf_ct_delete);
444
445static void death_by_timeout(unsigned long ul_conntrack)
446{
447 nf_ct_delete((struct nf_conn *)ul_conntrack, 0, 0);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800448}
449
Andrey Vaginc6825c02014-01-29 19:34:14 +0100450static inline bool
451nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
Daniel Borkmann308ac912015-08-08 21:40:01 +0200452 const struct nf_conntrack_tuple *tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200453 const struct nf_conntrack_zone *zone,
454 const struct net *net)
Andrey Vaginc6825c02014-01-29 19:34:14 +0100455{
456 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
457
458 /* A conntrack can be recreated with the equal tuple,
459 * so we need to check that the conntrack is confirmed
460 */
461 return nf_ct_tuple_equal(tuple, &h->tuple) &&
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200462 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200463 nf_ct_is_confirmed(ct) &&
464 net_eq(net, nf_ct_net(ct));
Andrey Vaginc6825c02014-01-29 19:34:14 +0100465}
466
Eric Dumazetea781f12009-03-25 21:05:46 +0100467/*
468 * Warning :
469 * - Caller must take a reference on returned object
470 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
Eric Dumazetea781f12009-03-25 21:05:46 +0100471 */
Changli Gao99f07e92010-09-21 17:49:20 +0200472static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200473____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200474 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800475{
476 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200477 struct hlist_nulls_head *ct_hash;
Eric Dumazetea781f12009-03-25 21:05:46 +0100478 struct hlist_nulls_node *n;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200479 unsigned int bucket, sequence;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480
Eric Dumazetea781f12009-03-25 21:05:46 +0100481begin:
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200482 do {
483 sequence = read_seqcount_begin(&nf_conntrack_generation);
Florian Westphal56d52d42016-05-02 18:39:55 +0200484 bucket = scale_hash(hash);
485 ct_hash = nf_conntrack_hash;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200486 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
487
488 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
Florian Westphale0c7d472016-04-28 19:13:45 +0200489 if (nf_ct_key_equal(h, tuple, zone, net)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200490 NF_CT_STAT_INC_ATOMIC(net, found);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800491 return h;
492 }
Florian Westphal2cf12342016-04-28 19:13:40 +0200493 NF_CT_STAT_INC_ATOMIC(net, searched);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800494 }
Eric Dumazetea781f12009-03-25 21:05:46 +0100495 /*
496 * if the nulls value we got at the end of this lookup is
497 * not the expected one, we must restart lookup.
498 * We probably met an item that was moved to another chain.
499 */
Changli Gao99f07e92010-09-21 17:49:20 +0200500 if (get_nulls_value(n) != bucket) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200501 NF_CT_STAT_INC_ATOMIC(net, search_restart);
Eric Dumazetea781f12009-03-25 21:05:46 +0100502 goto begin;
Jesper Dangaard Broueraf740b22010-04-23 12:34:56 +0200503 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800504
505 return NULL;
506}
Changli Gao99f07e92010-09-21 17:49:20 +0200507
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800508/* Find a connection corresponding to a tuple. */
Changli Gao99f07e92010-09-21 17:49:20 +0200509static struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200510__nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200511 const struct nf_conntrack_tuple *tuple, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512{
513 struct nf_conntrack_tuple_hash *h;
Patrick McHardy76507f62008-01-31 04:38:38 -0800514 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515
Patrick McHardy76507f62008-01-31 04:38:38 -0800516 rcu_read_lock();
Eric Dumazetea781f12009-03-25 21:05:46 +0100517begin:
Changli Gao99f07e92010-09-21 17:49:20 +0200518 h = ____nf_conntrack_find(net, zone, tuple, hash);
Patrick McHardy76507f62008-01-31 04:38:38 -0800519 if (h) {
520 ct = nf_ct_tuplehash_to_ctrack(h);
Patrick McHardy8d8890b72009-06-22 14:14:41 +0200521 if (unlikely(nf_ct_is_dying(ct) ||
522 !atomic_inc_not_zero(&ct->ct_general.use)))
Patrick McHardy76507f62008-01-31 04:38:38 -0800523 h = NULL;
Eric Dumazetea781f12009-03-25 21:05:46 +0100524 else {
Florian Westphale0c7d472016-04-28 19:13:45 +0200525 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
Eric Dumazetea781f12009-03-25 21:05:46 +0100526 nf_ct_put(ct);
527 goto begin;
528 }
529 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800530 }
531 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800532
533 return h;
534}
Changli Gao99f07e92010-09-21 17:49:20 +0200535
536struct nf_conntrack_tuple_hash *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200537nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200538 const struct nf_conntrack_tuple *tuple)
539{
540 return __nf_conntrack_find_get(net, zone, tuple,
Florian Westphal1b8c8a92016-05-03 00:25:58 +0200541 hash_conntrack_raw(tuple, net));
Changli Gao99f07e92010-09-21 17:49:20 +0200542}
Patrick McHardy13b18332006-12-02 22:11:25 -0800543EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800545static void __nf_conntrack_hash_insert(struct nf_conn *ct,
546 unsigned int hash,
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100547 unsigned int reply_hash)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800548{
Eric Dumazetea781f12009-03-25 21:05:46 +0100549 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200550 &nf_conntrack_hash[hash]);
Eric Dumazetea781f12009-03-25 21:05:46 +0100551 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
Florian Westphal56d52d42016-05-02 18:39:55 +0200552 &nf_conntrack_hash[reply_hash]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800553}
554
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100555int
556nf_conntrack_hash_check_insert(struct nf_conn *ct)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800557{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200558 const struct nf_conntrack_zone *zone;
Patrick McHardyd696c7b2010-02-08 11:18:07 -0800559 struct net *net = nf_ct_net(ct);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100560 unsigned int hash, reply_hash;
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100561 struct nf_conntrack_tuple_hash *h;
562 struct hlist_nulls_node *n;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100563 unsigned int sequence;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800564
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100565 zone = nf_ct_zone(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800566
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100567 local_bh_disable();
568 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200569 sequence = read_seqcount_begin(&nf_conntrack_generation);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200570 hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100571 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200572 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100573 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
574 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100575
576 /* See if there's one in the list already, including reverse */
Florian Westphal56d52d42016-05-02 18:39:55 +0200577 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200578 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200579 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100580 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200581
Florian Westphal56d52d42016-05-02 18:39:55 +0200582 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200583 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200584 zone, net))
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100585 goto out;
586
587 add_timer(&ct->timeout);
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100588 smp_wmb();
589 /* The caller holds a reference to this object */
590 atomic_set(&ct->ct_general.use, 2);
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100591 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100592 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100593 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100594 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100595 return 0;
596
597out:
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100598 nf_conntrack_double_unlock(hash, reply_hash);
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100599 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100600 local_bh_enable();
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100601 return -EEXIST;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800602}
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100603EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800604
Pablo Neira Ayusoba767382016-05-02 21:28:57 +0200605static inline void nf_ct_acct_update(struct nf_conn *ct,
606 enum ip_conntrack_info ctinfo,
607 unsigned int len)
608{
609 struct nf_conn_acct *acct;
610
611 acct = nf_conn_acct_find(ct);
612 if (acct) {
613 struct nf_conn_counter *counter = acct->counter;
614
615 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
616 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
617 }
618}
619
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800620/* Confirm a connection given skb; places it in hash table */
621int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700622__nf_conntrack_confirm(struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800623{
Daniel Borkmann308ac912015-08-08 21:40:01 +0200624 const struct nf_conntrack_zone *zone;
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100625 unsigned int hash, reply_hash;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700626 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800627 struct nf_conn *ct;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700628 struct nf_conn_help *help;
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100629 struct nf_conn_tstamp *tstamp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100630 struct hlist_nulls_node *n;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800631 enum ip_conntrack_info ctinfo;
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200632 struct net *net;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100633 unsigned int sequence;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800634
Herbert Xu3db05fe2007-10-15 00:53:15 -0700635 ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200636 net = nf_ct_net(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800637
638 /* ipt_REJECT uses nf_conntrack_attach to attach related
639 ICMP/TCP RST packets in other direction. Actual packet
640 which created connection will be IP_CT_NEW or for an
641 expected connection, IP_CT_RELATED. */
642 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
643 return NF_ACCEPT;
644
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100645 zone = nf_ct_zone(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100646 local_bh_disable();
647
648 do {
Florian Westphala3efd812016-04-18 16:16:59 +0200649 sequence = read_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100650 /* reuse the hash saved before */
651 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
Florian Westphal56d52d42016-05-02 18:39:55 +0200652 hash = scale_hash(hash);
Daniel Borkmanndeedb592015-08-14 16:03:39 +0200653 reply_hash = hash_conntrack(net,
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100654 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
655
656 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800657
658 /* We're not in hash table, and we refuse to set up related
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100659 * connections for unconfirmed conns. But packet copies and
660 * REJECT will give spurious warnings here.
661 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800662 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
663
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300664 /* No external references means no one else could have
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100665 * confirmed us.
666 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800667 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
Patrick McHardy0d537782007-07-07 22:39:38 -0700668 pr_debug("Confirming conntrack %p\n", ct);
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100669 /* We have to check the DYING flag after unlink to prevent
670 * a race against nf_ct_get_next_corpse() possibly called from
671 * user context, else we insert an already 'dead' hash, blocking
672 * further use of that particular connection -JM.
673 */
674 nf_ct_del_from_dying_or_unconfirmed_list(ct);
Joerg Marxfc350772010-05-20 15:55:30 +0200675
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100676 if (unlikely(nf_ct_is_dying(ct)))
677 goto out;
Joerg Marxfc350772010-05-20 15:55:30 +0200678
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800679 /* See if there's one in the list already, including reverse:
680 NAT could have grabbed it without realizing, since we're
681 not in the hash. If there is, we lost race. */
Florian Westphal56d52d42016-05-02 18:39:55 +0200682 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200683 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200684 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700685 goto out;
Florian Westphal86804342016-04-28 19:13:43 +0200686
Florian Westphal56d52d42016-05-02 18:39:55 +0200687 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
Florian Westphal86804342016-04-28 19:13:43 +0200688 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
Florian Westphale0c7d472016-04-28 19:13:45 +0200689 zone, net))
Patrick McHardydf0933d2006-09-20 11:57:53 -0700690 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800691
Patrick McHardydf0933d2006-09-20 11:57:53 -0700692 /* Timer relative to confirmation time, not original
693 setting time, otherwise we'd get timer wrap in
694 weird delay cases. */
695 ct->timeout.expires += jiffies;
696 add_timer(&ct->timeout);
697 atomic_inc(&ct->ct_general.use);
Changli Gao45eec342011-01-18 15:08:13 +0100698 ct->status |= IPS_CONFIRMED;
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200699
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100700 /* set conntrack timestamp, if enabled. */
701 tstamp = nf_conn_tstamp_find(ct);
702 if (tstamp) {
703 if (skb->tstamp.tv64 == 0)
Joe Perchese3192692012-06-03 17:41:40 +0000704 __net_timestamp(skb);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100705
706 tstamp->start = ktime_to_ns(skb->tstamp);
707 }
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200708 /* Since the lookup is lockless, hash insertion must be done after
709 * starting the timer and setting the CONFIRMED bit. The RCU barriers
710 * guarantee that no other CPU can find the conntrack before the above
711 * stores are visible.
712 */
Jesper Dangaard Brouerb476b722014-03-03 14:44:54 +0100713 __nf_conntrack_hash_insert(ct, hash, reply_hash);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100714 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200715 NF_CT_STAT_INC(net, insert);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100716 local_bh_enable();
Patrick McHardy5c8ec912009-06-22 14:14:16 +0200717
Patrick McHardydf0933d2006-09-20 11:57:53 -0700718 help = nfct_help(ct);
719 if (help && help->helper)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200720 nf_conntrack_event_cache(IPCT_HELPER, ct);
Pablo Neira Ayuso17e6e4e2009-06-02 20:08:46 +0200721
Patrick McHardydf0933d2006-09-20 11:57:53 -0700722 nf_conntrack_event_cache(master_ct(ct) ?
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200723 IPCT_RELATED : IPCT_NEW, ct);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700724 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800725
Patrick McHardydf0933d2006-09-20 11:57:53 -0700726out:
Pablo Neira Ayuso8ca3f5e2014-11-25 00:14:47 +0100727 nf_ct_add_to_dying_list(ct);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100728 nf_conntrack_double_unlock(hash, reply_hash);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200729 NF_CT_STAT_INC(net, insert_failed);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100730 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800731 return NF_DROP;
732}
Patrick McHardy13b18332006-12-02 22:11:25 -0800733EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800734
735/* Returns true if a connection correspondings to the tuple (required
736 for NAT). */
737int
738nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
739 const struct nf_conn *ignored_conntrack)
740{
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200741 struct net *net = nf_ct_net(ignored_conntrack);
Daniel Borkmann308ac912015-08-08 21:40:01 +0200742 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800743 struct nf_conntrack_tuple_hash *h;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200744 struct hlist_nulls_head *ct_hash;
745 unsigned int hash, sequence;
Eric Dumazetea781f12009-03-25 21:05:46 +0100746 struct hlist_nulls_node *n;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100747 struct nf_conn *ct;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200748
749 zone = nf_ct_zone(ignored_conntrack);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800750
Florian Westphal2cf12342016-04-28 19:13:40 +0200751 rcu_read_lock();
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200752 do {
753 sequence = read_seqcount_begin(&nf_conntrack_generation);
754 hash = hash_conntrack(net, tuple);
Florian Westphal56d52d42016-05-02 18:39:55 +0200755 ct_hash = nf_conntrack_hash;
Florian Westphal5e3c61f2016-04-28 19:13:41 +0200756 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
757
758 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100759 ct = nf_ct_tuplehash_to_ctrack(h);
760 if (ct != ignored_conntrack &&
Florian Westphale0c7d472016-04-28 19:13:45 +0200761 nf_ct_key_equal(h, tuple, zone, net)) {
Florian Westphal2cf12342016-04-28 19:13:40 +0200762 NF_CT_STAT_INC_ATOMIC(net, found);
763 rcu_read_unlock();
Patrick McHardyba419af2008-01-31 04:39:23 -0800764 return 1;
765 }
Florian Westphal2cf12342016-04-28 19:13:40 +0200766 NF_CT_STAT_INC_ATOMIC(net, searched);
Patrick McHardyba419af2008-01-31 04:39:23 -0800767 }
Florian Westphal2cf12342016-04-28 19:13:40 +0200768 rcu_read_unlock();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800769
Patrick McHardyba419af2008-01-31 04:39:23 -0800770 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800771}
Patrick McHardy13b18332006-12-02 22:11:25 -0800772EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800773
Patrick McHardy7ae77302007-07-07 22:37:38 -0700774#define NF_CT_EVICTION_RANGE 8
775
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800776/* There's a small race here where we may free a just-assured
777 connection. Too bad: we're in trouble anyway. */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100778static noinline int early_drop(struct net *net, unsigned int _hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800779{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700780 /* Use oldest entry, which is roughly LRU */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800781 struct nf_conntrack_tuple_hash *h;
Florian Westphal3e866382016-05-02 18:40:14 +0200782 struct nf_conn *tmp;
Eric Dumazetea781f12009-03-25 21:05:46 +0100783 struct hlist_nulls_node *n;
Florian Westphal3e866382016-05-02 18:40:14 +0200784 unsigned int i, hash, sequence;
785 struct nf_conn *ct = NULL;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100786 spinlock_t *lockp;
Florian Westphal3e866382016-05-02 18:40:14 +0200787 bool ret = false;
788
789 i = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800790
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100791 local_bh_disable();
792restart:
Florian Westphala3efd812016-04-18 16:16:59 +0200793 sequence = read_seqcount_begin(&nf_conntrack_generation);
Florian Westphal3e866382016-05-02 18:40:14 +0200794 for (; i < NF_CT_EVICTION_RANGE; i++) {
795 hash = scale_hash(_hash++);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100796 lockp = &nf_conntrack_locks[hash % CONNTRACK_LOCKS];
Sasha Levinb16c2912016-01-18 19:23:51 -0500797 nf_conntrack_lock(lockp);
Florian Westphala3efd812016-04-18 16:16:59 +0200798 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100799 spin_unlock(lockp);
800 goto restart;
801 }
Florian Westphal56d52d42016-05-02 18:39:55 +0200802 hlist_nulls_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash],
803 hnnode) {
Patrick McHardy7ae77302007-07-07 22:37:38 -0700804 tmp = nf_ct_tuplehash_to_ctrack(h);
Florian Westphal3e866382016-05-02 18:40:14 +0200805
806 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
807 !net_eq(nf_ct_net(tmp), net) ||
808 nf_ct_is_dying(tmp))
809 continue;
810
811 if (atomic_inc_not_zero(&tmp->ct_general.use)) {
Patrick McHardy7ae77302007-07-07 22:37:38 -0700812 ct = tmp;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100813 break;
814 }
Patrick McHardy7ae77302007-07-07 22:37:38 -0700815 }
Patrick McHardy76507f62008-01-31 04:38:38 -0800816
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100817 spin_unlock(lockp);
Florian Westphal3e866382016-05-02 18:40:14 +0200818 if (ct)
Patrick McHardy7ae77302007-07-07 22:37:38 -0700819 break;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800820 }
Florian Westphal3e866382016-05-02 18:40:14 +0200821
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100822 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800823
824 if (!ct)
Florian Westphal3e866382016-05-02 18:40:14 +0200825 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800826
Florian Westphal3e866382016-05-02 18:40:14 +0200827 /* kill only if in same netns -- might have moved due to
828 * SLAB_DESTROY_BY_RCU rules
829 */
830 if (net_eq(nf_ct_net(ct), net) && del_timer(&ct->timeout)) {
Florian Westphal02982c22013-07-29 15:41:54 +0200831 if (nf_ct_delete(ct, 0, 0)) {
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000832 NF_CT_STAT_INC_ATOMIC(net, early_drop);
Florian Westphal3e866382016-05-02 18:40:14 +0200833 ret = true;
Pablo Neira Ayuso741385112012-03-06 01:22:55 +0000834 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800835 }
Florian Westphal3e866382016-05-02 18:40:14 +0200836
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800837 nf_ct_put(ct);
Florian Westphal3e866382016-05-02 18:40:14 +0200838 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800839}
840
Changli Gao99f07e92010-09-21 17:49:20 +0200841static struct nf_conn *
Daniel Borkmann308ac912015-08-08 21:40:01 +0200842__nf_conntrack_alloc(struct net *net,
843 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200844 const struct nf_conntrack_tuple *orig,
845 const struct nf_conntrack_tuple *repl,
846 gfp_t gfp, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800847{
Julia Lawallcd7fcbf2009-01-12 00:06:08 +0000848 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800849
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700850 /* We don't want any race condition at early drop stage */
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200851 atomic_inc(&net->ct.count);
Pablo Neira Ayuso5251e2d2006-09-20 12:01:06 -0700852
Patrick McHardy76eb9462008-01-31 04:41:44 -0800853 if (nf_conntrack_max &&
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200854 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +0100855 if (!early_drop(net, hash)) {
Alexey Dobriyan49ac8712008-10-08 11:35:03 +0200856 atomic_dec(&net->ct.count);
Joe Perchese87cc472012-05-13 21:56:26 +0000857 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800858 return ERR_PTR(-ENOMEM);
859 }
860 }
861
Eric Dumazet941297f2009-07-16 14:03:40 +0200862 /*
863 * Do not use kmem_cache_zalloc(), as this cache uses
864 * SLAB_DESTROY_BY_RCU.
865 */
Eric Dumazet5b3501f2010-02-08 11:16:56 -0800866 ct = kmem_cache_alloc(net->ct.nf_conntrack_cachep, gfp);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200867 if (ct == NULL)
868 goto out;
869
Patrick McHardy440f0d52009-06-10 14:32:47 +0200870 spin_lock_init(&ct->lock);
Patrick McHardyc88130b2008-01-31 04:42:11 -0800871 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
Eric Dumazet941297f2009-07-16 14:03:40 +0200872 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
Patrick McHardyc88130b2008-01-31 04:42:11 -0800873 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
Changli Gao99f07e92010-09-21 17:49:20 +0200874 /* save hash for reusing when confirming */
875 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
Florian Westphalc41884c2014-11-24 15:25:57 +0100876 ct->status = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800877 /* Don't set timer yet: wait for confirmation */
Patrick McHardyc88130b2008-01-31 04:42:11 -0800878 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
Eric Dumazetc2d9ba92010-06-01 06:51:19 +0000879 write_pnet(&ct->ct_net, net);
Florian Westphalc41884c2014-11-24 15:25:57 +0100880 memset(&ct->__nfct_init_offset[0], 0,
881 offsetof(struct nf_conn, proto) -
882 offsetof(struct nf_conn, __nfct_init_offset[0]));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800883
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200884 if (zone && nf_ct_zone_add(ct, GFP_ATOMIC, zone) < 0)
885 goto out_free;
886
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100887 /* Because we use RCU lookups, we set ct_general.use to zero before
888 * this is inserted in any list.
Eric Dumazet941297f2009-07-16 14:03:40 +0200889 */
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100890 atomic_set(&ct->ct_general.use, 0);
Patrick McHardyc88130b2008-01-31 04:42:11 -0800891 return ct;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100892out_free:
893 kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200894out:
895 atomic_dec(&net->ct.count);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100896 return ERR_PTR(-ENOMEM);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800897}
Changli Gao99f07e92010-09-21 17:49:20 +0200898
Daniel Borkmann308ac912015-08-08 21:40:01 +0200899struct nf_conn *nf_conntrack_alloc(struct net *net,
900 const struct nf_conntrack_zone *zone,
Changli Gao99f07e92010-09-21 17:49:20 +0200901 const struct nf_conntrack_tuple *orig,
902 const struct nf_conntrack_tuple *repl,
903 gfp_t gfp)
904{
905 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
906}
Patrick McHardy13b18332006-12-02 22:11:25 -0800907EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800908
Patrick McHardyc88130b2008-01-31 04:42:11 -0800909void nf_conntrack_free(struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800910{
Eric Dumazet1d452092009-03-24 14:26:50 +0100911 struct net *net = nf_ct_net(ct);
912
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +0100913 /* A freed object has refcnt == 0, that's
914 * the golden rule for SLAB_DESTROY_BY_RCU
915 */
916 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
917
Patrick McHardyceeff752008-06-11 17:51:10 -0700918 nf_ct_ext_destroy(ct);
Eric Dumazetea781f12009-03-25 21:05:46 +0100919 nf_ct_ext_free(ct);
Eric Dumazet5b3501f2010-02-08 11:16:56 -0800920 kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100921 smp_mb__before_atomic();
Pablo Neira Ayuso0c3c6c02013-11-18 12:53:59 +0100922 atomic_dec(&net->ct.count);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800923}
Patrick McHardy13b18332006-12-02 22:11:25 -0800924EXPORT_SYMBOL_GPL(nf_conntrack_free);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800925
Florian Westphalc539f012013-01-11 06:30:44 +0000926
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800927/* Allocate a new conntrack: we return -ENOMEM if classification
928 failed due to stress. Otherwise it really is unclassifiable. */
929static struct nf_conntrack_tuple_hash *
Patrick McHardyb2a15a62010-02-03 14:13:03 +0100930init_conntrack(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200931 const struct nf_conntrack_tuple *tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800932 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100933 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800934 struct sk_buff *skb,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100935 unsigned int dataoff, u32 hash)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800936{
Patrick McHardyc88130b2008-01-31 04:42:11 -0800937 struct nf_conn *ct;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700938 struct nf_conn_help *help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800939 struct nf_conntrack_tuple repl_tuple;
Patrick McHardyb2a15a62010-02-03 14:13:03 +0100940 struct nf_conntrack_ecache *ecache;
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100941 struct nf_conntrack_expect *exp = NULL;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200942 const struct nf_conntrack_zone *zone;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100943 struct nf_conn_timeout *timeout_ext;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200944 struct nf_conntrack_zone tmp;
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100945 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800946
Martin Josefsson605dcad2006-11-29 02:35:06 +0100947 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700948 pr_debug("Can't invert tuple.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800949 return NULL;
950 }
951
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200952 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Changli Gao99f07e92010-09-21 17:49:20 +0200953 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
954 hash);
Joe Perches0a9ee812011-08-29 14:17:25 -0700955 if (IS_ERR(ct))
Patrick McHardyc88130b2008-01-31 04:42:11 -0800956 return (struct nf_conntrack_tuple_hash *)ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800957
Patrick McHardy48b1de42013-08-27 08:50:14 +0200958 if (tmpl && nfct_synproxy(tmpl)) {
959 nfct_seqadj_ext_add(ct);
960 nfct_synproxy_ext_add(ct);
961 }
962
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100963 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +0200964 if (timeout_ext) {
965 timeouts = nf_ct_timeout_data(timeout_ext);
966 if (unlikely(!timeouts))
967 timeouts = l4proto->get_timeouts(net);
968 } else {
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100969 timeouts = l4proto->get_timeouts(net);
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +0200970 }
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100971
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100972 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
Patrick McHardyc88130b2008-01-31 04:42:11 -0800973 nf_conntrack_free(ct);
Weongyo Jeongccd63c22016-03-15 10:57:44 -0700974 pr_debug("can't track with proto module\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800975 return NULL;
976 }
977
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100978 if (timeout_ext)
Pablo Neira Ayusoae2d7082015-10-05 16:51:01 +0200979 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
980 GFP_ATOMIC);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +0100981
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700982 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +0100983 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
Florian Westphalc539f012013-01-11 06:30:44 +0000984 nf_ct_labels_ext_add(ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +0100985
986 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
987 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
988 ecache ? ecache->expmask : 0,
989 GFP_ATOMIC);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700990
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100991 local_bh_disable();
992 if (net->ct.expect_count) {
993 spin_lock(&nf_conntrack_expect_lock);
994 exp = nf_ct_find_expectation(net, zone, tuple);
995 if (exp) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -0700996 pr_debug("expectation arrives ct=%p exp=%p\n",
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +0100997 ct, exp);
998 /* Welcome, Mr. Bond. We've been expecting you... */
999 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1000 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1001 ct->master = exp->master;
1002 if (exp->helper) {
1003 help = nf_ct_helper_ext_add(ct, exp->helper,
1004 GFP_ATOMIC);
1005 if (help)
1006 rcu_assign_pointer(help->helper, exp->helper);
1007 }
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001008
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001009#ifdef CONFIG_NF_CONNTRACK_MARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001010 ct->mark = exp->master->mark;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001011#endif
James Morris7c9728c2006-06-09 00:31:46 -07001012#ifdef CONFIG_NF_CONNTRACK_SECMARK
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001013 ct->secmark = exp->master->secmark;
James Morris7c9728c2006-06-09 00:31:46 -07001014#endif
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001015 NF_CT_STAT_INC(net, expect_new);
1016 }
1017 spin_unlock(&nf_conntrack_expect_lock);
1018 }
1019 if (!exp) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001020 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001021 NF_CT_STAT_INC(net, new);
Yasuyuki Kozakai22e74102006-11-27 10:25:59 -08001022 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001023
Pablo Neira Ayusoe53376b2014-02-03 20:01:53 +01001024 /* Now it is inserted into the unconfirmed list, bump refcount */
1025 nf_conntrack_get(&ct->ct_general);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001026 nf_ct_add_to_unconfirmed_list(ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001027
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001028 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001029
1030 if (exp) {
1031 if (exp->expectfn)
Patrick McHardyc88130b2008-01-31 04:42:11 -08001032 exp->expectfn(ct, exp);
Patrick McHardy68236452007-07-07 22:30:49 -07001033 nf_ct_expect_put(exp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001034 }
1035
Patrick McHardyc88130b2008-01-31 04:42:11 -08001036 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001037}
1038
1039/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1040static inline struct nf_conn *
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001041resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
Alexey Dobriyana702a652008-10-08 11:35:04 +02001042 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001043 unsigned int dataoff,
1044 u_int16_t l3num,
1045 u_int8_t protonum,
1046 struct nf_conntrack_l3proto *l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001047 struct nf_conntrack_l4proto *l4proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001048 int *set_reply,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001049 enum ip_conntrack_info *ctinfo)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001050{
Daniel Borkmann308ac912015-08-08 21:40:01 +02001051 const struct nf_conntrack_zone *zone;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001052 struct nf_conntrack_tuple tuple;
1053 struct nf_conntrack_tuple_hash *h;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001054 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001055 struct nf_conn *ct;
Changli Gao99f07e92010-09-21 17:49:20 +02001056 u32 hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001057
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001058 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -05001059 dataoff, l3num, protonum, net, &tuple, l3proto,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001060 l4proto)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001061 pr_debug("Can't get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001062 return NULL;
1063 }
1064
1065 /* look for tuple match */
Daniel Borkmann5e8018f2015-08-14 16:03:40 +02001066 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001067 hash = hash_conntrack_raw(&tuple, net);
Changli Gao99f07e92010-09-21 17:49:20 +02001068 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001069 if (!h) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001070 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001071 skb, dataoff, hash);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001072 if (!h)
1073 return NULL;
1074 if (IS_ERR(h))
1075 return (void *)h;
1076 }
1077 ct = nf_ct_tuplehash_to_ctrack(h);
1078
1079 /* It exists; we have (non-exclusive) reference. */
1080 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
Eric Dumazetfb048832011-05-19 15:44:27 +02001081 *ctinfo = IP_CT_ESTABLISHED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001082 /* Please set reply bit if this packet OK */
1083 *set_reply = 1;
1084 } else {
1085 /* Once we've had two way comms, always ESTABLISHED. */
1086 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001087 pr_debug("normal packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001088 *ctinfo = IP_CT_ESTABLISHED;
1089 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001090 pr_debug("related packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001091 *ctinfo = IP_CT_RELATED;
1092 } else {
Weongyo Jeongccd63c22016-03-15 10:57:44 -07001093 pr_debug("new packet for %p\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001094 *ctinfo = IP_CT_NEW;
1095 }
1096 *set_reply = 0;
1097 }
1098 skb->nfct = &ct->ct_general;
1099 skb->nfctinfo = *ctinfo;
1100 return ct;
1101}
1102
1103unsigned int
Alexey Dobriyana702a652008-10-08 11:35:04 +02001104nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1105 struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001106{
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001107 struct nf_conn *ct, *tmpl = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001108 enum ip_conntrack_info ctinfo;
1109 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001110 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001111 unsigned int *timeouts;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001112 unsigned int dataoff;
1113 u_int8_t protonum;
1114 int set_reply = 0;
1115 int ret;
1116
Herbert Xu3db05fe2007-10-15 00:53:15 -07001117 if (skb->nfct) {
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001118 /* Previously seen (loopback or untracked)? Ignore. */
1119 tmpl = (struct nf_conn *)skb->nfct;
1120 if (!nf_ct_is_template(tmpl)) {
1121 NF_CT_STAT_INC_ATOMIC(net, ignore);
1122 return NF_ACCEPT;
1123 }
1124 skb->nfct = NULL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001125 }
1126
Patrick McHardy923f4902007-02-12 11:12:57 -08001127 /* rcu_read_lock()ed by nf_hook_slow */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001128 l3proto = __nf_ct_l3proto_find(pf);
Herbert Xu3db05fe2007-10-15 00:53:15 -07001129 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -07001130 &dataoff, &protonum);
1131 if (ret <= 0) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001132 pr_debug("not prepared to track yet or error occurred\n");
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001133 NF_CT_STAT_INC_ATOMIC(net, error);
1134 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001135 ret = -ret;
1136 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001137 }
1138
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001139 l4proto = __nf_ct_l4proto_find(pf, protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001140
1141 /* It may be an special packet, error, unclean...
1142 * inverse of the return code tells to the netfilter
1143 * core what to do with the packet. */
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001144 if (l4proto->error != NULL) {
Patrick McHardy8fea97e2010-02-15 17:45:08 +01001145 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1146 pf, hooknum);
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001147 if (ret <= 0) {
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001148 NF_CT_STAT_INC_ATOMIC(net, error);
1149 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001150 ret = -ret;
1151 goto out;
Alexey Dobriyan74c51a12008-10-08 11:35:05 +02001152 }
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +02001153 /* ICMP[v6] protocol trackers may assign one conntrack. */
1154 if (skb->nfct)
1155 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001156 }
1157
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001158 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001159 l3proto, l4proto, &set_reply, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001160 if (!ct) {
1161 /* Not valid part of a connection */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001162 NF_CT_STAT_INC_ATOMIC(net, invalid);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001163 ret = NF_ACCEPT;
1164 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001165 }
1166
1167 if (IS_ERR(ct)) {
1168 /* Too stressed to deal. */
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001169 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001170 ret = NF_DROP;
1171 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001172 }
1173
Herbert Xu3db05fe2007-10-15 00:53:15 -07001174 NF_CT_ASSERT(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001175
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001176 /* Decide what timeout policy we want to apply to this flow. */
Pablo Neira Ayuso84b5ee92012-08-28 00:53:15 +00001177 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
Pablo Neira Ayuso60b5f8f2012-03-23 00:04:53 +01001178
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +01001179 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
Christoph Paaschec8d5402009-03-16 15:51:29 +01001180 if (ret <= 0) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001181 /* Invalid: inverse of the return code tells
1182 * the netfilter core what to do */
Patrick McHardy0d537782007-07-07 22:39:38 -07001183 pr_debug("nf_conntrack_in: Can't track with proto module\n");
Herbert Xu3db05fe2007-10-15 00:53:15 -07001184 nf_conntrack_put(skb->nfct);
1185 skb->nfct = NULL;
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001186 NF_CT_STAT_INC_ATOMIC(net, invalid);
Pablo Neira Ayuso7d1e0452009-02-24 14:48:01 +01001187 if (ret == -NF_DROP)
1188 NF_CT_STAT_INC_ATOMIC(net, drop);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001189 ret = -ret;
1190 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001191 }
1192
1193 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
Patrick McHardy858b31332010-02-03 13:48:53 +01001194 nf_conntrack_event_cache(IPCT_REPLY, ct);
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001195out:
Pablo Neira Ayusoc3174282011-02-09 08:08:20 +01001196 if (tmpl) {
1197 /* Special case: we have to repeat this hook, assign the
1198 * template again to this packet. We assume that this packet
1199 * has no conntrack assigned. This is used by nf_ct_tcp. */
1200 if (ret == NF_REPEAT)
1201 skb->nfct = (struct nf_conntrack *)tmpl;
1202 else
1203 nf_ct_put(tmpl);
1204 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001205
1206 return ret;
1207}
Patrick McHardy13b18332006-12-02 22:11:25 -08001208EXPORT_SYMBOL_GPL(nf_conntrack_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001209
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001210bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1211 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001212{
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +02001213 bool ret;
Patrick McHardy923f4902007-02-12 11:12:57 -08001214
1215 rcu_read_lock();
1216 ret = nf_ct_invert_tuple(inverse, orig,
1217 __nf_ct_l3proto_find(orig->src.l3num),
1218 __nf_ct_l4proto_find(orig->src.l3num,
1219 orig->dst.protonum));
1220 rcu_read_unlock();
1221 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001222}
Patrick McHardy13b18332006-12-02 22:11:25 -08001223EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001224
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001225/* Alter reply tuple (maybe alter helper). This is for NAT, and is
1226 implicitly racy: see __nf_conntrack_confirm */
1227void nf_conntrack_alter_reply(struct nf_conn *ct,
1228 const struct nf_conntrack_tuple *newreply)
1229{
1230 struct nf_conn_help *help = nfct_help(ct);
1231
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001232 /* Should be unconfirmed, so not in hash table yet */
1233 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1234
Patrick McHardy0d537782007-07-07 22:39:38 -07001235 pr_debug("Altering reply tuple of %p to ", ct);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +02001236 nf_ct_dump_tuple(newreply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001237
1238 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
Patrick McHardyef1a5a52008-04-14 11:21:01 +02001239 if (ct->master || (help && !hlist_empty(&help->expectations)))
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001240 return;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001241
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001242 rcu_read_lock();
Patrick McHardyb2a15a62010-02-03 14:13:03 +01001243 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
Patrick McHardyc52fbb42008-01-31 04:37:36 -08001244 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001245}
Patrick McHardy13b18332006-12-02 22:11:25 -08001246EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001247
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001248/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1249void __nf_ct_refresh_acct(struct nf_conn *ct,
1250 enum ip_conntrack_info ctinfo,
1251 const struct sk_buff *skb,
1252 unsigned long extra_jiffies,
1253 int do_acct)
1254{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001255 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
1256 NF_CT_ASSERT(skb);
1257
Eric Leblond997ae832006-05-29 18:24:20 -07001258 /* Only update if this is not a fixed timeout */
Patrick McHardy47d95042008-01-31 04:36:31 -08001259 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1260 goto acct;
Eric Leblond997ae832006-05-29 18:24:20 -07001261
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001262 /* If not in hash table, timer will not be active yet */
1263 if (!nf_ct_is_confirmed(ct)) {
1264 ct->timeout.expires = extra_jiffies;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001265 } else {
Martin Josefssonbe00c8e2006-11-29 02:35:12 +01001266 unsigned long newtime = jiffies + extra_jiffies;
1267
1268 /* Only update the timeout if the new timeout is at least
1269 HZ jiffies from the old timeout. Need del_timer for race
1270 avoidance (may already be dying). */
Patrick McHardy65cb9fd2009-06-13 12:21:49 +02001271 if (newtime - ct->timeout.expires >= HZ)
1272 mod_timer_pending(&ct->timeout, newtime);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001273 }
1274
Patrick McHardy47d95042008-01-31 04:36:31 -08001275acct:
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001276 if (do_acct)
1277 nf_ct_acct_update(ct, ctinfo, skb->len);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001278}
Patrick McHardy13b18332006-12-02 22:11:25 -08001279EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001280
David S. Miller4c889492008-07-14 20:22:38 -07001281bool __nf_ct_kill_acct(struct nf_conn *ct,
1282 enum ip_conntrack_info ctinfo,
1283 const struct sk_buff *skb,
1284 int do_acct)
Patrick McHardy51091762008-06-09 15:59:06 -07001285{
Pablo Neira Ayusoba767382016-05-02 21:28:57 +02001286 if (do_acct)
1287 nf_ct_acct_update(ct, ctinfo, skb->len);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001288
David S. Miller4c889492008-07-14 20:22:38 -07001289 if (del_timer(&ct->timeout)) {
Patrick McHardy51091762008-06-09 15:59:06 -07001290 ct->timeout.function((unsigned long)ct);
David S. Miller4c889492008-07-14 20:22:38 -07001291 return true;
1292 }
1293 return false;
Patrick McHardy51091762008-06-09 15:59:06 -07001294}
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -07001295EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
Patrick McHardy51091762008-06-09 15:59:06 -07001296
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001297#ifdef CONFIG_NF_CONNTRACK_ZONES
1298static struct nf_ct_ext_type nf_ct_zone_extend __read_mostly = {
1299 .len = sizeof(struct nf_conntrack_zone),
1300 .align = __alignof__(struct nf_conntrack_zone),
1301 .id = NF_CT_EXT_ZONE,
1302};
1303#endif
1304
Igor Maravićc0cd1152011-12-12 02:58:24 +00001305#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001306
1307#include <linux/netfilter/nfnetlink.h>
1308#include <linux/netfilter/nfnetlink_conntrack.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08001309#include <linux/mutex.h>
1310
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001311/* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1312 * in ip_conntrack_core, since we don't want the protocols to autoload
1313 * or depend on ctnetlink */
Patrick McHardyfdf70832007-09-28 14:37:41 -07001314int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001315 const struct nf_conntrack_tuple *tuple)
1316{
David S. Millerbae65be2012-04-01 18:58:28 -04001317 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1318 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1319 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001320 return 0;
1321
Patrick McHardydf6fb862007-09-28 14:37:03 -07001322nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001323 return -1;
1324}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001325EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001326
Patrick McHardyf73e9242007-09-28 14:39:55 -07001327const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1328 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1329 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001330};
Patrick McHardyf73e9242007-09-28 14:39:55 -07001331EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001332
Patrick McHardyfdf70832007-09-28 14:37:41 -07001333int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001334 struct nf_conntrack_tuple *t)
1335{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001336 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001337 return -EINVAL;
1338
Patrick McHardy77236b62007-12-17 22:29:45 -08001339 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1340 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001341
1342 return 0;
1343}
Patrick McHardyfdf70832007-09-28 14:37:41 -07001344EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
Holger Eitzenberger5c0de292009-03-25 21:52:17 +01001345
1346int nf_ct_port_nlattr_tuple_size(void)
1347{
1348 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1349}
1350EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001351#endif
1352
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001353/* Used by ipt_REJECT and ip6t_REJECT. */
Patrick McHardy312a0c162013-07-28 22:54:08 +02001354static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001355{
1356 struct nf_conn *ct;
1357 enum ip_conntrack_info ctinfo;
1358
1359 /* This ICMP is in reverse direction to the packet which caused it */
1360 ct = nf_ct_get(skb, &ctinfo);
1361 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
Eric Dumazetfb048832011-05-19 15:44:27 +02001362 ctinfo = IP_CT_RELATED_REPLY;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001363 else
1364 ctinfo = IP_CT_RELATED;
1365
1366 /* Attach to new skbuff, and increment count */
1367 nskb->nfct = &ct->ct_general;
1368 nskb->nfctinfo = ctinfo;
1369 nf_conntrack_get(nskb->nfct);
1370}
1371
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001372/* Bring out ya dead! */
Patrick McHardydf0933d2006-09-20 11:57:53 -07001373static struct nf_conn *
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001374get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001375 void *data, unsigned int *bucket)
1376{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001377 struct nf_conntrack_tuple_hash *h;
1378 struct nf_conn *ct;
Eric Dumazetea781f12009-03-25 21:05:46 +01001379 struct hlist_nulls_node *n;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001380 int cpu;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001381 spinlock_t *lockp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001382
Florian Westphal56d52d42016-05-02 18:39:55 +02001383 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001384 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1385 local_bh_disable();
Sasha Levinb16c2912016-01-18 19:23:51 -05001386 nf_conntrack_lock(lockp);
Florian Westphal56d52d42016-05-02 18:39:55 +02001387 if (*bucket < nf_conntrack_htable_size) {
1388 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001389 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1390 continue;
1391 ct = nf_ct_tuplehash_to_ctrack(h);
Florian Westphale0c7d472016-04-28 19:13:45 +02001392 if (net_eq(nf_ct_net(ct), net) &&
1393 iter(ct, data))
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001394 goto found;
1395 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001396 }
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001397 spin_unlock(lockp);
1398 local_bh_enable();
Florian Westphald93c6252016-01-20 11:16:43 +01001399 cond_resched();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001400 }
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001401
1402 for_each_possible_cpu(cpu) {
1403 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1404
1405 spin_lock_bh(&pcpu->lock);
1406 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1407 ct = nf_ct_tuplehash_to_ctrack(h);
1408 if (iter(ct, data))
1409 set_bit(IPS_DYING_BIT, &ct->status);
1410 }
1411 spin_unlock_bh(&pcpu->lock);
Florian Westphald93c6252016-01-20 11:16:43 +01001412 cond_resched();
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001413 }
Patrick McHardydf0933d2006-09-20 11:57:53 -07001414 return NULL;
1415found:
Martin Josefssonc073e3f2006-10-30 15:13:58 -08001416 atomic_inc(&ct->ct_general.use);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001417 spin_unlock(lockp);
1418 local_bh_enable();
Patrick McHardydf0933d2006-09-20 11:57:53 -07001419 return ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001420}
1421
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001422void nf_ct_iterate_cleanup(struct net *net,
1423 int (*iter)(struct nf_conn *i, void *data),
Florian Westphalc655bc62013-07-29 15:41:55 +02001424 void *data, u32 portid, int report)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001425{
Patrick McHardydf0933d2006-09-20 11:57:53 -07001426 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001427 unsigned int bucket = 0;
1428
Florian Westphald93c6252016-01-20 11:16:43 +01001429 might_sleep();
1430
Florian Westphal88b68bc2016-04-28 19:13:42 +02001431 if (atomic_read(&net->ct.count) == 0)
1432 return;
1433
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001434 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001435 /* Time to push up daises... */
1436 if (del_timer(&ct->timeout))
Florian Westphalc655bc62013-07-29 15:41:55 +02001437 nf_ct_delete(ct, portid, report);
Florian Westphal02982c22013-07-29 15:41:54 +02001438
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001439 /* ... else the timer will get him soon. */
1440
1441 nf_ct_put(ct);
Florian Westphald93c6252016-01-20 11:16:43 +01001442 cond_resched();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001443 }
1444}
Patrick McHardy13b18332006-12-02 22:11:25 -08001445EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001446
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +02001447static int kill_all(struct nf_conn *i, void *data)
1448{
1449 return 1;
1450}
1451
Patrick McHardyd862a662011-01-14 15:45:56 +01001452void nf_ct_free_hashtable(void *hash, unsigned int size)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001453{
Patrick McHardyd862a662011-01-14 15:45:56 +01001454 if (is_vmalloc_addr(hash))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001455 vfree(hash);
1456 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001457 free_pages((unsigned long)hash,
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001458 get_order(sizeof(struct hlist_head) * size));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001459}
Patrick McHardyac565e52007-07-07 22:30:08 -07001460EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001461
Eric Dumazetb3c51632010-06-09 14:43:38 +02001462static int untrack_refs(void)
1463{
1464 int cnt = 0, cpu;
1465
1466 for_each_possible_cpu(cpu) {
1467 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1468
1469 cnt += atomic_read(&ct->ct_general.use) - 1;
1470 }
1471 return cnt;
1472}
1473
Gao fengf94161c2013-01-21 22:10:24 +00001474void nf_conntrack_cleanup_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001475{
Gao fengf94161c2013-01-21 22:10:24 +00001476 RCU_INIT_POINTER(ip_ct_attach, NULL);
1477}
1478
1479void nf_conntrack_cleanup_end(void)
1480{
1481 RCU_INIT_POINTER(nf_ct_destroy, NULL);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001482 while (untrack_refs() > 0)
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001483 schedule();
1484
Florian Westphal56d52d42016-05-02 18:39:55 +02001485 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1486
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001487#ifdef CONFIG_NF_CONNTRACK_ZONES
1488 nf_ct_extend_unregister(&nf_ct_zone_extend);
1489#endif
Gao feng04d87002013-01-21 22:10:32 +00001490 nf_conntrack_proto_fini();
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001491 nf_conntrack_seqadj_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001492 nf_conntrack_labels_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001493 nf_conntrack_helper_fini();
Gao feng86840942013-01-21 22:10:29 +00001494 nf_conntrack_timeout_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001495 nf_conntrack_ecache_fini();
Gao feng73f40012013-01-21 22:10:27 +00001496 nf_conntrack_tstamp_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001497 nf_conntrack_acct_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001498 nf_conntrack_expect_fini();
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001499}
Yasuyuki Kozakai7d3cdc62006-02-15 15:22:21 -08001500
Gao fengf94161c2013-01-21 22:10:24 +00001501/*
1502 * Mishearing the voices in his head, our hero wonders how he's
1503 * supposed to kill the mall.
1504 */
1505void nf_conntrack_cleanup_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001506{
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001507 LIST_HEAD(single);
1508
1509 list_add(&net->exit_list, &single);
1510 nf_conntrack_cleanup_net_list(&single);
1511}
1512
1513void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1514{
1515 int busy;
1516 struct net *net;
1517
Gao fengf94161c2013-01-21 22:10:24 +00001518 /*
1519 * This makes sure all current packets have passed through
1520 * netfilter framework. Roll on, two-stage module
1521 * delete...
1522 */
1523 synchronize_net();
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001524i_see_dead_people:
1525 busy = 0;
1526 list_for_each_entry(net, net_exit_list, exit_list) {
Florian Westphalc655bc62013-07-29 15:41:55 +02001527 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001528 if (atomic_read(&net->ct.count) != 0)
1529 busy = 1;
1530 }
1531 if (busy) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001532 schedule();
1533 goto i_see_dead_people;
1534 }
1535
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001536 list_for_each_entry(net, net_exit_list, exit_list) {
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001537 nf_conntrack_proto_pernet_fini(net);
1538 nf_conntrack_helper_pernet_fini(net);
1539 nf_conntrack_ecache_pernet_fini(net);
1540 nf_conntrack_tstamp_pernet_fini(net);
1541 nf_conntrack_acct_pernet_fini(net);
1542 nf_conntrack_expect_pernet_fini(net);
1543 kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1544 kfree(net->ct.slabname);
1545 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001546 free_percpu(net->ct.pcpu_lists);
Vladimir Davydovdece40e2013-03-13 23:40:14 +00001547 }
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001548}
1549
Patrick McHardyd862a662011-01-14 15:45:56 +01001550void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001551{
Eric Dumazetea781f12009-03-25 21:05:46 +01001552 struct hlist_nulls_head *hash;
1553 unsigned int nr_slots, i;
1554 size_t sz;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001555
Eric Dumazetea781f12009-03-25 21:05:46 +01001556 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1557 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
1558 sz = nr_slots * sizeof(struct hlist_nulls_head);
1559 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1560 get_order(sz));
Pablo Neira Ayusof0ad4622015-07-23 13:06:10 +02001561 if (!hash)
Eric Dumazet966567b2011-12-19 16:01:38 -05001562 hash = vzalloc(sz);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001563
Eric Dumazetea781f12009-03-25 21:05:46 +01001564 if (hash && nulls)
1565 for (i = 0; i < nr_slots; i++)
1566 INIT_HLIST_NULLS_HEAD(&hash[i], i);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001567
1568 return hash;
1569}
Patrick McHardyac565e52007-07-07 22:30:08 -07001570EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001571
Patrick McHardyfae718d2007-12-24 21:09:10 -08001572int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001573{
Abhijit Pawar4b5511e2012-12-09 23:12:28 +00001574 int i, bucket, rc;
Stephen Hemminger96eb24d2008-01-31 04:07:29 -08001575 unsigned int hashsize, old_size;
Eric Dumazetea781f12009-03-25 21:05:46 +01001576 struct hlist_nulls_head *hash, *old_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001577 struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001578 struct nf_conn *ct;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001579
Patrick McHardyd696c7b2010-02-08 11:18:07 -08001580 if (current->nsproxy->net_ns != &init_net)
1581 return -EOPNOTSUPP;
1582
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001583 /* On boot, we can set this without any fancy locking. */
1584 if (!nf_conntrack_htable_size)
1585 return param_set_uint(val, kp);
1586
Abhijit Pawar4b5511e2012-12-09 23:12:28 +00001587 rc = kstrtouint(val, 0, &hashsize);
1588 if (rc)
1589 return rc;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001590 if (!hashsize)
1591 return -EINVAL;
1592
Patrick McHardyd862a662011-01-14 15:45:56 +01001593 hash = nf_ct_alloc_hashtable(&hashsize, 1);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001594 if (!hash)
1595 return -ENOMEM;
1596
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001597 local_bh_disable();
1598 nf_conntrack_all_lock();
Florian Westphala3efd812016-04-18 16:16:59 +02001599 write_seqcount_begin(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001600
Patrick McHardy76507f62008-01-31 04:38:38 -08001601 /* Lookups in the old hash might happen in parallel, which means we
1602 * might get false negatives during connection lookup. New connections
1603 * created because of a false negative won't make it into the hash
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001604 * though since that required taking the locks.
Patrick McHardy76507f62008-01-31 04:38:38 -08001605 */
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001606
Florian Westphal56d52d42016-05-02 18:39:55 +02001607 for (i = 0; i < nf_conntrack_htable_size; i++) {
1608 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1609 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1610 struct nf_conntrack_tuple_hash, hnnode);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001611 ct = nf_ct_tuplehash_to_ctrack(h);
Eric Dumazetea781f12009-03-25 21:05:46 +01001612 hlist_nulls_del_rcu(&h->hnnode);
Florian Westphal1b8c8a92016-05-03 00:25:58 +02001613 bucket = __hash_conntrack(nf_ct_net(ct),
1614 &h->tuple, hashsize);
Eric Dumazetea781f12009-03-25 21:05:46 +01001615 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001616 }
1617 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001618 old_size = nf_conntrack_htable_size;
1619 old_hash = nf_conntrack_hash;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001620
Florian Westphal56d52d42016-05-02 18:39:55 +02001621 nf_conntrack_hash = hash;
1622 nf_conntrack_htable_size = hashsize;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001623
Florian Westphala3efd812016-04-18 16:16:59 +02001624 write_seqcount_end(&nf_conntrack_generation);
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001625 nf_conntrack_all_unlock();
1626 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001627
Florian Westphal5e3c61f2016-04-28 19:13:41 +02001628 synchronize_net();
Patrick McHardyd862a662011-01-14 15:45:56 +01001629 nf_ct_free_hashtable(old_hash, old_size);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001630 return 0;
1631}
Patrick McHardyfae718d2007-12-24 21:09:10 -08001632EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001633
Patrick McHardyfae718d2007-12-24 21:09:10 -08001634module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001635 &nf_conntrack_htable_size, 0600);
1636
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001637void nf_ct_untracked_status_or(unsigned long bits)
1638{
Eric Dumazetb3c51632010-06-09 14:43:38 +02001639 int cpu;
1640
1641 for_each_possible_cpu(cpu)
1642 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001643}
1644EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1645
Gao fengf94161c2013-01-21 22:10:24 +00001646int nf_conntrack_init_start(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001647{
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001648 int max_factor = 8;
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001649 int i, ret, cpu;
1650
Florian Westphala3efd812016-04-18 16:16:59 +02001651 seqcount_init(&nf_conntrack_generation);
1652
Eric Dumazetd5d20912014-03-17 13:37:53 -07001653 for (i = 0; i < CONNTRACK_LOCKS; i++)
Jesper Dangaard Brouer93bb0ce2014-03-03 14:46:13 +01001654 spin_lock_init(&nf_conntrack_locks[i]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001655
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001656 if (!nf_conntrack_htable_size) {
Marcelo Leitner88eab472014-12-03 17:30:19 -02001657 /* Idea from tcp.c: use 1/16384 of memory.
1658 * On i386: 32MB machine has 512 buckets.
1659 * >= 1GB machines have 16384 buckets.
1660 * >= 4GB machines have 65536 buckets.
1661 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001662 nf_conntrack_htable_size
Jan Beulich44813742009-09-21 17:03:05 -07001663 = (((totalram_pages << PAGE_SHIFT) / 16384)
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001664 / sizeof(struct hlist_head));
Marcelo Leitner88eab472014-12-03 17:30:19 -02001665 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1666 nf_conntrack_htable_size = 65536;
1667 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001668 nf_conntrack_htable_size = 16384;
1669 if (nf_conntrack_htable_size < 32)
1670 nf_conntrack_htable_size = 32;
1671
1672 /* Use a max. factor of four by default to get the same max as
1673 * with the old struct list_heads. When a table size is given
1674 * we use the old value of 8 to avoid reducing the max.
1675 * entries. */
1676 max_factor = 4;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001677 }
Florian Westphal56d52d42016-05-02 18:39:55 +02001678
1679 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1680 if (!nf_conntrack_hash)
1681 return -ENOMEM;
1682
Patrick McHardyf205c5e2007-07-07 22:28:14 -07001683 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001684
Stephen Hemminger654d0fb2010-05-13 15:02:08 +02001685 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
Patrick McHardy8e5105a2007-07-07 22:27:33 -07001686 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1687 nf_conntrack_max);
Gao feng83b4dbe2013-01-21 22:10:25 +00001688
1689 ret = nf_conntrack_expect_init();
1690 if (ret < 0)
1691 goto err_expect;
1692
Gao fengb7ff3a12013-01-21 22:10:26 +00001693 ret = nf_conntrack_acct_init();
1694 if (ret < 0)
1695 goto err_acct;
1696
Gao feng73f40012013-01-21 22:10:27 +00001697 ret = nf_conntrack_tstamp_init();
1698 if (ret < 0)
1699 goto err_tstamp;
1700
Gao feng3fe0f942013-01-21 22:10:28 +00001701 ret = nf_conntrack_ecache_init();
1702 if (ret < 0)
1703 goto err_ecache;
1704
Gao feng86840942013-01-21 22:10:29 +00001705 ret = nf_conntrack_timeout_init();
1706 if (ret < 0)
1707 goto err_timeout;
1708
Gao feng5e615b22013-01-21 22:10:30 +00001709 ret = nf_conntrack_helper_init();
1710 if (ret < 0)
1711 goto err_helper;
1712
Gao feng5f69b8f2013-01-21 22:10:31 +00001713 ret = nf_conntrack_labels_init();
1714 if (ret < 0)
1715 goto err_labels;
1716
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001717 ret = nf_conntrack_seqadj_init();
1718 if (ret < 0)
1719 goto err_seqadj;
1720
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001721#ifdef CONFIG_NF_CONNTRACK_ZONES
1722 ret = nf_ct_extend_register(&nf_ct_zone_extend);
1723 if (ret < 0)
1724 goto err_extend;
1725#endif
Gao feng04d87002013-01-21 22:10:32 +00001726 ret = nf_conntrack_proto_init();
1727 if (ret < 0)
1728 goto err_proto;
1729
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001730 /* Set up fake conntrack: to never be deleted, not in any hashes */
Eric Dumazetb3c51632010-06-09 14:43:38 +02001731 for_each_possible_cpu(cpu) {
1732 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
Eric Dumazetb3c51632010-06-09 14:43:38 +02001733 write_pnet(&ct->ct_net, &init_net);
1734 atomic_set(&ct->ct_general.use, 1);
1735 }
Patrick McHardy9edd7ca2010-02-08 11:16:26 -08001736 /* - and look it like as a confirmed connection */
Eric Dumazet5bfddbd2010-06-08 16:09:52 +02001737 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001738 return 0;
1739
Gao feng04d87002013-01-21 22:10:32 +00001740err_proto:
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001741#ifdef CONFIG_NF_CONNTRACK_ZONES
Gao feng04d87002013-01-21 22:10:32 +00001742 nf_ct_extend_unregister(&nf_ct_zone_extend);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001743err_extend:
Eric Leblonda9006892012-04-18 11:20:41 +02001744#endif
Patrick McHardy41d73ec2013-08-27 08:50:12 +02001745 nf_conntrack_seqadj_fini();
1746err_seqadj:
Gao feng04d87002013-01-21 22:10:32 +00001747 nf_conntrack_labels_fini();
Gao feng5f69b8f2013-01-21 22:10:31 +00001748err_labels:
1749 nf_conntrack_helper_fini();
Gao feng5e615b22013-01-21 22:10:30 +00001750err_helper:
1751 nf_conntrack_timeout_fini();
Gao feng86840942013-01-21 22:10:29 +00001752err_timeout:
1753 nf_conntrack_ecache_fini();
Gao feng3fe0f942013-01-21 22:10:28 +00001754err_ecache:
1755 nf_conntrack_tstamp_fini();
Gao feng73f40012013-01-21 22:10:27 +00001756err_tstamp:
1757 nf_conntrack_acct_fini();
Gao fengb7ff3a12013-01-21 22:10:26 +00001758err_acct:
1759 nf_conntrack_expect_fini();
Gao feng83b4dbe2013-01-21 22:10:25 +00001760err_expect:
Florian Westphal56d52d42016-05-02 18:39:55 +02001761 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001762 return ret;
1763}
1764
Gao fengf94161c2013-01-21 22:10:24 +00001765void nf_conntrack_init_end(void)
1766{
1767 /* For use by REJECT target */
1768 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1769 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
Gao fengf94161c2013-01-21 22:10:24 +00001770}
1771
Eric Dumazet8cc20192009-06-22 14:13:55 +02001772/*
1773 * We need to use special "null" values, not used in hash table
1774 */
1775#define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1776#define DYING_NULLS_VAL ((1<<30)+1)
Pablo Neira Ayuso252b3e82012-12-11 04:07:42 +00001777#define TEMPLATE_NULLS_VAL ((1<<30)+2)
Eric Dumazet8cc20192009-06-22 14:13:55 +02001778
Gao fengf94161c2013-01-21 22:10:24 +00001779int nf_conntrack_init_net(struct net *net)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001780{
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001781 int ret = -ENOMEM;
1782 int cpu;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001783
1784 atomic_set(&net->ct.count, 0);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001785
1786 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1787 if (!net->ct.pcpu_lists)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001788 goto err_stat;
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001789
1790 for_each_possible_cpu(cpu) {
1791 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1792
1793 spin_lock_init(&pcpu->lock);
1794 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1795 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001796 }
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001797
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001798 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1799 if (!net->ct.stat)
1800 goto err_pcpu_lists;
1801
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001802 net->ct.slabname = kasprintf(GFP_KERNEL, "nf_conntrack_%p", net);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001803 if (!net->ct.slabname)
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001804 goto err_slabname;
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001805
1806 net->ct.nf_conntrack_cachep = kmem_cache_create(net->ct.slabname,
1807 sizeof(struct nf_conn), 0,
1808 SLAB_DESTROY_BY_RCU, NULL);
1809 if (!net->ct.nf_conntrack_cachep) {
1810 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001811 goto err_cache;
1812 }
Patrick McHardyd696c7b2010-02-08 11:18:07 -08001813
Gao feng83b4dbe2013-01-21 22:10:25 +00001814 ret = nf_conntrack_expect_pernet_init(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001815 if (ret < 0)
1816 goto err_expect;
Gao fengb7ff3a12013-01-21 22:10:26 +00001817 ret = nf_conntrack_acct_pernet_init(net);
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -07001818 if (ret < 0)
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001819 goto err_acct;
Gao feng73f40012013-01-21 22:10:27 +00001820 ret = nf_conntrack_tstamp_pernet_init(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001821 if (ret < 0)
1822 goto err_tstamp;
Gao feng3fe0f942013-01-21 22:10:28 +00001823 ret = nf_conntrack_ecache_pernet_init(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001824 if (ret < 0)
1825 goto err_ecache;
Gao feng5e615b22013-01-21 22:10:30 +00001826 ret = nf_conntrack_helper_pernet_init(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001827 if (ret < 0)
1828 goto err_helper;
Gao feng04d87002013-01-21 22:10:32 +00001829 ret = nf_conntrack_proto_pernet_init(net);
Gao fengf94161c2013-01-21 22:10:24 +00001830 if (ret < 0)
1831 goto err_proto;
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001832 return 0;
Florian Westphalc539f012013-01-11 06:30:44 +00001833
Gao fengf94161c2013-01-21 22:10:24 +00001834err_proto:
Gao feng5e615b22013-01-21 22:10:30 +00001835 nf_conntrack_helper_pernet_fini(net);
Eric Leblonda9006892012-04-18 11:20:41 +02001836err_helper:
Gao feng3fe0f942013-01-21 22:10:28 +00001837 nf_conntrack_ecache_pernet_fini(net);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +02001838err_ecache:
Gao feng73f40012013-01-21 22:10:27 +00001839 nf_conntrack_tstamp_pernet_fini(net);
Pablo Neira Ayusoa992ca22011-01-19 16:00:07 +01001840err_tstamp:
Gao fengb7ff3a12013-01-21 22:10:26 +00001841 nf_conntrack_acct_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001842err_acct:
Gao feng83b4dbe2013-01-21 22:10:25 +00001843 nf_conntrack_expect_pernet_fini(net);
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001844err_expect:
Eric Dumazet5b3501f2010-02-08 11:16:56 -08001845 kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1846err_cache:
1847 kfree(net->ct.slabname);
1848err_slabname:
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001849 free_percpu(net->ct.stat);
Jesper Dangaard Brouerb7779d02014-03-03 14:45:20 +01001850err_pcpu_lists:
1851 free_percpu(net->ct.pcpu_lists);
Alexey Dobriyan0d55af82008-10-08 11:35:07 +02001852err_stat:
Alexey Dobriyan08f65472008-10-08 11:35:09 +02001853 return ret;
1854}