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