[INET]: Consolidate xxx_the secret_rebuild
[safe/jmp/linux-2.6] / net / ipv4 / inet_fragment.c
1 /*
2  * inet fragments management
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  *              Authors:        Pavel Emelyanov <xemul@openvz.org>
10  *                              Started as consolidation of ipv4/ip_fragment.c,
11  *                              ipv6/reassembly. and ipv6 nf conntrack reassembly
12  */
13
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <linux/timer.h>
18 #include <linux/mm.h>
19 #include <linux/random.h>
20
21 #include <net/inet_frag.h>
22
23 static void inet_frag_secret_rebuild(unsigned long dummy)
24 {
25         struct inet_frags *f = (struct inet_frags *)dummy;
26         unsigned long now = jiffies;
27         int i;
28
29         write_lock(&f->lock);
30         get_random_bytes(&f->rnd, sizeof(u32));
31         for (i = 0; i < INETFRAGS_HASHSZ; i++) {
32                 struct inet_frag_queue *q;
33                 struct hlist_node *p, *n;
34
35                 hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
36                         unsigned int hval = f->hashfn(q);
37
38                         if (hval != i) {
39                                 hlist_del(&q->list);
40
41                                 /* Relink to new hash chain. */
42                                 hlist_add_head(&q->list, &f->hash[hval]);
43                         }
44                 }
45         }
46         write_unlock(&f->lock);
47
48         mod_timer(&f->secret_timer, now + f->ctl->secret_interval);
49 }
50
51 void inet_frags_init(struct inet_frags *f)
52 {
53         int i;
54
55         for (i = 0; i < INETFRAGS_HASHSZ; i++)
56                 INIT_HLIST_HEAD(&f->hash[i]);
57
58         INIT_LIST_HEAD(&f->lru_list);
59         rwlock_init(&f->lock);
60
61         f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
62                                    (jiffies ^ (jiffies >> 6)));
63
64         f->nqueues = 0;
65         atomic_set(&f->mem, 0);
66
67         init_timer(&f->secret_timer);
68         f->secret_timer.function = inet_frag_secret_rebuild;
69         f->secret_timer.data = (unsigned long)f;
70         f->secret_timer.expires = jiffies + f->ctl->secret_interval;
71         add_timer(&f->secret_timer);
72 }
73 EXPORT_SYMBOL(inet_frags_init);
74
75 void inet_frags_fini(struct inet_frags *f)
76 {
77         del_timer(&f->secret_timer);
78 }
79 EXPORT_SYMBOL(inet_frags_fini);
80
81 static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
82 {
83         write_lock(&f->lock);
84         hlist_del(&fq->list);
85         list_del(&fq->lru_list);
86         f->nqueues--;
87         write_unlock(&f->lock);
88 }
89
90 void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
91 {
92         if (del_timer(&fq->timer))
93                 atomic_dec(&fq->refcnt);
94
95         if (!(fq->last_in & COMPLETE)) {
96                 fq_unlink(fq, f);
97                 atomic_dec(&fq->refcnt);
98                 fq->last_in |= COMPLETE;
99         }
100 }
101
102 EXPORT_SYMBOL(inet_frag_kill);