inet_timewait_sock.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/slab.h>
  12. #include <linux/module.h>
  13. #include <net/inet_hashtables.h>
  14. #include <net/inet_timewait_sock.h>
  15. #include <net/ip.h>
  16. /**
  17. * inet_twsk_bind_unhash - unhash a timewait socket from bind hash
  18. * @tw: timewait socket
  19. * @hashinfo: hashinfo pointer
  20. *
  21. * unhash a timewait socket from bind hash, if hashed.
  22. * bind hash lock must be held by caller.
  23. * Returns 1 if caller should call inet_twsk_put() after lock release.
  24. */
  25. void inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
  26. struct inet_hashinfo *hashinfo)
  27. {
  28. struct inet_bind_bucket *tb = tw->tw_tb;
  29. if (!tb)
  30. return;
  31. __hlist_del(&tw->tw_bind_node);
  32. tw->tw_tb = NULL;
  33. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  34. __sock_put((struct sock *)tw);
  35. }
  36. /* Must be called with locally disabled BHs. */
  37. static void inet_twsk_kill(struct inet_timewait_sock *tw)
  38. {
  39. struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
  40. spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
  41. struct inet_bind_hashbucket *bhead;
  42. spin_lock(lock);
  43. sk_nulls_del_node_init_rcu((struct sock *)tw);
  44. spin_unlock(lock);
  45. /* Disassociate with bind bucket. */
  46. bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
  47. hashinfo->bhash_size)];
  48. spin_lock(&bhead->lock);
  49. inet_twsk_bind_unhash(tw, hashinfo);
  50. spin_unlock(&bhead->lock);
  51. atomic_dec(&tw->tw_dr->tw_count);
  52. inet_twsk_put(tw);
  53. }
  54. void inet_twsk_free(struct inet_timewait_sock *tw)
  55. {
  56. struct module *owner = tw->tw_prot->owner;
  57. twsk_destructor((struct sock *)tw);
  58. #ifdef SOCK_REFCNT_DEBUG
  59. pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
  60. #endif
  61. kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
  62. module_put(owner);
  63. }
  64. void inet_twsk_put(struct inet_timewait_sock *tw)
  65. {
  66. if (refcount_dec_and_test(&tw->tw_refcnt))
  67. inet_twsk_free(tw);
  68. }
  69. EXPORT_SYMBOL_GPL(inet_twsk_put);
  70. static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
  71. struct hlist_nulls_head *list)
  72. {
  73. hlist_nulls_add_head_rcu(&tw->tw_node, list);
  74. }
  75. static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
  76. struct hlist_head *list)
  77. {
  78. hlist_add_head(&tw->tw_bind_node, list);
  79. }
  80. /*
  81. * Enter the time wait state. This is called with locally disabled BH.
  82. * Essentially we whip up a timewait bucket, copy the relevant info into it
  83. * from the SK, and mess with hash chains and list linkage.
  84. */
  85. void inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
  86. struct inet_hashinfo *hashinfo)
  87. {
  88. const struct inet_sock *inet = inet_sk(sk);
  89. const struct inet_connection_sock *icsk = inet_csk(sk);
  90. struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
  91. spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  92. struct inet_bind_hashbucket *bhead;
  93. /* Step 1: Put TW into bind hash. Original socket stays there too.
  94. Note, that any socket with inet->num != 0 MUST be bound in
  95. binding cache, even if it is closed.
  96. */
  97. bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
  98. hashinfo->bhash_size)];
  99. spin_lock(&bhead->lock);
  100. tw->tw_tb = icsk->icsk_bind_hash;
  101. WARN_ON(!icsk->icsk_bind_hash);
  102. inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
  103. spin_unlock(&bhead->lock);
  104. spin_lock(lock);
  105. inet_twsk_add_node_rcu(tw, &ehead->chain);
  106. /* Step 3: Remove SK from hash chain */
  107. if (__sk_nulls_del_node_init_rcu(sk))
  108. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  109. spin_unlock(lock);
  110. /* tw_refcnt is set to 3 because we have :
  111. * - one reference for bhash chain.
  112. * - one reference for ehash chain.
  113. * - one reference for timer.
  114. * We can use atomic_set() because prior spin_lock()/spin_unlock()
  115. * committed into memory all tw fields.
  116. * Also note that after this point, we lost our implicit reference
  117. * so we are not allowed to use tw anymore.
  118. */
  119. refcount_set(&tw->tw_refcnt, 3);
  120. }
  121. EXPORT_SYMBOL_GPL(inet_twsk_hashdance);
  122. static void tw_timer_handler(struct timer_list *t)
  123. {
  124. struct inet_timewait_sock *tw = from_timer(tw, t, tw_timer);
  125. if (tw->tw_kill)
  126. __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
  127. else
  128. __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITED);
  129. inet_twsk_kill(tw);
  130. }
  131. struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  132. struct inet_timewait_death_row *dr,
  133. const int state)
  134. {
  135. struct inet_timewait_sock *tw;
  136. if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets)
  137. return NULL;
  138. tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
  139. GFP_ATOMIC);
  140. if (tw) {
  141. const struct inet_sock *inet = inet_sk(sk);
  142. tw->tw_dr = dr;
  143. /* Give us an identity. */
  144. tw->tw_daddr = inet->inet_daddr;
  145. tw->tw_rcv_saddr = inet->inet_rcv_saddr;
  146. tw->tw_bound_dev_if = sk->sk_bound_dev_if;
  147. tw->tw_tos = inet->tos;
  148. tw->tw_num = inet->inet_num;
  149. tw->tw_state = TCP_TIME_WAIT;
  150. tw->tw_substate = state;
  151. tw->tw_sport = inet->inet_sport;
  152. tw->tw_dport = inet->inet_dport;
  153. tw->tw_family = sk->sk_family;
  154. tw->tw_reuse = sk->sk_reuse;
  155. tw->tw_reuseport = sk->sk_reuseport;
  156. tw->tw_hash = sk->sk_hash;
  157. tw->tw_ipv6only = 0;
  158. tw->tw_transparent = inet->transparent;
  159. tw->tw_prot = sk->sk_prot_creator;
  160. atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
  161. twsk_net_set(tw, sock_net(sk));
  162. timer_setup(&tw->tw_timer, tw_timer_handler, TIMER_PINNED);
  163. /*
  164. * Because we use RCU lookups, we should not set tw_refcnt
  165. * to a non null value before everything is setup for this
  166. * timewait socket.
  167. */
  168. refcount_set(&tw->tw_refcnt, 0);
  169. __module_get(tw->tw_prot->owner);
  170. }
  171. return tw;
  172. }
  173. EXPORT_SYMBOL_GPL(inet_twsk_alloc);
  174. /* These are always called from BH context. See callers in
  175. * tcp_input.c to verify this.
  176. */
  177. /* This is for handling early-kills of TIME_WAIT sockets.
  178. * Warning : consume reference.
  179. * Caller should not access tw anymore.
  180. */
  181. void inet_twsk_deschedule_put(struct inet_timewait_sock *tw)
  182. {
  183. if (del_timer_sync(&tw->tw_timer))
  184. inet_twsk_kill(tw);
  185. inet_twsk_put(tw);
  186. }
  187. EXPORT_SYMBOL(inet_twsk_deschedule_put);
  188. void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
  189. {
  190. /* timeout := RTO * 3.5
  191. *
  192. * 3.5 = 1+2+0.5 to wait for two retransmits.
  193. *
  194. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  195. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  196. * FINs (or previous seqments) are lost (probability of such event
  197. * is p^(N+1), where p is probability to lose single packet and
  198. * time to detect the loss is about RTO*(2^N - 1) with exponential
  199. * backoff). Normal timewait length is calculated so, that we
  200. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  201. * [ BTW Linux. following BSD, violates this requirement waiting
  202. * only for 60sec, we should wait at least for 240 secs.
  203. * Well, 240 consumes too much of resources 8)
  204. * ]
  205. * This interval is not reduced to catch old duplicate and
  206. * responces to our wandering segments living for two MSLs.
  207. * However, if we use PAWS to detect
  208. * old duplicates, we can reduce the interval to bounds required
  209. * by RTO, rather than MSL. So, if peer understands PAWS, we
  210. * kill tw bucket after 3.5*RTO (it is important that this number
  211. * is greater than TS tick!) and detect old duplicates with help
  212. * of PAWS.
  213. */
  214. tw->tw_kill = timeo <= 4*HZ;
  215. if (!rearm) {
  216. BUG_ON(mod_timer(&tw->tw_timer, jiffies + timeo));
  217. atomic_inc(&tw->tw_dr->tw_count);
  218. } else {
  219. mod_timer_pending(&tw->tw_timer, jiffies + timeo);
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
  223. void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
  224. {
  225. struct inet_timewait_sock *tw;
  226. struct sock *sk;
  227. struct hlist_nulls_node *node;
  228. unsigned int slot;
  229. for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
  230. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  231. restart_rcu:
  232. cond_resched();
  233. rcu_read_lock();
  234. restart:
  235. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  236. if (sk->sk_state != TCP_TIME_WAIT)
  237. continue;
  238. tw = inet_twsk(sk);
  239. if ((tw->tw_family != family) ||
  240. refcount_read(&twsk_net(tw)->count))
  241. continue;
  242. if (unlikely(!refcount_inc_not_zero(&tw->tw_refcnt)))
  243. continue;
  244. if (unlikely((tw->tw_family != family) ||
  245. refcount_read(&twsk_net(tw)->count))) {
  246. inet_twsk_put(tw);
  247. goto restart;
  248. }
  249. rcu_read_unlock();
  250. local_bh_disable();
  251. inet_twsk_deschedule_put(tw);
  252. local_bh_enable();
  253. goto restart_rcu;
  254. }
  255. /* If the nulls value we got at the end of this lookup is
  256. * not the expected one, we must restart lookup.
  257. * We probably met an item that was moved to another chain.
  258. */
  259. if (get_nulls_value(node) != slot)
  260. goto restart;
  261. rcu_read_unlock();
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(inet_twsk_purge);