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