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