Commit | Line | Data |
---|---|---|
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 | ||
172589cc | 11 | #include <linux/kernel.h> |
9e337b0f | 12 | #include <linux/kmemcheck.h> |
5a0e3ad6 | 13 | #include <linux/slab.h> |
3a9a231d | 14 | #include <linux/module.h> |
e48c414e ACM |
15 | #include <net/inet_hashtables.h> |
16 | #include <net/inet_timewait_sock.h> | |
696ab2d3 | 17 | #include <net/ip.h> |
e48c414e | 18 | |
13475a30 | 19 | |
2a8875e7 ED |
20 | /** |
21 | * inet_twsk_unhash - unhash a timewait socket from established hash | |
22 | * @tw: timewait socket | |
23 | * | |
24 | * unhash a timewait socket from established hash, if hashed. | |
25 | * ehash lock must be held by caller. | |
26 | * Returns 1 if caller should call inet_twsk_put() after lock release. | |
13475a30 ED |
27 | */ |
28 | int inet_twsk_unhash(struct inet_timewait_sock *tw) | |
29 | { | |
30 | if (hlist_nulls_unhashed(&tw->tw_node)) | |
31 | return 0; | |
32 | ||
33 | hlist_nulls_del_rcu(&tw->tw_node); | |
34 | sk_nulls_node_init(&tw->tw_node); | |
2a8875e7 ED |
35 | /* |
36 | * We cannot call inet_twsk_put() ourself under lock, | |
37 | * caller must call it for us. | |
38 | */ | |
13475a30 ED |
39 | return 1; |
40 | } | |
41 | ||
2a8875e7 ED |
42 | /** |
43 | * inet_twsk_bind_unhash - unhash a timewait socket from bind hash | |
44 | * @tw: timewait socket | |
45 | * @hashinfo: hashinfo pointer | |
46 | * | |
47 | * unhash a timewait socket from bind hash, if hashed. | |
48 | * bind hash lock must be held by caller. | |
49 | * Returns 1 if caller should call inet_twsk_put() after lock release. | |
3cdaedae ED |
50 | */ |
51 | int inet_twsk_bind_unhash(struct inet_timewait_sock *tw, | |
52 | struct inet_hashinfo *hashinfo) | |
53 | { | |
54 | struct inet_bind_bucket *tb = tw->tw_tb; | |
55 | ||
56 | if (!tb) | |
57 | return 0; | |
58 | ||
59 | __hlist_del(&tw->tw_bind_node); | |
60 | tw->tw_tb = NULL; | |
61 | inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); | |
2a8875e7 ED |
62 | /* |
63 | * We cannot call inet_twsk_put() ourself under lock, | |
64 | * caller must call it for us. | |
65 | */ | |
3cdaedae ED |
66 | return 1; |
67 | } | |
68 | ||
e48c414e | 69 | /* Must be called with locally disabled BHs. */ |
789f558c | 70 | static void inet_twsk_kill(struct inet_timewait_sock *tw) |
e48c414e | 71 | { |
789f558c | 72 | struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo; |
e48c414e | 73 | struct inet_bind_hashbucket *bhead; |
13475a30 | 74 | int refcnt; |
e48c414e | 75 | /* Unlink from established hashes. */ |
9db66bdc | 76 | spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash); |
e48c414e | 77 | |
9db66bdc | 78 | spin_lock(lock); |
13475a30 | 79 | refcnt = inet_twsk_unhash(tw); |
9db66bdc | 80 | spin_unlock(lock); |
e48c414e ACM |
81 | |
82 | /* Disassociate with bind bucket. */ | |
7f635ab7 PE |
83 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num, |
84 | hashinfo->bhash_size)]; | |
3cdaedae | 85 | |
e48c414e | 86 | spin_lock(&bhead->lock); |
3cdaedae | 87 | refcnt += inet_twsk_bind_unhash(tw, hashinfo); |
e48c414e | 88 | spin_unlock(&bhead->lock); |
3cdaedae | 89 | |
05dbc7b5 ED |
90 | BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt)); |
91 | atomic_sub(refcnt, &tw->tw_refcnt); | |
789f558c ED |
92 | atomic_dec(&tw->tw_dr->tw_count); |
93 | inet_twsk_put(tw); | |
e48c414e ACM |
94 | } |
95 | ||
05dbc7b5 | 96 | void inet_twsk_free(struct inet_timewait_sock *tw) |
7054fb93 | 97 | { |
4dbc8ef7 ACM |
98 | struct module *owner = tw->tw_prot->owner; |
99 | twsk_destructor((struct sock *)tw); | |
7054fb93 | 100 | #ifdef SOCK_REFCNT_DEBUG |
4dbc8ef7 | 101 | pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw); |
7054fb93 | 102 | #endif |
4dbc8ef7 ACM |
103 | kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw); |
104 | module_put(owner); | |
105 | } | |
106 | ||
107 | void inet_twsk_put(struct inet_timewait_sock *tw) | |
108 | { | |
109 | if (atomic_dec_and_test(&tw->tw_refcnt)) | |
110 | inet_twsk_free(tw); | |
7054fb93 PE |
111 | } |
112 | EXPORT_SYMBOL_GPL(inet_twsk_put); | |
113 | ||
05dbc7b5 ED |
114 | static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, |
115 | struct hlist_nulls_head *list) | |
116 | { | |
117 | hlist_nulls_add_head_rcu(&tw->tw_node, list); | |
118 | } | |
119 | ||
120 | static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw, | |
121 | struct hlist_head *list) | |
122 | { | |
123 | hlist_add_head(&tw->tw_bind_node, list); | |
124 | } | |
125 | ||
e48c414e ACM |
126 | /* |
127 | * Enter the time wait state. This is called with locally disabled BH. | |
128 | * Essentially we whip up a timewait bucket, copy the relevant info into it | |
129 | * from the SK, and mess with hash chains and list linkage. | |
130 | */ | |
131 | void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk, | |
132 | struct inet_hashinfo *hashinfo) | |
133 | { | |
134 | const struct inet_sock *inet = inet_sk(sk); | |
463c84b9 | 135 | const struct inet_connection_sock *icsk = inet_csk(sk); |
81c3d547 | 136 | struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); |
9db66bdc | 137 | spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); |
e48c414e ACM |
138 | struct inet_bind_hashbucket *bhead; |
139 | /* Step 1: Put TW into bind hash. Original socket stays there too. | |
140 | Note, that any socket with inet->num != 0 MUST be bound in | |
141 | binding cache, even if it is closed. | |
142 | */ | |
c720c7e8 | 143 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num, |
7f635ab7 | 144 | hashinfo->bhash_size)]; |
e48c414e | 145 | spin_lock(&bhead->lock); |
463c84b9 | 146 | tw->tw_tb = icsk->icsk_bind_hash; |
547b792c | 147 | WARN_ON(!icsk->icsk_bind_hash); |
e48c414e ACM |
148 | inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); |
149 | spin_unlock(&bhead->lock); | |
150 | ||
9db66bdc | 151 | spin_lock(lock); |
e48c414e | 152 | |
3ab5aee7 | 153 | /* |
05dbc7b5 ED |
154 | * Step 2: Hash TW into tcp ehash chain. |
155 | * Notes : | |
156 | * - tw_refcnt is set to 3 because : | |
157 | * - We have one reference from bhash chain. | |
158 | * - We have one reference from ehash chain. | |
159 | * We can use atomic_set() because prior spin_lock()/spin_unlock() | |
160 | * committed into memory all tw fields. | |
3ab5aee7 | 161 | */ |
05dbc7b5 ED |
162 | atomic_set(&tw->tw_refcnt, 1 + 1 + 1); |
163 | inet_twsk_add_node_rcu(tw, &ehead->chain); | |
3ab5aee7 | 164 | |
05dbc7b5 | 165 | /* Step 3: Remove SK from hash chain */ |
3ab5aee7 ED |
166 | if (__sk_nulls_del_node_init_rcu(sk)) |
167 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | |
e48c414e | 168 | |
9db66bdc | 169 | spin_unlock(lock); |
e48c414e | 170 | } |
696ab2d3 ACM |
171 | EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); |
172 | ||
216f8bb9 | 173 | static void tw_timer_handler(unsigned long data) |
c676270b | 174 | { |
789f558c ED |
175 | struct inet_timewait_sock *tw = (struct inet_timewait_sock *)data; |
176 | ||
177 | if (tw->tw_kill) | |
178 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED); | |
179 | else | |
180 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED); | |
181 | inet_twsk_kill(tw); | |
182 | } | |
183 | ||
184 | struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, | |
185 | struct inet_timewait_death_row *dr, | |
186 | const int state) | |
187 | { | |
188 | struct inet_timewait_sock *tw; | |
189 | ||
190 | if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets) | |
191 | return NULL; | |
192 | ||
193 | tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, | |
194 | GFP_ATOMIC); | |
00db4124 | 195 | if (tw) { |
c676270b ACM |
196 | const struct inet_sock *inet = inet_sk(sk); |
197 | ||
9e337b0f VN |
198 | kmemcheck_annotate_bitfield(tw, flags); |
199 | ||
789f558c | 200 | tw->tw_dr = dr; |
c676270b | 201 | /* Give us an identity. */ |
c720c7e8 ED |
202 | tw->tw_daddr = inet->inet_daddr; |
203 | tw->tw_rcv_saddr = inet->inet_rcv_saddr; | |
c676270b | 204 | tw->tw_bound_dev_if = sk->sk_bound_dev_if; |
66b13d99 | 205 | tw->tw_tos = inet->tos; |
c720c7e8 | 206 | tw->tw_num = inet->inet_num; |
c676270b ACM |
207 | tw->tw_state = TCP_TIME_WAIT; |
208 | tw->tw_substate = state; | |
c720c7e8 ED |
209 | tw->tw_sport = inet->inet_sport; |
210 | tw->tw_dport = inet->inet_dport; | |
c676270b ACM |
211 | tw->tw_family = sk->sk_family; |
212 | tw->tw_reuse = sk->sk_reuse; | |
81c3d547 | 213 | tw->tw_hash = sk->sk_hash; |
c676270b | 214 | tw->tw_ipv6only = 0; |
f5715aea | 215 | tw->tw_transparent = inet->transparent; |
c676270b | 216 | tw->tw_prot = sk->sk_prot_creator; |
33cf7c90 | 217 | atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie)); |
efd7ef1c | 218 | twsk_net_set(tw, sock_net(sk)); |
789f558c | 219 | setup_timer(&tw->tw_timer, tw_timer_handler, (unsigned long)tw); |
47e1c323 ED |
220 | /* |
221 | * Because we use RCU lookups, we should not set tw_refcnt | |
222 | * to a non null value before everything is setup for this | |
223 | * timewait socket. | |
224 | */ | |
225 | atomic_set(&tw->tw_refcnt, 0); | |
789f558c | 226 | |
eeb2b856 | 227 | __module_get(tw->tw_prot->owner); |
c676270b ACM |
228 | } |
229 | ||
230 | return tw; | |
231 | } | |
696ab2d3 ACM |
232 | EXPORT_SYMBOL_GPL(inet_twsk_alloc); |
233 | ||
696ab2d3 ACM |
234 | /* These are always called from BH context. See callers in |
235 | * tcp_input.c to verify this. | |
236 | */ | |
237 | ||
238 | /* This is for handling early-kills of TIME_WAIT sockets. */ | |
789f558c | 239 | void inet_twsk_deschedule(struct inet_timewait_sock *tw) |
696ab2d3 | 240 | { |
789f558c ED |
241 | if (del_timer_sync(&tw->tw_timer)) |
242 | inet_twsk_kill(tw); | |
696ab2d3 | 243 | } |
696ab2d3 ACM |
244 | EXPORT_SYMBOL(inet_twsk_deschedule); |
245 | ||
789f558c | 246 | void inet_twsk_schedule(struct inet_timewait_sock *tw, const int timeo) |
696ab2d3 | 247 | { |
696ab2d3 ACM |
248 | /* timeout := RTO * 3.5 |
249 | * | |
250 | * 3.5 = 1+2+0.5 to wait for two retransmits. | |
251 | * | |
252 | * RATIONALE: if FIN arrived and we entered TIME-WAIT state, | |
253 | * our ACK acking that FIN can be lost. If N subsequent retransmitted | |
254 | * FINs (or previous seqments) are lost (probability of such event | |
255 | * is p^(N+1), where p is probability to lose single packet and | |
256 | * time to detect the loss is about RTO*(2^N - 1) with exponential | |
257 | * backoff). Normal timewait length is calculated so, that we | |
258 | * waited at least for one retransmitted FIN (maximal RTO is 120sec). | |
259 | * [ BTW Linux. following BSD, violates this requirement waiting | |
260 | * only for 60sec, we should wait at least for 240 secs. | |
261 | * Well, 240 consumes too much of resources 8) | |
262 | * ] | |
263 | * This interval is not reduced to catch old duplicate and | |
264 | * responces to our wandering segments living for two MSLs. | |
265 | * However, if we use PAWS to detect | |
266 | * old duplicates, we can reduce the interval to bounds required | |
267 | * by RTO, rather than MSL. So, if peer understands PAWS, we | |
268 | * kill tw bucket after 3.5*RTO (it is important that this number | |
269 | * is greater than TS tick!) and detect old duplicates with help | |
270 | * of PAWS. | |
271 | */ | |
696ab2d3 | 272 | |
789f558c ED |
273 | tw->tw_kill = timeo <= 4*HZ; |
274 | if (!mod_timer_pinned(&tw->tw_timer, jiffies + timeo)) { | |
696ab2d3 | 275 | atomic_inc(&tw->tw_refcnt); |
789f558c | 276 | atomic_inc(&tw->tw_dr->tw_count); |
696ab2d3 | 277 | } |
696ab2d3 | 278 | } |
696ab2d3 ACM |
279 | EXPORT_SYMBOL_GPL(inet_twsk_schedule); |
280 | ||
b099ce26 | 281 | void inet_twsk_purge(struct inet_hashinfo *hashinfo, |
d315492b DL |
282 | struct inet_timewait_death_row *twdr, int family) |
283 | { | |
284 | struct inet_timewait_sock *tw; | |
285 | struct sock *sk; | |
3ab5aee7 | 286 | struct hlist_nulls_node *node; |
575f4cd5 | 287 | unsigned int slot; |
d315492b | 288 | |
575f4cd5 EB |
289 | for (slot = 0; slot <= hashinfo->ehash_mask; slot++) { |
290 | struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; | |
291 | restart_rcu: | |
738e6d30 | 292 | cond_resched(); |
575f4cd5 | 293 | rcu_read_lock(); |
d315492b | 294 | restart: |
05dbc7b5 ED |
295 | sk_nulls_for_each_rcu(sk, node, &head->chain) { |
296 | if (sk->sk_state != TCP_TIME_WAIT) | |
297 | continue; | |
d315492b | 298 | tw = inet_twsk(sk); |
b099ce26 EB |
299 | if ((tw->tw_family != family) || |
300 | atomic_read(&twsk_net(tw)->count)) | |
d315492b DL |
301 | continue; |
302 | ||
575f4cd5 EB |
303 | if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt))) |
304 | continue; | |
305 | ||
b099ce26 EB |
306 | if (unlikely((tw->tw_family != family) || |
307 | atomic_read(&twsk_net(tw)->count))) { | |
575f4cd5 EB |
308 | inet_twsk_put(tw); |
309 | goto restart; | |
310 | } | |
311 | ||
312 | rcu_read_unlock(); | |
91035f0b | 313 | local_bh_disable(); |
789f558c | 314 | inet_twsk_deschedule(tw); |
91035f0b | 315 | local_bh_enable(); |
d315492b | 316 | inet_twsk_put(tw); |
575f4cd5 | 317 | goto restart_rcu; |
d315492b | 318 | } |
575f4cd5 EB |
319 | /* If the nulls value we got at the end of this lookup is |
320 | * not the expected one, we must restart lookup. | |
321 | * We probably met an item that was moved to another chain. | |
322 | */ | |
323 | if (get_nulls_value(node) != slot) | |
324 | goto restart; | |
325 | rcu_read_unlock(); | |
d315492b | 326 | } |
d315492b DL |
327 | } |
328 | EXPORT_SYMBOL_GPL(inet_twsk_purge); |