[PATCH] libertas: remove bss_descriptior->networktsf
[linux-2.6/libata-dev.git] / net / netfilter / nf_conntrack_expect.c
blob8a3e3af656bf721daadb01f0a8b1352c48365cac
1 /* Expectation handling for nf_conntrack. */
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>
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.
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 #include <linux/jhash.h>
23 #include <net/net_namespace.h>
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>
31 struct hlist_head *nf_ct_expect_hash __read_mostly;
32 EXPORT_SYMBOL_GPL(nf_ct_expect_hash);
34 unsigned int nf_ct_expect_hsize __read_mostly;
35 EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
37 static unsigned int nf_ct_expect_hash_rnd __read_mostly;
38 static unsigned int nf_ct_expect_count;
39 unsigned int nf_ct_expect_max __read_mostly;
40 static int nf_ct_expect_hash_rnd_initted __read_mostly;
41 static int nf_ct_expect_vmalloc;
43 static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
44 static unsigned int nf_ct_expect_next_id;
46 /* nf_conntrack_expect helper functions */
47 void nf_ct_unlink_expect(struct nf_conntrack_expect *exp)
49 struct nf_conn_help *master_help = nfct_help(exp->master);
51 NF_CT_ASSERT(master_help);
52 NF_CT_ASSERT(!timer_pending(&exp->timeout));
54 hlist_del(&exp->hnode);
55 nf_ct_expect_count--;
57 hlist_del(&exp->lnode);
58 master_help->expecting--;
59 nf_ct_expect_put(exp);
61 NF_CT_STAT_INC(expect_delete);
63 EXPORT_SYMBOL_GPL(nf_ct_unlink_expect);
65 static void nf_ct_expectation_timed_out(unsigned long ul_expect)
67 struct nf_conntrack_expect *exp = (void *)ul_expect;
69 write_lock_bh(&nf_conntrack_lock);
70 nf_ct_unlink_expect(exp);
71 write_unlock_bh(&nf_conntrack_lock);
72 nf_ct_expect_put(exp);
75 static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple)
77 if (unlikely(!nf_ct_expect_hash_rnd_initted)) {
78 get_random_bytes(&nf_ct_expect_hash_rnd, 4);
79 nf_ct_expect_hash_rnd_initted = 1;
82 return jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
83 (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
84 (__force __u16)tuple->dst.u.all) ^ nf_ct_expect_hash_rnd) %
85 nf_ct_expect_hsize;
88 struct nf_conntrack_expect *
89 __nf_ct_expect_find(const struct nf_conntrack_tuple *tuple)
91 struct nf_conntrack_expect *i;
92 struct hlist_node *n;
93 unsigned int h;
95 if (!nf_ct_expect_count)
96 return NULL;
98 h = nf_ct_expect_dst_hash(tuple);
99 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
100 if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask))
101 return i;
103 return NULL;
105 EXPORT_SYMBOL_GPL(__nf_ct_expect_find);
107 /* Just find a expectation corresponding to a tuple. */
108 struct nf_conntrack_expect *
109 nf_ct_expect_find_get(const struct nf_conntrack_tuple *tuple)
111 struct nf_conntrack_expect *i;
113 read_lock_bh(&nf_conntrack_lock);
114 i = __nf_ct_expect_find(tuple);
115 if (i)
116 atomic_inc(&i->use);
117 read_unlock_bh(&nf_conntrack_lock);
119 return i;
121 EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);
123 /* If an expectation for this connection is found, it gets delete from
124 * global list then returned. */
125 struct nf_conntrack_expect *
126 nf_ct_find_expectation(const struct nf_conntrack_tuple *tuple)
128 struct nf_conntrack_expect *exp;
130 exp = __nf_ct_expect_find(tuple);
131 if (!exp)
132 return NULL;
134 /* If master is not in hash table yet (ie. packet hasn't left
135 this machine yet), how can other end know about expected?
136 Hence these are not the droids you are looking for (if
137 master ct never got confirmed, we'd hold a reference to it
138 and weird things would happen to future packets). */
139 if (!nf_ct_is_confirmed(exp->master))
140 return NULL;
142 if (exp->flags & NF_CT_EXPECT_PERMANENT) {
143 atomic_inc(&exp->use);
144 return exp;
145 } else if (del_timer(&exp->timeout)) {
146 nf_ct_unlink_expect(exp);
147 return exp;
150 return NULL;
153 /* delete all expectations for this conntrack */
154 void nf_ct_remove_expectations(struct nf_conn *ct)
156 struct nf_conn_help *help = nfct_help(ct);
157 struct nf_conntrack_expect *exp;
158 struct hlist_node *n, *next;
160 /* Optimization: most connection never expect any others. */
161 if (!help || help->expecting == 0)
162 return;
164 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
165 if (del_timer(&exp->timeout)) {
166 nf_ct_unlink_expect(exp);
167 nf_ct_expect_put(exp);
171 EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
173 /* Would two expected things clash? */
174 static inline int expect_clash(const struct nf_conntrack_expect *a,
175 const struct nf_conntrack_expect *b)
177 /* Part covered by intersection of masks must be unequal,
178 otherwise they clash */
179 struct nf_conntrack_tuple_mask intersect_mask;
180 int count;
182 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
184 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
185 intersect_mask.src.u3.all[count] =
186 a->mask.src.u3.all[count] & b->mask.src.u3.all[count];
189 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);
192 static inline int expect_matches(const struct nf_conntrack_expect *a,
193 const struct nf_conntrack_expect *b)
195 return a->master == b->master
196 && nf_ct_tuple_equal(&a->tuple, &b->tuple)
197 && nf_ct_tuple_mask_equal(&a->mask, &b->mask);
200 /* Generally a bad idea to call this: could have matched already. */
201 void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
203 write_lock_bh(&nf_conntrack_lock);
204 if (del_timer(&exp->timeout)) {
205 nf_ct_unlink_expect(exp);
206 nf_ct_expect_put(exp);
208 write_unlock_bh(&nf_conntrack_lock);
210 EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
212 /* We don't increase the master conntrack refcount for non-fulfilled
213 * conntracks. During the conntrack destruction, the expectations are
214 * always killed before the conntrack itself */
215 struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
217 struct nf_conntrack_expect *new;
219 new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC);
220 if (!new)
221 return NULL;
223 new->master = me;
224 atomic_set(&new->use, 1);
225 return new;
227 EXPORT_SYMBOL_GPL(nf_ct_expect_alloc);
229 void nf_ct_expect_init(struct nf_conntrack_expect *exp, int family,
230 union nf_conntrack_address *saddr,
231 union nf_conntrack_address *daddr,
232 u_int8_t proto, __be16 *src, __be16 *dst)
234 int len;
236 if (family == AF_INET)
237 len = 4;
238 else
239 len = 16;
241 exp->flags = 0;
242 exp->expectfn = NULL;
243 exp->helper = NULL;
244 exp->tuple.src.l3num = family;
245 exp->tuple.dst.protonum = proto;
247 if (saddr) {
248 memcpy(&exp->tuple.src.u3, saddr, len);
249 if (sizeof(exp->tuple.src.u3) > len)
250 /* address needs to be cleared for nf_ct_tuple_equal */
251 memset((void *)&exp->tuple.src.u3 + len, 0x00,
252 sizeof(exp->tuple.src.u3) - len);
253 memset(&exp->mask.src.u3, 0xFF, len);
254 if (sizeof(exp->mask.src.u3) > len)
255 memset((void *)&exp->mask.src.u3 + len, 0x00,
256 sizeof(exp->mask.src.u3) - len);
257 } else {
258 memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
259 memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
262 if (src) {
263 exp->tuple.src.u.all = *src;
264 exp->mask.src.u.all = htons(0xFFFF);
265 } else {
266 exp->tuple.src.u.all = 0;
267 exp->mask.src.u.all = 0;
270 memcpy(&exp->tuple.dst.u3, daddr, len);
271 if (sizeof(exp->tuple.dst.u3) > len)
272 /* address needs to be cleared for nf_ct_tuple_equal */
273 memset((void *)&exp->tuple.dst.u3 + len, 0x00,
274 sizeof(exp->tuple.dst.u3) - len);
276 exp->tuple.dst.u.all = *dst;
278 EXPORT_SYMBOL_GPL(nf_ct_expect_init);
280 void nf_ct_expect_put(struct nf_conntrack_expect *exp)
282 if (atomic_dec_and_test(&exp->use))
283 kmem_cache_free(nf_ct_expect_cachep, exp);
285 EXPORT_SYMBOL_GPL(nf_ct_expect_put);
287 static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
289 struct nf_conn_help *master_help = nfct_help(exp->master);
290 unsigned int h = nf_ct_expect_dst_hash(&exp->tuple);
292 atomic_inc(&exp->use);
294 hlist_add_head(&exp->lnode, &master_help->expectations);
295 master_help->expecting++;
297 hlist_add_head(&exp->hnode, &nf_ct_expect_hash[h]);
298 nf_ct_expect_count++;
300 setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
301 (unsigned long)exp);
302 exp->timeout.expires = jiffies + master_help->helper->timeout * HZ;
303 add_timer(&exp->timeout);
305 exp->id = ++nf_ct_expect_next_id;
306 atomic_inc(&exp->use);
307 NF_CT_STAT_INC(expect_create);
310 /* Race with expectations being used means we could have none to find; OK. */
311 static void evict_oldest_expect(struct nf_conn *master)
313 struct nf_conn_help *master_help = nfct_help(master);
314 struct nf_conntrack_expect *exp = NULL;
315 struct hlist_node *n;
317 hlist_for_each_entry(exp, n, &master_help->expectations, lnode)
318 ; /* nothing */
320 if (exp && del_timer(&exp->timeout)) {
321 nf_ct_unlink_expect(exp);
322 nf_ct_expect_put(exp);
326 static inline int refresh_timer(struct nf_conntrack_expect *i)
328 struct nf_conn_help *master_help = nfct_help(i->master);
330 if (!del_timer(&i->timeout))
331 return 0;
333 i->timeout.expires = jiffies + master_help->helper->timeout*HZ;
334 add_timer(&i->timeout);
335 return 1;
338 int nf_ct_expect_related(struct nf_conntrack_expect *expect)
340 struct nf_conntrack_expect *i;
341 struct nf_conn *master = expect->master;
342 struct nf_conn_help *master_help = nfct_help(master);
343 struct hlist_node *n;
344 unsigned int h;
345 int ret;
347 NF_CT_ASSERT(master_help);
349 write_lock_bh(&nf_conntrack_lock);
350 if (!master_help->helper) {
351 ret = -ESHUTDOWN;
352 goto out;
354 h = nf_ct_expect_dst_hash(&expect->tuple);
355 hlist_for_each_entry(i, n, &nf_ct_expect_hash[h], hnode) {
356 if (expect_matches(i, expect)) {
357 /* Refresh timer: if it's dying, ignore.. */
358 if (refresh_timer(i)) {
359 ret = 0;
360 goto out;
362 } else if (expect_clash(i, expect)) {
363 ret = -EBUSY;
364 goto out;
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);
372 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;
380 nf_ct_expect_insert(expect);
381 nf_ct_expect_event(IPEXP_NEW, expect);
382 ret = 0;
383 out:
384 write_unlock_bh(&nf_conntrack_lock);
385 return ret;
387 EXPORT_SYMBOL_GPL(nf_ct_expect_related);
389 #ifdef CONFIG_PROC_FS
390 struct ct_expect_iter_state {
391 unsigned int bucket;
394 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
396 struct ct_expect_iter_state *st = seq->private;
398 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;
402 return NULL;
405 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
406 struct hlist_node *head)
408 struct ct_expect_iter_state *st = seq->private;
410 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;
416 return head;
419 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
421 struct hlist_node *head = ct_expect_get_first(seq);
423 if (head)
424 while (pos && (head = ct_expect_get_next(seq, head)))
425 pos--;
426 return pos ? NULL : head;
429 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
431 read_lock_bh(&nf_conntrack_lock);
432 return ct_expect_get_idx(seq, *pos);
435 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
437 (*pos)++;
438 return ct_expect_get_next(seq, v);
441 static void exp_seq_stop(struct seq_file *seq, void *v)
443 read_unlock_bh(&nf_conntrack_lock);
446 static int exp_seq_show(struct seq_file *s, void *v)
448 struct nf_conntrack_expect *expect;
449 struct hlist_node *n = v;
451 expect = hlist_entry(n, struct nf_conntrack_expect, hnode);
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),
463 __nf_ct_l4proto_find(expect->tuple.src.l3num,
464 expect->tuple.dst.protonum));
465 return seq_putc(s, '\n');
468 static const struct seq_operations exp_seq_ops = {
469 .start = exp_seq_start,
470 .next = exp_seq_next,
471 .stop = exp_seq_stop,
472 .show = exp_seq_show
475 static int exp_open(struct inode *inode, struct file *file)
477 struct seq_file *seq;
478 struct ct_expect_iter_state *st;
479 int ret;
481 st = kzalloc(sizeof(struct ct_expect_iter_state), GFP_KERNEL);
482 if (!st)
483 return -ENOMEM;
484 ret = seq_open(file, &exp_seq_ops);
485 if (ret)
486 goto out_free;
487 seq = file->private_data;
488 seq->private = st;
489 return ret;
490 out_free:
491 kfree(st);
492 return ret;
495 static const struct file_operations exp_file_ops = {
496 .owner = THIS_MODULE,
497 .open = exp_open,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .release = seq_release_private,
502 #endif /* CONFIG_PROC_FS */
504 static int __init exp_proc_init(void)
506 #ifdef CONFIG_PROC_FS
507 struct proc_dir_entry *proc;
509 proc = proc_net_fops_create(&init_net, "nf_conntrack_expect", 0440, &exp_file_ops);
510 if (!proc)
511 return -ENOMEM;
512 #endif /* CONFIG_PROC_FS */
513 return 0;
516 static void exp_proc_remove(void)
518 #ifdef CONFIG_PROC_FS
519 proc_net_remove(&init_net, "nf_conntrack_expect");
520 #endif /* CONFIG_PROC_FS */
523 module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0600);
525 int __init nf_conntrack_expect_init(void)
527 int err = -ENOMEM;
529 if (!nf_ct_expect_hsize) {
530 nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
531 if (!nf_ct_expect_hsize)
532 nf_ct_expect_hsize = 1;
534 nf_ct_expect_max = nf_ct_expect_hsize * 4;
536 nf_ct_expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize,
537 &nf_ct_expect_vmalloc);
538 if (nf_ct_expect_hash == NULL)
539 goto err1;
541 nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
542 sizeof(struct nf_conntrack_expect),
543 0, 0, NULL);
544 if (!nf_ct_expect_cachep)
545 goto err2;
547 err = exp_proc_init();
548 if (err < 0)
549 goto err3;
551 return 0;
553 err3:
554 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
555 nf_ct_expect_hsize);
556 err2:
557 kmem_cache_destroy(nf_ct_expect_cachep);
558 err1:
559 return err;
562 void nf_conntrack_expect_fini(void)
564 exp_proc_remove();
565 kmem_cache_destroy(nf_ct_expect_cachep);
566 nf_ct_free_hashtable(nf_ct_expect_hash, nf_ct_expect_vmalloc,
567 nf_ct_expect_hsize);