inet_timewait_sock.c 9.0 KB

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