inet_timewait_sock.c 13 KB

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