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> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * 23 Apr 2001: Harald Welte <laforge@gnumonks.org> |
| 14 | * - new API and handling of conntrack/nat helpers |
| 15 | * - now capable of multiple expectations for one master |
| 16 | * 16 Jul 2002: Harald Welte <laforge@gnumonks.org> |
| 17 | * - add usage/reference counts to ip_conntrack_expect |
| 18 | * - export ip_conntrack[_expect]_{find_get,put} functions |
| 19 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 20 | * - generalize L3 protocol denendent part. |
| 21 | * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 22 | * - add support various size of conntrack structures. |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 23 | * 26 Jan 2006: Harald Welte <laforge@netfilter.org> |
| 24 | * - restructure nf_conn (introduce nf_conn_help) |
| 25 | * - redesign 'features' how they were originally intended |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 26 | * 26 Feb 2006: Pablo Neira Ayuso <pablo@eurodev.net> |
| 27 | * - add support for L3 protocol module load on demand. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 28 | * |
| 29 | * Derived from net/ipv4/netfilter/ip_conntrack_core.c |
| 30 | */ |
| 31 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 32 | #include <linux/types.h> |
| 33 | #include <linux/netfilter.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/skbuff.h> |
| 36 | #include <linux/proc_fs.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | #include <linux/stddef.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/random.h> |
| 41 | #include <linux/jhash.h> |
| 42 | #include <linux/err.h> |
| 43 | #include <linux/percpu.h> |
| 44 | #include <linux/moduleparam.h> |
| 45 | #include <linux/notifier.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/netdevice.h> |
| 48 | #include <linux/socket.h> |
| 49 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 50 | #include <net/netfilter/nf_conntrack.h> |
| 51 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 52 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 53 | #include <net/netfilter/nf_conntrack_expect.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 54 | #include <net/netfilter/nf_conntrack_helper.h> |
| 55 | #include <net/netfilter/nf_conntrack_core.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 56 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 57 | #define NF_CONNTRACK_VERSION "0.5.0" |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 58 | |
| 59 | #if 0 |
| 60 | #define DEBUGP printk |
| 61 | #else |
| 62 | #define DEBUGP(format, args...) |
| 63 | #endif |
| 64 | |
| 65 | DEFINE_RWLOCK(nf_conntrack_lock); |
| 66 | |
| 67 | /* nf_conntrack_standalone needs this */ |
| 68 | atomic_t nf_conntrack_count = ATOMIC_INIT(0); |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(nf_conntrack_count); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 70 | |
| 71 | void (*nf_conntrack_destroyed)(struct nf_conn *conntrack) = NULL; |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 72 | unsigned int nf_conntrack_htable_size __read_mostly; |
Brian Haley | 94aec08 | 2006-09-18 00:05:22 -0700 | [diff] [blame] | 73 | int nf_conntrack_max __read_mostly; |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(nf_conntrack_max); |
Brian Haley | 1192e40 | 2006-09-20 12:03:46 -0700 | [diff] [blame] | 75 | struct list_head *nf_conntrack_hash __read_mostly; |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 76 | struct nf_conn nf_conntrack_untracked __read_mostly; |
Brian Haley | 94aec08 | 2006-09-18 00:05:22 -0700 | [diff] [blame] | 77 | unsigned int nf_ct_log_invalid __read_mostly; |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 78 | LIST_HEAD(unconfirmed); |
Brian Haley | 1192e40 | 2006-09-20 12:03:46 -0700 | [diff] [blame] | 79 | static int nf_conntrack_vmalloc __read_mostly; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 80 | |
Pablo Neira Ayuso | 4e3882f | 2006-03-22 13:55:11 -0800 | [diff] [blame] | 81 | static unsigned int nf_conntrack_next_id; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 82 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 83 | DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat); |
| 84 | EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat); |
| 85 | |
| 86 | /* |
| 87 | * This scheme offers various size of "struct nf_conn" dependent on |
| 88 | * features(helper, nat, ...) |
| 89 | */ |
| 90 | |
| 91 | #define NF_CT_FEATURES_NAMELEN 256 |
| 92 | static struct { |
| 93 | /* name of slab cache. printed in /proc/slabinfo */ |
| 94 | char *name; |
| 95 | |
| 96 | /* size of slab cache */ |
| 97 | size_t size; |
| 98 | |
| 99 | /* slab cache pointer */ |
| 100 | kmem_cache_t *cachep; |
| 101 | |
| 102 | /* allocated slab cache + modules which uses this slab cache */ |
| 103 | int use; |
| 104 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | } nf_ct_cache[NF_CT_F_NUM]; |
| 106 | |
| 107 | /* protect members of nf_ct_cache except of "use" */ |
| 108 | DEFINE_RWLOCK(nf_ct_cache_lock); |
| 109 | |
| 110 | /* This avoids calling kmem_cache_create() with same name simultaneously */ |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 111 | static DEFINE_MUTEX(nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 112 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 113 | static int nf_conntrack_hash_rnd_initted; |
| 114 | static unsigned int nf_conntrack_hash_rnd; |
| 115 | |
| 116 | static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple, |
| 117 | unsigned int size, unsigned int rnd) |
| 118 | { |
| 119 | unsigned int a, b; |
| 120 | a = jhash((void *)tuple->src.u3.all, sizeof(tuple->src.u3.all), |
| 121 | ((tuple->src.l3num) << 16) | tuple->dst.protonum); |
| 122 | b = jhash((void *)tuple->dst.u3.all, sizeof(tuple->dst.u3.all), |
| 123 | (tuple->src.u.all << 16) | tuple->dst.u.all); |
| 124 | |
| 125 | return jhash_2words(a, b, rnd) % size; |
| 126 | } |
| 127 | |
| 128 | static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple) |
| 129 | { |
| 130 | return __hash_conntrack(tuple, nf_conntrack_htable_size, |
| 131 | nf_conntrack_hash_rnd); |
| 132 | } |
| 133 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 134 | int nf_conntrack_register_cache(u_int32_t features, const char *name, |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 135 | size_t size) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 136 | { |
| 137 | int ret = 0; |
| 138 | char *cache_name; |
| 139 | kmem_cache_t *cachep; |
| 140 | |
| 141 | DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n", |
| 142 | features, name, size); |
| 143 | |
| 144 | if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) { |
| 145 | DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n", |
| 146 | features); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 150 | mutex_lock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 151 | |
| 152 | write_lock_bh(&nf_ct_cache_lock); |
| 153 | /* e.g: multiple helpers are loaded */ |
| 154 | if (nf_ct_cache[features].use > 0) { |
| 155 | DEBUGP("nf_conntrack_register_cache: already resisterd.\n"); |
| 156 | if ((!strncmp(nf_ct_cache[features].name, name, |
| 157 | NF_CT_FEATURES_NAMELEN)) |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 158 | && nf_ct_cache[features].size == size) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 159 | DEBUGP("nf_conntrack_register_cache: reusing.\n"); |
| 160 | nf_ct_cache[features].use++; |
| 161 | ret = 0; |
| 162 | } else |
| 163 | ret = -EBUSY; |
| 164 | |
| 165 | write_unlock_bh(&nf_ct_cache_lock); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 166 | mutex_unlock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | write_unlock_bh(&nf_ct_cache_lock); |
| 170 | |
| 171 | /* |
| 172 | * The memory space for name of slab cache must be alive until |
| 173 | * cache is destroyed. |
| 174 | */ |
| 175 | cache_name = kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN, GFP_ATOMIC); |
| 176 | if (cache_name == NULL) { |
| 177 | DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n"); |
| 178 | ret = -ENOMEM; |
| 179 | goto out_up_mutex; |
| 180 | } |
| 181 | |
| 182 | if (strlcpy(cache_name, name, NF_CT_FEATURES_NAMELEN) |
| 183 | >= NF_CT_FEATURES_NAMELEN) { |
| 184 | printk("nf_conntrack_register_cache: name too long\n"); |
| 185 | ret = -EINVAL; |
| 186 | goto out_free_name; |
| 187 | } |
| 188 | |
| 189 | cachep = kmem_cache_create(cache_name, size, 0, 0, |
| 190 | NULL, NULL); |
| 191 | if (!cachep) { |
| 192 | printk("nf_conntrack_register_cache: Can't create slab cache " |
| 193 | "for the features = 0x%x\n", features); |
| 194 | ret = -ENOMEM; |
| 195 | goto out_free_name; |
| 196 | } |
| 197 | |
| 198 | write_lock_bh(&nf_ct_cache_lock); |
| 199 | nf_ct_cache[features].use = 1; |
| 200 | nf_ct_cache[features].size = size; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 201 | nf_ct_cache[features].cachep = cachep; |
| 202 | nf_ct_cache[features].name = cache_name; |
| 203 | write_unlock_bh(&nf_ct_cache_lock); |
| 204 | |
| 205 | goto out_up_mutex; |
| 206 | |
| 207 | out_free_name: |
| 208 | kfree(cache_name); |
| 209 | out_up_mutex: |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 210 | mutex_unlock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */ |
| 215 | void nf_conntrack_unregister_cache(u_int32_t features) |
| 216 | { |
| 217 | kmem_cache_t *cachep; |
| 218 | char *name; |
| 219 | |
| 220 | /* |
| 221 | * This assures that kmem_cache_create() isn't called before destroying |
| 222 | * slab cache. |
| 223 | */ |
| 224 | DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 225 | mutex_lock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 226 | |
| 227 | write_lock_bh(&nf_ct_cache_lock); |
| 228 | if (--nf_ct_cache[features].use > 0) { |
| 229 | write_unlock_bh(&nf_ct_cache_lock); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 230 | mutex_unlock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | cachep = nf_ct_cache[features].cachep; |
| 234 | name = nf_ct_cache[features].name; |
| 235 | nf_ct_cache[features].cachep = NULL; |
| 236 | nf_ct_cache[features].name = NULL; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 237 | nf_ct_cache[features].size = 0; |
| 238 | write_unlock_bh(&nf_ct_cache_lock); |
| 239 | |
| 240 | synchronize_net(); |
| 241 | |
| 242 | kmem_cache_destroy(cachep); |
| 243 | kfree(name); |
| 244 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 245 | mutex_unlock(&nf_ct_cache_mutex); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int |
| 249 | nf_ct_get_tuple(const struct sk_buff *skb, |
| 250 | unsigned int nhoff, |
| 251 | unsigned int dataoff, |
| 252 | u_int16_t l3num, |
| 253 | u_int8_t protonum, |
| 254 | struct nf_conntrack_tuple *tuple, |
| 255 | const struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 256 | const struct nf_conntrack_l4proto *l4proto) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 257 | { |
| 258 | NF_CT_TUPLE_U_BLANK(tuple); |
| 259 | |
| 260 | tuple->src.l3num = l3num; |
| 261 | if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0) |
| 262 | return 0; |
| 263 | |
| 264 | tuple->dst.protonum = protonum; |
| 265 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; |
| 266 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 267 | return l4proto->pkt_to_tuple(skb, dataoff, tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int |
| 271 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, |
| 272 | const struct nf_conntrack_tuple *orig, |
| 273 | const struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 274 | const struct nf_conntrack_l4proto *l4proto) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 275 | { |
| 276 | NF_CT_TUPLE_U_BLANK(inverse); |
| 277 | |
| 278 | inverse->src.l3num = orig->src.l3num; |
| 279 | if (l3proto->invert_tuple(inverse, orig) == 0) |
| 280 | return 0; |
| 281 | |
| 282 | inverse->dst.dir = !orig->dst.dir; |
| 283 | |
| 284 | inverse->dst.protonum = orig->dst.protonum; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 285 | return l4proto->invert_tuple(inverse, orig); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 288 | static void |
| 289 | clean_from_lists(struct nf_conn *ct) |
| 290 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 291 | DEBUGP("clean_from_lists(%p)\n", ct); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 292 | list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list); |
| 293 | list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 294 | |
| 295 | /* Destroy all pending expectations */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 296 | nf_ct_remove_expectations(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void |
| 300 | destroy_conntrack(struct nf_conntrack *nfct) |
| 301 | { |
| 302 | struct nf_conn *ct = (struct nf_conn *)nfct; |
| 303 | struct nf_conntrack_l3proto *l3proto; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 304 | struct nf_conntrack_l4proto *l4proto; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 305 | |
| 306 | DEBUGP("destroy_conntrack(%p)\n", ct); |
| 307 | NF_CT_ASSERT(atomic_read(&nfct->use) == 0); |
| 308 | NF_CT_ASSERT(!timer_pending(&ct->timeout)); |
| 309 | |
| 310 | nf_conntrack_event(IPCT_DESTROY, ct); |
| 311 | set_bit(IPS_DYING_BIT, &ct->status); |
| 312 | |
| 313 | /* To make sure we don't get any weird locking issues here: |
| 314 | * destroy_conntrack() MUST NOT be called with a write lock |
| 315 | * to nf_conntrack_lock!!! -HW */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 316 | l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 317 | if (l3proto && l3proto->destroy) |
| 318 | l3proto->destroy(ct); |
| 319 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 320 | l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum); |
| 321 | if (l4proto && l4proto->destroy) |
| 322 | l4proto->destroy(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 323 | |
| 324 | if (nf_conntrack_destroyed) |
| 325 | nf_conntrack_destroyed(ct); |
| 326 | |
| 327 | write_lock_bh(&nf_conntrack_lock); |
| 328 | /* Expectations will have been removed in clean_from_lists, |
| 329 | * except TFTP can create an expectation on the first packet, |
| 330 | * before connection is in the list, so we need to clean here, |
| 331 | * too. */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 332 | nf_ct_remove_expectations(ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 333 | |
| 334 | /* We overload first tuple to link into unconfirmed list. */ |
| 335 | if (!nf_ct_is_confirmed(ct)) { |
| 336 | BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list)); |
| 337 | list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list); |
| 338 | } |
| 339 | |
| 340 | NF_CT_STAT_INC(delete); |
| 341 | write_unlock_bh(&nf_conntrack_lock); |
| 342 | |
| 343 | if (ct->master) |
| 344 | nf_ct_put(ct->master); |
| 345 | |
| 346 | DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct); |
| 347 | nf_conntrack_free(ct); |
| 348 | } |
| 349 | |
| 350 | static void death_by_timeout(unsigned long ul_conntrack) |
| 351 | { |
| 352 | struct nf_conn *ct = (void *)ul_conntrack; |
| 353 | |
| 354 | write_lock_bh(&nf_conntrack_lock); |
| 355 | /* Inside lock so preempt is disabled on module removal path. |
| 356 | * Otherwise we can get spurious warnings. */ |
| 357 | NF_CT_STAT_INC(delete_list); |
| 358 | clean_from_lists(ct); |
| 359 | write_unlock_bh(&nf_conntrack_lock); |
| 360 | nf_ct_put(ct); |
| 361 | } |
| 362 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 363 | struct nf_conntrack_tuple_hash * |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 364 | __nf_conntrack_find(const struct nf_conntrack_tuple *tuple, |
| 365 | const struct nf_conn *ignored_conntrack) |
| 366 | { |
| 367 | struct nf_conntrack_tuple_hash *h; |
| 368 | unsigned int hash = hash_conntrack(tuple); |
| 369 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 370 | list_for_each_entry(h, &nf_conntrack_hash[hash], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 371 | if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack && |
| 372 | nf_ct_tuple_equal(tuple, &h->tuple)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 373 | NF_CT_STAT_INC(found); |
| 374 | return h; |
| 375 | } |
| 376 | NF_CT_STAT_INC(searched); |
| 377 | } |
| 378 | |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | /* Find a connection corresponding to a tuple. */ |
| 383 | struct nf_conntrack_tuple_hash * |
| 384 | nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple, |
| 385 | const struct nf_conn *ignored_conntrack) |
| 386 | { |
| 387 | struct nf_conntrack_tuple_hash *h; |
| 388 | |
| 389 | read_lock_bh(&nf_conntrack_lock); |
| 390 | h = __nf_conntrack_find(tuple, ignored_conntrack); |
| 391 | if (h) |
| 392 | atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use); |
| 393 | read_unlock_bh(&nf_conntrack_lock); |
| 394 | |
| 395 | return h; |
| 396 | } |
| 397 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 398 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
| 399 | unsigned int hash, |
| 400 | unsigned int repl_hash) |
| 401 | { |
| 402 | ct->id = ++nf_conntrack_next_id; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 403 | list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list, |
| 404 | &nf_conntrack_hash[hash]); |
| 405 | list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list, |
| 406 | &nf_conntrack_hash[repl_hash]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void nf_conntrack_hash_insert(struct nf_conn *ct) |
| 410 | { |
| 411 | unsigned int hash, repl_hash; |
| 412 | |
| 413 | hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
| 414 | repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 415 | |
| 416 | write_lock_bh(&nf_conntrack_lock); |
| 417 | __nf_conntrack_hash_insert(ct, hash, repl_hash); |
| 418 | write_unlock_bh(&nf_conntrack_lock); |
| 419 | } |
| 420 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 421 | /* Confirm a connection given skb; places it in hash table */ |
| 422 | int |
| 423 | __nf_conntrack_confirm(struct sk_buff **pskb) |
| 424 | { |
| 425 | unsigned int hash, repl_hash; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 426 | struct nf_conntrack_tuple_hash *h; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 427 | struct nf_conn *ct; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 428 | struct nf_conn_help *help; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 429 | enum ip_conntrack_info ctinfo; |
| 430 | |
| 431 | ct = nf_ct_get(*pskb, &ctinfo); |
| 432 | |
| 433 | /* ipt_REJECT uses nf_conntrack_attach to attach related |
| 434 | ICMP/TCP RST packets in other direction. Actual packet |
| 435 | which created connection will be IP_CT_NEW or for an |
| 436 | expected connection, IP_CT_RELATED. */ |
| 437 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) |
| 438 | return NF_ACCEPT; |
| 439 | |
| 440 | hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
| 441 | repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 442 | |
| 443 | /* We're not in hash table, and we refuse to set up related |
| 444 | connections for unconfirmed conns. But packet copies and |
| 445 | REJECT will give spurious warnings here. */ |
| 446 | /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */ |
| 447 | |
| 448 | /* No external references means noone else could have |
| 449 | confirmed us. */ |
| 450 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
| 451 | DEBUGP("Confirming conntrack %p\n", ct); |
| 452 | |
| 453 | write_lock_bh(&nf_conntrack_lock); |
| 454 | |
| 455 | /* See if there's one in the list already, including reverse: |
| 456 | NAT could have grabbed it without realizing, since we're |
| 457 | not in the hash. If there is, we lost race. */ |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 458 | list_for_each_entry(h, &nf_conntrack_hash[hash], list) |
| 459 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 460 | &h->tuple)) |
| 461 | goto out; |
| 462 | list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list) |
| 463 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
| 464 | &h->tuple)) |
| 465 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 466 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 467 | /* Remove from unconfirmed list */ |
| 468 | list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list); |
| 469 | |
| 470 | __nf_conntrack_hash_insert(ct, hash, repl_hash); |
| 471 | /* Timer relative to confirmation time, not original |
| 472 | setting time, otherwise we'd get timer wrap in |
| 473 | weird delay cases. */ |
| 474 | ct->timeout.expires += jiffies; |
| 475 | add_timer(&ct->timeout); |
| 476 | atomic_inc(&ct->ct_general.use); |
| 477 | set_bit(IPS_CONFIRMED_BIT, &ct->status); |
| 478 | NF_CT_STAT_INC(insert); |
| 479 | write_unlock_bh(&nf_conntrack_lock); |
| 480 | help = nfct_help(ct); |
| 481 | if (help && help->helper) |
| 482 | nf_conntrack_event_cache(IPCT_HELPER, *pskb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 483 | #ifdef CONFIG_NF_NAT_NEEDED |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 484 | if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) || |
| 485 | test_bit(IPS_DST_NAT_DONE_BIT, &ct->status)) |
| 486 | nf_conntrack_event_cache(IPCT_NATINFO, *pskb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 487 | #endif |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 488 | nf_conntrack_event_cache(master_ct(ct) ? |
| 489 | IPCT_RELATED : IPCT_NEW, *pskb); |
| 490 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 491 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 492 | out: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 493 | NF_CT_STAT_INC(insert_failed); |
| 494 | write_unlock_bh(&nf_conntrack_lock); |
| 495 | return NF_DROP; |
| 496 | } |
| 497 | |
| 498 | /* Returns true if a connection correspondings to the tuple (required |
| 499 | for NAT). */ |
| 500 | int |
| 501 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, |
| 502 | const struct nf_conn *ignored_conntrack) |
| 503 | { |
| 504 | struct nf_conntrack_tuple_hash *h; |
| 505 | |
| 506 | read_lock_bh(&nf_conntrack_lock); |
| 507 | h = __nf_conntrack_find(tuple, ignored_conntrack); |
| 508 | read_unlock_bh(&nf_conntrack_lock); |
| 509 | |
| 510 | return h != NULL; |
| 511 | } |
| 512 | |
| 513 | /* There's a small race here where we may free a just-assured |
| 514 | connection. Too bad: we're in trouble anyway. */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 515 | static int early_drop(struct list_head *chain) |
| 516 | { |
| 517 | /* Traverse backwards: gives us oldest, which is roughly LRU */ |
| 518 | struct nf_conntrack_tuple_hash *h; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 519 | struct nf_conn *ct = NULL, *tmp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 520 | int dropped = 0; |
| 521 | |
| 522 | read_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 523 | list_for_each_entry_reverse(h, chain, list) { |
| 524 | tmp = nf_ct_tuplehash_to_ctrack(h); |
| 525 | if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) { |
| 526 | ct = tmp; |
| 527 | atomic_inc(&ct->ct_general.use); |
| 528 | break; |
| 529 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 530 | } |
| 531 | read_unlock_bh(&nf_conntrack_lock); |
| 532 | |
| 533 | if (!ct) |
| 534 | return dropped; |
| 535 | |
| 536 | if (del_timer(&ct->timeout)) { |
| 537 | death_by_timeout((unsigned long)ct); |
| 538 | dropped = 1; |
| 539 | NF_CT_STAT_INC(early_drop); |
| 540 | } |
| 541 | nf_ct_put(ct); |
| 542 | return dropped; |
| 543 | } |
| 544 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 545 | static struct nf_conn * |
| 546 | __nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
| 547 | const struct nf_conntrack_tuple *repl, |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 548 | const struct nf_conntrack_l3proto *l3proto, |
| 549 | u_int32_t features) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 550 | { |
| 551 | struct nf_conn *conntrack = NULL; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 552 | struct nf_conntrack_helper *helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 553 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 554 | if (unlikely(!nf_conntrack_hash_rnd_initted)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 555 | get_random_bytes(&nf_conntrack_hash_rnd, 4); |
| 556 | nf_conntrack_hash_rnd_initted = 1; |
| 557 | } |
| 558 | |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 559 | /* We don't want any race condition at early drop stage */ |
| 560 | atomic_inc(&nf_conntrack_count); |
| 561 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 562 | if (nf_conntrack_max |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 563 | && atomic_read(&nf_conntrack_count) > nf_conntrack_max) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 564 | unsigned int hash = hash_conntrack(orig); |
| 565 | /* Try dropping from this hash chain. */ |
| 566 | if (!early_drop(&nf_conntrack_hash[hash])) { |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 567 | atomic_dec(&nf_conntrack_count); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 568 | if (net_ratelimit()) |
| 569 | printk(KERN_WARNING |
| 570 | "nf_conntrack: table full, dropping" |
| 571 | " packet.\n"); |
| 572 | return ERR_PTR(-ENOMEM); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /* find features needed by this conntrack. */ |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 577 | features |= l3proto->get_features(orig); |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 578 | |
| 579 | /* FIXME: protect helper list per RCU */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 580 | read_lock_bh(&nf_conntrack_lock); |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 581 | helper = __nf_ct_helper_find(repl); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame^] | 582 | /* NAT might want to assign a helper later */ |
| 583 | if (helper || features & NF_CT_F_NAT) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 584 | features |= NF_CT_F_HELP; |
| 585 | read_unlock_bh(&nf_conntrack_lock); |
| 586 | |
| 587 | DEBUGP("nf_conntrack_alloc: features=0x%x\n", features); |
| 588 | |
| 589 | read_lock_bh(&nf_ct_cache_lock); |
| 590 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 591 | if (unlikely(!nf_ct_cache[features].use)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 592 | DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n", |
| 593 | features); |
| 594 | goto out; |
| 595 | } |
| 596 | |
| 597 | conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC); |
| 598 | if (conntrack == NULL) { |
| 599 | DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n"); |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | memset(conntrack, 0, nf_ct_cache[features].size); |
| 604 | conntrack->features = features; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 605 | atomic_set(&conntrack->ct_general.use, 1); |
| 606 | conntrack->ct_general.destroy = destroy_conntrack; |
| 607 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
| 608 | conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; |
| 609 | /* Don't set timer yet: wait for confirmation */ |
| 610 | init_timer(&conntrack->timeout); |
| 611 | conntrack->timeout.data = (unsigned long)conntrack; |
| 612 | conntrack->timeout.function = death_by_timeout; |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 613 | read_unlock_bh(&nf_ct_cache_lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 614 | |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 615 | return conntrack; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 616 | out: |
| 617 | read_unlock_bh(&nf_ct_cache_lock); |
Pablo Neira Ayuso | 5251e2d | 2006-09-20 12:01:06 -0700 | [diff] [blame] | 618 | atomic_dec(&nf_conntrack_count); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 619 | return conntrack; |
| 620 | } |
| 621 | |
| 622 | struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
| 623 | const struct nf_conntrack_tuple *repl) |
| 624 | { |
| 625 | struct nf_conntrack_l3proto *l3proto; |
| 626 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 627 | l3proto = __nf_ct_l3proto_find(orig->src.l3num); |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 628 | return __nf_conntrack_alloc(orig, repl, l3proto, 0); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void nf_conntrack_free(struct nf_conn *conntrack) |
| 632 | { |
| 633 | u_int32_t features = conntrack->features; |
| 634 | NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM); |
| 635 | DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features, |
| 636 | conntrack); |
| 637 | kmem_cache_free(nf_ct_cache[features].cachep, conntrack); |
| 638 | atomic_dec(&nf_conntrack_count); |
| 639 | } |
| 640 | |
| 641 | /* Allocate a new conntrack: we return -ENOMEM if classification |
| 642 | failed due to stress. Otherwise it really is unclassifiable. */ |
| 643 | static struct nf_conntrack_tuple_hash * |
| 644 | init_conntrack(const struct nf_conntrack_tuple *tuple, |
| 645 | struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 646 | struct nf_conntrack_l4proto *l4proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 647 | struct sk_buff *skb, |
| 648 | unsigned int dataoff) |
| 649 | { |
| 650 | struct nf_conn *conntrack; |
| 651 | struct nf_conntrack_tuple repl_tuple; |
| 652 | struct nf_conntrack_expect *exp; |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 653 | u_int32_t features = 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 654 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 655 | if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 656 | DEBUGP("Can't invert tuple.\n"); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 660 | read_lock_bh(&nf_conntrack_lock); |
| 661 | exp = __nf_conntrack_expect_find(tuple); |
| 662 | if (exp && exp->helper) |
| 663 | features = NF_CT_F_HELP; |
| 664 | read_unlock_bh(&nf_conntrack_lock); |
| 665 | |
| 666 | conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 667 | if (conntrack == NULL || IS_ERR(conntrack)) { |
| 668 | DEBUGP("Can't allocate conntrack.\n"); |
| 669 | return (struct nf_conntrack_tuple_hash *)conntrack; |
| 670 | } |
| 671 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 672 | if (!l4proto->new(conntrack, skb, dataoff)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 673 | nf_conntrack_free(conntrack); |
| 674 | DEBUGP("init conntrack: can't track with proto module\n"); |
| 675 | return NULL; |
| 676 | } |
| 677 | |
| 678 | write_lock_bh(&nf_conntrack_lock); |
| 679 | exp = find_expectation(tuple); |
| 680 | |
| 681 | if (exp) { |
| 682 | DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n", |
| 683 | conntrack, exp); |
| 684 | /* Welcome, Mr. Bond. We've been expecting you... */ |
| 685 | __set_bit(IPS_EXPECTED_BIT, &conntrack->status); |
| 686 | conntrack->master = exp->master; |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 687 | if (exp->helper) |
| 688 | nfct_help(conntrack)->helper = exp->helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 689 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 690 | conntrack->mark = exp->master->mark; |
| 691 | #endif |
James Morris | 7c9728c | 2006-06-09 00:31:46 -0700 | [diff] [blame] | 692 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 693 | conntrack->secmark = exp->master->secmark; |
| 694 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 695 | nf_conntrack_get(&conntrack->master->ct_general); |
| 696 | NF_CT_STAT_INC(expect_new); |
Yasuyuki Kozakai | 22e7410 | 2006-11-27 10:25:59 -0800 | [diff] [blame] | 697 | } else { |
| 698 | struct nf_conn_help *help = nfct_help(conntrack); |
| 699 | |
| 700 | if (help) |
| 701 | help->helper = __nf_ct_helper_find(&repl_tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 702 | NF_CT_STAT_INC(new); |
Yasuyuki Kozakai | 22e7410 | 2006-11-27 10:25:59 -0800 | [diff] [blame] | 703 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 704 | |
| 705 | /* Overload tuple linked list to put us in unconfirmed list. */ |
| 706 | list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed); |
| 707 | |
| 708 | write_unlock_bh(&nf_conntrack_lock); |
| 709 | |
| 710 | if (exp) { |
| 711 | if (exp->expectfn) |
| 712 | exp->expectfn(conntrack, exp); |
| 713 | nf_conntrack_expect_put(exp); |
| 714 | } |
| 715 | |
| 716 | return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL]; |
| 717 | } |
| 718 | |
| 719 | /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ |
| 720 | static inline struct nf_conn * |
| 721 | resolve_normal_ct(struct sk_buff *skb, |
| 722 | unsigned int dataoff, |
| 723 | u_int16_t l3num, |
| 724 | u_int8_t protonum, |
| 725 | struct nf_conntrack_l3proto *l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 726 | struct nf_conntrack_l4proto *l4proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 727 | int *set_reply, |
| 728 | enum ip_conntrack_info *ctinfo) |
| 729 | { |
| 730 | struct nf_conntrack_tuple tuple; |
| 731 | struct nf_conntrack_tuple_hash *h; |
| 732 | struct nf_conn *ct; |
| 733 | |
| 734 | if (!nf_ct_get_tuple(skb, (unsigned int)(skb->nh.raw - skb->data), |
| 735 | dataoff, l3num, protonum, &tuple, l3proto, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 736 | l4proto)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 737 | DEBUGP("resolve_normal_ct: Can't get tuple\n"); |
| 738 | return NULL; |
| 739 | } |
| 740 | |
| 741 | /* look for tuple match */ |
| 742 | h = nf_conntrack_find_get(&tuple, NULL); |
| 743 | if (!h) { |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 744 | h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 745 | if (!h) |
| 746 | return NULL; |
| 747 | if (IS_ERR(h)) |
| 748 | return (void *)h; |
| 749 | } |
| 750 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 751 | |
| 752 | /* It exists; we have (non-exclusive) reference. */ |
| 753 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { |
| 754 | *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY; |
| 755 | /* Please set reply bit if this packet OK */ |
| 756 | *set_reply = 1; |
| 757 | } else { |
| 758 | /* Once we've had two way comms, always ESTABLISHED. */ |
| 759 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
| 760 | DEBUGP("nf_conntrack_in: normal packet for %p\n", ct); |
| 761 | *ctinfo = IP_CT_ESTABLISHED; |
| 762 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { |
| 763 | DEBUGP("nf_conntrack_in: related packet for %p\n", ct); |
| 764 | *ctinfo = IP_CT_RELATED; |
| 765 | } else { |
| 766 | DEBUGP("nf_conntrack_in: new packet for %p\n", ct); |
| 767 | *ctinfo = IP_CT_NEW; |
| 768 | } |
| 769 | *set_reply = 0; |
| 770 | } |
| 771 | skb->nfct = &ct->ct_general; |
| 772 | skb->nfctinfo = *ctinfo; |
| 773 | return ct; |
| 774 | } |
| 775 | |
| 776 | unsigned int |
| 777 | nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb) |
| 778 | { |
| 779 | struct nf_conn *ct; |
| 780 | enum ip_conntrack_info ctinfo; |
| 781 | struct nf_conntrack_l3proto *l3proto; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 782 | struct nf_conntrack_l4proto *l4proto; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 783 | unsigned int dataoff; |
| 784 | u_int8_t protonum; |
| 785 | int set_reply = 0; |
| 786 | int ret; |
| 787 | |
| 788 | /* Previously seen (loopback or untracked)? Ignore. */ |
| 789 | if ((*pskb)->nfct) { |
| 790 | NF_CT_STAT_INC(ignore); |
| 791 | return NF_ACCEPT; |
| 792 | } |
| 793 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 794 | l3proto = __nf_ct_l3proto_find((u_int16_t)pf); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 795 | if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) { |
| 796 | DEBUGP("not prepared to track yet or error occured\n"); |
| 797 | return -ret; |
| 798 | } |
| 799 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 800 | l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 801 | |
| 802 | /* It may be an special packet, error, unclean... |
| 803 | * inverse of the return code tells to the netfilter |
| 804 | * core what to do with the packet. */ |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 805 | if (l4proto->error != NULL && |
| 806 | (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 807 | NF_CT_STAT_INC(error); |
| 808 | NF_CT_STAT_INC(invalid); |
| 809 | return -ret; |
| 810 | } |
| 811 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 812 | ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 813 | &set_reply, &ctinfo); |
| 814 | if (!ct) { |
| 815 | /* Not valid part of a connection */ |
| 816 | NF_CT_STAT_INC(invalid); |
| 817 | return NF_ACCEPT; |
| 818 | } |
| 819 | |
| 820 | if (IS_ERR(ct)) { |
| 821 | /* Too stressed to deal. */ |
| 822 | NF_CT_STAT_INC(drop); |
| 823 | return NF_DROP; |
| 824 | } |
| 825 | |
| 826 | NF_CT_ASSERT((*pskb)->nfct); |
| 827 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 828 | ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 829 | if (ret < 0) { |
| 830 | /* Invalid: inverse of the return code tells |
| 831 | * the netfilter core what to do */ |
| 832 | DEBUGP("nf_conntrack_in: Can't track with proto module\n"); |
| 833 | nf_conntrack_put((*pskb)->nfct); |
| 834 | (*pskb)->nfct = NULL; |
| 835 | NF_CT_STAT_INC(invalid); |
| 836 | return -ret; |
| 837 | } |
| 838 | |
| 839 | if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) |
| 840 | nf_conntrack_event_cache(IPCT_STATUS, *pskb); |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, |
| 846 | const struct nf_conntrack_tuple *orig) |
| 847 | { |
| 848 | return nf_ct_invert_tuple(inverse, orig, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 849 | __nf_ct_l3proto_find(orig->src.l3num), |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 850 | __nf_ct_l4proto_find(orig->src.l3num, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 851 | orig->dst.protonum)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame^] | 854 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
| 855 | implicitly racy: see __nf_conntrack_confirm */ |
| 856 | void nf_conntrack_alter_reply(struct nf_conn *ct, |
| 857 | const struct nf_conntrack_tuple *newreply) |
| 858 | { |
| 859 | struct nf_conn_help *help = nfct_help(ct); |
| 860 | |
| 861 | write_lock_bh(&nf_conntrack_lock); |
| 862 | /* Should be unconfirmed, so not in hash table yet */ |
| 863 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); |
| 864 | |
| 865 | DEBUGP("Altering reply tuple of %p to ", ct); |
| 866 | NF_CT_DUMP_TUPLE(newreply); |
| 867 | |
| 868 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; |
| 869 | if (!ct->master && help && help->expecting == 0) |
| 870 | help->helper = __nf_ct_helper_find(newreply); |
| 871 | write_unlock_bh(&nf_conntrack_lock); |
| 872 | } |
| 873 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 874 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
| 875 | void __nf_ct_refresh_acct(struct nf_conn *ct, |
| 876 | enum ip_conntrack_info ctinfo, |
| 877 | const struct sk_buff *skb, |
| 878 | unsigned long extra_jiffies, |
| 879 | int do_acct) |
| 880 | { |
| 881 | int event = 0; |
| 882 | |
| 883 | NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct); |
| 884 | NF_CT_ASSERT(skb); |
| 885 | |
| 886 | write_lock_bh(&nf_conntrack_lock); |
| 887 | |
Eric Leblond | 997ae83 | 2006-05-29 18:24:20 -0700 | [diff] [blame] | 888 | /* Only update if this is not a fixed timeout */ |
| 889 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) { |
| 890 | write_unlock_bh(&nf_conntrack_lock); |
| 891 | return; |
| 892 | } |
| 893 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 894 | /* If not in hash table, timer will not be active yet */ |
| 895 | if (!nf_ct_is_confirmed(ct)) { |
| 896 | ct->timeout.expires = extra_jiffies; |
| 897 | event = IPCT_REFRESH; |
| 898 | } else { |
Martin Josefsson | be00c8e | 2006-11-29 02:35:12 +0100 | [diff] [blame] | 899 | unsigned long newtime = jiffies + extra_jiffies; |
| 900 | |
| 901 | /* Only update the timeout if the new timeout is at least |
| 902 | HZ jiffies from the old timeout. Need del_timer for race |
| 903 | avoidance (may already be dying). */ |
| 904 | if (newtime - ct->timeout.expires >= HZ |
| 905 | && del_timer(&ct->timeout)) { |
| 906 | ct->timeout.expires = newtime; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 907 | add_timer(&ct->timeout); |
| 908 | event = IPCT_REFRESH; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | #ifdef CONFIG_NF_CT_ACCT |
| 913 | if (do_acct) { |
| 914 | ct->counters[CTINFO2DIR(ctinfo)].packets++; |
| 915 | ct->counters[CTINFO2DIR(ctinfo)].bytes += |
| 916 | skb->len - (unsigned int)(skb->nh.raw - skb->data); |
Martin Josefsson | 3ffd5ee | 2006-11-29 02:35:10 +0100 | [diff] [blame] | 917 | |
| 918 | if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000) |
| 919 | || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000)) |
| 920 | event |= IPCT_COUNTER_FILLING; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 921 | } |
| 922 | #endif |
| 923 | |
| 924 | write_unlock_bh(&nf_conntrack_lock); |
| 925 | |
| 926 | /* must be unlocked when calling event cache */ |
| 927 | if (event) |
| 928 | nf_conntrack_event_cache(event, skb); |
| 929 | } |
| 930 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 931 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 932 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 933 | |
| 934 | #include <linux/netfilter/nfnetlink.h> |
| 935 | #include <linux/netfilter/nfnetlink_conntrack.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 936 | #include <linux/mutex.h> |
| 937 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 938 | |
| 939 | /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be |
| 940 | * in ip_conntrack_core, since we don't want the protocols to autoload |
| 941 | * or depend on ctnetlink */ |
| 942 | int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb, |
| 943 | const struct nf_conntrack_tuple *tuple) |
| 944 | { |
| 945 | NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t), |
| 946 | &tuple->src.u.tcp.port); |
| 947 | NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t), |
| 948 | &tuple->dst.u.tcp.port); |
| 949 | return 0; |
| 950 | |
| 951 | nfattr_failure: |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { |
| 956 | [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t), |
| 957 | [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t) |
| 958 | }; |
| 959 | |
| 960 | int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[], |
| 961 | struct nf_conntrack_tuple *t) |
| 962 | { |
| 963 | if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1]) |
| 964 | return -EINVAL; |
| 965 | |
| 966 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) |
| 967 | return -EINVAL; |
| 968 | |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 969 | t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]); |
| 970 | t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | #endif |
| 975 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 976 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
| 977 | void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb) |
| 978 | { |
| 979 | struct nf_conn *ct; |
| 980 | enum ip_conntrack_info ctinfo; |
| 981 | |
| 982 | /* This ICMP is in reverse direction to the packet which caused it */ |
| 983 | ct = nf_ct_get(skb, &ctinfo); |
| 984 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) |
| 985 | ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY; |
| 986 | else |
| 987 | ctinfo = IP_CT_RELATED; |
| 988 | |
| 989 | /* Attach to new skbuff, and increment count */ |
| 990 | nskb->nfct = &ct->ct_general; |
| 991 | nskb->nfctinfo = ctinfo; |
| 992 | nf_conntrack_get(nskb->nfct); |
| 993 | } |
| 994 | |
| 995 | static inline int |
| 996 | do_iter(const struct nf_conntrack_tuple_hash *i, |
| 997 | int (*iter)(struct nf_conn *i, void *data), |
| 998 | void *data) |
| 999 | { |
| 1000 | return iter(nf_ct_tuplehash_to_ctrack(i), data); |
| 1001 | } |
| 1002 | |
| 1003 | /* Bring out ya dead! */ |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1004 | static struct nf_conn * |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1005 | get_next_corpse(int (*iter)(struct nf_conn *i, void *data), |
| 1006 | void *data, unsigned int *bucket) |
| 1007 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1008 | struct nf_conntrack_tuple_hash *h; |
| 1009 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1010 | |
| 1011 | write_lock_bh(&nf_conntrack_lock); |
| 1012 | for (; *bucket < nf_conntrack_htable_size; (*bucket)++) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1013 | list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) { |
| 1014 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 1015 | if (iter(ct, data)) |
| 1016 | goto found; |
| 1017 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1018 | } |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1019 | list_for_each_entry(h, &unconfirmed, list) { |
| 1020 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 1021 | if (iter(ct, data)) |
| 1022 | goto found; |
| 1023 | } |
Martin Josefsson | c073e3f | 2006-10-30 15:13:58 -0800 | [diff] [blame] | 1024 | write_unlock_bh(&nf_conntrack_lock); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1025 | return NULL; |
| 1026 | found: |
Martin Josefsson | c073e3f | 2006-10-30 15:13:58 -0800 | [diff] [blame] | 1027 | atomic_inc(&ct->ct_general.use); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1028 | write_unlock_bh(&nf_conntrack_lock); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1029 | return ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | void |
| 1033 | nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data) |
| 1034 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1035 | struct nf_conn *ct; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1036 | unsigned int bucket = 0; |
| 1037 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1038 | while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1039 | /* Time to push up daises... */ |
| 1040 | if (del_timer(&ct->timeout)) |
| 1041 | death_by_timeout((unsigned long)ct); |
| 1042 | /* ... else the timer will get him soon. */ |
| 1043 | |
| 1044 | nf_ct_put(ct); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static int kill_all(struct nf_conn *i, void *data) |
| 1049 | { |
| 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size) |
| 1054 | { |
| 1055 | if (vmalloced) |
| 1056 | vfree(hash); |
| 1057 | else |
| 1058 | free_pages((unsigned long)hash, |
| 1059 | get_order(sizeof(struct list_head) * size)); |
| 1060 | } |
| 1061 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1062 | void nf_conntrack_flush() |
| 1063 | { |
| 1064 | nf_ct_iterate_cleanup(kill_all, NULL); |
| 1065 | } |
| 1066 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1067 | /* Mishearing the voices in his head, our hero wonders how he's |
| 1068 | supposed to kill the mall. */ |
| 1069 | void nf_conntrack_cleanup(void) |
| 1070 | { |
| 1071 | int i; |
| 1072 | |
Yasuyuki Kozakai | 7d3cdc6 | 2006-02-15 15:22:21 -0800 | [diff] [blame] | 1073 | ip_ct_attach = NULL; |
| 1074 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1075 | /* This makes sure all current packets have passed through |
| 1076 | netfilter framework. Roll on, two-stage module |
| 1077 | delete... */ |
| 1078 | synchronize_net(); |
| 1079 | |
| 1080 | nf_ct_event_cache_flush(); |
| 1081 | i_see_dead_people: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1082 | nf_conntrack_flush(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1083 | if (atomic_read(&nf_conntrack_count) != 0) { |
| 1084 | schedule(); |
| 1085 | goto i_see_dead_people; |
| 1086 | } |
Patrick McHardy | 6636568 | 2005-12-05 13:36:50 -0800 | [diff] [blame] | 1087 | /* wait until all references to nf_conntrack_untracked are dropped */ |
| 1088 | while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1) |
| 1089 | schedule(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1090 | |
| 1091 | for (i = 0; i < NF_CT_F_NUM; i++) { |
| 1092 | if (nf_ct_cache[i].use == 0) |
| 1093 | continue; |
| 1094 | |
| 1095 | NF_CT_ASSERT(nf_ct_cache[i].use == 1); |
| 1096 | nf_ct_cache[i].use = 1; |
| 1097 | nf_conntrack_unregister_cache(i); |
| 1098 | } |
| 1099 | kmem_cache_destroy(nf_conntrack_expect_cachep); |
| 1100 | free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc, |
| 1101 | nf_conntrack_htable_size); |
KOVACS Krisztian | 5a6f294 | 2005-11-15 16:47:34 -0800 | [diff] [blame] | 1102 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 1103 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_generic); |
| 1104 | |
KOVACS Krisztian | 5a6f294 | 2005-11-15 16:47:34 -0800 | [diff] [blame] | 1105 | /* free l3proto protocol tables */ |
| 1106 | for (i = 0; i < PF_MAX; i++) |
| 1107 | if (nf_ct_protos[i]) { |
| 1108 | kfree(nf_ct_protos[i]); |
| 1109 | nf_ct_protos[i] = NULL; |
| 1110 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static struct list_head *alloc_hashtable(int size, int *vmalloced) |
| 1114 | { |
| 1115 | struct list_head *hash; |
| 1116 | unsigned int i; |
| 1117 | |
| 1118 | *vmalloced = 0; |
| 1119 | hash = (void*)__get_free_pages(GFP_KERNEL, |
| 1120 | get_order(sizeof(struct list_head) |
| 1121 | * size)); |
| 1122 | if (!hash) { |
| 1123 | *vmalloced = 1; |
| 1124 | printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n"); |
| 1125 | hash = vmalloc(sizeof(struct list_head) * size); |
| 1126 | } |
| 1127 | |
| 1128 | if (hash) |
| 1129 | for (i = 0; i < size; i++) |
| 1130 | INIT_LIST_HEAD(&hash[i]); |
| 1131 | |
| 1132 | return hash; |
| 1133 | } |
| 1134 | |
| 1135 | int set_hashsize(const char *val, struct kernel_param *kp) |
| 1136 | { |
| 1137 | int i, bucket, hashsize, vmalloced; |
| 1138 | int old_vmalloced, old_size; |
| 1139 | int rnd; |
| 1140 | struct list_head *hash, *old_hash; |
| 1141 | struct nf_conntrack_tuple_hash *h; |
| 1142 | |
| 1143 | /* On boot, we can set this without any fancy locking. */ |
| 1144 | if (!nf_conntrack_htable_size) |
| 1145 | return param_set_uint(val, kp); |
| 1146 | |
| 1147 | hashsize = simple_strtol(val, NULL, 0); |
| 1148 | if (!hashsize) |
| 1149 | return -EINVAL; |
| 1150 | |
| 1151 | hash = alloc_hashtable(hashsize, &vmalloced); |
| 1152 | if (!hash) |
| 1153 | return -ENOMEM; |
| 1154 | |
| 1155 | /* We have to rehahs for the new table anyway, so we also can |
| 1156 | * use a newrandom seed */ |
| 1157 | get_random_bytes(&rnd, 4); |
| 1158 | |
| 1159 | write_lock_bh(&nf_conntrack_lock); |
| 1160 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
| 1161 | while (!list_empty(&nf_conntrack_hash[i])) { |
| 1162 | h = list_entry(nf_conntrack_hash[i].next, |
| 1163 | struct nf_conntrack_tuple_hash, list); |
| 1164 | list_del(&h->list); |
| 1165 | bucket = __hash_conntrack(&h->tuple, hashsize, rnd); |
| 1166 | list_add_tail(&h->list, &hash[bucket]); |
| 1167 | } |
| 1168 | } |
| 1169 | old_size = nf_conntrack_htable_size; |
| 1170 | old_vmalloced = nf_conntrack_vmalloc; |
| 1171 | old_hash = nf_conntrack_hash; |
| 1172 | |
| 1173 | nf_conntrack_htable_size = hashsize; |
| 1174 | nf_conntrack_vmalloc = vmalloced; |
| 1175 | nf_conntrack_hash = hash; |
| 1176 | nf_conntrack_hash_rnd = rnd; |
| 1177 | write_unlock_bh(&nf_conntrack_lock); |
| 1178 | |
| 1179 | free_conntrack_hash(old_hash, old_vmalloced, old_size); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | module_param_call(hashsize, set_hashsize, param_get_uint, |
| 1184 | &nf_conntrack_htable_size, 0600); |
| 1185 | |
| 1186 | int __init nf_conntrack_init(void) |
| 1187 | { |
| 1188 | unsigned int i; |
| 1189 | int ret; |
| 1190 | |
| 1191 | /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB |
| 1192 | * machine has 256 buckets. >= 1GB machines have 8192 buckets. */ |
| 1193 | if (!nf_conntrack_htable_size) { |
| 1194 | nf_conntrack_htable_size |
| 1195 | = (((num_physpages << PAGE_SHIFT) / 16384) |
| 1196 | / sizeof(struct list_head)); |
| 1197 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
| 1198 | nf_conntrack_htable_size = 8192; |
| 1199 | if (nf_conntrack_htable_size < 16) |
| 1200 | nf_conntrack_htable_size = 16; |
| 1201 | } |
| 1202 | nf_conntrack_max = 8 * nf_conntrack_htable_size; |
| 1203 | |
| 1204 | printk("nf_conntrack version %s (%u buckets, %d max)\n", |
| 1205 | NF_CONNTRACK_VERSION, nf_conntrack_htable_size, |
| 1206 | nf_conntrack_max); |
| 1207 | |
| 1208 | nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size, |
| 1209 | &nf_conntrack_vmalloc); |
| 1210 | if (!nf_conntrack_hash) { |
| 1211 | printk(KERN_ERR "Unable to create nf_conntrack_hash\n"); |
| 1212 | goto err_out; |
| 1213 | } |
| 1214 | |
| 1215 | ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic", |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 1216 | sizeof(struct nf_conn)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1217 | if (ret < 0) { |
| 1218 | printk(KERN_ERR "Unable to create nf_conn slab cache\n"); |
| 1219 | goto err_free_hash; |
| 1220 | } |
| 1221 | |
| 1222 | nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect", |
| 1223 | sizeof(struct nf_conntrack_expect), |
| 1224 | 0, 0, NULL, NULL); |
| 1225 | if (!nf_conntrack_expect_cachep) { |
| 1226 | printk(KERN_ERR "Unable to create nf_expect slab cache\n"); |
| 1227 | goto err_free_conntrack_slab; |
| 1228 | } |
| 1229 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 1230 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_generic); |
| 1231 | if (ret < 0) |
| 1232 | goto out_free_expect_slab; |
| 1233 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1234 | /* Don't NEED lock here, but good form anyway. */ |
| 1235 | write_lock_bh(&nf_conntrack_lock); |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 1236 | for (i = 0; i < AF_MAX; i++) |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1237 | nf_ct_l3protos[i] = &nf_conntrack_l3proto_generic; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1238 | write_unlock_bh(&nf_conntrack_lock); |
| 1239 | |
Yasuyuki Kozakai | 7d3cdc6 | 2006-02-15 15:22:21 -0800 | [diff] [blame] | 1240 | /* For use by REJECT target */ |
| 1241 | ip_ct_attach = __nf_conntrack_attach; |
| 1242 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1243 | /* Set up fake conntrack: |
| 1244 | - to never be deleted, not in any hashes */ |
| 1245 | atomic_set(&nf_conntrack_untracked.ct_general.use, 1); |
| 1246 | /* - and look it like as a confirmed connection */ |
| 1247 | set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status); |
| 1248 | |
| 1249 | return ret; |
| 1250 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 1251 | out_free_expect_slab: |
| 1252 | kmem_cache_destroy(nf_conntrack_expect_cachep); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1253 | err_free_conntrack_slab: |
| 1254 | nf_conntrack_unregister_cache(NF_CT_F_BASIC); |
| 1255 | err_free_hash: |
| 1256 | free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc, |
| 1257 | nf_conntrack_htable_size); |
| 1258 | err_out: |
| 1259 | return -ENOMEM; |
| 1260 | } |