inet_timewait_sock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #include <linux/config.h>
  11. #include <net/inet_hashtables.h>
  12. #include <net/inet_timewait_sock.h>
  13. #include <net/ip.h>
  14. /* Must be called with locally disabled BHs. */
  15. void __inet_twsk_kill(struct inet_timewait_sock *tw, struct inet_hashinfo *hashinfo)
  16. {
  17. struct inet_bind_hashbucket *bhead;
  18. struct inet_bind_bucket *tb;
  19. /* Unlink from established hashes. */
  20. struct inet_ehash_bucket *ehead = &hashinfo->ehash[tw->tw_hashent];
  21. write_lock(&ehead->lock);
  22. if (hlist_unhashed(&tw->tw_node)) {
  23. write_unlock(&ehead->lock);
  24. return;
  25. }
  26. __hlist_del(&tw->tw_node);
  27. sk_node_init(&tw->tw_node);
  28. write_unlock(&ehead->lock);
  29. /* Disassociate with bind bucket. */
  30. bhead = &hashinfo->bhash[inet_bhashfn(tw->tw_num, hashinfo->bhash_size)];
  31. spin_lock(&bhead->lock);
  32. tb = tw->tw_tb;
  33. __hlist_del(&tw->tw_bind_node);
  34. tw->tw_tb = NULL;
  35. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  36. spin_unlock(&bhead->lock);
  37. #ifdef SOCK_REFCNT_DEBUG
  38. if (atomic_read(&tw->tw_refcnt) != 1) {
  39. printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
  40. tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
  41. }
  42. #endif
  43. inet_twsk_put(tw);
  44. }
  45. EXPORT_SYMBOL_GPL(__inet_twsk_kill);
  46. /*
  47. * Enter the time wait state. This is called with locally disabled BH.
  48. * Essentially we whip up a timewait bucket, copy the relevant info into it
  49. * from the SK, and mess with hash chains and list linkage.
  50. */
  51. void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
  52. struct inet_hashinfo *hashinfo)
  53. {
  54. const struct inet_sock *inet = inet_sk(sk);
  55. const struct inet_connection_sock *icsk = inet_csk(sk);
  56. struct inet_ehash_bucket *ehead = &hashinfo->ehash[sk->sk_hashent];
  57. struct inet_bind_hashbucket *bhead;
  58. /* Step 1: Put TW into bind hash. Original socket stays there too.
  59. Note, that any socket with inet->num != 0 MUST be bound in
  60. binding cache, even if it is closed.
  61. */
  62. bhead = &hashinfo->bhash[inet_bhashfn(inet->num, hashinfo->bhash_size)];
  63. spin_lock(&bhead->lock);
  64. tw->tw_tb = icsk->icsk_bind_hash;
  65. BUG_TRAP(icsk->icsk_bind_hash);
  66. inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
  67. spin_unlock(&bhead->lock);
  68. write_lock(&ehead->lock);
  69. /* Step 2: Remove SK from established hash. */
  70. if (__sk_del_node_init(sk))
  71. sock_prot_dec_use(sk->sk_prot);
  72. /* Step 3: Hash TW into TIMEWAIT half of established hash table. */
  73. inet_twsk_add_node(tw, &(ehead + hashinfo->ehash_size)->chain);
  74. atomic_inc(&tw->tw_refcnt);
  75. write_unlock(&ehead->lock);
  76. }
  77. EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
  78. struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
  79. {
  80. struct inet_timewait_sock *tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_slab,
  81. SLAB_ATOMIC);
  82. if (tw != NULL) {
  83. const struct inet_sock *inet = inet_sk(sk);
  84. /* Give us an identity. */
  85. tw->tw_daddr = inet->daddr;
  86. tw->tw_rcv_saddr = inet->rcv_saddr;
  87. tw->tw_bound_dev_if = sk->sk_bound_dev_if;
  88. tw->tw_num = inet->num;
  89. tw->tw_state = TCP_TIME_WAIT;
  90. tw->tw_substate = state;
  91. tw->tw_sport = inet->sport;
  92. tw->tw_dport = inet->dport;
  93. tw->tw_family = sk->sk_family;
  94. tw->tw_reuse = sk->sk_reuse;
  95. tw->tw_hashent = sk->sk_hashent;
  96. tw->tw_ipv6only = 0;
  97. tw->tw_prot = sk->sk_prot_creator;
  98. atomic_set(&tw->tw_refcnt, 1);
  99. inet_twsk_dead_node_init(tw);
  100. }
  101. return tw;
  102. }
  103. EXPORT_SYMBOL_GPL(inet_twsk_alloc);
  104. /* Returns non-zero if quota exceeded. */
  105. static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
  106. const int slot)
  107. {
  108. struct inet_timewait_sock *tw;
  109. struct hlist_node *node;
  110. unsigned int killed;
  111. int ret;
  112. /* NOTE: compare this to previous version where lock
  113. * was released after detaching chain. It was racy,
  114. * because tw buckets are scheduled in not serialized context
  115. * in 2.3 (with netfilter), and with softnet it is common, because
  116. * soft irqs are not sequenced.
  117. */
  118. killed = 0;
  119. ret = 0;
  120. rescan:
  121. inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
  122. __inet_twsk_del_dead_node(tw);
  123. spin_unlock(&twdr->death_lock);
  124. __inet_twsk_kill(tw, twdr->hashinfo);
  125. inet_twsk_put(tw);
  126. killed++;
  127. spin_lock(&twdr->death_lock);
  128. if (killed > INET_TWDR_TWKILL_QUOTA) {
  129. ret = 1;
  130. break;
  131. }
  132. /* While we dropped twdr->death_lock, another cpu may have
  133. * killed off the next TW bucket in the list, therefore
  134. * do a fresh re-read of the hlist head node with the
  135. * lock reacquired. We still use the hlist traversal
  136. * macro in order to get the prefetches.
  137. */
  138. goto rescan;
  139. }
  140. twdr->tw_count -= killed;
  141. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
  142. return ret;
  143. }
  144. void inet_twdr_hangman(unsigned long data)
  145. {
  146. struct inet_timewait_death_row *twdr;
  147. int unsigned need_timer;
  148. twdr = (struct inet_timewait_death_row *)data;
  149. spin_lock(&twdr->death_lock);
  150. if (twdr->tw_count == 0)
  151. goto out;
  152. need_timer = 0;
  153. if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
  154. twdr->thread_slots |= (1 << twdr->slot);
  155. mb();
  156. schedule_work(&twdr->twkill_work);
  157. need_timer = 1;
  158. } else {
  159. /* We purged the entire slot, anything left? */
  160. if (twdr->tw_count)
  161. need_timer = 1;
  162. }
  163. twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
  164. if (need_timer)
  165. mod_timer(&twdr->tw_timer, jiffies + twdr->period);
  166. out:
  167. spin_unlock(&twdr->death_lock);
  168. }
  169. EXPORT_SYMBOL_GPL(inet_twdr_hangman);
  170. extern void twkill_slots_invalid(void);
  171. void inet_twdr_twkill_work(void *data)
  172. {
  173. struct inet_timewait_death_row *twdr = data;
  174. int i;
  175. if ((INET_TWDR_TWKILL_SLOTS - 1) > (sizeof(twdr->thread_slots) * 8))
  176. twkill_slots_invalid();
  177. while (twdr->thread_slots) {
  178. spin_lock_bh(&twdr->death_lock);
  179. for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
  180. if (!(twdr->thread_slots & (1 << i)))
  181. continue;
  182. while (inet_twdr_do_twkill_work(twdr, i) != 0) {
  183. if (need_resched()) {
  184. spin_unlock_bh(&twdr->death_lock);
  185. schedule();
  186. spin_lock_bh(&twdr->death_lock);
  187. }
  188. }
  189. twdr->thread_slots &= ~(1 << i);
  190. }
  191. spin_unlock_bh(&twdr->death_lock);
  192. }
  193. }
  194. EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
  195. /* These are always called from BH context. See callers in
  196. * tcp_input.c to verify this.
  197. */
  198. /* This is for handling early-kills of TIME_WAIT sockets. */
  199. void inet_twsk_deschedule(struct inet_timewait_sock *tw,
  200. struct inet_timewait_death_row *twdr)
  201. {
  202. spin_lock(&twdr->death_lock);
  203. if (inet_twsk_del_dead_node(tw)) {
  204. inet_twsk_put(tw);
  205. if (--twdr->tw_count == 0)
  206. del_timer(&twdr->tw_timer);
  207. }
  208. spin_unlock(&twdr->death_lock);
  209. __inet_twsk_kill(tw, twdr->hashinfo);
  210. }
  211. EXPORT_SYMBOL(inet_twsk_deschedule);
  212. void inet_twsk_schedule(struct inet_timewait_sock *tw,
  213. struct inet_timewait_death_row *twdr,
  214. const int timeo, const int timewait_len)
  215. {
  216. struct hlist_head *list;
  217. int slot;
  218. /* timeout := RTO * 3.5
  219. *
  220. * 3.5 = 1+2+0.5 to wait for two retransmits.
  221. *
  222. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  223. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  224. * FINs (or previous seqments) are lost (probability of such event
  225. * is p^(N+1), where p is probability to lose single packet and
  226. * time to detect the loss is about RTO*(2^N - 1) with exponential
  227. * backoff). Normal timewait length is calculated so, that we
  228. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  229. * [ BTW Linux. following BSD, violates this requirement waiting
  230. * only for 60sec, we should wait at least for 240 secs.
  231. * Well, 240 consumes too much of resources 8)
  232. * ]
  233. * This interval is not reduced to catch old duplicate and
  234. * responces to our wandering segments living for two MSLs.
  235. * However, if we use PAWS to detect
  236. * old duplicates, we can reduce the interval to bounds required
  237. * by RTO, rather than MSL. So, if peer understands PAWS, we
  238. * kill tw bucket after 3.5*RTO (it is important that this number
  239. * is greater than TS tick!) and detect old duplicates with help
  240. * of PAWS.
  241. */
  242. slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
  243. spin_lock(&twdr->death_lock);
  244. /* Unlink it, if it was scheduled */
  245. if (inet_twsk_del_dead_node(tw))
  246. twdr->tw_count--;
  247. else
  248. atomic_inc(&tw->tw_refcnt);
  249. if (slot >= INET_TWDR_RECYCLE_SLOTS) {
  250. /* Schedule to slow timer */
  251. if (timeo >= timewait_len) {
  252. slot = INET_TWDR_TWKILL_SLOTS - 1;
  253. } else {
  254. slot = (timeo + twdr->period - 1) / twdr->period;
  255. if (slot >= INET_TWDR_TWKILL_SLOTS)
  256. slot = INET_TWDR_TWKILL_SLOTS - 1;
  257. }
  258. tw->tw_ttd = jiffies + timeo;
  259. slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
  260. list = &twdr->cells[slot];
  261. } else {
  262. tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
  263. if (twdr->twcal_hand < 0) {
  264. twdr->twcal_hand = 0;
  265. twdr->twcal_jiffie = jiffies;
  266. twdr->twcal_timer.expires = twdr->twcal_jiffie +
  267. (slot << INET_TWDR_RECYCLE_TICK);
  268. add_timer(&twdr->twcal_timer);
  269. } else {
  270. if (time_after(twdr->twcal_timer.expires,
  271. jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
  272. mod_timer(&twdr->twcal_timer,
  273. jiffies + (slot << INET_TWDR_RECYCLE_TICK));
  274. slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
  275. }
  276. list = &twdr->twcal_row[slot];
  277. }
  278. hlist_add_head(&tw->tw_death_node, list);
  279. if (twdr->tw_count++ == 0)
  280. mod_timer(&twdr->tw_timer, jiffies + twdr->period);
  281. spin_unlock(&twdr->death_lock);
  282. }
  283. EXPORT_SYMBOL_GPL(inet_twsk_schedule);
  284. void inet_twdr_twcal_tick(unsigned long data)
  285. {
  286. struct inet_timewait_death_row *twdr;
  287. int n, slot;
  288. unsigned long j;
  289. unsigned long now = jiffies;
  290. int killed = 0;
  291. int adv = 0;
  292. twdr = (struct inet_timewait_death_row *)data;
  293. spin_lock(&twdr->death_lock);
  294. if (twdr->twcal_hand < 0)
  295. goto out;
  296. slot = twdr->twcal_hand;
  297. j = twdr->twcal_jiffie;
  298. for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
  299. if (time_before_eq(j, now)) {
  300. struct hlist_node *node, *safe;
  301. struct inet_timewait_sock *tw;
  302. inet_twsk_for_each_inmate_safe(tw, node, safe,
  303. &twdr->twcal_row[slot]) {
  304. __inet_twsk_del_dead_node(tw);
  305. __inet_twsk_kill(tw, twdr->hashinfo);
  306. inet_twsk_put(tw);
  307. killed++;
  308. }
  309. } else {
  310. if (!adv) {
  311. adv = 1;
  312. twdr->twcal_jiffie = j;
  313. twdr->twcal_hand = slot;
  314. }
  315. if (!hlist_empty(&twdr->twcal_row[slot])) {
  316. mod_timer(&twdr->twcal_timer, j);
  317. goto out;
  318. }
  319. }
  320. j += 1 << INET_TWDR_RECYCLE_TICK;
  321. slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
  322. }
  323. twdr->twcal_hand = -1;
  324. out:
  325. if ((twdr->tw_count -= killed) == 0)
  326. del_timer(&twdr->tw_timer);
  327. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
  328. spin_unlock(&twdr->death_lock);
  329. }
  330. EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);