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> |
| 22 | |
| 23 | #include <net/netfilter/nf_conntrack.h> |
| 24 | #include <net/netfilter/nf_conntrack_core.h> |
| 25 | #include <net/netfilter/nf_conntrack_expect.h> |
| 26 | #include <net/netfilter/nf_conntrack_helper.h> |
| 27 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 28 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 29 | LIST_HEAD(nf_ct_expect_list); |
| 30 | EXPORT_SYMBOL_GPL(nf_ct_expect_list); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 31 | |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame^] | 32 | static struct kmem_cache *nf_ct_expect_cachep __read_mostly; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 33 | static unsigned int nf_ct_expect_next_id; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 34 | |
| 35 | /* nf_conntrack_expect helper functions */ |
| 36 | void nf_ct_unlink_expect(struct nf_conntrack_expect *exp) |
| 37 | { |
| 38 | struct nf_conn_help *master_help = nfct_help(exp->master); |
| 39 | |
| 40 | NF_CT_ASSERT(master_help); |
| 41 | NF_CT_ASSERT(!timer_pending(&exp->timeout)); |
| 42 | |
| 43 | list_del(&exp->list); |
| 44 | NF_CT_STAT_INC(expect_delete); |
| 45 | master_help->expecting--; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 46 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 47 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 48 | EXPORT_SYMBOL_GPL(nf_ct_unlink_expect); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 49 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 50 | static void nf_ct_expectation_timed_out(unsigned long ul_expect) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 51 | { |
| 52 | struct nf_conntrack_expect *exp = (void *)ul_expect; |
| 53 | |
| 54 | write_lock_bh(&nf_conntrack_lock); |
| 55 | nf_ct_unlink_expect(exp); |
| 56 | write_unlock_bh(&nf_conntrack_lock); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 57 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 61 | __nf_ct_expect_find(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 62 | { |
| 63 | struct nf_conntrack_expect *i; |
| 64 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 65 | list_for_each_entry(i, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 66 | if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask)) |
| 67 | return i; |
| 68 | } |
| 69 | return NULL; |
| 70 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL_GPL(__nf_ct_expect_find); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 72 | |
| 73 | /* Just find a expectation corresponding to a tuple. */ |
| 74 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 75 | nf_ct_expect_find_get(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 76 | { |
| 77 | struct nf_conntrack_expect *i; |
| 78 | |
| 79 | read_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 80 | i = __nf_ct_expect_find(tuple); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 81 | if (i) |
| 82 | atomic_inc(&i->use); |
| 83 | read_unlock_bh(&nf_conntrack_lock); |
| 84 | |
| 85 | return i; |
| 86 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(nf_ct_expect_find_get); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 88 | |
| 89 | /* If an expectation for this connection is found, it gets delete from |
| 90 | * global list then returned. */ |
| 91 | struct nf_conntrack_expect * |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 92 | nf_ct_find_expectation(const struct nf_conntrack_tuple *tuple) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 93 | { |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 94 | struct nf_conntrack_expect *exp; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 95 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 96 | exp = __nf_ct_expect_find(tuple); |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 97 | if (!exp) |
| 98 | return NULL; |
| 99 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 100 | /* If master is not in hash table yet (ie. packet hasn't left |
| 101 | this machine yet), how can other end know about expected? |
| 102 | Hence these are not the droids you are looking for (if |
| 103 | master ct never got confirmed, we'd hold a reference to it |
| 104 | and weird things would happen to future packets). */ |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 105 | if (!nf_ct_is_confirmed(exp->master)) |
| 106 | return NULL; |
| 107 | |
| 108 | if (exp->flags & NF_CT_EXPECT_PERMANENT) { |
| 109 | atomic_inc(&exp->use); |
| 110 | return exp; |
| 111 | } else if (del_timer(&exp->timeout)) { |
| 112 | nf_ct_unlink_expect(exp); |
| 113 | return exp; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 114 | } |
Yasuyuki Kozakai | ece0064 | 2006-12-05 13:44:57 -0800 | [diff] [blame] | 115 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /* delete all expectations for this conntrack */ |
| 120 | void nf_ct_remove_expectations(struct nf_conn *ct) |
| 121 | { |
| 122 | struct nf_conntrack_expect *i, *tmp; |
| 123 | struct nf_conn_help *help = nfct_help(ct); |
| 124 | |
| 125 | /* Optimization: most connection never expect any others. */ |
| 126 | if (!help || help->expecting == 0) |
| 127 | return; |
| 128 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 129 | list_for_each_entry_safe(i, tmp, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 130 | if (i->master == ct && del_timer(&i->timeout)) { |
| 131 | nf_ct_unlink_expect(i); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 132 | nf_ct_expect_put(i); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 133 | } |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(nf_ct_remove_expectations); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 137 | |
| 138 | /* Would two expected things clash? */ |
| 139 | static inline int expect_clash(const struct nf_conntrack_expect *a, |
| 140 | const struct nf_conntrack_expect *b) |
| 141 | { |
| 142 | /* Part covered by intersection of masks must be unequal, |
| 143 | otherwise they clash */ |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 144 | struct nf_conntrack_tuple_mask intersect_mask; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 145 | int count; |
| 146 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 147 | 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] | 148 | |
| 149 | for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){ |
| 150 | intersect_mask.src.u3.all[count] = |
| 151 | a->mask.src.u3.all[count] & b->mask.src.u3.all[count]; |
| 152 | } |
| 153 | |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 154 | return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask); |
| 155 | } |
| 156 | |
| 157 | static inline int expect_matches(const struct nf_conntrack_expect *a, |
| 158 | const struct nf_conntrack_expect *b) |
| 159 | { |
| 160 | return a->master == b->master |
| 161 | && nf_ct_tuple_equal(&a->tuple, &b->tuple) |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 162 | && nf_ct_tuple_mask_equal(&a->mask, &b->mask); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Generally a bad idea to call this: could have matched already. */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 166 | void nf_ct_unexpect_related(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 167 | { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 168 | write_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | 4e1d4e6 | 2007-07-07 22:32:03 -0700 | [diff] [blame] | 169 | if (del_timer(&exp->timeout)) { |
| 170 | nf_ct_unlink_expect(exp); |
| 171 | nf_ct_expect_put(exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 172 | } |
| 173 | write_unlock_bh(&nf_conntrack_lock); |
| 174 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 175 | EXPORT_SYMBOL_GPL(nf_ct_unexpect_related); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 176 | |
| 177 | /* We don't increase the master conntrack refcount for non-fulfilled |
| 178 | * conntracks. During the conntrack destruction, the expectations are |
| 179 | * always killed before the conntrack itself */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 180 | struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 181 | { |
| 182 | struct nf_conntrack_expect *new; |
| 183 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 184 | new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 185 | if (!new) |
| 186 | return NULL; |
| 187 | |
| 188 | new->master = me; |
| 189 | atomic_set(&new->use, 1); |
| 190 | return new; |
| 191 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 192 | EXPORT_SYMBOL_GPL(nf_ct_expect_alloc); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 193 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 194 | void nf_ct_expect_init(struct nf_conntrack_expect *exp, int family, |
| 195 | union nf_conntrack_address *saddr, |
| 196 | union nf_conntrack_address *daddr, |
| 197 | u_int8_t proto, __be16 *src, __be16 *dst) |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 198 | { |
| 199 | int len; |
| 200 | |
| 201 | if (family == AF_INET) |
| 202 | len = 4; |
| 203 | else |
| 204 | len = 16; |
| 205 | |
| 206 | exp->flags = 0; |
| 207 | exp->expectfn = NULL; |
| 208 | exp->helper = NULL; |
| 209 | exp->tuple.src.l3num = family; |
| 210 | exp->tuple.dst.protonum = proto; |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 211 | |
| 212 | if (saddr) { |
| 213 | memcpy(&exp->tuple.src.u3, saddr, len); |
| 214 | if (sizeof(exp->tuple.src.u3) > len) |
| 215 | /* address needs to be cleared for nf_ct_tuple_equal */ |
| 216 | memset((void *)&exp->tuple.src.u3 + len, 0x00, |
| 217 | sizeof(exp->tuple.src.u3) - len); |
| 218 | memset(&exp->mask.src.u3, 0xFF, len); |
| 219 | if (sizeof(exp->mask.src.u3) > len) |
| 220 | memset((void *)&exp->mask.src.u3 + len, 0x00, |
| 221 | sizeof(exp->mask.src.u3) - len); |
| 222 | } else { |
| 223 | memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3)); |
| 224 | memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3)); |
| 225 | } |
| 226 | |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 227 | if (src) { |
| 228 | exp->tuple.src.u.all = (__force u16)*src; |
| 229 | exp->mask.src.u.all = 0xFFFF; |
| 230 | } else { |
| 231 | exp->tuple.src.u.all = 0; |
| 232 | exp->mask.src.u.all = 0; |
| 233 | } |
| 234 | |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 235 | memcpy(&exp->tuple.dst.u3, daddr, len); |
| 236 | if (sizeof(exp->tuple.dst.u3) > len) |
| 237 | /* address needs to be cleared for nf_ct_tuple_equal */ |
| 238 | memset((void *)&exp->tuple.dst.u3 + len, 0x00, |
| 239 | sizeof(exp->tuple.dst.u3) - len); |
| 240 | |
| 241 | exp->tuple.dst.u.all = (__force u16)*dst; |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 242 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 243 | EXPORT_SYMBOL_GPL(nf_ct_expect_init); |
Patrick McHardy | d6a9b65 | 2006-12-02 22:08:01 -0800 | [diff] [blame] | 244 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 245 | void nf_ct_expect_put(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 246 | { |
| 247 | if (atomic_dec_and_test(&exp->use)) |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 248 | kmem_cache_free(nf_ct_expect_cachep, exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 249 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 250 | EXPORT_SYMBOL_GPL(nf_ct_expect_put); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 251 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 252 | static void nf_ct_expect_insert(struct nf_conntrack_expect *exp) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 253 | { |
| 254 | struct nf_conn_help *master_help = nfct_help(exp->master); |
| 255 | |
| 256 | atomic_inc(&exp->use); |
| 257 | master_help->expecting++; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 258 | list_add(&exp->list, &nf_ct_expect_list); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 259 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 260 | setup_timer(&exp->timeout, nf_ct_expectation_timed_out, |
| 261 | (unsigned long)exp); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 262 | exp->timeout.expires = jiffies + master_help->helper->timeout * HZ; |
| 263 | add_timer(&exp->timeout); |
| 264 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 265 | exp->id = ++nf_ct_expect_next_id; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 266 | atomic_inc(&exp->use); |
| 267 | NF_CT_STAT_INC(expect_create); |
| 268 | } |
| 269 | |
| 270 | /* Race with expectations being used means we could have none to find; OK. */ |
| 271 | static void evict_oldest_expect(struct nf_conn *master) |
| 272 | { |
| 273 | struct nf_conntrack_expect *i; |
| 274 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 275 | list_for_each_entry_reverse(i, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 276 | if (i->master == master) { |
| 277 | if (del_timer(&i->timeout)) { |
| 278 | nf_ct_unlink_expect(i); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 279 | nf_ct_expect_put(i); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 280 | } |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static inline int refresh_timer(struct nf_conntrack_expect *i) |
| 287 | { |
| 288 | struct nf_conn_help *master_help = nfct_help(i->master); |
| 289 | |
| 290 | if (!del_timer(&i->timeout)) |
| 291 | return 0; |
| 292 | |
| 293 | i->timeout.expires = jiffies + master_help->helper->timeout*HZ; |
| 294 | add_timer(&i->timeout); |
| 295 | return 1; |
| 296 | } |
| 297 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 298 | int nf_ct_expect_related(struct nf_conntrack_expect *expect) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 299 | { |
| 300 | struct nf_conntrack_expect *i; |
| 301 | struct nf_conn *master = expect->master; |
| 302 | struct nf_conn_help *master_help = nfct_help(master); |
| 303 | int ret; |
| 304 | |
| 305 | NF_CT_ASSERT(master_help); |
| 306 | |
| 307 | write_lock_bh(&nf_conntrack_lock); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 308 | if (!master_help->helper) { |
| 309 | ret = -ESHUTDOWN; |
| 310 | goto out; |
| 311 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 312 | list_for_each_entry(i, &nf_ct_expect_list, list) { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 313 | if (expect_matches(i, expect)) { |
| 314 | /* Refresh timer: if it's dying, ignore.. */ |
| 315 | if (refresh_timer(i)) { |
| 316 | ret = 0; |
| 317 | goto out; |
| 318 | } |
| 319 | } else if (expect_clash(i, expect)) { |
| 320 | ret = -EBUSY; |
| 321 | goto out; |
| 322 | } |
| 323 | } |
| 324 | /* Will be over limit? */ |
| 325 | if (master_help->helper->max_expected && |
| 326 | master_help->expecting >= master_help->helper->max_expected) |
| 327 | evict_oldest_expect(master); |
| 328 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 329 | nf_ct_expect_insert(expect); |
| 330 | nf_ct_expect_event(IPEXP_NEW, expect); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 331 | ret = 0; |
| 332 | out: |
| 333 | write_unlock_bh(&nf_conntrack_lock); |
| 334 | return ret; |
| 335 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 336 | EXPORT_SYMBOL_GPL(nf_ct_expect_related); |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 337 | |
| 338 | #ifdef CONFIG_PROC_FS |
| 339 | static void *exp_seq_start(struct seq_file *s, loff_t *pos) |
| 340 | { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 341 | struct list_head *e = &nf_ct_expect_list; |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 342 | loff_t i; |
| 343 | |
| 344 | /* strange seq_file api calls stop even if we fail, |
| 345 | * thus we need to grab lock since stop unlocks */ |
| 346 | read_lock_bh(&nf_conntrack_lock); |
| 347 | |
| 348 | if (list_empty(e)) |
| 349 | return NULL; |
| 350 | |
| 351 | for (i = 0; i <= *pos; i++) { |
| 352 | e = e->next; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 353 | if (e == &nf_ct_expect_list) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | return e; |
| 357 | } |
| 358 | |
| 359 | static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 360 | { |
| 361 | struct list_head *e = v; |
| 362 | |
| 363 | ++*pos; |
| 364 | e = e->next; |
| 365 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 366 | if (e == &nf_ct_expect_list) |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 367 | return NULL; |
| 368 | |
| 369 | return e; |
| 370 | } |
| 371 | |
| 372 | static void exp_seq_stop(struct seq_file *s, void *v) |
| 373 | { |
| 374 | read_unlock_bh(&nf_conntrack_lock); |
| 375 | } |
| 376 | |
| 377 | static int exp_seq_show(struct seq_file *s, void *v) |
| 378 | { |
| 379 | struct nf_conntrack_expect *expect = v; |
| 380 | |
| 381 | if (expect->timeout.function) |
| 382 | seq_printf(s, "%ld ", timer_pending(&expect->timeout) |
| 383 | ? (long)(expect->timeout.expires - jiffies)/HZ : 0); |
| 384 | else |
| 385 | seq_printf(s, "- "); |
| 386 | seq_printf(s, "l3proto = %u proto=%u ", |
| 387 | expect->tuple.src.l3num, |
| 388 | expect->tuple.dst.protonum); |
| 389 | print_tuple(s, &expect->tuple, |
| 390 | __nf_ct_l3proto_find(expect->tuple.src.l3num), |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 391 | __nf_ct_l4proto_find(expect->tuple.src.l3num, |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 392 | expect->tuple.dst.protonum)); |
| 393 | return seq_putc(s, '\n'); |
| 394 | } |
| 395 | |
| 396 | static struct seq_operations exp_seq_ops = { |
| 397 | .start = exp_seq_start, |
| 398 | .next = exp_seq_next, |
| 399 | .stop = exp_seq_stop, |
| 400 | .show = exp_seq_show |
| 401 | }; |
| 402 | |
| 403 | static int exp_open(struct inode *inode, struct file *file) |
| 404 | { |
| 405 | return seq_open(file, &exp_seq_ops); |
| 406 | } |
| 407 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 408 | const struct file_operations exp_file_ops = { |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 409 | .owner = THIS_MODULE, |
| 410 | .open = exp_open, |
| 411 | .read = seq_read, |
| 412 | .llseek = seq_lseek, |
| 413 | .release = seq_release |
| 414 | }; |
| 415 | #endif /* CONFIG_PROC_FS */ |
Patrick McHardy | e9c1b08 | 2007-07-07 22:32:53 -0700 | [diff] [blame^] | 416 | |
| 417 | static int __init exp_proc_init(void) |
| 418 | { |
| 419 | #ifdef CONFIG_PROC_FS |
| 420 | struct proc_dir_entry *proc; |
| 421 | |
| 422 | proc = proc_net_fops_create("nf_conntrack_expect", 0440, &exp_file_ops); |
| 423 | if (!proc) |
| 424 | return -ENOMEM; |
| 425 | #endif /* CONFIG_PROC_FS */ |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static void exp_proc_remove(void) |
| 430 | { |
| 431 | #ifdef CONFIG_PROC_FS |
| 432 | proc_net_remove("nf_conntrack_expect"); |
| 433 | #endif /* CONFIG_PROC_FS */ |
| 434 | } |
| 435 | |
| 436 | int __init nf_conntrack_expect_init(void) |
| 437 | { |
| 438 | int err; |
| 439 | |
| 440 | nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect", |
| 441 | sizeof(struct nf_conntrack_expect), |
| 442 | 0, 0, NULL, NULL); |
| 443 | if (!nf_ct_expect_cachep) |
| 444 | return -ENOMEM; |
| 445 | |
| 446 | err = exp_proc_init(); |
| 447 | if (err < 0) |
| 448 | goto err1; |
| 449 | |
| 450 | return 0; |
| 451 | |
| 452 | err1: |
| 453 | kmem_cache_destroy(nf_ct_expect_cachep); |
| 454 | return err; |
| 455 | } |
| 456 | |
| 457 | void nf_conntrack_expect_fini(void) |
| 458 | { |
| 459 | exp_proc_remove(); |
| 460 | kmem_cache_destroy(nf_ct_expect_cachep); |
| 461 | } |