2 * inet fragments management
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.
9 * Authors: Pavel Emelyanov <xemul@openvz.org>
10 * Started as consolidation of ipv4/ip_fragment.c,
11 * ipv6/reassembly. and ipv6 nf conntrack reassembly
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <linux/timer.h>
19 #include <linux/random.h>
21 #include <net/inet_frag.h>
23 static void inet_frag_secret_rebuild(unsigned long dummy)
25 struct inet_frags *f = (struct inet_frags *)dummy;
26 unsigned long now = jiffies;
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;
35 hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
36 unsigned int hval = f->hashfn(q);
41 /* Relink to new hash chain. */
42 hlist_add_head(&q->list, &f->hash[hval]);
46 write_unlock(&f->lock);
48 mod_timer(&f->secret_timer, now + f->ctl->secret_interval);
51 void inet_frags_init(struct inet_frags *f)
55 for (i = 0; i < INETFRAGS_HASHSZ; i++)
56 INIT_HLIST_HEAD(&f->hash[i]);
58 INIT_LIST_HEAD(&f->lru_list);
59 rwlock_init(&f->lock);
61 f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
62 (jiffies ^ (jiffies >> 6)));
65 atomic_set(&f->mem, 0);
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);
73 EXPORT_SYMBOL(inet_frags_init);
75 void inet_frags_fini(struct inet_frags *f)
77 del_timer(&f->secret_timer);
79 EXPORT_SYMBOL(inet_frags_fini);
81 static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
85 list_del(&fq->lru_list);
87 write_unlock(&f->lock);
90 void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
92 if (del_timer(&fq->timer))
93 atomic_dec(&fq->refcnt);
95 if (!(fq->last_in & COMPLETE)) {
97 atomic_dec(&fq->refcnt);
98 fq->last_in |= COMPLETE;
102 EXPORT_SYMBOL(inet_frag_kill);