blob: e0cd9d00aa61e872f2f769c142e065a529fcdf23 [file] [log] [blame]
Martin Josefsson77ab9cf2006-11-29 02:34:58 +01001/* 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 McHardya71c0852007-07-07 22:33:47 -070022#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020023#include <net/net_namespace.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010024
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 McHardya71c0852007-07-07 22:33:47 -070031struct hlist_head *nf_ct_expect_hash __read_mostly;
32EXPORT_SYMBOL_GPL(nf_ct_expect_hash);
33
34unsigned int nf_ct_expect_hsize __read_mostly;
35EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
36
37static unsigned int nf_ct_expect_hash_rnd __read_mostly;
38static unsigned int nf_ct_expect_count;
Patrick McHardyf264a7d2007-07-07 22:36:24 -070039unsigned int nf_ct_expect_max __read_mostly;
Patrick McHardya71c0852007-07-07 22:33:47 -070040static int nf_ct_expect_hash_rnd_initted __read_mostly;
41static int nf_ct_expect_vmalloc;
42
Patrick McHardye9c1b082007-07-07 22:32:53 -070043static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010044
45/* nf_conntrack_expect helper functions */
46void 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 McHardya71c0852007-07-07 22:33:47 -070053 hlist_del(&exp->hnode);
54 nf_ct_expect_count--;
55
Patrick McHardyb5605802007-07-07 22:35:56 -070056 hlist_del(&exp->lnode);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010057 master_help->expecting--;
Patrick McHardy68236452007-07-07 22:30:49 -070058 nf_ct_expect_put(exp);
Patrick McHardyb5605802007-07-07 22:35:56 -070059
60 NF_CT_STAT_INC(expect_delete);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010061}
Patrick McHardy13b18332006-12-02 22:11:25 -080062EXPORT_SYMBOL_GPL(nf_ct_unlink_expect);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010063
Patrick McHardy68236452007-07-07 22:30:49 -070064static void nf_ct_expectation_timed_out(unsigned long ul_expect)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010065{
66 struct nf_conntrack_expect *exp = (void *)ul_expect;
67
68 write_lock_bh(&nf_conntrack_lock);
69 nf_ct_unlink_expect(exp);
70 write_unlock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -070071 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010072}
73
Patrick McHardya71c0852007-07-07 22:33:47 -070074static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple)
75{
Patrick McHardy34498822007-12-17 22:45:52 -080076 unsigned int hash;
77
Patrick McHardya71c0852007-07-07 22:33:47 -070078 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 McHardy34498822007-12-17 22:45:52 -080083 hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
Patrick McHardya71c0852007-07-07 22:33:47 -070084 (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
Patrick McHardy34498822007-12-17 22:45:52 -080085 (__force __u16)tuple->dst.u.all) ^ nf_ct_expect_hash_rnd);
86 return ((u64)hash * nf_ct_expect_hsize) >> 32;
Patrick McHardya71c0852007-07-07 22:33:47 -070087}
88
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010089struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -070090__nf_ct_expect_find(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010091{
92 struct nf_conntrack_expect *i;
Patrick McHardya71c0852007-07-07 22:33:47 -070093 struct hlist_node *n;
94 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010095
Patrick McHardya71c0852007-07-07 22:33:47 -070096 if (!nf_ct_expect_count)
97 return NULL;
98
99 h = nf_ct_expect_dst_hash(tuple);
100 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100101 if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask))
102 return i;
103 }
104 return NULL;
105}
Patrick McHardy68236452007-07-07 22:30:49 -0700106EXPORT_SYMBOL_GPL(__nf_ct_expect_find);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100107
108/* Just find a expectation corresponding to a tuple. */
109struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -0700110nf_ct_expect_find_get(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100111{
112 struct nf_conntrack_expect *i;
113
114 read_lock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -0700115 i = __nf_ct_expect_find(tuple);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100116 if (i)
117 atomic_inc(&i->use);
118 read_unlock_bh(&nf_conntrack_lock);
119
120 return i;
121}
Patrick McHardy68236452007-07-07 22:30:49 -0700122EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100123
124/* If an expectation for this connection is found, it gets delete from
125 * global list then returned. */
126struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -0700127nf_ct_find_expectation(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100128{
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800129 struct nf_conntrack_expect *exp;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100130
Patrick McHardy68236452007-07-07 22:30:49 -0700131 exp = __nf_ct_expect_find(tuple);
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800132 if (!exp)
133 return NULL;
134
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100135 /* 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 Kozakaiece00642006-12-05 13:44:57 -0800140 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 Josefsson77ab9cf2006-11-29 02:34:58 +0100149 }
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800150
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100151 return NULL;
152}
153
154/* delete all expectations for this conntrack */
155void nf_ct_remove_expectations(struct nf_conn *ct)
156{
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100157 struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyb5605802007-07-07 22:35:56 -0700158 struct nf_conntrack_expect *exp;
159 struct hlist_node *n, *next;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100160
161 /* Optimization: most connection never expect any others. */
162 if (!help || help->expecting == 0)
163 return;
164
Patrick McHardyb5605802007-07-07 22:35:56 -0700165 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 Hideaki601e68e2007-02-12 11:15:49 -0800169 }
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100170 }
171}
Patrick McHardy13b18332006-12-02 22:11:25 -0800172EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100173
174/* Would two expected things clash? */
175static 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 McHardyd4156e82007-07-07 22:31:32 -0700180 struct nf_conntrack_tuple_mask intersect_mask;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100181 int count;
182
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100183 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100184
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 Josefsson77ab9cf2006-11-29 02:34:58 +0100190 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);
191}
192
193static 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 McHardyd4156e82007-07-07 22:31:32 -0700198 && nf_ct_tuple_mask_equal(&a->mask, &b->mask);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100199}
200
201/* Generally a bad idea to call this: could have matched already. */
Patrick McHardy68236452007-07-07 22:30:49 -0700202void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100203{
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100204 write_lock_bh(&nf_conntrack_lock);
Patrick McHardy4e1d4e62007-07-07 22:32:03 -0700205 if (del_timer(&exp->timeout)) {
206 nf_ct_unlink_expect(exp);
207 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100208 }
209 write_unlock_bh(&nf_conntrack_lock);
210}
Patrick McHardy68236452007-07-07 22:30:49 -0700211EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100212
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 McHardy68236452007-07-07 22:30:49 -0700216struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100217{
218 struct nf_conntrack_expect *new;
219
Patrick McHardy68236452007-07-07 22:30:49 -0700220 new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100221 if (!new)
222 return NULL;
223
224 new->master = me;
225 atomic_set(&new->use, 1);
226 return new;
227}
Patrick McHardy68236452007-07-07 22:30:49 -0700228EXPORT_SYMBOL_GPL(nf_ct_expect_alloc);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100229
Patrick McHardy68236452007-07-07 22:30:49 -0700230void nf_ct_expect_init(struct nf_conntrack_expect *exp, int family,
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800231 union nf_inet_addr *saddr,
232 union nf_inet_addr *daddr,
Patrick McHardy68236452007-07-07 22:30:49 -0700233 u_int8_t proto, __be16 *src, __be16 *dst)
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800234{
235 int len;
236
237 if (family == AF_INET)
238 len = 4;
239 else
240 len = 16;
241
242 exp->flags = 0;
243 exp->expectfn = NULL;
244 exp->helper = NULL;
245 exp->tuple.src.l3num = family;
246 exp->tuple.dst.protonum = proto;
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800247
248 if (saddr) {
249 memcpy(&exp->tuple.src.u3, saddr, len);
250 if (sizeof(exp->tuple.src.u3) > len)
251 /* address needs to be cleared for nf_ct_tuple_equal */
252 memset((void *)&exp->tuple.src.u3 + len, 0x00,
253 sizeof(exp->tuple.src.u3) - len);
254 memset(&exp->mask.src.u3, 0xFF, len);
255 if (sizeof(exp->mask.src.u3) > len)
256 memset((void *)&exp->mask.src.u3 + len, 0x00,
257 sizeof(exp->mask.src.u3) - len);
258 } else {
259 memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
260 memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
261 }
262
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800263 if (src) {
Al Viroa34c4582007-07-26 17:33:19 +0100264 exp->tuple.src.u.all = *src;
265 exp->mask.src.u.all = htons(0xFFFF);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800266 } else {
267 exp->tuple.src.u.all = 0;
268 exp->mask.src.u.all = 0;
269 }
270
Patrick McHardyd4156e82007-07-07 22:31:32 -0700271 memcpy(&exp->tuple.dst.u3, daddr, len);
272 if (sizeof(exp->tuple.dst.u3) > len)
273 /* address needs to be cleared for nf_ct_tuple_equal */
274 memset((void *)&exp->tuple.dst.u3 + len, 0x00,
275 sizeof(exp->tuple.dst.u3) - len);
276
Al Viroa34c4582007-07-26 17:33:19 +0100277 exp->tuple.dst.u.all = *dst;
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800278}
Patrick McHardy68236452007-07-07 22:30:49 -0700279EXPORT_SYMBOL_GPL(nf_ct_expect_init);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800280
Patrick McHardy68236452007-07-07 22:30:49 -0700281void nf_ct_expect_put(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100282{
283 if (atomic_dec_and_test(&exp->use))
Patrick McHardy68236452007-07-07 22:30:49 -0700284 kmem_cache_free(nf_ct_expect_cachep, exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100285}
Patrick McHardy68236452007-07-07 22:30:49 -0700286EXPORT_SYMBOL_GPL(nf_ct_expect_put);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100287
Patrick McHardy68236452007-07-07 22:30:49 -0700288static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100289{
290 struct nf_conn_help *master_help = nfct_help(exp->master);
Patrick McHardya71c0852007-07-07 22:33:47 -0700291 unsigned int h = nf_ct_expect_dst_hash(&exp->tuple);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100292
293 atomic_inc(&exp->use);
Patrick McHardyb5605802007-07-07 22:35:56 -0700294
295 hlist_add_head(&exp->lnode, &master_help->expectations);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100296 master_help->expecting++;
Patrick McHardya71c0852007-07-07 22:33:47 -0700297
Patrick McHardya71c0852007-07-07 22:33:47 -0700298 hlist_add_head(&exp->hnode, &nf_ct_expect_hash[h]);
299 nf_ct_expect_count++;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100300
Patrick McHardy68236452007-07-07 22:30:49 -0700301 setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
302 (unsigned long)exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100303 exp->timeout.expires = jiffies + master_help->helper->timeout * HZ;
304 add_timer(&exp->timeout);
305
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100306 atomic_inc(&exp->use);
307 NF_CT_STAT_INC(expect_create);
308}
309
310/* Race with expectations being used means we could have none to find; OK. */
311static void evict_oldest_expect(struct nf_conn *master)
312{
Patrick McHardyb5605802007-07-07 22:35:56 -0700313 struct nf_conn_help *master_help = nfct_help(master);
314 struct nf_conntrack_expect *exp = NULL;
315 struct hlist_node *n;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100316
Patrick McHardyb5605802007-07-07 22:35:56 -0700317 hlist_for_each_entry(exp, n, &master_help->expectations, lnode)
318 ; /* nothing */
319
320 if (exp && del_timer(&exp->timeout)) {
321 nf_ct_unlink_expect(exp);
322 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100323 }
324}
325
326static inline int refresh_timer(struct nf_conntrack_expect *i)
327{
328 struct nf_conn_help *master_help = nfct_help(i->master);
329
330 if (!del_timer(&i->timeout))
331 return 0;
332
333 i->timeout.expires = jiffies + master_help->helper->timeout*HZ;
334 add_timer(&i->timeout);
335 return 1;
336}
337
Patrick McHardy68236452007-07-07 22:30:49 -0700338int nf_ct_expect_related(struct nf_conntrack_expect *expect)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100339{
340 struct nf_conntrack_expect *i;
341 struct nf_conn *master = expect->master;
342 struct nf_conn_help *master_help = nfct_help(master);
Patrick McHardya71c0852007-07-07 22:33:47 -0700343 struct hlist_node *n;
344 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100345 int ret;
346
347 NF_CT_ASSERT(master_help);
348
349 write_lock_bh(&nf_conntrack_lock);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700350 if (!master_help->helper) {
351 ret = -ESHUTDOWN;
352 goto out;
353 }
Patrick McHardya71c0852007-07-07 22:33:47 -0700354 h = nf_ct_expect_dst_hash(&expect->tuple);
355 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100356 if (expect_matches(i, expect)) {
357 /* Refresh timer: if it's dying, ignore.. */
358 if (refresh_timer(i)) {
359 ret = 0;
360 goto out;
361 }
362 } else if (expect_clash(i, expect)) {
363 ret = -EBUSY;
364 goto out;
365 }
366 }
367 /* Will be over limit? */
368 if (master_help->helper->max_expected &&
369 master_help->expecting >= master_help->helper->max_expected)
370 evict_oldest_expect(master);
371
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700372 if (nf_ct_expect_count >= nf_ct_expect_max) {
373 if (net_ratelimit())
374 printk(KERN_WARNING
375 "nf_conntrack: expectation table full");
376 ret = -EMFILE;
377 goto out;
378 }
379
Patrick McHardy68236452007-07-07 22:30:49 -0700380 nf_ct_expect_insert(expect);
381 nf_ct_expect_event(IPEXP_NEW, expect);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100382 ret = 0;
383out:
384 write_unlock_bh(&nf_conntrack_lock);
385 return ret;
386}
Patrick McHardy68236452007-07-07 22:30:49 -0700387EXPORT_SYMBOL_GPL(nf_ct_expect_related);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100388
389#ifdef CONFIG_PROC_FS
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700390struct ct_expect_iter_state {
391 unsigned int bucket;
392};
393
394static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100395{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700396 struct ct_expect_iter_state *st = seq->private;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100397
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700398 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
399 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
400 return nf_ct_expect_hash[st->bucket].first;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100401 }
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700402 return NULL;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100403}
404
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700405static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
406 struct hlist_node *head)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100407{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700408 struct ct_expect_iter_state *st = seq->private;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100409
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700410 head = head->next;
411 while (head == NULL) {
412 if (++st->bucket >= nf_ct_expect_hsize)
413 return NULL;
414 head = nf_ct_expect_hash[st->bucket].first;
415 }
416 return head;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100417}
418
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700419static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
420{
421 struct hlist_node *head = ct_expect_get_first(seq);
422
423 if (head)
424 while (pos && (head = ct_expect_get_next(seq, head)))
425 pos--;
426 return pos ? NULL : head;
427}
428
429static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
430{
431 read_lock_bh(&nf_conntrack_lock);
432 return ct_expect_get_idx(seq, *pos);
433}
434
435static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
436{
437 (*pos)++;
438 return ct_expect_get_next(seq, v);
439}
440
441static void exp_seq_stop(struct seq_file *seq, void *v)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100442{
443 read_unlock_bh(&nf_conntrack_lock);
444}
445
446static int exp_seq_show(struct seq_file *s, void *v)
447{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700448 struct nf_conntrack_expect *expect;
449 struct hlist_node *n = v;
450
451 expect = hlist_entry(n, struct nf_conntrack_expect, hnode);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100452
453 if (expect->timeout.function)
454 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
455 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
456 else
457 seq_printf(s, "- ");
458 seq_printf(s, "l3proto = %u proto=%u ",
459 expect->tuple.src.l3num,
460 expect->tuple.dst.protonum);
461 print_tuple(s, &expect->tuple,
462 __nf_ct_l3proto_find(expect->tuple.src.l3num),
Martin Josefsson605dcad2006-11-29 02:35:06 +0100463 __nf_ct_l4proto_find(expect->tuple.src.l3num,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100464 expect->tuple.dst.protonum));
465 return seq_putc(s, '\n');
466}
467
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700468static const struct seq_operations exp_seq_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100469 .start = exp_seq_start,
470 .next = exp_seq_next,
471 .stop = exp_seq_stop,
472 .show = exp_seq_show
473};
474
475static int exp_open(struct inode *inode, struct file *file)
476{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700477 return seq_open_private(file, &exp_seq_ops,
478 sizeof(struct ct_expect_iter_state));
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100479}
480
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700481static const struct file_operations exp_file_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100482 .owner = THIS_MODULE,
483 .open = exp_open,
484 .read = seq_read,
485 .llseek = seq_lseek,
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700486 .release = seq_release_private,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100487};
488#endif /* CONFIG_PROC_FS */
Patrick McHardye9c1b082007-07-07 22:32:53 -0700489
490static int __init exp_proc_init(void)
491{
492#ifdef CONFIG_PROC_FS
493 struct proc_dir_entry *proc;
494
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200495 proc = proc_net_fops_create(&init_net, "nf_conntrack_expect", 0440, &exp_file_ops);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700496 if (!proc)
497 return -ENOMEM;
498#endif /* CONFIG_PROC_FS */
499 return 0;
500}
501
502static void exp_proc_remove(void)
503{
504#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200505 proc_net_remove(&init_net, "nf_conntrack_expect");
Patrick McHardye9c1b082007-07-07 22:32:53 -0700506#endif /* CONFIG_PROC_FS */
507}
508
Patrick McHardya71c0852007-07-07 22:33:47 -0700509module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0600);
510
Patrick McHardye9c1b082007-07-07 22:32:53 -0700511int __init nf_conntrack_expect_init(void)
512{
Patrick McHardya71c0852007-07-07 22:33:47 -0700513 int err = -ENOMEM;
514
515 if (!nf_ct_expect_hsize) {
516 nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
517 if (!nf_ct_expect_hsize)
518 nf_ct_expect_hsize = 1;
519 }
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700520 nf_ct_expect_max = nf_ct_expect_hsize * 4;
Patrick McHardya71c0852007-07-07 22:33:47 -0700521
522 nf_ct_expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize,
523 &nf_ct_expect_vmalloc);
524 if (nf_ct_expect_hash == NULL)
525 goto err1;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700526
527 nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
528 sizeof(struct nf_conntrack_expect),
Paul Mundt20c2df82007-07-20 10:11:58 +0900529 0, 0, NULL);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700530 if (!nf_ct_expect_cachep)
Patrick McHardya71c0852007-07-07 22:33:47 -0700531 goto err2;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700532
533 err = exp_proc_init();
534 if (err < 0)
Patrick McHardya71c0852007-07-07 22:33:47 -0700535 goto err3;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700536
537 return 0;
538
Patrick McHardya71c0852007-07-07 22:33:47 -0700539err3:
540 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
541 nf_ct_expect_hsize);
542err2:
Patrick McHardye9c1b082007-07-07 22:32:53 -0700543 kmem_cache_destroy(nf_ct_expect_cachep);
Patrick McHardya71c0852007-07-07 22:33:47 -0700544err1:
Patrick McHardye9c1b082007-07-07 22:32:53 -0700545 return err;
546}
547
548void nf_conntrack_expect_fini(void)
549{
550 exp_proc_remove();
551 kmem_cache_destroy(nf_ct_expect_cachep);
Patrick McHardya71c0852007-07-07 22:33:47 -0700552 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
553 nf_ct_expect_hsize);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700554}