inet_timewait_sock.c 11 KB

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