inet_timewait_sock.c 12 KB

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