diff options
Diffstat (limited to 'net/ipv4/inet_timewait_sock.c')
-rw-r--r-- | net/ipv4/inet_timewait_sock.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c new file mode 100644 index 000000000000..d38d160faeb7 --- /dev/null +++ b/net/ipv4/inet_timewait_sock.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | ||
3 | * operating system. INET is implemented using the BSD Socket | ||
4 | * interface as the means of communication with the user level. | ||
5 | * | ||
6 | * Generic TIME_WAIT sockets functions | ||
7 | * | ||
8 | * From code orinally in TCP | ||
9 | */ | ||
10 | |||
11 | #include <linux/config.h> | ||
12 | |||
13 | #include <net/inet_hashtables.h> | ||
14 | #include <net/inet_timewait_sock.h> | ||
15 | |||
16 | /* Must be called with locally disabled BHs. */ | ||
17 | void __inet_twsk_kill(struct inet_timewait_sock *tw, struct inet_hashinfo *hashinfo) | ||
18 | { | ||
19 | struct inet_bind_hashbucket *bhead; | ||
20 | struct inet_bind_bucket *tb; | ||
21 | /* Unlink from established hashes. */ | ||
22 | struct inet_ehash_bucket *ehead = &hashinfo->ehash[tw->tw_hashent]; | ||
23 | |||
24 | write_lock(&ehead->lock); | ||
25 | if (hlist_unhashed(&tw->tw_node)) { | ||
26 | write_unlock(&ehead->lock); | ||
27 | return; | ||
28 | } | ||
29 | __hlist_del(&tw->tw_node); | ||
30 | sk_node_init(&tw->tw_node); | ||
31 | write_unlock(&ehead->lock); | ||
32 | |||
33 | /* Disassociate with bind bucket. */ | ||
34 | bhead = &hashinfo->bhash[inet_bhashfn(tw->tw_num, hashinfo->bhash_size)]; | ||
35 | spin_lock(&bhead->lock); | ||
36 | tb = tw->tw_tb; | ||
37 | __hlist_del(&tw->tw_bind_node); | ||
38 | tw->tw_tb = NULL; | ||
39 | inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); | ||
40 | spin_unlock(&bhead->lock); | ||
41 | #ifdef SOCK_REFCNT_DEBUG | ||
42 | if (atomic_read(&tw->tw_refcnt) != 1) { | ||
43 | printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n", | ||
44 | tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt)); | ||
45 | } | ||
46 | #endif | ||
47 | inet_twsk_put(tw); | ||
48 | } | ||
49 | |||
50 | /* | ||
51 | * Enter the time wait state. This is called with locally disabled BH. | ||
52 | * Essentially we whip up a timewait bucket, copy the relevant info into it | ||
53 | * from the SK, and mess with hash chains and list linkage. | ||
54 | */ | ||
55 | void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk, | ||
56 | struct inet_hashinfo *hashinfo) | ||
57 | { | ||
58 | const struct inet_sock *inet = inet_sk(sk); | ||
59 | struct inet_ehash_bucket *ehead = &hashinfo->ehash[sk->sk_hashent]; | ||
60 | struct inet_bind_hashbucket *bhead; | ||
61 | /* Step 1: Put TW into bind hash. Original socket stays there too. | ||
62 | Note, that any socket with inet->num != 0 MUST be bound in | ||
63 | binding cache, even if it is closed. | ||
64 | */ | ||
65 | bhead = &hashinfo->bhash[inet_bhashfn(inet->num, hashinfo->bhash_size)]; | ||
66 | spin_lock(&bhead->lock); | ||
67 | tw->tw_tb = inet->bind_hash; | ||
68 | BUG_TRAP(inet->bind_hash); | ||
69 | inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); | ||
70 | spin_unlock(&bhead->lock); | ||
71 | |||
72 | write_lock(&ehead->lock); | ||
73 | |||
74 | /* Step 2: Remove SK from established hash. */ | ||
75 | if (__sk_del_node_init(sk)) | ||
76 | sock_prot_dec_use(sk->sk_prot); | ||
77 | |||
78 | /* Step 3: Hash TW into TIMEWAIT half of established hash table. */ | ||
79 | inet_twsk_add_node(tw, &(ehead + hashinfo->ehash_size)->chain); | ||
80 | atomic_inc(&tw->tw_refcnt); | ||
81 | |||
82 | write_unlock(&ehead->lock); | ||
83 | } | ||