inet_timewait_sock.c 11 KB

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