[SOCK]: Introduce sk_clone
[deliverable/linux.git] / net / ipv4 / inet_timewait_sock.c
CommitLineData
e48c414e
ACM
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. */
17void __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 */
55void __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}
c676270b
ACM
84
85struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
86{
87 struct inet_timewait_sock *tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_slab,
88 SLAB_ATOMIC);
89 if (tw != NULL) {
90 const struct inet_sock *inet = inet_sk(sk);
91
92 /* Give us an identity. */
93 tw->tw_daddr = inet->daddr;
94 tw->tw_rcv_saddr = inet->rcv_saddr;
95 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
96 tw->tw_num = inet->num;
97 tw->tw_state = TCP_TIME_WAIT;
98 tw->tw_substate = state;
99 tw->tw_sport = inet->sport;
100 tw->tw_dport = inet->dport;
101 tw->tw_family = sk->sk_family;
102 tw->tw_reuse = sk->sk_reuse;
103 tw->tw_hashent = sk->sk_hashent;
104 tw->tw_ipv6only = 0;
105 tw->tw_prot = sk->sk_prot_creator;
106 atomic_set(&tw->tw_refcnt, 1);
107 inet_twsk_dead_node_init(tw);
108 }
109
110 return tw;
111}
This page took 0.028892 seconds and 5 git commands to generate.