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