net: annotate inet_timewait_sock bitfields
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / inet_timewait_sock.c
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/kernel.h>
12 #include <linux/kmemcheck.h>
13 #include <net/inet_hashtables.h>
14 #include <net/inet_timewait_sock.h>
15 #include <net/ip.h>
16
17 /* Must be called with locally disabled BHs. */
18 static void __inet_twsk_kill(struct inet_timewait_sock *tw,
19 struct inet_hashinfo *hashinfo)
20 {
21 struct inet_bind_hashbucket *bhead;
22 struct inet_bind_bucket *tb;
23 /* Unlink from established hashes. */
24 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
25
26 spin_lock(lock);
27 if (hlist_nulls_unhashed(&tw->tw_node)) {
28 spin_unlock(lock);
29 return;
30 }
31 hlist_nulls_del_rcu(&tw->tw_node);
32 sk_nulls_node_init(&tw->tw_node);
33 spin_unlock(lock);
34
35 /* Disassociate with bind bucket. */
36 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
37 hashinfo->bhash_size)];
38 spin_lock(&bhead->lock);
39 tb = tw->tw_tb;
40 __hlist_del(&tw->tw_bind_node);
41 tw->tw_tb = NULL;
42 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
43 spin_unlock(&bhead->lock);
44 #ifdef SOCK_REFCNT_DEBUG
45 if (atomic_read(&tw->tw_refcnt) != 1) {
46 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
47 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
48 }
49 #endif
50 inet_twsk_put(tw);
51 }
52
53 void inet_twsk_put(struct inet_timewait_sock *tw)
54 {
55 if (atomic_dec_and_test(&tw->tw_refcnt)) {
56 struct module *owner = tw->tw_prot->owner;
57 twsk_destructor((struct sock *)tw);
58 #ifdef SOCK_REFCNT_DEBUG
59 printk(KERN_DEBUG "%s timewait_sock %p released\n",
60 tw->tw_prot->name, tw);
61 #endif
62 release_net(twsk_net(tw));
63 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
64 module_put(owner);
65 }
66 }
67 EXPORT_SYMBOL_GPL(inet_twsk_put);
68
69 /*
70 * Enter the time wait state. This is called with locally disabled BH.
71 * Essentially we whip up a timewait bucket, copy the relevant info into it
72 * from the SK, and mess with hash chains and list linkage.
73 */
74 void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
75 struct inet_hashinfo *hashinfo)
76 {
77 const struct inet_sock *inet = inet_sk(sk);
78 const struct inet_connection_sock *icsk = inet_csk(sk);
79 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
80 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
81 struct inet_bind_hashbucket *bhead;
82 /* Step 1: Put TW into bind hash. Original socket stays there too.
83 Note, that any socket with inet->num != 0 MUST be bound in
84 binding cache, even if it is closed.
85 */
86 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->num,
87 hashinfo->bhash_size)];
88 spin_lock(&bhead->lock);
89 tw->tw_tb = icsk->icsk_bind_hash;
90 WARN_ON(!icsk->icsk_bind_hash);
91 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
92 spin_unlock(&bhead->lock);
93
94 spin_lock(lock);
95
96 /*
97 * Step 2: Hash TW into TIMEWAIT chain.
98 * Should be done before removing sk from established chain
99 * because readers are lockless and search established first.
100 */
101 atomic_inc(&tw->tw_refcnt);
102 inet_twsk_add_node_rcu(tw, &ehead->twchain);
103
104 /* Step 3: Remove SK from established hash. */
105 if (__sk_nulls_del_node_init_rcu(sk))
106 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
107
108 spin_unlock(lock);
109 }
110
111 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
112
113 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
114 {
115 struct inet_timewait_sock *tw =
116 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
117 GFP_ATOMIC);
118 if (tw != NULL) {
119 const struct inet_sock *inet = inet_sk(sk);
120
121 kmemcheck_annotate_bitfield(tw, flags);
122
123 /* Give us an identity. */
124 tw->tw_daddr = inet->daddr;
125 tw->tw_rcv_saddr = inet->rcv_saddr;
126 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
127 tw->tw_num = inet->num;
128 tw->tw_state = TCP_TIME_WAIT;
129 tw->tw_substate = state;
130 tw->tw_sport = inet->sport;
131 tw->tw_dport = inet->dport;
132 tw->tw_family = sk->sk_family;
133 tw->tw_reuse = sk->sk_reuse;
134 tw->tw_hash = sk->sk_hash;
135 tw->tw_ipv6only = 0;
136 tw->tw_transparent = inet->transparent;
137 tw->tw_prot = sk->sk_prot_creator;
138 twsk_net_set(tw, hold_net(sock_net(sk)));
139 atomic_set(&tw->tw_refcnt, 1);
140 inet_twsk_dead_node_init(tw);
141 __module_get(tw->tw_prot->owner);
142 }
143
144 return tw;
145 }
146
147 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
148
149 /* Returns non-zero if quota exceeded. */
150 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
151 const int slot)
152 {
153 struct inet_timewait_sock *tw;
154 struct hlist_node *node;
155 unsigned int killed;
156 int ret;
157
158 /* NOTE: compare this to previous version where lock
159 * was released after detaching chain. It was racy,
160 * because tw buckets are scheduled in not serialized context
161 * in 2.3 (with netfilter), and with softnet it is common, because
162 * soft irqs are not sequenced.
163 */
164 killed = 0;
165 ret = 0;
166 rescan:
167 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
168 __inet_twsk_del_dead_node(tw);
169 spin_unlock(&twdr->death_lock);
170 __inet_twsk_kill(tw, twdr->hashinfo);
171 #ifdef CONFIG_NET_NS
172 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
173 #endif
174 inet_twsk_put(tw);
175 killed++;
176 spin_lock(&twdr->death_lock);
177 if (killed > INET_TWDR_TWKILL_QUOTA) {
178 ret = 1;
179 break;
180 }
181
182 /* While we dropped twdr->death_lock, another cpu may have
183 * killed off the next TW bucket in the list, therefore
184 * do a fresh re-read of the hlist head node with the
185 * lock reacquired. We still use the hlist traversal
186 * macro in order to get the prefetches.
187 */
188 goto rescan;
189 }
190
191 twdr->tw_count -= killed;
192 #ifndef CONFIG_NET_NS
193 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
194 #endif
195 return ret;
196 }
197
198 void inet_twdr_hangman(unsigned long data)
199 {
200 struct inet_timewait_death_row *twdr;
201 int unsigned need_timer;
202
203 twdr = (struct inet_timewait_death_row *)data;
204 spin_lock(&twdr->death_lock);
205
206 if (twdr->tw_count == 0)
207 goto out;
208
209 need_timer = 0;
210 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
211 twdr->thread_slots |= (1 << twdr->slot);
212 schedule_work(&twdr->twkill_work);
213 need_timer = 1;
214 } else {
215 /* We purged the entire slot, anything left? */
216 if (twdr->tw_count)
217 need_timer = 1;
218 }
219 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
220 if (need_timer)
221 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
222 out:
223 spin_unlock(&twdr->death_lock);
224 }
225
226 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
227
228 void inet_twdr_twkill_work(struct work_struct *work)
229 {
230 struct inet_timewait_death_row *twdr =
231 container_of(work, struct inet_timewait_death_row, twkill_work);
232 int i;
233
234 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
235 (sizeof(twdr->thread_slots) * 8));
236
237 while (twdr->thread_slots) {
238 spin_lock_bh(&twdr->death_lock);
239 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
240 if (!(twdr->thread_slots & (1 << i)))
241 continue;
242
243 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
244 if (need_resched()) {
245 spin_unlock_bh(&twdr->death_lock);
246 schedule();
247 spin_lock_bh(&twdr->death_lock);
248 }
249 }
250
251 twdr->thread_slots &= ~(1 << i);
252 }
253 spin_unlock_bh(&twdr->death_lock);
254 }
255 }
256
257 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
258
259 /* These are always called from BH context. See callers in
260 * tcp_input.c to verify this.
261 */
262
263 /* This is for handling early-kills of TIME_WAIT sockets. */
264 void inet_twsk_deschedule(struct inet_timewait_sock *tw,
265 struct inet_timewait_death_row *twdr)
266 {
267 spin_lock(&twdr->death_lock);
268 if (inet_twsk_del_dead_node(tw)) {
269 inet_twsk_put(tw);
270 if (--twdr->tw_count == 0)
271 del_timer(&twdr->tw_timer);
272 }
273 spin_unlock(&twdr->death_lock);
274 __inet_twsk_kill(tw, twdr->hashinfo);
275 }
276
277 EXPORT_SYMBOL(inet_twsk_deschedule);
278
279 void inet_twsk_schedule(struct inet_timewait_sock *tw,
280 struct inet_timewait_death_row *twdr,
281 const int timeo, const int timewait_len)
282 {
283 struct hlist_head *list;
284 int slot;
285
286 /* timeout := RTO * 3.5
287 *
288 * 3.5 = 1+2+0.5 to wait for two retransmits.
289 *
290 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
291 * our ACK acking that FIN can be lost. If N subsequent retransmitted
292 * FINs (or previous seqments) are lost (probability of such event
293 * is p^(N+1), where p is probability to lose single packet and
294 * time to detect the loss is about RTO*(2^N - 1) with exponential
295 * backoff). Normal timewait length is calculated so, that we
296 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
297 * [ BTW Linux. following BSD, violates this requirement waiting
298 * only for 60sec, we should wait at least for 240 secs.
299 * Well, 240 consumes too much of resources 8)
300 * ]
301 * This interval is not reduced to catch old duplicate and
302 * responces to our wandering segments living for two MSLs.
303 * However, if we use PAWS to detect
304 * old duplicates, we can reduce the interval to bounds required
305 * by RTO, rather than MSL. So, if peer understands PAWS, we
306 * kill tw bucket after 3.5*RTO (it is important that this number
307 * is greater than TS tick!) and detect old duplicates with help
308 * of PAWS.
309 */
310 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
311
312 spin_lock(&twdr->death_lock);
313
314 /* Unlink it, if it was scheduled */
315 if (inet_twsk_del_dead_node(tw))
316 twdr->tw_count--;
317 else
318 atomic_inc(&tw->tw_refcnt);
319
320 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
321 /* Schedule to slow timer */
322 if (timeo >= timewait_len) {
323 slot = INET_TWDR_TWKILL_SLOTS - 1;
324 } else {
325 slot = DIV_ROUND_UP(timeo, twdr->period);
326 if (slot >= INET_TWDR_TWKILL_SLOTS)
327 slot = INET_TWDR_TWKILL_SLOTS - 1;
328 }
329 tw->tw_ttd = jiffies + timeo;
330 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
331 list = &twdr->cells[slot];
332 } else {
333 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
334
335 if (twdr->twcal_hand < 0) {
336 twdr->twcal_hand = 0;
337 twdr->twcal_jiffie = jiffies;
338 twdr->twcal_timer.expires = twdr->twcal_jiffie +
339 (slot << INET_TWDR_RECYCLE_TICK);
340 add_timer(&twdr->twcal_timer);
341 } else {
342 if (time_after(twdr->twcal_timer.expires,
343 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
344 mod_timer(&twdr->twcal_timer,
345 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
346 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
347 }
348 list = &twdr->twcal_row[slot];
349 }
350
351 hlist_add_head(&tw->tw_death_node, list);
352
353 if (twdr->tw_count++ == 0)
354 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
355 spin_unlock(&twdr->death_lock);
356 }
357
358 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
359
360 void inet_twdr_twcal_tick(unsigned long data)
361 {
362 struct inet_timewait_death_row *twdr;
363 int n, slot;
364 unsigned long j;
365 unsigned long now = jiffies;
366 int killed = 0;
367 int adv = 0;
368
369 twdr = (struct inet_timewait_death_row *)data;
370
371 spin_lock(&twdr->death_lock);
372 if (twdr->twcal_hand < 0)
373 goto out;
374
375 slot = twdr->twcal_hand;
376 j = twdr->twcal_jiffie;
377
378 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
379 if (time_before_eq(j, now)) {
380 struct hlist_node *node, *safe;
381 struct inet_timewait_sock *tw;
382
383 inet_twsk_for_each_inmate_safe(tw, node, safe,
384 &twdr->twcal_row[slot]) {
385 __inet_twsk_del_dead_node(tw);
386 __inet_twsk_kill(tw, twdr->hashinfo);
387 #ifdef CONFIG_NET_NS
388 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
389 #endif
390 inet_twsk_put(tw);
391 killed++;
392 }
393 } else {
394 if (!adv) {
395 adv = 1;
396 twdr->twcal_jiffie = j;
397 twdr->twcal_hand = slot;
398 }
399
400 if (!hlist_empty(&twdr->twcal_row[slot])) {
401 mod_timer(&twdr->twcal_timer, j);
402 goto out;
403 }
404 }
405 j += 1 << INET_TWDR_RECYCLE_TICK;
406 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
407 }
408 twdr->twcal_hand = -1;
409
410 out:
411 if ((twdr->tw_count -= killed) == 0)
412 del_timer(&twdr->tw_timer);
413 #ifndef CONFIG_NET_NS
414 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
415 #endif
416 spin_unlock(&twdr->death_lock);
417 }
418
419 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
420
421 void inet_twsk_purge(struct net *net, struct inet_hashinfo *hashinfo,
422 struct inet_timewait_death_row *twdr, int family)
423 {
424 struct inet_timewait_sock *tw;
425 struct sock *sk;
426 struct hlist_nulls_node *node;
427 int h;
428
429 local_bh_disable();
430 for (h = 0; h < (hashinfo->ehash_size); h++) {
431 struct inet_ehash_bucket *head =
432 inet_ehash_bucket(hashinfo, h);
433 spinlock_t *lock = inet_ehash_lockp(hashinfo, h);
434 restart:
435 spin_lock(lock);
436 sk_nulls_for_each(sk, node, &head->twchain) {
437
438 tw = inet_twsk(sk);
439 if (!net_eq(twsk_net(tw), net) ||
440 tw->tw_family != family)
441 continue;
442
443 atomic_inc(&tw->tw_refcnt);
444 spin_unlock(lock);
445 inet_twsk_deschedule(tw, twdr);
446 inet_twsk_put(tw);
447
448 goto restart;
449 }
450 spin_unlock(lock);
451 }
452 local_bh_enable();
453 }
454 EXPORT_SYMBOL_GPL(inet_twsk_purge);