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