inet_timewait_sock.c 9.5 KB

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