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