blob: 175c8d1a199219ee57144b9c1d0574c4374abf01 [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{
76 if (unlikely(!nf_ct_expect_hash_rnd_initted)) {
77 get_random_bytes(&nf_ct_expect_hash_rnd, 4);
78 nf_ct_expect_hash_rnd_initted = 1;
79 }
80
81 return jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
82 (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
Al Viroa34c4582007-07-26 17:33:19 +010083 (__force __u16)tuple->dst.u.all) ^ nf_ct_expect_hash_rnd) %
Patrick McHardya71c0852007-07-07 22:33:47 -070084 nf_ct_expect_hsize;
85}
86
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010087struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -070088__nf_ct_expect_find(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010089{
90 struct nf_conntrack_expect *i;
Patrick McHardya71c0852007-07-07 22:33:47 -070091 struct hlist_node *n;
92 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010093
Patrick McHardya71c0852007-07-07 22:33:47 -070094 if (!nf_ct_expect_count)
95 return NULL;
96
97 h = nf_ct_expect_dst_hash(tuple);
98 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010099 if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask))
100 return i;
101 }
102 return NULL;
103}
Patrick McHardy68236452007-07-07 22:30:49 -0700104EXPORT_SYMBOL_GPL(__nf_ct_expect_find);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100105
106/* Just find a expectation corresponding to a tuple. */
107struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -0700108nf_ct_expect_find_get(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100109{
110 struct nf_conntrack_expect *i;
111
112 read_lock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -0700113 i = __nf_ct_expect_find(tuple);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100114 if (i)
115 atomic_inc(&i->use);
116 read_unlock_bh(&nf_conntrack_lock);
117
118 return i;
119}
Patrick McHardy68236452007-07-07 22:30:49 -0700120EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100121
122/* If an expectation for this connection is found, it gets delete from
123 * global list then returned. */
124struct nf_conntrack_expect *
Patrick McHardy68236452007-07-07 22:30:49 -0700125nf_ct_find_expectation(const struct nf_conntrack_tuple *tuple)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100126{
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800127 struct nf_conntrack_expect *exp;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100128
Patrick McHardy68236452007-07-07 22:30:49 -0700129 exp = __nf_ct_expect_find(tuple);
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800130 if (!exp)
131 return NULL;
132
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100133 /* If master is not in hash table yet (ie. packet hasn't left
134 this machine yet), how can other end know about expected?
135 Hence these are not the droids you are looking for (if
136 master ct never got confirmed, we'd hold a reference to it
137 and weird things would happen to future packets). */
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800138 if (!nf_ct_is_confirmed(exp->master))
139 return NULL;
140
141 if (exp->flags & NF_CT_EXPECT_PERMANENT) {
142 atomic_inc(&exp->use);
143 return exp;
144 } else if (del_timer(&exp->timeout)) {
145 nf_ct_unlink_expect(exp);
146 return exp;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100147 }
Yasuyuki Kozakaiece00642006-12-05 13:44:57 -0800148
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100149 return NULL;
150}
151
152/* delete all expectations for this conntrack */
153void nf_ct_remove_expectations(struct nf_conn *ct)
154{
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100155 struct nf_conn_help *help = nfct_help(ct);
Patrick McHardyb5605802007-07-07 22:35:56 -0700156 struct nf_conntrack_expect *exp;
157 struct hlist_node *n, *next;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100158
159 /* Optimization: most connection never expect any others. */
160 if (!help || help->expecting == 0)
161 return;
162
Patrick McHardyb5605802007-07-07 22:35:56 -0700163 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
164 if (del_timer(&exp->timeout)) {
165 nf_ct_unlink_expect(exp);
166 nf_ct_expect_put(exp);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800167 }
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100168 }
169}
Patrick McHardy13b18332006-12-02 22:11:25 -0800170EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100171
172/* Would two expected things clash? */
173static 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 McHardyd4156e82007-07-07 22:31:32 -0700178 struct nf_conntrack_tuple_mask intersect_mask;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100179 int count;
180
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100181 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100182
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 Josefsson77ab9cf2006-11-29 02:34:58 +0100188 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);
189}
190
191static 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 McHardyd4156e82007-07-07 22:31:32 -0700196 && nf_ct_tuple_mask_equal(&a->mask, &b->mask);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100197}
198
199/* Generally a bad idea to call this: could have matched already. */
Patrick McHardy68236452007-07-07 22:30:49 -0700200void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100201{
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100202 write_lock_bh(&nf_conntrack_lock);
Patrick McHardy4e1d4e62007-07-07 22:32:03 -0700203 if (del_timer(&exp->timeout)) {
204 nf_ct_unlink_expect(exp);
205 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100206 }
207 write_unlock_bh(&nf_conntrack_lock);
208}
Patrick McHardy68236452007-07-07 22:30:49 -0700209EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100210
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 McHardy68236452007-07-07 22:30:49 -0700214struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100215{
216 struct nf_conntrack_expect *new;
217
Patrick McHardy68236452007-07-07 22:30:49 -0700218 new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100219 if (!new)
220 return NULL;
221
222 new->master = me;
223 atomic_set(&new->use, 1);
224 return new;
225}
Patrick McHardy68236452007-07-07 22:30:49 -0700226EXPORT_SYMBOL_GPL(nf_ct_expect_alloc);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100227
Patrick McHardy68236452007-07-07 22:30:49 -0700228void 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 McHardyd6a9b652006-12-02 22:08:01 -0800232{
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 McHardyd6a9b652006-12-02 22:08:01 -0800245
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 McHardyd6a9b652006-12-02 22:08:01 -0800261 if (src) {
Al Viroa34c4582007-07-26 17:33:19 +0100262 exp->tuple.src.u.all = *src;
263 exp->mask.src.u.all = htons(0xFFFF);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800264 } else {
265 exp->tuple.src.u.all = 0;
266 exp->mask.src.u.all = 0;
267 }
268
Patrick McHardyd4156e82007-07-07 22:31:32 -0700269 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
Al Viroa34c4582007-07-26 17:33:19 +0100275 exp->tuple.dst.u.all = *dst;
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800276}
Patrick McHardy68236452007-07-07 22:30:49 -0700277EXPORT_SYMBOL_GPL(nf_ct_expect_init);
Patrick McHardyd6a9b652006-12-02 22:08:01 -0800278
Patrick McHardy68236452007-07-07 22:30:49 -0700279void nf_ct_expect_put(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100280{
281 if (atomic_dec_and_test(&exp->use))
Patrick McHardy68236452007-07-07 22:30:49 -0700282 kmem_cache_free(nf_ct_expect_cachep, exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100283}
Patrick McHardy68236452007-07-07 22:30:49 -0700284EXPORT_SYMBOL_GPL(nf_ct_expect_put);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100285
Patrick McHardy68236452007-07-07 22:30:49 -0700286static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100287{
288 struct nf_conn_help *master_help = nfct_help(exp->master);
Patrick McHardya71c0852007-07-07 22:33:47 -0700289 unsigned int h = nf_ct_expect_dst_hash(&exp->tuple);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100290
291 atomic_inc(&exp->use);
Patrick McHardyb5605802007-07-07 22:35:56 -0700292
293 hlist_add_head(&exp->lnode, &master_help->expectations);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100294 master_help->expecting++;
Patrick McHardya71c0852007-07-07 22:33:47 -0700295
Patrick McHardya71c0852007-07-07 22:33:47 -0700296 hlist_add_head(&exp->hnode, &nf_ct_expect_hash[h]);
297 nf_ct_expect_count++;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100298
Patrick McHardy68236452007-07-07 22:30:49 -0700299 setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
300 (unsigned long)exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100301 exp->timeout.expires = jiffies + master_help->helper->timeout * HZ;
302 add_timer(&exp->timeout);
303
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100304 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. */
309static void evict_oldest_expect(struct nf_conn *master)
310{
Patrick McHardyb5605802007-07-07 22:35:56 -0700311 struct nf_conn_help *master_help = nfct_help(master);
312 struct nf_conntrack_expect *exp = NULL;
313 struct hlist_node *n;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100314
Patrick McHardyb5605802007-07-07 22:35:56 -0700315 hlist_for_each_entry(exp, n, &master_help->expectations, lnode)
316 ; /* nothing */
317
318 if (exp && del_timer(&exp->timeout)) {
319 nf_ct_unlink_expect(exp);
320 nf_ct_expect_put(exp);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100321 }
322}
323
324static 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 McHardy68236452007-07-07 22:30:49 -0700336int nf_ct_expect_related(struct nf_conntrack_expect *expect)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100337{
338 struct nf_conntrack_expect *i;
339 struct nf_conn *master = expect->master;
340 struct nf_conn_help *master_help = nfct_help(master);
Patrick McHardya71c0852007-07-07 22:33:47 -0700341 struct hlist_node *n;
342 unsigned int h;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100343 int ret;
344
345 NF_CT_ASSERT(master_help);
346
347 write_lock_bh(&nf_conntrack_lock);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700348 if (!master_help->helper) {
349 ret = -ESHUTDOWN;
350 goto out;
351 }
Patrick McHardya71c0852007-07-07 22:33:47 -0700352 h = nf_ct_expect_dst_hash(&expect->tuple);
353 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100354 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 McHardyf264a7d2007-07-07 22:36:24 -0700370 if (nf_ct_expect_count >= nf_ct_expect_max) {
371 if (net_ratelimit())
372 printk(KERN_WARNING
373 "nf_conntrack: expectation table full");
374 ret = -EMFILE;
375 goto out;
376 }
377
Patrick McHardy68236452007-07-07 22:30:49 -0700378 nf_ct_expect_insert(expect);
379 nf_ct_expect_event(IPEXP_NEW, expect);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100380 ret = 0;
381out:
382 write_unlock_bh(&nf_conntrack_lock);
383 return ret;
384}
Patrick McHardy68236452007-07-07 22:30:49 -0700385EXPORT_SYMBOL_GPL(nf_ct_expect_related);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100386
387#ifdef CONFIG_PROC_FS
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700388struct ct_expect_iter_state {
389 unsigned int bucket;
390};
391
392static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100393{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700394 struct ct_expect_iter_state *st = seq->private;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100395
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700396 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
397 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
398 return nf_ct_expect_hash[st->bucket].first;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100399 }
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700400 return NULL;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100401}
402
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700403static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
404 struct hlist_node *head)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100405{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700406 struct ct_expect_iter_state *st = seq->private;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100407
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700408 head = head->next;
409 while (head == NULL) {
410 if (++st->bucket >= nf_ct_expect_hsize)
411 return NULL;
412 head = nf_ct_expect_hash[st->bucket].first;
413 }
414 return head;
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100415}
416
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700417static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
418{
419 struct hlist_node *head = ct_expect_get_first(seq);
420
421 if (head)
422 while (pos && (head = ct_expect_get_next(seq, head)))
423 pos--;
424 return pos ? NULL : head;
425}
426
427static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
428{
429 read_lock_bh(&nf_conntrack_lock);
430 return ct_expect_get_idx(seq, *pos);
431}
432
433static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
434{
435 (*pos)++;
436 return ct_expect_get_next(seq, v);
437}
438
439static void exp_seq_stop(struct seq_file *seq, void *v)
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100440{
441 read_unlock_bh(&nf_conntrack_lock);
442}
443
444static int exp_seq_show(struct seq_file *s, void *v)
445{
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700446 struct nf_conntrack_expect *expect;
447 struct hlist_node *n = v;
448
449 expect = hlist_entry(n, struct nf_conntrack_expect, hnode);
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100450
451 if (expect->timeout.function)
452 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
453 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
454 else
455 seq_printf(s, "- ");
456 seq_printf(s, "l3proto = %u proto=%u ",
457 expect->tuple.src.l3num,
458 expect->tuple.dst.protonum);
459 print_tuple(s, &expect->tuple,
460 __nf_ct_l3proto_find(expect->tuple.src.l3num),
Martin Josefsson605dcad2006-11-29 02:35:06 +0100461 __nf_ct_l4proto_find(expect->tuple.src.l3num,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100462 expect->tuple.dst.protonum));
463 return seq_putc(s, '\n');
464}
465
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700466static const struct seq_operations exp_seq_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100467 .start = exp_seq_start,
468 .next = exp_seq_next,
469 .stop = exp_seq_stop,
470 .show = exp_seq_show
471};
472
473static int exp_open(struct inode *inode, struct file *file)
474{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700475 return seq_open_private(file, &exp_seq_ops,
476 sizeof(struct ct_expect_iter_state));
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100477}
478
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700479static const struct file_operations exp_file_ops = {
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100480 .owner = THIS_MODULE,
481 .open = exp_open,
482 .read = seq_read,
483 .llseek = seq_lseek,
Patrick McHardy5d08ad42007-07-07 22:34:07 -0700484 .release = seq_release_private,
Martin Josefsson77ab9cf2006-11-29 02:34:58 +0100485};
486#endif /* CONFIG_PROC_FS */
Patrick McHardye9c1b082007-07-07 22:32:53 -0700487
488static int __init exp_proc_init(void)
489{
490#ifdef CONFIG_PROC_FS
491 struct proc_dir_entry *proc;
492
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200493 proc = proc_net_fops_create(&init_net, "nf_conntrack_expect", 0440, &exp_file_ops);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700494 if (!proc)
495 return -ENOMEM;
496#endif /* CONFIG_PROC_FS */
497 return 0;
498}
499
500static void exp_proc_remove(void)
501{
502#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200503 proc_net_remove(&init_net, "nf_conntrack_expect");
Patrick McHardye9c1b082007-07-07 22:32:53 -0700504#endif /* CONFIG_PROC_FS */
505}
506
Patrick McHardya71c0852007-07-07 22:33:47 -0700507module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0600);
508
Patrick McHardye9c1b082007-07-07 22:32:53 -0700509int __init nf_conntrack_expect_init(void)
510{
Patrick McHardya71c0852007-07-07 22:33:47 -0700511 int err = -ENOMEM;
512
513 if (!nf_ct_expect_hsize) {
514 nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
515 if (!nf_ct_expect_hsize)
516 nf_ct_expect_hsize = 1;
517 }
Patrick McHardyf264a7d2007-07-07 22:36:24 -0700518 nf_ct_expect_max = nf_ct_expect_hsize * 4;
Patrick McHardya71c0852007-07-07 22:33:47 -0700519
520 nf_ct_expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize,
521 &nf_ct_expect_vmalloc);
522 if (nf_ct_expect_hash == NULL)
523 goto err1;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700524
525 nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
526 sizeof(struct nf_conntrack_expect),
Paul Mundt20c2df82007-07-20 10:11:58 +0900527 0, 0, NULL);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700528 if (!nf_ct_expect_cachep)
Patrick McHardya71c0852007-07-07 22:33:47 -0700529 goto err2;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700530
531 err = exp_proc_init();
532 if (err < 0)
Patrick McHardya71c0852007-07-07 22:33:47 -0700533 goto err3;
Patrick McHardye9c1b082007-07-07 22:32:53 -0700534
535 return 0;
536
Patrick McHardya71c0852007-07-07 22:33:47 -0700537err3:
538 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
539 nf_ct_expect_hsize);
540err2:
Patrick McHardye9c1b082007-07-07 22:32:53 -0700541 kmem_cache_destroy(nf_ct_expect_cachep);
Patrick McHardya71c0852007-07-07 22:33:47 -0700542err1:
Patrick McHardye9c1b082007-07-07 22:32:53 -0700543 return err;
544}
545
546void nf_conntrack_expect_fini(void)
547{
548 exp_proc_remove();
549 kmem_cache_destroy(nf_ct_expect_cachep);
Patrick McHardya71c0852007-07-07 22:33:47 -0700550 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
551 nf_ct_expect_hsize);
Patrick McHardye9c1b082007-07-07 22:32:53 -0700552}