Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 1 | /* Expectation handling for nf_conntrack. */ |
| 2 | |
| 3 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 4 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 5 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/netfilter.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/proc_fs.h> |
| 16 | #include <linux/seq_file.h> |
| 17 | #include <linux/stddef.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/percpu.h> |
| 21 | #include <linux/kernel.h> |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 22 | #include <linux/jhash.h> |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 23 | |
| 24 | #include <net/netfilter/nf_conntrack.h> |
| 25 | #include <net/netfilter/nf_conntrack_core.h> |
| 26 | #include <net/netfilter/nf_conntrack_expect.h> |
| 27 | #include <net/netfilter/nf_conntrack_helper.h> |
| 28 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 29 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 30 | LIST_HEAD(nf_ct_expect_list); |
| 31 | EXPORT_SYMBOL_GPL(nf_ct_expect_list); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 32 | |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 33 | struct hlist_head *nf_ct_expect_hash __read_mostly; |
| 34 | EXPORT_SYMBOL_GPL(nf_ct_expect_hash); |
| 35 | |
| 36 | unsigned int nf_ct_expect_hsize __read_mostly; |
| 37 | EXPORT_SYMBOL_GPL(nf_ct_expect_hsize); |
| 38 | |
| 39 | static unsigned int nf_ct_expect_hash_rnd __read_mostly; |
| 40 | static unsigned int nf_ct_expect_count; |
| 41 | static int nf_ct_expect_hash_rnd_initted __read_mostly; |
| 42 | static int nf_ct_expect_vmalloc; |
| 43 | |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 44 | static struct kmem_cache *nf_ct_expect_cachep __read_mostly; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 45 | static unsigned int nf_ct_expect_next_id; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 46 | |
| 47 | /* nf_conntrack_expect helper functions */ |
| 48 | void nf_ct_unlink_expect(struct nf_conntrack_expect *exp) |
| 49 | { |
| 50 | struct nf_conn_help *master_help = nfct_help(exp->master); |
| 51 | |
| 52 | NF_CT_ASSERT(master_help); |
| 53 | NF_CT_ASSERT(!timer_pending(&exp->timeout)); |
| 54 | |
| 55 | list_del(&exp->list); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 56 | hlist_del(&exp->hnode); |
| 57 | nf_ct_expect_count--; |
| 58 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 59 | NF_CT_STAT_INC(expect_delete); |
| 60 | master_help->expecting--; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 61 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 62 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(nf_ct_unlink_expect); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 64 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 65 | static void nf_ct_expectation_timed_out(unsigned long ul_expect) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 66 | { |
| 67 | struct nf_conntrack_expect *exp = (void *)ul_expect; |
| 68 | |
| 69 | write_lock_bh(&nf_conntrack_lock); |
| 70 | nf_ct_unlink_expect(exp); |
| 71 | write_unlock_bh(&nf_conntrack_lock); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 72 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 75 | static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple) |
| 76 | { |
| 77 | if (unlikely(!nf_ct_expect_hash_rnd_initted)) { |
| 78 | get_random_bytes(&nf_ct_expect_hash_rnd, 4); |
| 79 | nf_ct_expect_hash_rnd_initted = 1; |
| 80 | } |
| 81 | |
| 82 | return jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all), |
| 83 | (((tuple->dst.protonum ^ tuple->src.l3num) << 16) | |
| 84 | tuple->dst.u.all) ^ nf_ct_expect_hash_rnd) % |
| 85 | nf_ct_expect_hsize; |
| 86 | } |
| 87 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 88 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 89 | __nf_ct_expect_find(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 90 | { |
| 91 | struct nf_conntrack_expect *i; |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 92 | struct hlist_node *n; |
| 93 | unsigned int h; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 94 | |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 95 | if (!nf_ct_expect_count) |
| 96 | return NULL; |
| 97 | |
| 98 | h = nf_ct_expect_dst_hash(tuple); |
| 99 | hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 100 | if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask)) |
| 101 | return i; |
| 102 | } |
| 103 | return NULL; |
| 104 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(__nf_ct_expect_find); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 106 | |
| 107 | /* Just find a expectation corresponding to a tuple. */ |
| 108 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 109 | nf_ct_expect_find_get(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 110 | { |
| 111 | struct nf_conntrack_expect *i; |
| 112 | |
| 113 | read_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 114 | i = __nf_ct_expect_find(tuple); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 115 | if (i) |
| 116 | atomic_inc(&i->use); |
| 117 | read_unlock_bh(&nf_conntrack_lock); |
| 118 | |
| 119 | return i; |
| 120 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(nf_ct_expect_find_get); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 122 | |
| 123 | /* If an expectation for this connection is found, it gets delete from |
| 124 | * global list then returned. */ |
| 125 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 126 | nf_ct_find_expectation(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 127 | { |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 128 | struct nf_conntrack_expect *exp; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 129 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 130 | exp = __nf_ct_expect_find(tuple); |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 131 | if (!exp) |
| 132 | return NULL; |
| 133 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 134 | /* If master is not in hash table yet (ie. packet hasn't left |
| 135 | this machine yet), how can other end know about expected? |
| 136 | Hence these are not the droids you are looking for (if |
| 137 | master ct never got confirmed, we'd hold a reference to it |
| 138 | and weird things would happen to future packets). */ |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 139 | if (!nf_ct_is_confirmed(exp->master)) |
| 140 | return NULL; |
| 141 | |
| 142 | if (exp->flags & NF_CT_EXPECT_PERMANENT) { |
| 143 | atomic_inc(&exp->use); |
| 144 | return exp; |
| 145 | } else if (del_timer(&exp->timeout)) { |
| 146 | nf_ct_unlink_expect(exp); |
| 147 | return exp; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 148 | } |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 149 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | /* delete all expectations for this conntrack */ |
| 154 | void nf_ct_remove_expectations(struct nf_conn *ct) |
| 155 | { |
| 156 | struct nf_conntrack_expect *i, *tmp; |
| 157 | struct nf_conn_help *help = nfct_help(ct); |
| 158 | |
| 159 | /* Optimization: most connection never expect any others. */ |
| 160 | if (!help || help->expecting == 0) |
| 161 | return; |
| 162 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 163 | list_for_each_entry_safe(i, tmp, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 164 | if (i->master == ct && del_timer(&i->timeout)) { |
| 165 | nf_ct_unlink_expect(i); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 166 | nf_ct_expect_put(i); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 167 | } |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(nf_ct_remove_expectations); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 171 | |
| 172 | /* Would two expected things clash? */ |
| 173 | static inline int expect_clash(const struct nf_conntrack_expect *a, |
| 174 | const struct nf_conntrack_expect *b) |
| 175 | { |
| 176 | /* Part covered by intersection of masks must be unequal, |
| 177 | otherwise they clash */ |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 178 | struct nf_conntrack_tuple_mask intersect_mask; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 179 | int count; |
| 180 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 181 | intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 182 | |
| 183 | for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){ |
| 184 | intersect_mask.src.u3.all[count] = |
| 185 | a->mask.src.u3.all[count] & b->mask.src.u3.all[count]; |
| 186 | } |
| 187 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 188 | return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask); |
| 189 | } |
| 190 | |
| 191 | static inline int expect_matches(const struct nf_conntrack_expect *a, |
| 192 | const struct nf_conntrack_expect *b) |
| 193 | { |
| 194 | return a->master == b->master |
| 195 | && nf_ct_tuple_equal(&a->tuple, &b->tuple) |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 196 | && nf_ct_tuple_mask_equal(&a->mask, &b->mask); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* Generally a bad idea to call this: could have matched already. */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 200 | void nf_ct_unexpect_related(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 201 | { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 202 | write_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | 4e1d4e6 | 2007-07-07 22:32:03 -0700 | [diff] [blame] | 203 | if (del_timer(&exp->timeout)) { |
| 204 | nf_ct_unlink_expect(exp); |
| 205 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 206 | } |
| 207 | write_unlock_bh(&nf_conntrack_lock); |
| 208 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(nf_ct_unexpect_related); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 210 | |
| 211 | /* We don't increase the master conntrack refcount for non-fulfilled |
| 212 | * conntracks. During the conntrack destruction, the expectations are |
| 213 | * always killed before the conntrack itself */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 214 | struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 215 | { |
| 216 | struct nf_conntrack_expect *new; |
| 217 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 218 | new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 219 | if (!new) |
| 220 | return NULL; |
| 221 | |
| 222 | new->master = me; |
| 223 | atomic_set(&new->use, 1); |
| 224 | return new; |
| 225 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(nf_ct_expect_alloc); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 227 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 228 | void nf_ct_expect_init(struct nf_conntrack_expect *exp, int family, |
| 229 | union nf_conntrack_address *saddr, |
| 230 | union nf_conntrack_address *daddr, |
| 231 | u_int8_t proto, __be16 *src, __be16 *dst) |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 232 | { |
| 233 | int len; |
| 234 | |
| 235 | if (family == AF_INET) |
| 236 | len = 4; |
| 237 | else |
| 238 | len = 16; |
| 239 | |
| 240 | exp->flags = 0; |
| 241 | exp->expectfn = NULL; |
| 242 | exp->helper = NULL; |
| 243 | exp->tuple.src.l3num = family; |
| 244 | exp->tuple.dst.protonum = proto; |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 245 | |
| 246 | if (saddr) { |
| 247 | memcpy(&exp->tuple.src.u3, saddr, len); |
| 248 | if (sizeof(exp->tuple.src.u3) > len) |
| 249 | /* address needs to be cleared for nf_ct_tuple_equal */ |
| 250 | memset((void *)&exp->tuple.src.u3 + len, 0x00, |
| 251 | sizeof(exp->tuple.src.u3) - len); |
| 252 | memset(&exp->mask.src.u3, 0xFF, len); |
| 253 | if (sizeof(exp->mask.src.u3) > len) |
| 254 | memset((void *)&exp->mask.src.u3 + len, 0x00, |
| 255 | sizeof(exp->mask.src.u3) - len); |
| 256 | } else { |
| 257 | memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3)); |
| 258 | memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3)); |
| 259 | } |
| 260 | |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 261 | if (src) { |
| 262 | exp->tuple.src.u.all = (__force u16)*src; |
| 263 | exp->mask.src.u.all = 0xFFFF; |
| 264 | } else { |
| 265 | exp->tuple.src.u.all = 0; |
| 266 | exp->mask.src.u.all = 0; |
| 267 | } |
| 268 | |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 269 | memcpy(&exp->tuple.dst.u3, daddr, len); |
| 270 | if (sizeof(exp->tuple.dst.u3) > len) |
| 271 | /* address needs to be cleared for nf_ct_tuple_equal */ |
| 272 | memset((void *)&exp->tuple.dst.u3 + len, 0x00, |
| 273 | sizeof(exp->tuple.dst.u3) - len); |
| 274 | |
| 275 | exp->tuple.dst.u.all = (__force u16)*dst; |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 276 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(nf_ct_expect_init); |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 278 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 279 | void nf_ct_expect_put(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 280 | { |
| 281 | if (atomic_dec_and_test(&exp->use)) |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 282 | kmem_cache_free(nf_ct_expect_cachep, exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 283 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(nf_ct_expect_put); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 285 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 286 | static void nf_ct_expect_insert(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 287 | { |
| 288 | struct nf_conn_help *master_help = nfct_help(exp->master); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 289 | unsigned int h = nf_ct_expect_dst_hash(&exp->tuple); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 290 | |
| 291 | atomic_inc(&exp->use); |
| 292 | master_help->expecting++; |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 293 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 294 | list_add(&exp->list, &nf_ct_expect_list); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 295 | hlist_add_head(&exp->hnode, &nf_ct_expect_hash[h]); |
| 296 | nf_ct_expect_count++; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 297 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 298 | setup_timer(&exp->timeout, nf_ct_expectation_timed_out, |
| 299 | (unsigned long)exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 300 | exp->timeout.expires = jiffies + master_help->helper->timeout * HZ; |
| 301 | add_timer(&exp->timeout); |
| 302 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 303 | exp->id = ++nf_ct_expect_next_id; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 304 | atomic_inc(&exp->use); |
| 305 | NF_CT_STAT_INC(expect_create); |
| 306 | } |
| 307 | |
| 308 | /* Race with expectations being used means we could have none to find; OK. */ |
| 309 | static void evict_oldest_expect(struct nf_conn *master) |
| 310 | { |
| 311 | struct nf_conntrack_expect *i; |
| 312 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 313 | list_for_each_entry_reverse(i, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 314 | if (i->master == master) { |
| 315 | if (del_timer(&i->timeout)) { |
| 316 | nf_ct_unlink_expect(i); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 317 | nf_ct_expect_put(i); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 318 | } |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static inline int refresh_timer(struct nf_conntrack_expect *i) |
| 325 | { |
| 326 | struct nf_conn_help *master_help = nfct_help(i->master); |
| 327 | |
| 328 | if (!del_timer(&i->timeout)) |
| 329 | return 0; |
| 330 | |
| 331 | i->timeout.expires = jiffies + master_help->helper->timeout*HZ; |
| 332 | add_timer(&i->timeout); |
| 333 | return 1; |
| 334 | } |
| 335 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 336 | int nf_ct_expect_related(struct nf_conntrack_expect *expect) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 337 | { |
| 338 | struct nf_conntrack_expect *i; |
| 339 | struct nf_conn *master = expect->master; |
| 340 | struct nf_conn_help *master_help = nfct_help(master); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 341 | struct hlist_node *n; |
| 342 | unsigned int h; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 343 | int ret; |
| 344 | |
| 345 | NF_CT_ASSERT(master_help); |
| 346 | |
| 347 | write_lock_bh(&nf_conntrack_lock); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 348 | if (!master_help->helper) { |
| 349 | ret = -ESHUTDOWN; |
| 350 | goto out; |
| 351 | } |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 352 | h = nf_ct_expect_dst_hash(&expect->tuple); |
| 353 | hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 354 | if (expect_matches(i, expect)) { |
| 355 | /* Refresh timer: if it's dying, ignore.. */ |
| 356 | if (refresh_timer(i)) { |
| 357 | ret = 0; |
| 358 | goto out; |
| 359 | } |
| 360 | } else if (expect_clash(i, expect)) { |
| 361 | ret = -EBUSY; |
| 362 | goto out; |
| 363 | } |
| 364 | } |
| 365 | /* Will be over limit? */ |
| 366 | if (master_help->helper->max_expected && |
| 367 | master_help->expecting >= master_help->helper->max_expected) |
| 368 | evict_oldest_expect(master); |
| 369 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 370 | nf_ct_expect_insert(expect); |
| 371 | nf_ct_expect_event(IPEXP_NEW, expect); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 372 | ret = 0; |
| 373 | out: |
| 374 | write_unlock_bh(&nf_conntrack_lock); |
| 375 | return ret; |
| 376 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 377 | EXPORT_SYMBOL_GPL(nf_ct_expect_related); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 378 | |
| 379 | #ifdef CONFIG_PROC_FS |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 380 | struct ct_expect_iter_state { |
| 381 | unsigned int bucket; |
| 382 | }; |
| 383 | |
| 384 | static struct hlist_node *ct_expect_get_first(struct seq_file *seq) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 385 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 386 | struct ct_expect_iter_state *st = seq->private; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 387 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 388 | for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) { |
| 389 | if (!hlist_empty(&nf_ct_expect_hash[st->bucket])) |
| 390 | return nf_ct_expect_hash[st->bucket].first; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 391 | } |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 392 | return NULL; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 395 | static struct hlist_node *ct_expect_get_next(struct seq_file *seq, |
| 396 | struct hlist_node *head) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 397 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 398 | struct ct_expect_iter_state *st = seq->private; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 399 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 400 | head = head->next; |
| 401 | while (head == NULL) { |
| 402 | if (++st->bucket >= nf_ct_expect_hsize) |
| 403 | return NULL; |
| 404 | head = nf_ct_expect_hash[st->bucket].first; |
| 405 | } |
| 406 | return head; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 409 | static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos) |
| 410 | { |
| 411 | struct hlist_node *head = ct_expect_get_first(seq); |
| 412 | |
| 413 | if (head) |
| 414 | while (pos && (head = ct_expect_get_next(seq, head))) |
| 415 | pos--; |
| 416 | return pos ? NULL : head; |
| 417 | } |
| 418 | |
| 419 | static void *exp_seq_start(struct seq_file *seq, loff_t *pos) |
| 420 | { |
| 421 | read_lock_bh(&nf_conntrack_lock); |
| 422 | return ct_expect_get_idx(seq, *pos); |
| 423 | } |
| 424 | |
| 425 | static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 426 | { |
| 427 | (*pos)++; |
| 428 | return ct_expect_get_next(seq, v); |
| 429 | } |
| 430 | |
| 431 | static void exp_seq_stop(struct seq_file *seq, void *v) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 432 | { |
| 433 | read_unlock_bh(&nf_conntrack_lock); |
| 434 | } |
| 435 | |
| 436 | static int exp_seq_show(struct seq_file *s, void *v) |
| 437 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 438 | struct nf_conntrack_expect *expect; |
| 439 | struct hlist_node *n = v; |
| 440 | |
| 441 | expect = hlist_entry(n, struct nf_conntrack_expect, hnode); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 442 | |
| 443 | if (expect->timeout.function) |
| 444 | seq_printf(s, "%ld ", timer_pending(&expect->timeout) |
| 445 | ? (long)(expect->timeout.expires - jiffies)/HZ : 0); |
| 446 | else |
| 447 | seq_printf(s, "- "); |
| 448 | seq_printf(s, "l3proto = %u proto=%u ", |
| 449 | expect->tuple.src.l3num, |
| 450 | expect->tuple.dst.protonum); |
| 451 | print_tuple(s, &expect->tuple, |
| 452 | __nf_ct_l3proto_find(expect->tuple.src.l3num), |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 453 | __nf_ct_l4proto_find(expect->tuple.src.l3num, |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 454 | expect->tuple.dst.protonum)); |
| 455 | return seq_putc(s, '\n'); |
| 456 | } |
| 457 | |
| 458 | static struct seq_operations exp_seq_ops = { |
| 459 | .start = exp_seq_start, |
| 460 | .next = exp_seq_next, |
| 461 | .stop = exp_seq_stop, |
| 462 | .show = exp_seq_show |
| 463 | }; |
| 464 | |
| 465 | static int exp_open(struct inode *inode, struct file *file) |
| 466 | { |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 467 | struct seq_file *seq; |
| 468 | struct ct_expect_iter_state *st; |
| 469 | int ret; |
| 470 | |
| 471 | st = kmalloc(sizeof(struct ct_expect_iter_state), GFP_KERNEL); |
| 472 | if (st == NULL) |
| 473 | return -ENOMEM; |
| 474 | ret = seq_open(file, &exp_seq_ops); |
| 475 | if (ret) |
| 476 | goto out_free; |
| 477 | seq = file->private_data; |
| 478 | seq->private = st; |
| 479 | memset(st, 0, sizeof(struct ct_expect_iter_state)); |
| 480 | return ret; |
| 481 | out_free: |
| 482 | kfree(st); |
| 483 | return ret; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 486 | static const struct file_operations exp_file_ops = { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 487 | .owner = THIS_MODULE, |
| 488 | .open = exp_open, |
| 489 | .read = seq_read, |
| 490 | .llseek = seq_lseek, |
Patrick McHardy | 5d08ad4 | 2007-07-07 22:34:07 -0700 | [diff] [blame^] | 491 | .release = seq_release_private, |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 492 | }; |
| 493 | #endif /* CONFIG_PROC_FS */ |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 494 | |
| 495 | static int __init exp_proc_init(void) |
| 496 | { |
| 497 | #ifdef CONFIG_PROC_FS |
| 498 | struct proc_dir_entry *proc; |
| 499 | |
| 500 | proc = proc_net_fops_create("nf_conntrack_expect", 0440, &exp_file_ops); |
| 501 | if (!proc) |
| 502 | return -ENOMEM; |
| 503 | #endif /* CONFIG_PROC_FS */ |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static void exp_proc_remove(void) |
| 508 | { |
| 509 | #ifdef CONFIG_PROC_FS |
| 510 | proc_net_remove("nf_conntrack_expect"); |
| 511 | #endif /* CONFIG_PROC_FS */ |
| 512 | } |
| 513 | |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 514 | module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0600); |
| 515 | |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 516 | int __init nf_conntrack_expect_init(void) |
| 517 | { |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 518 | int err = -ENOMEM; |
| 519 | |
| 520 | if (!nf_ct_expect_hsize) { |
| 521 | nf_ct_expect_hsize = nf_conntrack_htable_size / 256; |
| 522 | if (!nf_ct_expect_hsize) |
| 523 | nf_ct_expect_hsize = 1; |
| 524 | } |
| 525 | |
| 526 | nf_ct_expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize, |
| 527 | &nf_ct_expect_vmalloc); |
| 528 | if (nf_ct_expect_hash == NULL) |
| 529 | goto err1; |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 530 | |
| 531 | nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect", |
| 532 | sizeof(struct nf_conntrack_expect), |
| 533 | 0, 0, NULL, NULL); |
| 534 | if (!nf_ct_expect_cachep) |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 535 | goto err2; |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 536 | |
| 537 | err = exp_proc_init(); |
| 538 | if (err < 0) |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 539 | goto err3; |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 540 | |
| 541 | return 0; |
| 542 | |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 543 | err3: |
| 544 | nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc, |
| 545 | nf_ct_expect_hsize); |
| 546 | err2: |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 547 | kmem_cache_destroy(nf_ct_expect_cachep); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 548 | err1: |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 549 | return err; |
| 550 | } |
| 551 | |
| 552 | void nf_conntrack_expect_fini(void) |
| 553 | { |
| 554 | exp_proc_remove(); |
| 555 | kmem_cache_destroy(nf_ct_expect_cachep); |
Patrick McHardy | a71c085 | 2007-07-07 22:33:47 -0700 | [diff] [blame] | 556 | nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc, |
| 557 | nf_ct_expect_hsize); |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame] | 558 | } |