Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* 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 Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 6 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 7 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 8 | * (C) 2005-2012 Patrick McHardy <kaber@trash.net> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 9 | * |
| 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 17 | #include <linux/types.h> |
| 18 | #include <linux/netfilter.h> |
| 19 | #include <linux/module.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/proc_fs.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/stddef.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/random.h> |
| 27 | #include <linux/jhash.h> |
Florian Westphal | 1922476 | 2019-08-17 00:01:34 +0100 | [diff] [blame] | 28 | #include <linux/siphash.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 29 | #include <linux/err.h> |
| 30 | #include <linux/percpu.h> |
| 31 | #include <linux/moduleparam.h> |
| 32 | #include <linux/notifier.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/socket.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 36 | #include <linux/mm.h> |
Patrick McHardy | d696c7b | 2010-02-08 11:18:07 -0800 | [diff] [blame] | 37 | #include <linux/nsproxy.h> |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 38 | #include <linux/rculist_nulls.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 39 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 40 | #include <net/netfilter/nf_conntrack.h> |
| 41 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 42 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 43 | #include <net/netfilter/nf_conntrack_expect.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 44 | #include <net/netfilter/nf_conntrack_helper.h> |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 45 | #include <net/netfilter/nf_conntrack_seqadj.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 46 | #include <net/netfilter/nf_conntrack_core.h> |
Yasuyuki Kozakai | ecfab2c | 2007-07-07 22:23:21 -0700 | [diff] [blame] | 47 | #include <net/netfilter/nf_conntrack_extend.h> |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 48 | #include <net/netfilter/nf_conntrack_acct.h> |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 49 | #include <net/netfilter/nf_conntrack_ecache.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 50 | #include <net/netfilter/nf_conntrack_zones.h> |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 51 | #include <net/netfilter/nf_conntrack_timestamp.h> |
Pablo Neira Ayuso | dd70507 | 2012-02-28 23:36:48 +0100 | [diff] [blame] | 52 | #include <net/netfilter/nf_conntrack_timeout.h> |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 53 | #include <net/netfilter/nf_conntrack_labels.h> |
Patrick McHardy | 48b1de4 | 2013-08-27 08:50:14 +0200 | [diff] [blame] | 54 | #include <net/netfilter/nf_conntrack_synproxy.h> |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 55 | #include <net/netfilter/nf_nat.h> |
Patrick McHardy | e17b666 | 2008-11-18 12:24:17 +0100 | [diff] [blame] | 56 | #include <net/netfilter/nf_nat_core.h> |
stephen hemminger | 4937636 | 2013-03-16 07:00:28 +0000 | [diff] [blame] | 57 | #include <net/netfilter/nf_nat_helper.h> |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 58 | #include <net/netns/hash.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 59 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 60 | #define NF_CONNTRACK_VERSION "0.5.0" |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 61 | |
Patrick McHardy | e17b666 | 2008-11-18 12:24:17 +0100 | [diff] [blame] | 62 | int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct, |
| 63 | enum nf_nat_manip_type manip, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 64 | const struct nlattr *attr) __read_mostly; |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 65 | EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook); |
| 66 | |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 67 | __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS]; |
| 68 | EXPORT_SYMBOL_GPL(nf_conntrack_locks); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 69 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 70 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock); |
| 71 | EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock); |
| 72 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 73 | struct hlist_nulls_head *nf_conntrack_hash __read_mostly; |
| 74 | EXPORT_SYMBOL_GPL(nf_conntrack_hash); |
| 75 | |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 76 | struct conntrack_gc_work { |
| 77 | struct delayed_work dwork; |
| 78 | u32 last_bucket; |
| 79 | bool exiting; |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 80 | long next_gc_run; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 83 | static __read_mostly struct kmem_cache *nf_conntrack_cachep; |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 84 | static __read_mostly spinlock_t nf_conntrack_locks_all_lock; |
Florian Westphal | 70d72b7 | 2016-04-24 01:17:14 +0200 | [diff] [blame] | 85 | static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock); |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 86 | static __read_mostly bool nf_conntrack_locks_all; |
| 87 | |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 88 | /* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */ |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 89 | #define GC_MAX_BUCKETS_DIV 128u |
| 90 | /* upper bound of full table scan */ |
| 91 | #define GC_MAX_SCAN_JIFFIES (16u * HZ) |
| 92 | /* desired ratio of entries found to be expired */ |
| 93 | #define GC_EVICT_RATIO 50u |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 94 | |
| 95 | static struct conntrack_gc_work conntrack_gc_work; |
| 96 | |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 97 | void nf_conntrack_lock(spinlock_t *lock) __acquires(lock) |
| 98 | { |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 99 | /* 1) Acquire the lock */ |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 100 | spin_lock(lock); |
Peter Zijlstra | b316ff7 | 2016-05-24 15:00:38 +0200 | [diff] [blame] | 101 | |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 102 | /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics |
| 103 | * It pairs with the smp_store_release() in nf_conntrack_all_unlock() |
| 104 | */ |
| 105 | if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false)) |
| 106 | return; |
| 107 | |
| 108 | /* fast path failed, unlock */ |
| 109 | spin_unlock(lock); |
| 110 | |
| 111 | /* Slow path 1) get global lock */ |
| 112 | spin_lock(&nf_conntrack_locks_all_lock); |
| 113 | |
| 114 | /* Slow path 2) get the lock we want */ |
| 115 | spin_lock(lock); |
| 116 | |
| 117 | /* Slow path 3) release the global lock */ |
| 118 | spin_unlock(&nf_conntrack_locks_all_lock); |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL_GPL(nf_conntrack_lock); |
| 121 | |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 122 | static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2) |
| 123 | { |
| 124 | h1 %= CONNTRACK_LOCKS; |
| 125 | h2 %= CONNTRACK_LOCKS; |
| 126 | spin_unlock(&nf_conntrack_locks[h1]); |
| 127 | if (h1 != h2) |
| 128 | spin_unlock(&nf_conntrack_locks[h2]); |
| 129 | } |
| 130 | |
| 131 | /* return true if we need to recompute hashes (in case hash table was resized) */ |
| 132 | static bool nf_conntrack_double_lock(struct net *net, unsigned int h1, |
| 133 | unsigned int h2, unsigned int sequence) |
| 134 | { |
| 135 | h1 %= CONNTRACK_LOCKS; |
| 136 | h2 %= CONNTRACK_LOCKS; |
| 137 | if (h1 <= h2) { |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 138 | nf_conntrack_lock(&nf_conntrack_locks[h1]); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 139 | if (h1 != h2) |
| 140 | spin_lock_nested(&nf_conntrack_locks[h2], |
| 141 | SINGLE_DEPTH_NESTING); |
| 142 | } else { |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 143 | nf_conntrack_lock(&nf_conntrack_locks[h2]); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 144 | spin_lock_nested(&nf_conntrack_locks[h1], |
| 145 | SINGLE_DEPTH_NESTING); |
| 146 | } |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 147 | if (read_seqcount_retry(&nf_conntrack_generation, sequence)) { |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 148 | nf_conntrack_double_unlock(h1, h2); |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | static void nf_conntrack_all_lock(void) |
| 155 | { |
| 156 | int i; |
| 157 | |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 158 | spin_lock(&nf_conntrack_locks_all_lock); |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 159 | |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 160 | nf_conntrack_locks_all = true; |
| 161 | |
| 162 | for (i = 0; i < CONNTRACK_LOCKS; i++) { |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 163 | spin_lock(&nf_conntrack_locks[i]); |
| 164 | |
| 165 | /* This spin_unlock provides the "release" to ensure that |
| 166 | * nf_conntrack_locks_all==true is visible to everyone that |
| 167 | * acquired spin_lock(&nf_conntrack_locks[]). |
| 168 | */ |
| 169 | spin_unlock(&nf_conntrack_locks[i]); |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 170 | } |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void nf_conntrack_all_unlock(void) |
| 174 | { |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 175 | /* All prior stores must be complete before we clear |
Peter Zijlstra | b316ff7 | 2016-05-24 15:00:38 +0200 | [diff] [blame] | 176 | * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock() |
| 177 | * might observe the false value but not the entire |
Manfred Spraul | 4c7f54a | 2017-07-06 20:45:59 +0200 | [diff] [blame] | 178 | * critical section. |
| 179 | * It pairs with the smp_load_acquire() in nf_conntrack_lock() |
Peter Zijlstra | b316ff7 | 2016-05-24 15:00:38 +0200 | [diff] [blame] | 180 | */ |
| 181 | smp_store_release(&nf_conntrack_locks_all, false); |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 182 | spin_unlock(&nf_conntrack_locks_all_lock); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 185 | unsigned int nf_conntrack_htable_size __read_mostly; |
Pablo Neira Ayuso | 2567c4e | 2016-08-18 11:15:12 +0200 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(nf_conntrack_htable_size); |
| 187 | |
Hagen Paul Pfeifer | e478075 | 2009-02-20 10:47:09 +0100 | [diff] [blame] | 188 | unsigned int nf_conntrack_max __read_mostly; |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 189 | seqcount_t nf_conntrack_generation __read_mostly; |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 190 | |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 191 | DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked); |
| 192 | EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 193 | |
Florian Westphal | 141658f | 2016-04-18 16:17:01 +0200 | [diff] [blame] | 194 | static unsigned int nf_conntrack_hash_rnd __read_mostly; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 195 | |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 196 | static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple, |
| 197 | const struct net *net) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 198 | { |
Patrick McHardy | 0794935 | 2008-01-31 04:40:52 -0800 | [diff] [blame] | 199 | unsigned int n; |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 200 | u32 seed; |
Sami Farin | 9b88790 | 2007-03-14 16:43:00 -0700 | [diff] [blame] | 201 | |
Florian Westphal | 141658f | 2016-04-18 16:17:01 +0200 | [diff] [blame] | 202 | get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd)); |
| 203 | |
Patrick McHardy | 0794935 | 2008-01-31 04:40:52 -0800 | [diff] [blame] | 204 | /* The direction must be ignored, so we hash everything up to the |
| 205 | * destination ports (which is a multiple of 4) and treat the last |
| 206 | * three bytes manually. |
| 207 | */ |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 208 | seed = nf_conntrack_hash_rnd ^ net_hash_mix(net); |
Patrick McHardy | 0794935 | 2008-01-31 04:40:52 -0800 | [diff] [blame] | 209 | n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32); |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 210 | return jhash2((u32 *)tuple, n, seed ^ |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 211 | (((__force __u16)tuple->dst.u.all << 16) | |
| 212 | tuple->dst.protonum)); |
| 213 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 214 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 215 | static u32 scale_hash(u32 hash) |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 216 | { |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 217 | return reciprocal_scale(hash, nf_conntrack_htable_size); |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 220 | static u32 __hash_conntrack(const struct net *net, |
| 221 | const struct nf_conntrack_tuple *tuple, |
| 222 | unsigned int size) |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 223 | { |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 224 | return reciprocal_scale(hash_conntrack_raw(tuple, net), size); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 227 | static u32 hash_conntrack(const struct net *net, |
| 228 | const struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 229 | { |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 230 | return scale_hash(hash_conntrack_raw(tuple, net)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 233 | bool |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 234 | nf_ct_get_tuple(const struct sk_buff *skb, |
| 235 | unsigned int nhoff, |
| 236 | unsigned int dataoff, |
| 237 | u_int16_t l3num, |
| 238 | u_int8_t protonum, |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 239 | struct net *net, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 240 | struct nf_conntrack_tuple *tuple, |
| 241 | const struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 242 | const struct nf_conntrack_l4proto *l4proto) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 243 | { |
Philip Craig | 443a70d | 2008-04-29 03:35:10 -0700 | [diff] [blame] | 244 | memset(tuple, 0, sizeof(*tuple)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 245 | |
| 246 | tuple->src.l3num = l3num; |
| 247 | if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0) |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 248 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 249 | |
| 250 | tuple->dst.protonum = protonum; |
| 251 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; |
| 252 | |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 253 | return l4proto->pkt_to_tuple(skb, dataoff, net, tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 254 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(nf_ct_get_tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 256 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 257 | bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff, |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 258 | u_int16_t l3num, |
| 259 | struct net *net, struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 260 | { |
| 261 | struct nf_conntrack_l3proto *l3proto; |
| 262 | struct nf_conntrack_l4proto *l4proto; |
| 263 | unsigned int protoff; |
| 264 | u_int8_t protonum; |
| 265 | int ret; |
| 266 | |
| 267 | rcu_read_lock(); |
| 268 | |
| 269 | l3proto = __nf_ct_l3proto_find(l3num); |
| 270 | ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum); |
| 271 | if (ret != NF_ACCEPT) { |
| 272 | rcu_read_unlock(); |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 273 | return false; |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | l4proto = __nf_ct_l4proto_find(l3num, protonum); |
| 277 | |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 278 | ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple, |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 279 | l3proto, l4proto); |
| 280 | |
| 281 | rcu_read_unlock(); |
| 282 | return ret; |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr); |
| 285 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 286 | bool |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 287 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, |
| 288 | const struct nf_conntrack_tuple *orig, |
| 289 | const struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 290 | const struct nf_conntrack_l4proto *l4proto) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 291 | { |
Philip Craig | 443a70d | 2008-04-29 03:35:10 -0700 | [diff] [blame] | 292 | memset(inverse, 0, sizeof(*inverse)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 293 | |
| 294 | inverse->src.l3num = orig->src.l3num; |
| 295 | if (l3proto->invert_tuple(inverse, orig) == 0) |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 296 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 297 | |
| 298 | inverse->dst.dir = !orig->dst.dir; |
| 299 | |
| 300 | inverse->dst.protonum = orig->dst.protonum; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 301 | return l4proto->invert_tuple(inverse, orig); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 302 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 303 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 304 | |
Florian Westphal | 1922476 | 2019-08-17 00:01:34 +0100 | [diff] [blame] | 305 | /* Generate a almost-unique pseudo-id for a given conntrack. |
| 306 | * |
| 307 | * intentionally doesn't re-use any of the seeds used for hash |
| 308 | * table location, we assume id gets exposed to userspace. |
| 309 | * |
| 310 | * Following nf_conn items do not change throughout lifetime |
Dirk Morris | 62b0863 | 2019-08-08 13:57:51 -0700 | [diff] [blame] | 311 | * of the nf_conn: |
Florian Westphal | 1922476 | 2019-08-17 00:01:34 +0100 | [diff] [blame] | 312 | * |
| 313 | * 1. nf_conn address |
Dirk Morris | 62b0863 | 2019-08-08 13:57:51 -0700 | [diff] [blame] | 314 | * 2. nf_conn->master address (normally NULL) |
| 315 | * 3. the associated net namespace |
| 316 | * 4. the original direction tuple |
Florian Westphal | 1922476 | 2019-08-17 00:01:34 +0100 | [diff] [blame] | 317 | */ |
| 318 | u32 nf_ct_get_id(const struct nf_conn *ct) |
| 319 | { |
| 320 | static __read_mostly siphash_key_t ct_id_seed; |
| 321 | unsigned long a, b, c, d; |
| 322 | |
| 323 | net_get_random_once(&ct_id_seed, sizeof(ct_id_seed)); |
| 324 | |
| 325 | a = (unsigned long)ct; |
Dirk Morris | 62b0863 | 2019-08-08 13:57:51 -0700 | [diff] [blame] | 326 | b = (unsigned long)ct->master; |
| 327 | c = (unsigned long)nf_ct_net(ct); |
| 328 | d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 329 | sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple), |
Florian Westphal | 1922476 | 2019-08-17 00:01:34 +0100 | [diff] [blame] | 330 | &ct_id_seed); |
| 331 | #ifdef CONFIG_64BIT |
| 332 | return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed); |
| 333 | #else |
| 334 | return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed); |
| 335 | #endif |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(nf_ct_get_id); |
| 338 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 339 | static void |
| 340 | clean_from_lists(struct nf_conn *ct) |
| 341 | { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 342 | pr_debug("clean_from_lists(%p)\n", ct); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 343 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); |
| 344 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 345 | |
| 346 | /* Destroy all pending expectations */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 347 | nf_ct_remove_expectations(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 350 | /* must be called with local_bh_disable */ |
| 351 | static void nf_ct_add_to_dying_list(struct nf_conn *ct) |
| 352 | { |
| 353 | struct ct_pcpu *pcpu; |
| 354 | |
| 355 | /* add this conntrack to the (per cpu) dying list */ |
| 356 | ct->cpu = smp_processor_id(); |
| 357 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); |
| 358 | |
| 359 | spin_lock(&pcpu->lock); |
| 360 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, |
| 361 | &pcpu->dying); |
| 362 | spin_unlock(&pcpu->lock); |
| 363 | } |
| 364 | |
| 365 | /* must be called with local_bh_disable */ |
| 366 | static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct) |
| 367 | { |
| 368 | struct ct_pcpu *pcpu; |
| 369 | |
| 370 | /* add this conntrack to the (per cpu) unconfirmed list */ |
| 371 | ct->cpu = smp_processor_id(); |
| 372 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); |
| 373 | |
| 374 | spin_lock(&pcpu->lock); |
| 375 | hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, |
| 376 | &pcpu->unconfirmed); |
| 377 | spin_unlock(&pcpu->lock); |
| 378 | } |
| 379 | |
| 380 | /* must be called with local_bh_disable */ |
| 381 | static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct) |
| 382 | { |
| 383 | struct ct_pcpu *pcpu; |
| 384 | |
| 385 | /* We overload first tuple to link into unconfirmed or dying list.*/ |
| 386 | pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu); |
| 387 | |
| 388 | spin_lock(&pcpu->lock); |
| 389 | BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode)); |
| 390 | hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode); |
| 391 | spin_unlock(&pcpu->lock); |
| 392 | } |
| 393 | |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 394 | /* Released via destroy_conntrack() */ |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 395 | struct nf_conn *nf_ct_tmpl_alloc(struct net *net, |
| 396 | const struct nf_conntrack_zone *zone, |
| 397 | gfp_t flags) |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 398 | { |
| 399 | struct nf_conn *tmpl; |
| 400 | |
Joe Stringer | f58e5aa | 2015-08-04 18:34:00 -0700 | [diff] [blame] | 401 | tmpl = kzalloc(sizeof(*tmpl), flags); |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 402 | if (tmpl == NULL) |
| 403 | return NULL; |
| 404 | |
| 405 | tmpl->status = IPS_TEMPLATE; |
| 406 | write_pnet(&tmpl->ct_net, net); |
Florian Westphal | 6c8dee9 | 2016-06-11 21:57:35 +0200 | [diff] [blame] | 407 | nf_ct_zone_add(tmpl, zone); |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 408 | atomic_set(&tmpl->ct_general.use, 0); |
| 409 | |
| 410 | return tmpl; |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 411 | } |
| 412 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc); |
| 413 | |
Daniel Borkmann | 9cf94ea | 2015-08-31 19:11:02 +0200 | [diff] [blame] | 414 | void nf_ct_tmpl_free(struct nf_conn *tmpl) |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 415 | { |
| 416 | nf_ct_ext_destroy(tmpl); |
| 417 | nf_ct_ext_free(tmpl); |
| 418 | kfree(tmpl); |
| 419 | } |
Daniel Borkmann | 9cf94ea | 2015-08-31 19:11:02 +0200 | [diff] [blame] | 420 | EXPORT_SYMBOL_GPL(nf_ct_tmpl_free); |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 421 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 422 | static void |
| 423 | destroy_conntrack(struct nf_conntrack *nfct) |
| 424 | { |
| 425 | struct nf_conn *ct = (struct nf_conn *)nfct; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 426 | struct nf_conntrack_l4proto *l4proto; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 427 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 428 | pr_debug("destroy_conntrack(%p)\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 429 | NF_CT_ASSERT(atomic_read(&nfct->use) == 0); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 430 | |
Pablo Neira Ayuso | 0838aa7 | 2015-07-13 15:11:48 +0200 | [diff] [blame] | 431 | if (unlikely(nf_ct_is_template(ct))) { |
| 432 | nf_ct_tmpl_free(ct); |
| 433 | return; |
| 434 | } |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 435 | rcu_read_lock(); |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 436 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
Pablo Neira Ayuso | 4b4ceb9 | 2016-05-01 00:34:37 +0200 | [diff] [blame] | 437 | if (l4proto->destroy) |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 438 | l4proto->destroy(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 439 | |
Patrick McHardy | 982d9a9 | 2007-02-12 11:14:11 -0800 | [diff] [blame] | 440 | rcu_read_unlock(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 441 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 442 | local_bh_disable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 443 | /* Expectations will have been removed in clean_from_lists, |
| 444 | * except TFTP can create an expectation on the first packet, |
| 445 | * before connection is in the list, so we need to clean here, |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 446 | * too. |
| 447 | */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 448 | nf_ct_remove_expectations(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 449 | |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 450 | nf_ct_del_from_dying_or_unconfirmed_list(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 451 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 452 | local_bh_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 453 | |
| 454 | if (ct->master) |
| 455 | nf_ct_put(ct->master); |
| 456 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 457 | pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 458 | nf_conntrack_free(ct); |
| 459 | } |
| 460 | |
Florian Westphal | 02982c2 | 2013-07-29 15:41:54 +0200 | [diff] [blame] | 461 | static void nf_ct_delete_from_lists(struct nf_conn *ct) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 462 | { |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 463 | struct net *net = nf_ct_net(ct); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 464 | unsigned int hash, reply_hash; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 465 | unsigned int sequence; |
Patrick McHardy | 5397e97 | 2007-05-19 14:23:52 -0700 | [diff] [blame] | 466 | |
Pablo Neira Ayuso | 9858a3a | 2009-06-13 12:28:22 +0200 | [diff] [blame] | 467 | nf_ct_helper_destroy(ct); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 468 | |
| 469 | local_bh_disable(); |
| 470 | do { |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 471 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 472 | hash = hash_conntrack(net, |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 473 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 474 | reply_hash = hash_conntrack(net, |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 475 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 476 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); |
| 477 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 478 | clean_from_lists(ct); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 479 | nf_conntrack_double_unlock(hash, reply_hash); |
| 480 | |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 481 | nf_ct_add_to_dying_list(ct); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 482 | |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 483 | local_bh_enable(); |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 484 | } |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 485 | |
Florian Westphal | 02982c2 | 2013-07-29 15:41:54 +0200 | [diff] [blame] | 486 | bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report) |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 487 | { |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 488 | struct nf_conn_tstamp *tstamp; |
| 489 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 490 | if (test_and_set_bit(IPS_DYING_BIT, &ct->status)) |
| 491 | return false; |
| 492 | |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 493 | tstamp = nf_conn_tstamp_find(ct); |
| 494 | if (tstamp && tstamp->stop == 0) |
Eric Dumazet | d2de875 | 2014-08-22 18:32:09 -0700 | [diff] [blame] | 495 | tstamp->stop = ktime_get_real_ns(); |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 496 | |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 497 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
| 498 | portid, report) < 0) { |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 499 | /* destroy event was not delivered. nf_ct_put will |
| 500 | * be done by event cache worker on redelivery. |
| 501 | */ |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 502 | nf_ct_delete_from_lists(ct); |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 503 | nf_conntrack_ecache_delayed_work(nf_ct_net(ct)); |
Florian Westphal | 02982c2 | 2013-07-29 15:41:54 +0200 | [diff] [blame] | 504 | return false; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 505 | } |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 506 | |
| 507 | nf_conntrack_ecache_work(nf_ct_net(ct)); |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 508 | nf_ct_delete_from_lists(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 509 | nf_ct_put(ct); |
Florian Westphal | 02982c2 | 2013-07-29 15:41:54 +0200 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | EXPORT_SYMBOL_GPL(nf_ct_delete); |
| 513 | |
Andrey Vagin | c6825c0 | 2014-01-29 19:34:14 +0100 | [diff] [blame] | 514 | static inline bool |
| 515 | nf_ct_key_equal(struct nf_conntrack_tuple_hash *h, |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 516 | const struct nf_conntrack_tuple *tuple, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 517 | const struct nf_conntrack_zone *zone, |
| 518 | const struct net *net) |
Andrey Vagin | c6825c0 | 2014-01-29 19:34:14 +0100 | [diff] [blame] | 519 | { |
| 520 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 521 | |
| 522 | /* A conntrack can be recreated with the equal tuple, |
| 523 | * so we need to check that the conntrack is confirmed |
| 524 | */ |
| 525 | return nf_ct_tuple_equal(tuple, &h->tuple) && |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 526 | nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) && |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 527 | nf_ct_is_confirmed(ct) && |
| 528 | net_eq(net, nf_ct_net(ct)); |
Andrey Vagin | c6825c0 | 2014-01-29 19:34:14 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 531 | /* caller must hold rcu readlock and none of the nf_conntrack_locks */ |
| 532 | static void nf_ct_gc_expired(struct nf_conn *ct) |
| 533 | { |
| 534 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
| 535 | return; |
| 536 | |
| 537 | if (nf_ct_should_gc(ct)) |
| 538 | nf_ct_kill(ct); |
| 539 | |
| 540 | nf_ct_put(ct); |
| 541 | } |
| 542 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 543 | /* |
| 544 | * Warning : |
| 545 | * - Caller must take a reference on returned object |
| 546 | * and recheck nf_ct_tuple_equal(tuple, &h->tuple) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 547 | */ |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 548 | static struct nf_conntrack_tuple_hash * |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 549 | ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone, |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 550 | const struct nf_conntrack_tuple *tuple, u32 hash) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 551 | { |
| 552 | struct nf_conntrack_tuple_hash *h; |
Florian Westphal | 5e3c61f | 2016-04-28 19:13:41 +0200 | [diff] [blame] | 553 | struct hlist_nulls_head *ct_hash; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 554 | struct hlist_nulls_node *n; |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 555 | unsigned int bucket, hsize; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 556 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 557 | begin: |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 558 | nf_conntrack_get_ht(&ct_hash, &hsize); |
| 559 | bucket = reciprocal_scale(hash, hsize); |
Florian Westphal | 5e3c61f | 2016-04-28 19:13:41 +0200 | [diff] [blame] | 560 | |
| 561 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) { |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 562 | struct nf_conn *ct; |
| 563 | |
| 564 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 565 | if (nf_ct_is_expired(ct)) { |
| 566 | nf_ct_gc_expired(ct); |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | if (nf_ct_is_dying(ct)) |
| 571 | continue; |
| 572 | |
Florian Westphal | 8e8118f | 2016-09-11 22:55:53 +0200 | [diff] [blame] | 573 | if (nf_ct_key_equal(h, tuple, zone, net)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 574 | return h; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 575 | } |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 576 | /* |
| 577 | * if the nulls value we got at the end of this lookup is |
| 578 | * not the expected one, we must restart lookup. |
| 579 | * We probably met an item that was moved to another chain. |
| 580 | */ |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 581 | if (get_nulls_value(n) != bucket) { |
Florian Westphal | 2cf1234 | 2016-04-28 19:13:40 +0200 | [diff] [blame] | 582 | NF_CT_STAT_INC_ATOMIC(net, search_restart); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 583 | goto begin; |
Jesper Dangaard Brouer | af740b2 | 2010-04-23 12:34:56 +0200 | [diff] [blame] | 584 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 585 | |
| 586 | return NULL; |
| 587 | } |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 588 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 589 | /* Find a connection corresponding to a tuple. */ |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 590 | static struct nf_conntrack_tuple_hash * |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 591 | __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 592 | const struct nf_conntrack_tuple *tuple, u32 hash) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 593 | { |
| 594 | struct nf_conntrack_tuple_hash *h; |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 595 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 596 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 597 | rcu_read_lock(); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 598 | begin: |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 599 | h = ____nf_conntrack_find(net, zone, tuple, hash); |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 600 | if (h) { |
| 601 | ct = nf_ct_tuplehash_to_ctrack(h); |
Patrick McHardy | 8d8890b7 | 2009-06-22 14:14:41 +0200 | [diff] [blame] | 602 | if (unlikely(nf_ct_is_dying(ct) || |
| 603 | !atomic_inc_not_zero(&ct->ct_general.use))) |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 604 | h = NULL; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 605 | else { |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 606 | if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) { |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 607 | nf_ct_put(ct); |
| 608 | goto begin; |
| 609 | } |
| 610 | } |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 611 | } |
| 612 | rcu_read_unlock(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 613 | |
| 614 | return h; |
| 615 | } |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 616 | |
| 617 | struct nf_conntrack_tuple_hash * |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 618 | nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 619 | const struct nf_conntrack_tuple *tuple) |
| 620 | { |
| 621 | return __nf_conntrack_find_get(net, zone, tuple, |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 622 | hash_conntrack_raw(tuple, net)); |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 623 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 624 | EXPORT_SYMBOL_GPL(nf_conntrack_find_get); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 625 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 626 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
| 627 | unsigned int hash, |
Jesper Dangaard Brouer | b476b72 | 2014-03-03 14:44:54 +0100 | [diff] [blame] | 628 | unsigned int reply_hash) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 629 | { |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 630 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode, |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 631 | &nf_conntrack_hash[hash]); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 632 | hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode, |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 633 | &nf_conntrack_hash[reply_hash]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 636 | int |
| 637 | nf_conntrack_hash_check_insert(struct nf_conn *ct) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 638 | { |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 639 | const struct nf_conntrack_zone *zone; |
Patrick McHardy | d696c7b | 2010-02-08 11:18:07 -0800 | [diff] [blame] | 640 | struct net *net = nf_ct_net(ct); |
Jesper Dangaard Brouer | b476b72 | 2014-03-03 14:44:54 +0100 | [diff] [blame] | 641 | unsigned int hash, reply_hash; |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 642 | struct nf_conntrack_tuple_hash *h; |
| 643 | struct hlist_nulls_node *n; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 644 | unsigned int sequence; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 645 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 646 | zone = nf_ct_zone(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 647 | |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 648 | local_bh_disable(); |
| 649 | do { |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 650 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 651 | hash = hash_conntrack(net, |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 652 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 653 | reply_hash = hash_conntrack(net, |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 654 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 655 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 656 | |
| 657 | /* See if there's one in the list already, including reverse */ |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 658 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 659 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 660 | zone, net)) |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 661 | goto out; |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 662 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 663 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 664 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 665 | zone, net)) |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 666 | goto out; |
| 667 | |
Pablo Neira Ayuso | e53376b | 2014-02-03 20:01:53 +0100 | [diff] [blame] | 668 | smp_wmb(); |
| 669 | /* The caller holds a reference to this object */ |
| 670 | atomic_set(&ct->ct_general.use, 2); |
Jesper Dangaard Brouer | b476b72 | 2014-03-03 14:44:54 +0100 | [diff] [blame] | 671 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 672 | nf_conntrack_double_unlock(hash, reply_hash); |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 673 | NF_CT_STAT_INC(net, insert); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 674 | local_bh_enable(); |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 675 | return 0; |
| 676 | |
| 677 | out: |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 678 | nf_conntrack_double_unlock(hash, reply_hash); |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 679 | NF_CT_STAT_INC(net, insert_failed); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 680 | local_bh_enable(); |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 681 | return -EEXIST; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 682 | } |
Jozsef Kadlecsik | 7d367e0 | 2012-02-24 11:45:49 +0100 | [diff] [blame] | 683 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 684 | |
Pablo Neira Ayuso | ba76738 | 2016-05-02 21:28:57 +0200 | [diff] [blame] | 685 | static inline void nf_ct_acct_update(struct nf_conn *ct, |
| 686 | enum ip_conntrack_info ctinfo, |
| 687 | unsigned int len) |
| 688 | { |
| 689 | struct nf_conn_acct *acct; |
| 690 | |
| 691 | acct = nf_conn_acct_find(ct); |
| 692 | if (acct) { |
| 693 | struct nf_conn_counter *counter = acct->counter; |
| 694 | |
| 695 | atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets); |
| 696 | atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes); |
| 697 | } |
| 698 | } |
| 699 | |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 700 | static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
| 701 | const struct nf_conn *loser_ct) |
| 702 | { |
| 703 | struct nf_conn_acct *acct; |
| 704 | |
| 705 | acct = nf_conn_acct_find(loser_ct); |
| 706 | if (acct) { |
| 707 | struct nf_conn_counter *counter = acct->counter; |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 708 | unsigned int bytes; |
| 709 | |
| 710 | /* u32 should be fine since we must have seen one packet. */ |
| 711 | bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes); |
| 712 | nf_ct_acct_update(ct, ctinfo, bytes); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* Resolve race on insertion if this protocol allows this. */ |
| 717 | static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb, |
| 718 | enum ip_conntrack_info ctinfo, |
| 719 | struct nf_conntrack_tuple_hash *h) |
| 720 | { |
| 721 | /* This is the conntrack entry already in hashes that won race. */ |
| 722 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 723 | struct nf_conntrack_l4proto *l4proto; |
| 724 | |
| 725 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
| 726 | if (l4proto->allow_clash && |
Florian Westphal | 25db12f | 2017-03-28 10:31:03 +0200 | [diff] [blame] | 727 | ((ct->status & IPS_NAT_DONE_MASK) == 0) && |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 728 | !nf_ct_is_dying(ct) && |
| 729 | atomic_inc_not_zero(&ct->ct_general.use)) { |
| 730 | nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct); |
| 731 | nf_conntrack_put(skb->nfct); |
| 732 | /* Assign conntrack already in hashes to this skbuff. Don't |
| 733 | * modify skb->nfctinfo to ensure consistent stateful filtering. |
| 734 | */ |
| 735 | skb->nfct = &ct->ct_general; |
| 736 | return NF_ACCEPT; |
| 737 | } |
| 738 | NF_CT_STAT_INC(net, drop); |
| 739 | return NF_DROP; |
| 740 | } |
| 741 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 742 | /* Confirm a connection given skb; places it in hash table */ |
| 743 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 744 | __nf_conntrack_confirm(struct sk_buff *skb) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 745 | { |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 746 | const struct nf_conntrack_zone *zone; |
Jesper Dangaard Brouer | b476b72 | 2014-03-03 14:44:54 +0100 | [diff] [blame] | 747 | unsigned int hash, reply_hash; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 748 | struct nf_conntrack_tuple_hash *h; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 749 | struct nf_conn *ct; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 750 | struct nf_conn_help *help; |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 751 | struct nf_conn_tstamp *tstamp; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 752 | struct hlist_nulls_node *n; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 753 | enum ip_conntrack_info ctinfo; |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 754 | struct net *net; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 755 | unsigned int sequence; |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 756 | int ret = NF_DROP; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 757 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 758 | ct = nf_ct_get(skb, &ctinfo); |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 759 | net = nf_ct_net(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 760 | |
| 761 | /* ipt_REJECT uses nf_conntrack_attach to attach related |
| 762 | ICMP/TCP RST packets in other direction. Actual packet |
| 763 | which created connection will be IP_CT_NEW or for an |
| 764 | expected connection, IP_CT_RELATED. */ |
| 765 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) |
| 766 | return NF_ACCEPT; |
| 767 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 768 | zone = nf_ct_zone(ct); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 769 | local_bh_disable(); |
| 770 | |
| 771 | do { |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 772 | sequence = read_seqcount_begin(&nf_conntrack_generation); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 773 | /* reuse the hash saved before */ |
| 774 | hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev; |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 775 | hash = scale_hash(hash); |
Daniel Borkmann | deedb59 | 2015-08-14 16:03:39 +0200 | [diff] [blame] | 776 | reply_hash = hash_conntrack(net, |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 777 | &ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 778 | |
| 779 | } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 780 | |
| 781 | /* We're not in hash table, and we refuse to set up related |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 782 | * connections for unconfirmed conns. But packet copies and |
| 783 | * REJECT will give spurious warnings here. |
| 784 | */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 785 | /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */ |
| 786 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 787 | /* No external references means no one else could have |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 788 | * confirmed us. |
| 789 | */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 790 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 791 | pr_debug("Confirming conntrack %p\n", ct); |
Pablo Neira Ayuso | 8ca3f5e | 2014-11-25 00:14:47 +0100 | [diff] [blame] | 792 | /* We have to check the DYING flag after unlink to prevent |
| 793 | * a race against nf_ct_get_next_corpse() possibly called from |
| 794 | * user context, else we insert an already 'dead' hash, blocking |
| 795 | * further use of that particular connection -JM. |
| 796 | */ |
| 797 | nf_ct_del_from_dying_or_unconfirmed_list(ct); |
Joerg Marx | fc35077 | 2010-05-20 15:55:30 +0200 | [diff] [blame] | 798 | |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 799 | if (unlikely(nf_ct_is_dying(ct))) { |
| 800 | nf_ct_add_to_dying_list(ct); |
| 801 | goto dying; |
| 802 | } |
Joerg Marx | fc35077 | 2010-05-20 15:55:30 +0200 | [diff] [blame] | 803 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 804 | /* See if there's one in the list already, including reverse: |
| 805 | NAT could have grabbed it without realizing, since we're |
| 806 | not in the hash. If there is, we lost race. */ |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 807 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 808 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 809 | zone, net)) |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 810 | goto out; |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 811 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 812 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) |
Florian Westphal | 8680434 | 2016-04-28 19:13:43 +0200 | [diff] [blame] | 813 | if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 814 | zone, net)) |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 815 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 816 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 817 | /* Timer relative to confirmation time, not original |
| 818 | setting time, otherwise we'd get timer wrap in |
| 819 | weird delay cases. */ |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 820 | ct->timeout += nfct_time_stamp; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 821 | atomic_inc(&ct->ct_general.use); |
Changli Gao | 45eec34 | 2011-01-18 15:08:13 +0100 | [diff] [blame] | 822 | ct->status |= IPS_CONFIRMED; |
Patrick McHardy | 5c8ec91 | 2009-06-22 14:14:16 +0200 | [diff] [blame] | 823 | |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 824 | /* set conntrack timestamp, if enabled. */ |
| 825 | tstamp = nf_conn_tstamp_find(ct); |
| 826 | if (tstamp) { |
| 827 | if (skb->tstamp.tv64 == 0) |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 828 | __net_timestamp(skb); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 829 | |
| 830 | tstamp->start = ktime_to_ns(skb->tstamp); |
| 831 | } |
Patrick McHardy | 5c8ec91 | 2009-06-22 14:14:16 +0200 | [diff] [blame] | 832 | /* Since the lookup is lockless, hash insertion must be done after |
| 833 | * starting the timer and setting the CONFIRMED bit. The RCU barriers |
| 834 | * guarantee that no other CPU can find the conntrack before the above |
| 835 | * stores are visible. |
| 836 | */ |
Jesper Dangaard Brouer | b476b72 | 2014-03-03 14:44:54 +0100 | [diff] [blame] | 837 | __nf_conntrack_hash_insert(ct, hash, reply_hash); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 838 | nf_conntrack_double_unlock(hash, reply_hash); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 839 | local_bh_enable(); |
Patrick McHardy | 5c8ec91 | 2009-06-22 14:14:16 +0200 | [diff] [blame] | 840 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 841 | help = nfct_help(ct); |
| 842 | if (help && help->helper) |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 843 | nf_conntrack_event_cache(IPCT_HELPER, ct); |
Pablo Neira Ayuso | 17e6e4e | 2009-06-02 20:08:46 +0200 | [diff] [blame] | 844 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 845 | nf_conntrack_event_cache(master_ct(ct) ? |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 846 | IPCT_RELATED : IPCT_NEW, ct); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 847 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 848 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 849 | out: |
Pablo Neira Ayuso | 8ca3f5e | 2014-11-25 00:14:47 +0100 | [diff] [blame] | 850 | nf_ct_add_to_dying_list(ct); |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 851 | ret = nf_ct_resolve_clash(net, skb, ctinfo, h); |
| 852 | dying: |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 853 | nf_conntrack_double_unlock(hash, reply_hash); |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 854 | NF_CT_STAT_INC(net, insert_failed); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 855 | local_bh_enable(); |
Pablo Neira Ayuso | 71d8c47 | 2016-05-01 00:28:40 +0200 | [diff] [blame] | 856 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 857 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 858 | EXPORT_SYMBOL_GPL(__nf_conntrack_confirm); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 859 | |
| 860 | /* Returns true if a connection correspondings to the tuple (required |
| 861 | for NAT). */ |
| 862 | int |
| 863 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, |
| 864 | const struct nf_conn *ignored_conntrack) |
| 865 | { |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 866 | struct net *net = nf_ct_net(ignored_conntrack); |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 867 | const struct nf_conntrack_zone *zone; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 868 | struct nf_conntrack_tuple_hash *h; |
Florian Westphal | 5e3c61f | 2016-04-28 19:13:41 +0200 | [diff] [blame] | 869 | struct hlist_nulls_head *ct_hash; |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 870 | unsigned int hash, hsize; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 871 | struct hlist_nulls_node *n; |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 872 | struct nf_conn *ct; |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 873 | |
| 874 | zone = nf_ct_zone(ignored_conntrack); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 875 | |
Florian Westphal | 2cf1234 | 2016-04-28 19:13:40 +0200 | [diff] [blame] | 876 | rcu_read_lock(); |
Florian Westphal | 95a8d19 | 2016-08-25 15:33:29 +0200 | [diff] [blame] | 877 | begin: |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 878 | nf_conntrack_get_ht(&ct_hash, &hsize); |
| 879 | hash = __hash_conntrack(net, tuple, hsize); |
Florian Westphal | 5e3c61f | 2016-04-28 19:13:41 +0200 | [diff] [blame] | 880 | |
| 881 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) { |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 882 | ct = nf_ct_tuplehash_to_ctrack(h); |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 883 | |
| 884 | if (ct == ignored_conntrack) |
| 885 | continue; |
| 886 | |
| 887 | if (nf_ct_is_expired(ct)) { |
| 888 | nf_ct_gc_expired(ct); |
| 889 | continue; |
| 890 | } |
| 891 | |
| 892 | if (nf_ct_key_equal(h, tuple, zone, net)) { |
Martynas Pumputis | 9204492 | 2019-01-29 15:51:42 +0100 | [diff] [blame] | 893 | /* Tuple is taken already, so caller will need to find |
| 894 | * a new source port to use. |
| 895 | * |
| 896 | * Only exception: |
| 897 | * If the *original tuples* are identical, then both |
| 898 | * conntracks refer to the same flow. |
| 899 | * This is a rare situation, it can occur e.g. when |
| 900 | * more than one UDP packet is sent from same socket |
| 901 | * in different threads. |
| 902 | * |
| 903 | * Let nf_ct_resolve_clash() deal with this later. |
| 904 | */ |
| 905 | if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 906 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)) |
| 907 | continue; |
| 908 | |
Florian Westphal | 2cf1234 | 2016-04-28 19:13:40 +0200 | [diff] [blame] | 909 | NF_CT_STAT_INC_ATOMIC(net, found); |
| 910 | rcu_read_unlock(); |
Patrick McHardy | ba419af | 2008-01-31 04:39:23 -0800 | [diff] [blame] | 911 | return 1; |
| 912 | } |
Patrick McHardy | ba419af | 2008-01-31 04:39:23 -0800 | [diff] [blame] | 913 | } |
Florian Westphal | 95a8d19 | 2016-08-25 15:33:29 +0200 | [diff] [blame] | 914 | |
| 915 | if (get_nulls_value(n) != hash) { |
| 916 | NF_CT_STAT_INC_ATOMIC(net, search_restart); |
| 917 | goto begin; |
| 918 | } |
| 919 | |
Florian Westphal | 2cf1234 | 2016-04-28 19:13:40 +0200 | [diff] [blame] | 920 | rcu_read_unlock(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 921 | |
Patrick McHardy | ba419af | 2008-01-31 04:39:23 -0800 | [diff] [blame] | 922 | return 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 923 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 924 | EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 925 | |
Patrick McHardy | 7ae7730 | 2007-07-07 22:37:38 -0700 | [diff] [blame] | 926 | #define NF_CT_EVICTION_RANGE 8 |
| 927 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 928 | /* There's a small race here where we may free a just-assured |
| 929 | connection. Too bad: we're in trouble anyway. */ |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 930 | static unsigned int early_drop_list(struct net *net, |
| 931 | struct hlist_nulls_head *head) |
| 932 | { |
| 933 | struct nf_conntrack_tuple_hash *h; |
| 934 | struct hlist_nulls_node *n; |
| 935 | unsigned int drops = 0; |
| 936 | struct nf_conn *tmp; |
| 937 | |
| 938 | hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) { |
| 939 | tmp = nf_ct_tuplehash_to_ctrack(h); |
| 940 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 941 | if (nf_ct_is_expired(tmp)) { |
| 942 | nf_ct_gc_expired(tmp); |
| 943 | continue; |
| 944 | } |
| 945 | |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 946 | if (test_bit(IPS_ASSURED_BIT, &tmp->status) || |
| 947 | !net_eq(nf_ct_net(tmp), net) || |
| 948 | nf_ct_is_dying(tmp)) |
| 949 | continue; |
| 950 | |
| 951 | if (!atomic_inc_not_zero(&tmp->ct_general.use)) |
| 952 | continue; |
| 953 | |
| 954 | /* kill only if still in same netns -- might have moved due to |
| 955 | * SLAB_DESTROY_BY_RCU rules. |
| 956 | * |
| 957 | * We steal the timer reference. If that fails timer has |
| 958 | * already fired or someone else deleted it. Just drop ref |
| 959 | * and move to next entry. |
| 960 | */ |
| 961 | if (net_eq(nf_ct_net(tmp), net) && |
| 962 | nf_ct_is_confirmed(tmp) && |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 963 | nf_ct_delete(tmp, 0, 0)) |
| 964 | drops++; |
| 965 | |
| 966 | nf_ct_put(tmp); |
| 967 | } |
| 968 | |
| 969 | return drops; |
| 970 | } |
| 971 | |
Vasily Khoruzhick | 7e1e195 | 2018-10-25 12:15:43 -0700 | [diff] [blame] | 972 | static noinline int early_drop(struct net *net, unsigned int hash) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 973 | { |
Vasily Khoruzhick | 7e1e195 | 2018-10-25 12:15:43 -0700 | [diff] [blame] | 974 | unsigned int i, bucket; |
Florian Westphal | 3e86638 | 2016-05-02 18:40:14 +0200 | [diff] [blame] | 975 | |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 976 | for (i = 0; i < NF_CT_EVICTION_RANGE; i++) { |
| 977 | struct hlist_nulls_head *ct_hash; |
Vasily Khoruzhick | 7e1e195 | 2018-10-25 12:15:43 -0700 | [diff] [blame] | 978 | unsigned int hsize, drops; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 979 | |
Liping Zhang | 3101e0f | 2016-07-12 19:45:00 +0800 | [diff] [blame] | 980 | rcu_read_lock(); |
Liping Zhang | 92e47ba | 2016-08-13 22:35:36 +0800 | [diff] [blame] | 981 | nf_conntrack_get_ht(&ct_hash, &hsize); |
Vasily Khoruzhick | 7e1e195 | 2018-10-25 12:15:43 -0700 | [diff] [blame] | 982 | if (!i) |
| 983 | bucket = reciprocal_scale(hash, hsize); |
| 984 | else |
| 985 | bucket = (bucket + 1) % hsize; |
Florian Westphal | 3e86638 | 2016-05-02 18:40:14 +0200 | [diff] [blame] | 986 | |
Vasily Khoruzhick | 7e1e195 | 2018-10-25 12:15:43 -0700 | [diff] [blame] | 987 | drops = early_drop_list(net, &ct_hash[bucket]); |
Liping Zhang | 3101e0f | 2016-07-12 19:45:00 +0800 | [diff] [blame] | 988 | rcu_read_unlock(); |
Florian Westphal | 3e86638 | 2016-05-02 18:40:14 +0200 | [diff] [blame] | 989 | |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 990 | if (drops) { |
| 991 | NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops); |
| 992 | return true; |
Pablo Neira Ayuso | 74138511 | 2012-03-06 01:22:55 +0000 | [diff] [blame] | 993 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 994 | } |
Florian Westphal | 3e86638 | 2016-05-02 18:40:14 +0200 | [diff] [blame] | 995 | |
Florian Westphal | 242922a | 2016-07-03 20:44:01 +0200 | [diff] [blame] | 996 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 999 | static void gc_worker(struct work_struct *work) |
| 1000 | { |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1001 | unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u); |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1002 | unsigned int i, goal, buckets = 0, expired_count = 0; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1003 | struct conntrack_gc_work *gc_work; |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1004 | unsigned int ratio, scanned = 0; |
| 1005 | unsigned long next_run; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1006 | |
| 1007 | gc_work = container_of(work, struct conntrack_gc_work, dwork.work); |
| 1008 | |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1009 | goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1010 | i = gc_work->last_bucket; |
| 1011 | |
| 1012 | do { |
| 1013 | struct nf_conntrack_tuple_hash *h; |
| 1014 | struct hlist_nulls_head *ct_hash; |
| 1015 | struct hlist_nulls_node *n; |
| 1016 | unsigned int hashsz; |
| 1017 | struct nf_conn *tmp; |
| 1018 | |
| 1019 | i++; |
| 1020 | rcu_read_lock(); |
| 1021 | |
| 1022 | nf_conntrack_get_ht(&ct_hash, &hashsz); |
| 1023 | if (i >= hashsz) |
| 1024 | i = 0; |
| 1025 | |
| 1026 | hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) { |
| 1027 | tmp = nf_ct_tuplehash_to_ctrack(h); |
| 1028 | |
Florian Westphal | c023c0e | 2016-08-25 15:33:34 +0200 | [diff] [blame] | 1029 | scanned++; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1030 | if (nf_ct_is_expired(tmp)) { |
| 1031 | nf_ct_gc_expired(tmp); |
| 1032 | expired_count++; |
| 1033 | continue; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /* could check get_nulls_value() here and restart if ct |
| 1038 | * was moved to another chain. But given gc is best-effort |
| 1039 | * we will just continue with next hash slot. |
| 1040 | */ |
| 1041 | rcu_read_unlock(); |
| 1042 | cond_resched_rcu_qs(); |
Florian Westphal | 5f7ff59 | 2017-01-16 18:24:56 +0100 | [diff] [blame] | 1043 | } while (++buckets < goal); |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1044 | |
| 1045 | if (gc_work->exiting) |
| 1046 | return; |
| 1047 | |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1048 | /* |
| 1049 | * Eviction will normally happen from the packet path, and not |
| 1050 | * from this gc worker. |
| 1051 | * |
| 1052 | * This worker is only here to reap expired entries when system went |
| 1053 | * idle after a busy period. |
| 1054 | * |
| 1055 | * The heuristics below are supposed to balance conflicting goals: |
| 1056 | * |
| 1057 | * 1. Minimize time until we notice a stale entry |
| 1058 | * 2. Maximize scan intervals to not waste cycles |
| 1059 | * |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1060 | * Normally, expire ratio will be close to 0. |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1061 | * |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1062 | * As soon as a sizeable fraction of the entries have expired |
| 1063 | * increase scan frequency. |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1064 | */ |
Florian Westphal | c023c0e | 2016-08-25 15:33:34 +0200 | [diff] [blame] | 1065 | ratio = scanned ? expired_count * 100 / scanned : 0; |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1066 | if (ratio > GC_EVICT_RATIO) { |
| 1067 | gc_work->next_gc_run = min_interval; |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1068 | } else { |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1069 | unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV; |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1070 | |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1071 | BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0); |
| 1072 | |
| 1073 | gc_work->next_gc_run += min_interval; |
| 1074 | if (gc_work->next_gc_run > max) |
| 1075 | gc_work->next_gc_run = max; |
Florian Westphal | e0df8ca | 2016-11-04 16:54:58 +0100 | [diff] [blame] | 1076 | } |
Florian Westphal | c023c0e | 2016-08-25 15:33:34 +0200 | [diff] [blame] | 1077 | |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1078 | next_run = gc_work->next_gc_run; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1079 | gc_work->last_bucket = i; |
Vincent Guittot | 4406671 | 2017-11-02 16:16:07 +0100 | [diff] [blame] | 1080 | queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run); |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work) |
| 1084 | { |
| 1085 | INIT_DELAYED_WORK(&gc_work->dwork, gc_worker); |
Florian Westphal | 371d034 | 2017-01-18 02:01:22 +0100 | [diff] [blame] | 1086 | gc_work->next_gc_run = HZ; |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1087 | gc_work->exiting = false; |
| 1088 | } |
| 1089 | |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1090 | static struct nf_conn * |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 1091 | __nf_conntrack_alloc(struct net *net, |
| 1092 | const struct nf_conntrack_zone *zone, |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1093 | const struct nf_conntrack_tuple *orig, |
| 1094 | const struct nf_conntrack_tuple *repl, |
| 1095 | gfp_t gfp, u32 hash) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1096 | { |
Julia Lawall | cd7fcbf | 2009-01-12 00:06:08 +0000 | [diff] [blame] | 1097 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1098 | |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 1099 | /* We don't want any race condition at early drop stage */ |
Alexey Dobriyan | 49ac871 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1100 | atomic_inc(&net->ct.count); |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 1101 | |
Patrick McHardy | 76eb946 | 2008-01-31 04:41:44 -0800 | [diff] [blame] | 1102 | if (nf_conntrack_max && |
Alexey Dobriyan | 49ac871 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1103 | unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) { |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1104 | if (!early_drop(net, hash)) { |
Alexey Dobriyan | 49ac871 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1105 | atomic_dec(&net->ct.count); |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1106 | net_warn_ratelimited("nf_conntrack: table full, dropping packet\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1107 | return ERR_PTR(-ENOMEM); |
| 1108 | } |
| 1109 | } |
| 1110 | |
Eric Dumazet | 941297f | 2009-07-16 14:03:40 +0200 | [diff] [blame] | 1111 | /* |
| 1112 | * Do not use kmem_cache_zalloc(), as this cache uses |
| 1113 | * SLAB_DESTROY_BY_RCU. |
| 1114 | */ |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 1115 | ct = kmem_cache_alloc(nf_conntrack_cachep, gfp); |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1116 | if (ct == NULL) |
| 1117 | goto out; |
| 1118 | |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 1119 | spin_lock_init(&ct->lock); |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1120 | ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
Eric Dumazet | 941297f | 2009-07-16 14:03:40 +0200 | [diff] [blame] | 1121 | ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL; |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1122 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1123 | /* save hash for reusing when confirming */ |
| 1124 | *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash; |
Florian Westphal | c41884c | 2014-11-24 15:25:57 +0100 | [diff] [blame] | 1125 | ct->status = 0; |
Eric Dumazet | c2d9ba9 | 2010-06-01 06:51:19 +0000 | [diff] [blame] | 1126 | write_pnet(&ct->ct_net, net); |
Florian Westphal | c41884c | 2014-11-24 15:25:57 +0100 | [diff] [blame] | 1127 | memset(&ct->__nfct_init_offset[0], 0, |
| 1128 | offsetof(struct nf_conn, proto) - |
| 1129 | offsetof(struct nf_conn, __nfct_init_offset[0])); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1130 | |
Florian Westphal | 6c8dee9 | 2016-06-11 21:57:35 +0200 | [diff] [blame] | 1131 | nf_ct_zone_add(ct, zone); |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1132 | |
Pablo Neira Ayuso | e53376b | 2014-02-03 20:01:53 +0100 | [diff] [blame] | 1133 | /* Because we use RCU lookups, we set ct_general.use to zero before |
| 1134 | * this is inserted in any list. |
Eric Dumazet | 941297f | 2009-07-16 14:03:40 +0200 | [diff] [blame] | 1135 | */ |
Pablo Neira Ayuso | e53376b | 2014-02-03 20:01:53 +0100 | [diff] [blame] | 1136 | atomic_set(&ct->ct_general.use, 0); |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1137 | return ct; |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1138 | out: |
| 1139 | atomic_dec(&net->ct.count); |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 1140 | return ERR_PTR(-ENOMEM); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1141 | } |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1142 | |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 1143 | struct nf_conn *nf_conntrack_alloc(struct net *net, |
| 1144 | const struct nf_conntrack_zone *zone, |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1145 | const struct nf_conntrack_tuple *orig, |
| 1146 | const struct nf_conntrack_tuple *repl, |
| 1147 | gfp_t gfp) |
| 1148 | { |
| 1149 | return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0); |
| 1150 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1151 | EXPORT_SYMBOL_GPL(nf_conntrack_alloc); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1152 | |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1153 | void nf_conntrack_free(struct nf_conn *ct) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1154 | { |
Eric Dumazet | 1d45209 | 2009-03-24 14:26:50 +0100 | [diff] [blame] | 1155 | struct net *net = nf_ct_net(ct); |
| 1156 | |
Pablo Neira Ayuso | e53376b | 2014-02-03 20:01:53 +0100 | [diff] [blame] | 1157 | /* A freed object has refcnt == 0, that's |
| 1158 | * the golden rule for SLAB_DESTROY_BY_RCU |
| 1159 | */ |
| 1160 | NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0); |
| 1161 | |
Patrick McHardy | ceeff75 | 2008-06-11 17:51:10 -0700 | [diff] [blame] | 1162 | nf_ct_ext_destroy(ct); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1163 | nf_ct_ext_free(ct); |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 1164 | kmem_cache_free(nf_conntrack_cachep, ct); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1165 | smp_mb__before_atomic(); |
Pablo Neira Ayuso | 0c3c6c0 | 2013-11-18 12:53:59 +0100 | [diff] [blame] | 1166 | atomic_dec(&net->ct.count); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1167 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1168 | EXPORT_SYMBOL_GPL(nf_conntrack_free); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1169 | |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 1170 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1171 | /* Allocate a new conntrack: we return -ENOMEM if classification |
| 1172 | failed due to stress. Otherwise it really is unclassifiable. */ |
| 1173 | static struct nf_conntrack_tuple_hash * |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1174 | init_conntrack(struct net *net, struct nf_conn *tmpl, |
Alexey Dobriyan | 5a1fb39 | 2008-10-08 11:35:02 +0200 | [diff] [blame] | 1175 | const struct nf_conntrack_tuple *tuple, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1176 | struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1177 | struct nf_conntrack_l4proto *l4proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1178 | struct sk_buff *skb, |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1179 | unsigned int dataoff, u32 hash) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1180 | { |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1181 | struct nf_conn *ct; |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 1182 | struct nf_conn_help *help; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1183 | struct nf_conntrack_tuple repl_tuple; |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1184 | struct nf_conntrack_ecache *ecache; |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1185 | struct nf_conntrack_expect *exp = NULL; |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 1186 | const struct nf_conntrack_zone *zone; |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1187 | struct nf_conn_timeout *timeout_ext; |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1188 | struct nf_conntrack_zone tmp; |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1189 | unsigned int *timeouts; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1190 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1191 | if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 1192 | pr_debug("Can't invert tuple.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1193 | return NULL; |
| 1194 | } |
| 1195 | |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1196 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1197 | ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC, |
| 1198 | hash); |
Joe Perches | 0a9ee81 | 2011-08-29 14:17:25 -0700 | [diff] [blame] | 1199 | if (IS_ERR(ct)) |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1200 | return (struct nf_conntrack_tuple_hash *)ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1201 | |
Gao Feng | 4440a2a | 2016-09-13 08:49:18 +0800 | [diff] [blame] | 1202 | if (!nf_ct_add_synproxy(ct, tmpl)) { |
| 1203 | nf_conntrack_free(ct); |
| 1204 | return ERR_PTR(-ENOMEM); |
Patrick McHardy | 48b1de4 | 2013-08-27 08:50:14 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1207 | timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL; |
Pablo Neira Ayuso | ae2d708 | 2015-10-05 16:51:01 +0200 | [diff] [blame] | 1208 | if (timeout_ext) { |
| 1209 | timeouts = nf_ct_timeout_data(timeout_ext); |
| 1210 | if (unlikely(!timeouts)) |
| 1211 | timeouts = l4proto->get_timeouts(net); |
| 1212 | } else { |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1213 | timeouts = l4proto->get_timeouts(net); |
Pablo Neira Ayuso | ae2d708 | 2015-10-05 16:51:01 +0200 | [diff] [blame] | 1214 | } |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1215 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 1216 | if (!l4proto->new(ct, skb, dataoff, timeouts)) { |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1217 | nf_conntrack_free(ct); |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1218 | pr_debug("can't track with proto module\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1219 | return NULL; |
| 1220 | } |
| 1221 | |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1222 | if (timeout_ext) |
Pablo Neira Ayuso | ae2d708 | 2015-10-05 16:51:01 +0200 | [diff] [blame] | 1223 | nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout), |
| 1224 | GFP_ATOMIC); |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1225 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 1226 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 1227 | nf_ct_tstamp_ext_add(ct, GFP_ATOMIC); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 1228 | nf_ct_labels_ext_add(ct); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1229 | |
| 1230 | ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL; |
| 1231 | nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0, |
| 1232 | ecache ? ecache->expmask : 0, |
| 1233 | GFP_ATOMIC); |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 1234 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1235 | local_bh_disable(); |
| 1236 | if (net->ct.expect_count) { |
| 1237 | spin_lock(&nf_conntrack_expect_lock); |
| 1238 | exp = nf_ct_find_expectation(net, zone, tuple); |
| 1239 | if (exp) { |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1240 | pr_debug("expectation arrives ct=%p exp=%p\n", |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1241 | ct, exp); |
| 1242 | /* Welcome, Mr. Bond. We've been expecting you... */ |
| 1243 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
| 1244 | /* exp->master safe, refcnt bumped in nf_ct_find_expectation */ |
| 1245 | ct->master = exp->master; |
| 1246 | if (exp->helper) { |
| 1247 | help = nf_ct_helper_ext_add(ct, exp->helper, |
| 1248 | GFP_ATOMIC); |
| 1249 | if (help) |
| 1250 | rcu_assign_pointer(help->helper, exp->helper); |
| 1251 | } |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 1252 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1253 | #ifdef CONFIG_NF_CONNTRACK_MARK |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1254 | ct->mark = exp->master->mark; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1255 | #endif |
James Morris | 7c9728c | 2006-06-09 00:31:46 -0700 | [diff] [blame] | 1256 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1257 | ct->secmark = exp->master->secmark; |
James Morris | 7c9728c | 2006-06-09 00:31:46 -0700 | [diff] [blame] | 1258 | #endif |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1259 | NF_CT_STAT_INC(net, expect_new); |
| 1260 | } |
| 1261 | spin_unlock(&nf_conntrack_expect_lock); |
| 1262 | } |
Florian Westphal | 8e8118f | 2016-09-11 22:55:53 +0200 | [diff] [blame] | 1263 | if (!exp) |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1264 | __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1265 | |
Pablo Neira Ayuso | e53376b | 2014-02-03 20:01:53 +0100 | [diff] [blame] | 1266 | /* Now it is inserted into the unconfirmed list, bump refcount */ |
| 1267 | nf_conntrack_get(&ct->ct_general); |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 1268 | nf_ct_add_to_unconfirmed_list(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1269 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1270 | local_bh_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1271 | |
| 1272 | if (exp) { |
| 1273 | if (exp->expectfn) |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1274 | exp->expectfn(ct, exp); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1275 | nf_ct_expect_put(exp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 1278 | return &ct->tuplehash[IP_CT_DIR_ORIGINAL]; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ |
| 1282 | static inline struct nf_conn * |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1283 | resolve_normal_ct(struct net *net, struct nf_conn *tmpl, |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 1284 | struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1285 | unsigned int dataoff, |
| 1286 | u_int16_t l3num, |
| 1287 | u_int8_t protonum, |
| 1288 | struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1289 | struct nf_conntrack_l4proto *l4proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1290 | int *set_reply, |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1291 | enum ip_conntrack_info *ctinfo) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1292 | { |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 1293 | const struct nf_conntrack_zone *zone; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1294 | struct nf_conntrack_tuple tuple; |
| 1295 | struct nf_conntrack_tuple_hash *h; |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1296 | struct nf_conntrack_zone tmp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1297 | struct nf_conn *ct; |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1298 | u32 hash; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1299 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 1300 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 1301 | dataoff, l3num, protonum, net, &tuple, l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1302 | l4proto)) { |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1303 | pr_debug("Can't get tuple\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | /* look for tuple match */ |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 1308 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 1309 | hash = hash_conntrack_raw(&tuple, net); |
Changli Gao | 99f07e9 | 2010-09-21 17:49:20 +0200 | [diff] [blame] | 1310 | h = __nf_conntrack_find_get(net, zone, &tuple, hash); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1311 | if (!h) { |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1312 | h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto, |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1313 | skb, dataoff, hash); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1314 | if (!h) |
| 1315 | return NULL; |
| 1316 | if (IS_ERR(h)) |
| 1317 | return (void *)h; |
| 1318 | } |
| 1319 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 1320 | |
| 1321 | /* It exists; we have (non-exclusive) reference. */ |
| 1322 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 1323 | *ctinfo = IP_CT_ESTABLISHED_REPLY; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1324 | /* Please set reply bit if this packet OK */ |
| 1325 | *set_reply = 1; |
| 1326 | } else { |
| 1327 | /* Once we've had two way comms, always ESTABLISHED. */ |
| 1328 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1329 | pr_debug("normal packet for %p\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1330 | *ctinfo = IP_CT_ESTABLISHED; |
| 1331 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1332 | pr_debug("related packet for %p\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1333 | *ctinfo = IP_CT_RELATED; |
| 1334 | } else { |
Weongyo Jeong | ccd63c2 | 2016-03-15 10:57:44 -0700 | [diff] [blame] | 1335 | pr_debug("new packet for %p\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1336 | *ctinfo = IP_CT_NEW; |
| 1337 | } |
| 1338 | *set_reply = 0; |
| 1339 | } |
| 1340 | skb->nfct = &ct->ct_general; |
| 1341 | skb->nfctinfo = *ctinfo; |
| 1342 | return ct; |
| 1343 | } |
| 1344 | |
| 1345 | unsigned int |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 1346 | nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum, |
| 1347 | struct sk_buff *skb) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1348 | { |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1349 | struct nf_conn *ct, *tmpl = NULL; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1350 | enum ip_conntrack_info ctinfo; |
| 1351 | struct nf_conntrack_l3proto *l3proto; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1352 | struct nf_conntrack_l4proto *l4proto; |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 1353 | unsigned int *timeouts; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1354 | unsigned int dataoff; |
| 1355 | u_int8_t protonum; |
| 1356 | int set_reply = 0; |
| 1357 | int ret; |
| 1358 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1359 | if (skb->nfct) { |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1360 | /* Previously seen (loopback or untracked)? Ignore. */ |
| 1361 | tmpl = (struct nf_conn *)skb->nfct; |
| 1362 | if (!nf_ct_is_template(tmpl)) { |
| 1363 | NF_CT_STAT_INC_ATOMIC(net, ignore); |
| 1364 | return NF_ACCEPT; |
| 1365 | } |
| 1366 | skb->nfct = NULL; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
Aaron Conole | e2361cb | 2016-09-21 11:35:04 -0400 | [diff] [blame] | 1369 | /* rcu_read_lock()ed by nf_hook_thresh */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1370 | l3proto = __nf_ct_l3proto_find(pf); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1371 | ret = l3proto->get_l4proto(skb, skb_network_offset(skb), |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 1372 | &dataoff, &protonum); |
| 1373 | if (ret <= 0) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1374 | pr_debug("not prepared to track yet or error occurred\n"); |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 1375 | NF_CT_STAT_INC_ATOMIC(net, error); |
| 1376 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1377 | ret = -ret; |
| 1378 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1381 | l4proto = __nf_ct_l4proto_find(pf, protonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1382 | |
| 1383 | /* It may be an special packet, error, unclean... |
| 1384 | * inverse of the return code tells to the netfilter |
| 1385 | * core what to do with the packet. */ |
Alexey Dobriyan | 74c51a1 | 2008-10-08 11:35:05 +0200 | [diff] [blame] | 1386 | if (l4proto->error != NULL) { |
Patrick McHardy | 8fea97e | 2010-02-15 17:45:08 +0100 | [diff] [blame] | 1387 | ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo, |
| 1388 | pf, hooknum); |
Alexey Dobriyan | 74c51a1 | 2008-10-08 11:35:05 +0200 | [diff] [blame] | 1389 | if (ret <= 0) { |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 1390 | NF_CT_STAT_INC_ATOMIC(net, error); |
| 1391 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1392 | ret = -ret; |
| 1393 | goto out; |
Alexey Dobriyan | 74c51a1 | 2008-10-08 11:35:05 +0200 | [diff] [blame] | 1394 | } |
Pablo Neira Ayuso | 88ed01d | 2011-06-02 15:08:45 +0200 | [diff] [blame] | 1395 | /* ICMP[v6] protocol trackers may assign one conntrack. */ |
| 1396 | if (skb->nfct) |
| 1397 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1400 | ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum, |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1401 | l3proto, l4proto, &set_reply, &ctinfo); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1402 | if (!ct) { |
| 1403 | /* Not valid part of a connection */ |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 1404 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1405 | ret = NF_ACCEPT; |
| 1406 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | if (IS_ERR(ct)) { |
| 1410 | /* Too stressed to deal. */ |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 1411 | NF_CT_STAT_INC_ATOMIC(net, drop); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1412 | ret = NF_DROP; |
| 1413 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1416 | NF_CT_ASSERT(skb->nfct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1417 | |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1418 | /* Decide what timeout policy we want to apply to this flow. */ |
Pablo Neira Ayuso | 84b5ee9 | 2012-08-28 00:53:15 +0000 | [diff] [blame] | 1419 | timeouts = nf_ct_timeout_lookup(net, ct, l4proto); |
Pablo Neira Ayuso | 60b5f8f | 2012-03-23 00:04:53 +0100 | [diff] [blame] | 1420 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 1421 | ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts); |
Christoph Paasch | ec8d540 | 2009-03-16 15:51:29 +0100 | [diff] [blame] | 1422 | if (ret <= 0) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1423 | /* Invalid: inverse of the return code tells |
| 1424 | * the netfilter core what to do */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 1425 | pr_debug("nf_conntrack_in: Can't track with proto module\n"); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1426 | nf_conntrack_put(skb->nfct); |
| 1427 | skb->nfct = NULL; |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 1428 | NF_CT_STAT_INC_ATOMIC(net, invalid); |
Pablo Neira Ayuso | 7d1e045 | 2009-02-24 14:48:01 +0100 | [diff] [blame] | 1429 | if (ret == -NF_DROP) |
| 1430 | NF_CT_STAT_INC_ATOMIC(net, drop); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1431 | ret = -ret; |
| 1432 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) |
Patrick McHardy | 858b3133 | 2010-02-03 13:48:53 +0100 | [diff] [blame] | 1436 | nf_conntrack_event_cache(IPCT_REPLY, ct); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1437 | out: |
Pablo Neira Ayuso | c317428 | 2011-02-09 08:08:20 +0100 | [diff] [blame] | 1438 | if (tmpl) { |
| 1439 | /* Special case: we have to repeat this hook, assign the |
| 1440 | * template again to this packet. We assume that this packet |
| 1441 | * has no conntrack assigned. This is used by nf_ct_tcp. */ |
| 1442 | if (ret == NF_REPEAT) |
| 1443 | skb->nfct = (struct nf_conntrack *)tmpl; |
| 1444 | else |
| 1445 | nf_ct_put(tmpl); |
| 1446 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1447 | |
| 1448 | return ret; |
| 1449 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1450 | EXPORT_SYMBOL_GPL(nf_conntrack_in); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1451 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 1452 | bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, |
| 1453 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1454 | { |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 1455 | bool ret; |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 1456 | |
| 1457 | rcu_read_lock(); |
| 1458 | ret = nf_ct_invert_tuple(inverse, orig, |
| 1459 | __nf_ct_l3proto_find(orig->src.l3num), |
| 1460 | __nf_ct_l4proto_find(orig->src.l3num, |
| 1461 | orig->dst.protonum)); |
| 1462 | rcu_read_unlock(); |
| 1463 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1464 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1465 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1466 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1467 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
| 1468 | implicitly racy: see __nf_conntrack_confirm */ |
| 1469 | void nf_conntrack_alter_reply(struct nf_conn *ct, |
| 1470 | const struct nf_conntrack_tuple *newreply) |
| 1471 | { |
| 1472 | struct nf_conn_help *help = nfct_help(ct); |
| 1473 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1474 | /* Should be unconfirmed, so not in hash table yet */ |
| 1475 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
| 1476 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 1477 | pr_debug("Altering reply tuple of %p to ", ct); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 1478 | nf_ct_dump_tuple(newreply); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1479 | |
| 1480 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; |
Patrick McHardy | ef1a5a5 | 2008-04-14 11:21:01 +0200 | [diff] [blame] | 1481 | if (ct->master || (help && !hlist_empty(&help->expectations))) |
Patrick McHardy | c52fbb4 | 2008-01-31 04:37:36 -0800 | [diff] [blame] | 1482 | return; |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 1483 | |
Patrick McHardy | c52fbb4 | 2008-01-31 04:37:36 -0800 | [diff] [blame] | 1484 | rcu_read_lock(); |
Patrick McHardy | b2a15a6 | 2010-02-03 14:13:03 +0100 | [diff] [blame] | 1485 | __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC); |
Patrick McHardy | c52fbb4 | 2008-01-31 04:37:36 -0800 | [diff] [blame] | 1486 | rcu_read_unlock(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1487 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1488 | EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1489 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1490 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
| 1491 | void __nf_ct_refresh_acct(struct nf_conn *ct, |
| 1492 | enum ip_conntrack_info ctinfo, |
| 1493 | const struct sk_buff *skb, |
| 1494 | unsigned long extra_jiffies, |
| 1495 | int do_acct) |
| 1496 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1497 | NF_CT_ASSERT(skb); |
| 1498 | |
Eric Leblond | 997ae83 | 2006-05-29 18:24:20 -0700 | [diff] [blame] | 1499 | /* Only update if this is not a fixed timeout */ |
Patrick McHardy | 47d9504 | 2008-01-31 04:36:31 -0800 | [diff] [blame] | 1500 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) |
| 1501 | goto acct; |
Eric Leblond | 997ae83 | 2006-05-29 18:24:20 -0700 | [diff] [blame] | 1502 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1503 | /* If not in hash table, timer will not be active yet */ |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 1504 | if (nf_ct_is_confirmed(ct)) |
| 1505 | extra_jiffies += nfct_time_stamp; |
Martin Josefsson | be00c8e | 2006-11-29 02:35:12 +0100 | [diff] [blame] | 1506 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 1507 | ct->timeout = extra_jiffies; |
Patrick McHardy | 47d9504 | 2008-01-31 04:36:31 -0800 | [diff] [blame] | 1508 | acct: |
Pablo Neira Ayuso | ba76738 | 2016-05-02 21:28:57 +0200 | [diff] [blame] | 1509 | if (do_acct) |
| 1510 | nf_ct_acct_update(ct, ctinfo, skb->len); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1511 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1512 | EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1513 | |
Florian Westphal | ad66713 | 2016-08-25 15:33:35 +0200 | [diff] [blame] | 1514 | bool nf_ct_kill_acct(struct nf_conn *ct, |
| 1515 | enum ip_conntrack_info ctinfo, |
| 1516 | const struct sk_buff *skb) |
Patrick McHardy | 5109176 | 2008-06-09 15:59:06 -0700 | [diff] [blame] | 1517 | { |
Florian Westphal | ad66713 | 2016-08-25 15:33:35 +0200 | [diff] [blame] | 1518 | nf_ct_acct_update(ct, ctinfo, skb->len); |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 1519 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 1520 | return nf_ct_delete(ct, 0, 0); |
Patrick McHardy | 5109176 | 2008-06-09 15:59:06 -0700 | [diff] [blame] | 1521 | } |
Florian Westphal | ad66713 | 2016-08-25 15:33:35 +0200 | [diff] [blame] | 1522 | EXPORT_SYMBOL_GPL(nf_ct_kill_acct); |
Patrick McHardy | 5109176 | 2008-06-09 15:59:06 -0700 | [diff] [blame] | 1523 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 1524 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1525 | |
| 1526 | #include <linux/netfilter/nfnetlink.h> |
| 1527 | #include <linux/netfilter/nfnetlink_conntrack.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 1528 | #include <linux/mutex.h> |
| 1529 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1530 | /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be |
| 1531 | * in ip_conntrack_core, since we don't want the protocols to autoload |
| 1532 | * or depend on ctnetlink */ |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 1533 | int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1534 | const struct nf_conntrack_tuple *tuple) |
| 1535 | { |
David S. Miller | bae65be | 2012-04-01 18:58:28 -0400 | [diff] [blame] | 1536 | if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) || |
| 1537 | nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port)) |
| 1538 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1539 | return 0; |
| 1540 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1541 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1542 | return -1; |
| 1543 | } |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 1544 | EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1545 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1546 | const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = { |
| 1547 | [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 }, |
| 1548 | [CTA_PROTO_DST_PORT] = { .type = NLA_U16 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1549 | }; |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1550 | EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1551 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 1552 | int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1553 | struct nf_conntrack_tuple *t) |
| 1554 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1555 | if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1556 | return -EINVAL; |
| 1557 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1558 | t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]); |
| 1559 | t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1560 | |
| 1561 | return 0; |
| 1562 | } |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 1563 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple); |
Holger Eitzenberger | 5c0de29 | 2009-03-25 21:52:17 +0100 | [diff] [blame] | 1564 | |
| 1565 | int nf_ct_port_nlattr_tuple_size(void) |
| 1566 | { |
| 1567 | return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1); |
| 1568 | } |
| 1569 | EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1570 | #endif |
| 1571 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1572 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
Patrick McHardy | 312a0c16 | 2013-07-28 22:54:08 +0200 | [diff] [blame] | 1573 | static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1574 | { |
| 1575 | struct nf_conn *ct; |
| 1576 | enum ip_conntrack_info ctinfo; |
| 1577 | |
| 1578 | /* This ICMP is in reverse direction to the packet which caused it */ |
| 1579 | ct = nf_ct_get(skb, &ctinfo); |
| 1580 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 1581 | ctinfo = IP_CT_RELATED_REPLY; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1582 | else |
| 1583 | ctinfo = IP_CT_RELATED; |
| 1584 | |
| 1585 | /* Attach to new skbuff, and increment count */ |
| 1586 | nskb->nfct = &ct->ct_general; |
| 1587 | nskb->nfctinfo = ctinfo; |
| 1588 | nf_conntrack_get(nskb->nfct); |
| 1589 | } |
| 1590 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1591 | /* Bring out ya dead! */ |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1592 | static struct nf_conn * |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1593 | get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data), |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1594 | void *data, unsigned int *bucket) |
| 1595 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1596 | struct nf_conntrack_tuple_hash *h; |
| 1597 | struct nf_conn *ct; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1598 | struct hlist_nulls_node *n; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1599 | spinlock_t *lockp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1600 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1601 | for (; *bucket < nf_conntrack_htable_size; (*bucket)++) { |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1602 | lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS]; |
| 1603 | local_bh_disable(); |
Sasha Levin | b16c291 | 2016-01-18 19:23:51 -0500 | [diff] [blame] | 1604 | nf_conntrack_lock(lockp); |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1605 | if (*bucket < nf_conntrack_htable_size) { |
| 1606 | hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) { |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1607 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
| 1608 | continue; |
| 1609 | ct = nf_ct_tuplehash_to_ctrack(h); |
Florian Westphal | e0c7d47 | 2016-04-28 19:13:45 +0200 | [diff] [blame] | 1610 | if (net_eq(nf_ct_net(ct), net) && |
| 1611 | iter(ct, data)) |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1612 | goto found; |
| 1613 | } |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1614 | } |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1615 | spin_unlock(lockp); |
| 1616 | local_bh_enable(); |
Florian Westphal | d93c625 | 2016-01-20 11:16:43 +0100 | [diff] [blame] | 1617 | cond_resched(); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1618 | } |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 1619 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1620 | return NULL; |
| 1621 | found: |
Martin Josefsson | c073e3f | 2006-10-30 15:13:58 -0800 | [diff] [blame] | 1622 | atomic_inc(&ct->ct_general.use); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1623 | spin_unlock(lockp); |
| 1624 | local_bh_enable(); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1625 | return ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Florian Westphal | f51c454 | 2017-05-21 12:52:56 +0200 | [diff] [blame] | 1628 | static void |
| 1629 | __nf_ct_unconfirmed_destroy(struct net *net) |
| 1630 | { |
| 1631 | int cpu; |
| 1632 | |
| 1633 | for_each_possible_cpu(cpu) { |
| 1634 | struct nf_conntrack_tuple_hash *h; |
| 1635 | struct hlist_nulls_node *n; |
| 1636 | struct ct_pcpu *pcpu; |
| 1637 | |
| 1638 | pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); |
| 1639 | |
| 1640 | spin_lock_bh(&pcpu->lock); |
| 1641 | hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) { |
| 1642 | struct nf_conn *ct; |
| 1643 | |
| 1644 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 1645 | |
| 1646 | /* we cannot call iter() on unconfirmed list, the |
| 1647 | * owning cpu can reallocate ct->ext at any time. |
| 1648 | */ |
| 1649 | set_bit(IPS_DYING_BIT, &ct->status); |
| 1650 | } |
| 1651 | spin_unlock_bh(&pcpu->lock); |
| 1652 | cond_resched(); |
| 1653 | } |
| 1654 | } |
| 1655 | |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1656 | void nf_ct_iterate_cleanup(struct net *net, |
| 1657 | int (*iter)(struct nf_conn *i, void *data), |
Florian Westphal | c655bc6 | 2013-07-29 15:41:55 +0200 | [diff] [blame] | 1658 | void *data, u32 portid, int report) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1659 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1660 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1661 | unsigned int bucket = 0; |
| 1662 | |
Florian Westphal | d93c625 | 2016-01-20 11:16:43 +0100 | [diff] [blame] | 1663 | might_sleep(); |
| 1664 | |
Florian Westphal | 88b68bc | 2016-04-28 19:13:42 +0200 | [diff] [blame] | 1665 | if (atomic_read(&net->ct.count) == 0) |
| 1666 | return; |
| 1667 | |
Florian Westphal | f51c454 | 2017-05-21 12:52:56 +0200 | [diff] [blame] | 1668 | __nf_ct_unconfirmed_destroy(net); |
| 1669 | |
| 1670 | synchronize_net(); |
| 1671 | |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1672 | while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1673 | /* Time to push up daises... */ |
Florian Westphal | 02982c2 | 2013-07-29 15:41:54 +0200 | [diff] [blame] | 1674 | |
Florian Westphal | f330a7f | 2016-08-25 15:33:31 +0200 | [diff] [blame] | 1675 | nf_ct_delete(ct, portid, report); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1676 | nf_ct_put(ct); |
Florian Westphal | d93c625 | 2016-01-20 11:16:43 +0100 | [diff] [blame] | 1677 | cond_resched(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1678 | } |
| 1679 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 1680 | EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1681 | |
Pablo Neira Ayuso | 274d383 | 2009-06-02 20:08:38 +0200 | [diff] [blame] | 1682 | static int kill_all(struct nf_conn *i, void *data) |
| 1683 | { |
| 1684 | return 1; |
| 1685 | } |
| 1686 | |
Patrick McHardy | d862a66 | 2011-01-14 15:45:56 +0100 | [diff] [blame] | 1687 | void nf_ct_free_hashtable(void *hash, unsigned int size) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1688 | { |
Patrick McHardy | d862a66 | 2011-01-14 15:45:56 +0100 | [diff] [blame] | 1689 | if (is_vmalloc_addr(hash)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1690 | vfree(hash); |
| 1691 | else |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1692 | free_pages((unsigned long)hash, |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 1693 | get_order(sizeof(struct hlist_head) * size)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1694 | } |
Patrick McHardy | ac565e5 | 2007-07-07 22:30:08 -0700 | [diff] [blame] | 1695 | EXPORT_SYMBOL_GPL(nf_ct_free_hashtable); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1696 | |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 1697 | static int untrack_refs(void) |
| 1698 | { |
| 1699 | int cnt = 0, cpu; |
| 1700 | |
| 1701 | for_each_possible_cpu(cpu) { |
| 1702 | struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu); |
| 1703 | |
| 1704 | cnt += atomic_read(&ct->ct_general.use) - 1; |
| 1705 | } |
| 1706 | return cnt; |
| 1707 | } |
| 1708 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 1709 | void nf_conntrack_cleanup_start(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1710 | { |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1711 | conntrack_gc_work.exiting = true; |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 1712 | RCU_INIT_POINTER(ip_ct_attach, NULL); |
| 1713 | } |
| 1714 | |
| 1715 | void nf_conntrack_cleanup_end(void) |
| 1716 | { |
| 1717 | RCU_INIT_POINTER(nf_ct_destroy, NULL); |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 1718 | while (untrack_refs() > 0) |
Patrick McHardy | 9edd7ca | 2010-02-08 11:16:26 -0800 | [diff] [blame] | 1719 | schedule(); |
| 1720 | |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1721 | cancel_delayed_work_sync(&conntrack_gc_work.dwork); |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1722 | nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size); |
| 1723 | |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 1724 | nf_conntrack_proto_fini(); |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 1725 | nf_conntrack_seqadj_fini(); |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 1726 | nf_conntrack_labels_fini(); |
Gao feng | 5e615b2 | 2013-01-21 22:10:30 +0000 | [diff] [blame] | 1727 | nf_conntrack_helper_fini(); |
Gao feng | 8684094 | 2013-01-21 22:10:29 +0000 | [diff] [blame] | 1728 | nf_conntrack_timeout_fini(); |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 1729 | nf_conntrack_ecache_fini(); |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 1730 | nf_conntrack_tstamp_fini(); |
Gao feng | b7ff3a1 | 2013-01-21 22:10:26 +0000 | [diff] [blame] | 1731 | nf_conntrack_acct_fini(); |
Gao feng | 83b4dbe | 2013-01-21 22:10:25 +0000 | [diff] [blame] | 1732 | nf_conntrack_expect_fini(); |
Florian Westphal | 7757114 | 2016-06-10 17:25:19 +0200 | [diff] [blame] | 1733 | |
| 1734 | kmem_cache_destroy(nf_conntrack_cachep); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 1735 | } |
Yasuyuki Kozakai | 7d3cdc6 | 2006-02-15 15:22:21 -0800 | [diff] [blame] | 1736 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 1737 | /* |
| 1738 | * Mishearing the voices in his head, our hero wonders how he's |
| 1739 | * supposed to kill the mall. |
| 1740 | */ |
| 1741 | void nf_conntrack_cleanup_net(struct net *net) |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 1742 | { |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1743 | LIST_HEAD(single); |
| 1744 | |
| 1745 | list_add(&net->exit_list, &single); |
| 1746 | nf_conntrack_cleanup_net_list(&single); |
| 1747 | } |
| 1748 | |
| 1749 | void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list) |
| 1750 | { |
| 1751 | int busy; |
| 1752 | struct net *net; |
| 1753 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 1754 | /* |
| 1755 | * This makes sure all current packets have passed through |
| 1756 | * netfilter framework. Roll on, two-stage module |
| 1757 | * delete... |
| 1758 | */ |
| 1759 | synchronize_net(); |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1760 | i_see_dead_people: |
| 1761 | busy = 0; |
| 1762 | list_for_each_entry(net, net_exit_list, exit_list) { |
Florian Westphal | c655bc6 | 2013-07-29 15:41:55 +0200 | [diff] [blame] | 1763 | nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0); |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1764 | if (atomic_read(&net->ct.count) != 0) |
| 1765 | busy = 1; |
| 1766 | } |
| 1767 | if (busy) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1768 | schedule(); |
| 1769 | goto i_see_dead_people; |
| 1770 | } |
| 1771 | |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1772 | list_for_each_entry(net, net_exit_list, exit_list) { |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1773 | nf_conntrack_proto_pernet_fini(net); |
| 1774 | nf_conntrack_helper_pernet_fini(net); |
| 1775 | nf_conntrack_ecache_pernet_fini(net); |
| 1776 | nf_conntrack_tstamp_pernet_fini(net); |
| 1777 | nf_conntrack_acct_pernet_fini(net); |
| 1778 | nf_conntrack_expect_pernet_fini(net); |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1779 | free_percpu(net->ct.stat); |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 1780 | free_percpu(net->ct.pcpu_lists); |
Vladimir Davydov | dece40e | 2013-03-13 23:40:14 +0000 | [diff] [blame] | 1781 | } |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Patrick McHardy | d862a66 | 2011-01-14 15:45:56 +0100 | [diff] [blame] | 1784 | void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1785 | { |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1786 | struct hlist_nulls_head *hash; |
| 1787 | unsigned int nr_slots, i; |
| 1788 | size_t sz; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1789 | |
Florian Westphal | 9cc1c73 | 2016-04-24 01:18:21 +0200 | [diff] [blame] | 1790 | if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head))) |
| 1791 | return NULL; |
| 1792 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1793 | BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head)); |
| 1794 | nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head)); |
Florian Westphal | 9cc1c73 | 2016-04-24 01:18:21 +0200 | [diff] [blame] | 1795 | |
| 1796 | if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head))) |
| 1797 | return NULL; |
| 1798 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1799 | sz = nr_slots * sizeof(struct hlist_nulls_head); |
| 1800 | hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, |
| 1801 | get_order(sz)); |
Pablo Neira Ayuso | f0ad462 | 2015-07-23 13:06:10 +0200 | [diff] [blame] | 1802 | if (!hash) |
Eric Dumazet | 966567b | 2011-12-19 16:01:38 -0500 | [diff] [blame] | 1803 | hash = vzalloc(sz); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1804 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1805 | if (hash && nulls) |
| 1806 | for (i = 0; i < nr_slots; i++) |
| 1807 | INIT_HLIST_NULLS_HEAD(&hash[i], i); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1808 | |
| 1809 | return hash; |
| 1810 | } |
Patrick McHardy | ac565e5 | 2007-07-07 22:30:08 -0700 | [diff] [blame] | 1811 | EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1812 | |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1813 | int nf_conntrack_hash_resize(unsigned int hashsize) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1814 | { |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1815 | int i, bucket; |
| 1816 | unsigned int old_size; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1817 | struct hlist_nulls_head *hash, *old_hash; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1818 | struct nf_conntrack_tuple_hash *h; |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 1819 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1820 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1821 | if (!hashsize) |
| 1822 | return -EINVAL; |
| 1823 | |
Patrick McHardy | d862a66 | 2011-01-14 15:45:56 +0100 | [diff] [blame] | 1824 | hash = nf_ct_alloc_hashtable(&hashsize, 1); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1825 | if (!hash) |
| 1826 | return -ENOMEM; |
| 1827 | |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1828 | old_size = nf_conntrack_htable_size; |
| 1829 | if (old_size == hashsize) { |
| 1830 | nf_ct_free_hashtable(hash, hashsize); |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1834 | local_bh_disable(); |
| 1835 | nf_conntrack_all_lock(); |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 1836 | write_seqcount_begin(&nf_conntrack_generation); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1837 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 1838 | /* Lookups in the old hash might happen in parallel, which means we |
| 1839 | * might get false negatives during connection lookup. New connections |
| 1840 | * created because of a false negative won't make it into the hash |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1841 | * though since that required taking the locks. |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 1842 | */ |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1843 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1844 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
| 1845 | while (!hlist_nulls_empty(&nf_conntrack_hash[i])) { |
| 1846 | h = hlist_nulls_entry(nf_conntrack_hash[i].first, |
| 1847 | struct nf_conntrack_tuple_hash, hnnode); |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 1848 | ct = nf_ct_tuplehash_to_ctrack(h); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1849 | hlist_nulls_del_rcu(&h->hnnode); |
Florian Westphal | 1b8c8a9 | 2016-05-03 00:25:58 +0200 | [diff] [blame] | 1850 | bucket = __hash_conntrack(nf_ct_net(ct), |
| 1851 | &h->tuple, hashsize); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1852 | hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1853 | } |
| 1854 | } |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1855 | old_size = nf_conntrack_htable_size; |
| 1856 | old_hash = nf_conntrack_hash; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1857 | |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1858 | nf_conntrack_hash = hash; |
| 1859 | nf_conntrack_htable_size = hashsize; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1860 | |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 1861 | write_seqcount_end(&nf_conntrack_generation); |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1862 | nf_conntrack_all_unlock(); |
| 1863 | local_bh_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1864 | |
Florian Westphal | 5e3c61f | 2016-04-28 19:13:41 +0200 | [diff] [blame] | 1865 | synchronize_net(); |
Patrick McHardy | d862a66 | 2011-01-14 15:45:56 +0100 | [diff] [blame] | 1866 | nf_ct_free_hashtable(old_hash, old_size); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1867 | return 0; |
| 1868 | } |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1869 | |
Kees Cook | 24da2c8 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1870 | int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp) |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1871 | { |
| 1872 | unsigned int hashsize; |
| 1873 | int rc; |
| 1874 | |
| 1875 | if (current->nsproxy->net_ns != &init_net) |
| 1876 | return -EOPNOTSUPP; |
| 1877 | |
| 1878 | /* On boot, we can set this without any fancy locking. */ |
Andrey Ryabinin | d8a77d1 | 2018-07-06 16:38:53 +0300 | [diff] [blame] | 1879 | if (!nf_conntrack_hash) |
Florian Westphal | 3183ab8 | 2016-06-22 13:26:10 +0200 | [diff] [blame] | 1880 | return param_set_uint(val, kp); |
| 1881 | |
| 1882 | rc = kstrtouint(val, 0, &hashsize); |
| 1883 | if (rc) |
| 1884 | return rc; |
| 1885 | |
| 1886 | return nf_conntrack_hash_resize(hashsize); |
| 1887 | } |
Patrick McHardy | fae718d | 2007-12-24 21:09:10 -0800 | [diff] [blame] | 1888 | EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1889 | |
Patrick McHardy | fae718d | 2007-12-24 21:09:10 -0800 | [diff] [blame] | 1890 | module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1891 | &nf_conntrack_htable_size, 0600); |
| 1892 | |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 1893 | void nf_ct_untracked_status_or(unsigned long bits) |
| 1894 | { |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 1895 | int cpu; |
| 1896 | |
| 1897 | for_each_possible_cpu(cpu) |
| 1898 | per_cpu(nf_conntrack_untracked, cpu).status |= bits; |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 1899 | } |
| 1900 | EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or); |
| 1901 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 1902 | int nf_conntrack_init_start(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1903 | { |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 1904 | int max_factor = 8; |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 1905 | int ret = -ENOMEM; |
| 1906 | int i, cpu; |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1907 | |
Florian Westphal | a3efd81 | 2016-04-18 16:16:59 +0200 | [diff] [blame] | 1908 | seqcount_init(&nf_conntrack_generation); |
| 1909 | |
Eric Dumazet | d5d2091 | 2014-03-17 13:37:53 -0700 | [diff] [blame] | 1910 | for (i = 0; i < CONNTRACK_LOCKS; i++) |
Jesper Dangaard Brouer | 93bb0ce | 2014-03-03 14:46:13 +0100 | [diff] [blame] | 1911 | spin_lock_init(&nf_conntrack_locks[i]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1912 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1913 | if (!nf_conntrack_htable_size) { |
Marcelo Leitner | 88eab47 | 2014-12-03 17:30:19 -0200 | [diff] [blame] | 1914 | /* Idea from tcp.c: use 1/16384 of memory. |
| 1915 | * On i386: 32MB machine has 512 buckets. |
| 1916 | * >= 1GB machines have 16384 buckets. |
| 1917 | * >= 4GB machines have 65536 buckets. |
| 1918 | */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1919 | nf_conntrack_htable_size |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 1920 | = (((totalram_pages << PAGE_SHIFT) / 16384) |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 1921 | / sizeof(struct hlist_head)); |
Marcelo Leitner | 88eab47 | 2014-12-03 17:30:19 -0200 | [diff] [blame] | 1922 | if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE))) |
| 1923 | nf_conntrack_htable_size = 65536; |
| 1924 | else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 1925 | nf_conntrack_htable_size = 16384; |
| 1926 | if (nf_conntrack_htable_size < 32) |
| 1927 | nf_conntrack_htable_size = 32; |
| 1928 | |
| 1929 | /* Use a max. factor of four by default to get the same max as |
| 1930 | * with the old struct list_heads. When a table size is given |
| 1931 | * we use the old value of 8 to avoid reducing the max. |
| 1932 | * entries. */ |
| 1933 | max_factor = 4; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1934 | } |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 1935 | |
| 1936 | nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1); |
| 1937 | if (!nf_conntrack_hash) |
| 1938 | return -ENOMEM; |
| 1939 | |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 1940 | nf_conntrack_max = max_factor * nf_conntrack_htable_size; |
Patrick McHardy | 8e5105a | 2007-07-07 22:27:33 -0700 | [diff] [blame] | 1941 | |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 1942 | nf_conntrack_cachep = kmem_cache_create("nf_conntrack", |
| 1943 | sizeof(struct nf_conn), 0, |
Florian Westphal | 5a75cde | 2016-06-10 22:25:22 +0200 | [diff] [blame] | 1944 | SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL); |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 1945 | if (!nf_conntrack_cachep) |
| 1946 | goto err_cachep; |
| 1947 | |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 1948 | printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n", |
Patrick McHardy | 8e5105a | 2007-07-07 22:27:33 -0700 | [diff] [blame] | 1949 | NF_CONNTRACK_VERSION, nf_conntrack_htable_size, |
| 1950 | nf_conntrack_max); |
Gao feng | 83b4dbe | 2013-01-21 22:10:25 +0000 | [diff] [blame] | 1951 | |
| 1952 | ret = nf_conntrack_expect_init(); |
| 1953 | if (ret < 0) |
| 1954 | goto err_expect; |
| 1955 | |
Gao feng | b7ff3a1 | 2013-01-21 22:10:26 +0000 | [diff] [blame] | 1956 | ret = nf_conntrack_acct_init(); |
| 1957 | if (ret < 0) |
| 1958 | goto err_acct; |
| 1959 | |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 1960 | ret = nf_conntrack_tstamp_init(); |
| 1961 | if (ret < 0) |
| 1962 | goto err_tstamp; |
| 1963 | |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 1964 | ret = nf_conntrack_ecache_init(); |
| 1965 | if (ret < 0) |
| 1966 | goto err_ecache; |
| 1967 | |
Gao feng | 8684094 | 2013-01-21 22:10:29 +0000 | [diff] [blame] | 1968 | ret = nf_conntrack_timeout_init(); |
| 1969 | if (ret < 0) |
| 1970 | goto err_timeout; |
| 1971 | |
Gao feng | 5e615b2 | 2013-01-21 22:10:30 +0000 | [diff] [blame] | 1972 | ret = nf_conntrack_helper_init(); |
| 1973 | if (ret < 0) |
| 1974 | goto err_helper; |
| 1975 | |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 1976 | ret = nf_conntrack_labels_init(); |
| 1977 | if (ret < 0) |
| 1978 | goto err_labels; |
| 1979 | |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 1980 | ret = nf_conntrack_seqadj_init(); |
| 1981 | if (ret < 0) |
| 1982 | goto err_seqadj; |
| 1983 | |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 1984 | ret = nf_conntrack_proto_init(); |
| 1985 | if (ret < 0) |
| 1986 | goto err_proto; |
| 1987 | |
Patrick McHardy | 9edd7ca | 2010-02-08 11:16:26 -0800 | [diff] [blame] | 1988 | /* Set up fake conntrack: to never be deleted, not in any hashes */ |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 1989 | for_each_possible_cpu(cpu) { |
| 1990 | struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu); |
Eric Dumazet | b3c5163 | 2010-06-09 14:43:38 +0200 | [diff] [blame] | 1991 | write_pnet(&ct->ct_net, &init_net); |
| 1992 | atomic_set(&ct->ct_general.use, 1); |
| 1993 | } |
Patrick McHardy | 9edd7ca | 2010-02-08 11:16:26 -0800 | [diff] [blame] | 1994 | /* - and look it like as a confirmed connection */ |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 1995 | nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED); |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1996 | |
| 1997 | conntrack_gc_work_init(&conntrack_gc_work); |
Vincent Guittot | 4406671 | 2017-11-02 16:16:07 +0100 | [diff] [blame] | 1998 | queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ); |
Florian Westphal | b87a2f9 | 2016-08-25 15:33:33 +0200 | [diff] [blame] | 1999 | |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2000 | return 0; |
| 2001 | |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 2002 | err_proto: |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 2003 | nf_conntrack_seqadj_fini(); |
| 2004 | err_seqadj: |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 2005 | nf_conntrack_labels_fini(); |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 2006 | err_labels: |
| 2007 | nf_conntrack_helper_fini(); |
Gao feng | 5e615b2 | 2013-01-21 22:10:30 +0000 | [diff] [blame] | 2008 | err_helper: |
| 2009 | nf_conntrack_timeout_fini(); |
Gao feng | 8684094 | 2013-01-21 22:10:29 +0000 | [diff] [blame] | 2010 | err_timeout: |
| 2011 | nf_conntrack_ecache_fini(); |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 2012 | err_ecache: |
| 2013 | nf_conntrack_tstamp_fini(); |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 2014 | err_tstamp: |
| 2015 | nf_conntrack_acct_fini(); |
Gao feng | b7ff3a1 | 2013-01-21 22:10:26 +0000 | [diff] [blame] | 2016 | err_acct: |
| 2017 | nf_conntrack_expect_fini(); |
Gao feng | 83b4dbe | 2013-01-21 22:10:25 +0000 | [diff] [blame] | 2018 | err_expect: |
Florian Westphal | 0c5366b | 2016-05-09 16:24:32 +0200 | [diff] [blame] | 2019 | kmem_cache_destroy(nf_conntrack_cachep); |
| 2020 | err_cachep: |
Florian Westphal | 56d52d4 | 2016-05-02 18:39:55 +0200 | [diff] [blame] | 2021 | nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2022 | return ret; |
| 2023 | } |
| 2024 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 2025 | void nf_conntrack_init_end(void) |
| 2026 | { |
| 2027 | /* For use by REJECT target */ |
| 2028 | RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach); |
| 2029 | RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack); |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Eric Dumazet | 8cc2019 | 2009-06-22 14:13:55 +0200 | [diff] [blame] | 2032 | /* |
| 2033 | * We need to use special "null" values, not used in hash table |
| 2034 | */ |
| 2035 | #define UNCONFIRMED_NULLS_VAL ((1<<30)+0) |
| 2036 | #define DYING_NULLS_VAL ((1<<30)+1) |
Pablo Neira Ayuso | 252b3e8 | 2012-12-11 04:07:42 +0000 | [diff] [blame] | 2037 | #define TEMPLATE_NULLS_VAL ((1<<30)+2) |
Eric Dumazet | 8cc2019 | 2009-06-22 14:13:55 +0200 | [diff] [blame] | 2038 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 2039 | int nf_conntrack_init_net(struct net *net) |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2040 | { |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 2041 | int ret = -ENOMEM; |
| 2042 | int cpu; |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2043 | |
| 2044 | atomic_set(&net->ct.count, 0); |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 2045 | |
| 2046 | net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu); |
| 2047 | if (!net->ct.pcpu_lists) |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2048 | goto err_stat; |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 2049 | |
| 2050 | for_each_possible_cpu(cpu) { |
| 2051 | struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu); |
| 2052 | |
| 2053 | spin_lock_init(&pcpu->lock); |
| 2054 | INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL); |
| 2055 | INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2056 | } |
Eric Dumazet | 5b3501f | 2010-02-08 11:16:56 -0800 | [diff] [blame] | 2057 | |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 2058 | net->ct.stat = alloc_percpu(struct ip_conntrack_stat); |
| 2059 | if (!net->ct.stat) |
| 2060 | goto err_pcpu_lists; |
| 2061 | |
Gao feng | 83b4dbe | 2013-01-21 22:10:25 +0000 | [diff] [blame] | 2062 | ret = nf_conntrack_expect_pernet_init(net); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2063 | if (ret < 0) |
| 2064 | goto err_expect; |
Gao feng | b7ff3a1 | 2013-01-21 22:10:26 +0000 | [diff] [blame] | 2065 | ret = nf_conntrack_acct_pernet_init(net); |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 2066 | if (ret < 0) |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2067 | goto err_acct; |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 2068 | ret = nf_conntrack_tstamp_pernet_init(net); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 2069 | if (ret < 0) |
| 2070 | goto err_tstamp; |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 2071 | ret = nf_conntrack_ecache_pernet_init(net); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 2072 | if (ret < 0) |
| 2073 | goto err_ecache; |
Gao feng | 5e615b2 | 2013-01-21 22:10:30 +0000 | [diff] [blame] | 2074 | ret = nf_conntrack_helper_pernet_init(net); |
Eric Leblond | a900689 | 2012-04-18 11:20:41 +0200 | [diff] [blame] | 2075 | if (ret < 0) |
| 2076 | goto err_helper; |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 2077 | ret = nf_conntrack_proto_pernet_init(net); |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 2078 | if (ret < 0) |
| 2079 | goto err_proto; |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2080 | return 0; |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 2081 | |
Gao feng | f94161c | 2013-01-21 22:10:24 +0000 | [diff] [blame] | 2082 | err_proto: |
Gao feng | 5e615b2 | 2013-01-21 22:10:30 +0000 | [diff] [blame] | 2083 | nf_conntrack_helper_pernet_fini(net); |
Eric Leblond | a900689 | 2012-04-18 11:20:41 +0200 | [diff] [blame] | 2084 | err_helper: |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 2085 | nf_conntrack_ecache_pernet_fini(net); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 2086 | err_ecache: |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 2087 | nf_conntrack_tstamp_pernet_fini(net); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 2088 | err_tstamp: |
Gao feng | b7ff3a1 | 2013-01-21 22:10:26 +0000 | [diff] [blame] | 2089 | nf_conntrack_acct_pernet_fini(net); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2090 | err_acct: |
Gao feng | 83b4dbe | 2013-01-21 22:10:25 +0000 | [diff] [blame] | 2091 | nf_conntrack_expect_pernet_fini(net); |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2092 | err_expect: |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 2093 | free_percpu(net->ct.stat); |
Jesper Dangaard Brouer | b7779d0 | 2014-03-03 14:45:20 +0100 | [diff] [blame] | 2094 | err_pcpu_lists: |
| 2095 | free_percpu(net->ct.pcpu_lists); |
Alexey Dobriyan | 0d55af8 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 2096 | err_stat: |
Alexey Dobriyan | 08f6547 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 2097 | return ret; |
| 2098 | } |