inet_timewait_sock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Generic TIME_WAIT sockets functions
  8. *
  9. * From code orinally in TCP
  10. */
  11. #include <linux/kernel.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_bind2_bucket *tb2 = tw->tw_tb2;
  30. struct inet_bind_bucket *tb = tw->tw_tb;
  31. if (!tb)
  32. return;
  33. __sk_del_bind_node((struct sock *)tw);
  34. tw->tw_tb = NULL;
  35. tw->tw_tb2 = NULL;
  36. inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
  37. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  38. __sock_put((struct sock *)tw);
  39. }
  40. /* Must be called with locally disabled BHs. */
  41. static void inet_twsk_kill(struct inet_timewait_sock *tw)
  42. {
  43. struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
  44. spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
  45. struct inet_bind_hashbucket *bhead, *bhead2;
  46. spin_lock(lock);
  47. sk_nulls_del_node_init_rcu((struct sock *)tw);
  48. spin_unlock(lock);
  49. /* Disassociate with bind bucket. */
  50. bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
  51. hashinfo->bhash_size)];
  52. bhead2 = inet_bhashfn_portaddr(hashinfo, (struct sock *)tw,
  53. twsk_net(tw), tw->tw_num);
  54. spin_lock(&bhead->lock);
  55. spin_lock(&bhead2->lock);
  56. inet_twsk_bind_unhash(tw, hashinfo);
  57. spin_unlock(&bhead2->lock);
  58. spin_unlock(&bhead->lock);
  59. refcount_dec(&tw->tw_dr->tw_refcount);
  60. inet_twsk_put(tw);
  61. }
  62. void inet_twsk_free(struct inet_timewait_sock *tw)
  63. {
  64. struct module *owner = tw->tw_prot->owner;
  65. twsk_destructor((struct sock *)tw);
  66. kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
  67. module_put(owner);
  68. }
  69. void inet_twsk_put(struct inet_timewait_sock *tw)
  70. {
  71. if (refcount_dec_and_test(&tw->tw_refcnt))
  72. inet_twsk_free(tw);
  73. }
  74. EXPORT_SYMBOL_GPL(inet_twsk_put);
  75. static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
  76. struct hlist_nulls_head *list)
  77. {
  78. hlist_nulls_add_head_rcu(&tw->tw_node, list);
  79. }
  80. static void inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo)
  81. {
  82. __inet_twsk_schedule(tw, timeo, false);
  83. }
  84. /*
  85. * Enter the time wait state.
  86. * Essentially we whip up a timewait bucket, copy the relevant info into it
  87. * from the SK, and mess with hash chains and list linkage.
  88. *
  89. * The caller must not access @tw anymore after this function returns.
  90. */
  91. void inet_twsk_hashdance_schedule(struct inet_timewait_sock *tw,
  92. struct sock *sk,
  93. struct inet_hashinfo *hashinfo,
  94. int timeo)
  95. {
  96. const struct inet_sock *inet = inet_sk(sk);
  97. const struct inet_connection_sock *icsk = inet_csk(sk);
  98. struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
  99. spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  100. struct inet_bind_hashbucket *bhead, *bhead2;
  101. /* Step 1: Put TW into bind hash. Original socket stays there too.
  102. Note, that any socket with inet->num != 0 MUST be bound in
  103. binding cache, even if it is closed.
  104. */
  105. bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
  106. hashinfo->bhash_size)];
  107. bhead2 = inet_bhashfn_portaddr(hashinfo, sk, twsk_net(tw), inet->inet_num);
  108. local_bh_disable();
  109. spin_lock(&bhead->lock);
  110. spin_lock(&bhead2->lock);
  111. tw->tw_tb = icsk->icsk_bind_hash;
  112. WARN_ON(!icsk->icsk_bind_hash);
  113. tw->tw_tb2 = icsk->icsk_bind2_hash;
  114. WARN_ON(!icsk->icsk_bind2_hash);
  115. sk_add_bind_node((struct sock *)tw, &tw->tw_tb2->owners);
  116. spin_unlock(&bhead2->lock);
  117. spin_unlock(&bhead->lock);
  118. spin_lock(lock);
  119. /* Step 2: Hash TW into tcp ehash chain */
  120. inet_twsk_add_node_rcu(tw, &ehead->chain);
  121. /* Step 3: Remove SK from hash chain */
  122. if (__sk_nulls_del_node_init_rcu(sk))
  123. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  124. /* Ensure above writes are committed into memory before updating the
  125. * refcount.
  126. * Provides ordering vs later refcount_inc().
  127. */
  128. smp_wmb();
  129. /* tw_refcnt is set to 3 because we have :
  130. * - one reference for bhash chain.
  131. * - one reference for ehash chain.
  132. * - one reference for timer.
  133. * Also note that after this point, we lost our implicit reference
  134. * so we are not allowed to use tw anymore.
  135. */
  136. refcount_set(&tw->tw_refcnt, 3);
  137. inet_twsk_schedule(tw, timeo);
  138. spin_unlock(lock);
  139. local_bh_enable();
  140. }
  141. EXPORT_SYMBOL_GPL(inet_twsk_hashdance_schedule);
  142. static void tw_timer_handler(struct timer_list *t)
  143. {
  144. struct inet_timewait_sock *tw = from_timer(tw, t, tw_timer);
  145. inet_twsk_kill(tw);
  146. }
  147. struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  148. struct inet_timewait_death_row *dr,
  149. const int state)
  150. {
  151. struct inet_timewait_sock *tw;
  152. if (refcount_read(&dr->tw_refcount) - 1 >=
  153. READ_ONCE(dr->sysctl_max_tw_buckets))
  154. return NULL;
  155. tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
  156. GFP_ATOMIC);
  157. if (tw) {
  158. const struct inet_sock *inet = inet_sk(sk);
  159. tw->tw_dr = dr;
  160. /* Give us an identity. */
  161. tw->tw_daddr = inet->inet_daddr;
  162. tw->tw_rcv_saddr = inet->inet_rcv_saddr;
  163. tw->tw_bound_dev_if = sk->sk_bound_dev_if;
  164. tw->tw_tos = inet->tos;
  165. tw->tw_num = inet->inet_num;
  166. tw->tw_state = TCP_TIME_WAIT;
  167. tw->tw_substate = state;
  168. tw->tw_sport = inet->inet_sport;
  169. tw->tw_dport = inet->inet_dport;
  170. tw->tw_family = sk->sk_family;
  171. tw->tw_reuse = sk->sk_reuse;
  172. tw->tw_reuseport = sk->sk_reuseport;
  173. tw->tw_hash = sk->sk_hash;
  174. tw->tw_ipv6only = 0;
  175. tw->tw_transparent = inet_test_bit(TRANSPARENT, sk);
  176. tw->tw_prot = sk->sk_prot_creator;
  177. atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
  178. twsk_net_set(tw, sock_net(sk));
  179. timer_setup(&tw->tw_timer, tw_timer_handler, 0);
  180. /*
  181. * Because we use RCU lookups, we should not set tw_refcnt
  182. * to a non null value before everything is setup for this
  183. * timewait socket.
  184. */
  185. refcount_set(&tw->tw_refcnt, 0);
  186. __module_get(tw->tw_prot->owner);
  187. }
  188. return tw;
  189. }
  190. EXPORT_SYMBOL_GPL(inet_twsk_alloc);
  191. /* These are always called from BH context. See callers in
  192. * tcp_input.c to verify this.
  193. */
  194. /* This is for handling early-kills of TIME_WAIT sockets.
  195. * Warning : consume reference.
  196. * Caller should not access tw anymore.
  197. */
  198. void inet_twsk_deschedule_put(struct inet_timewait_sock *tw)
  199. {
  200. struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
  201. spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
  202. /* inet_twsk_purge() walks over all sockets, including tw ones,
  203. * and removes them via inet_twsk_deschedule_put() after a
  204. * refcount_inc_not_zero().
  205. *
  206. * inet_twsk_hashdance_schedule() must (re)init the refcount before
  207. * arming the timer, i.e. inet_twsk_purge can obtain a reference to
  208. * a twsk that did not yet schedule the timer.
  209. *
  210. * The ehash lock synchronizes these two:
  211. * After acquiring the lock, the timer is always scheduled (else
  212. * timer_shutdown returns false), because hashdance_schedule releases
  213. * the ehash lock only after completing the timer initialization.
  214. *
  215. * Without grabbing the ehash lock, we get:
  216. * 1) cpu x sets twsk refcount to 3
  217. * 2) cpu y bumps refcount to 4
  218. * 3) cpu y calls inet_twsk_deschedule_put() and shuts timer down
  219. * 4) cpu x tries to start timer, but mod_timer is a noop post-shutdown
  220. * -> timer refcount is never decremented.
  221. */
  222. spin_lock(lock);
  223. /* Makes sure hashdance_schedule() has completed */
  224. spin_unlock(lock);
  225. if (timer_shutdown_sync(&tw->tw_timer))
  226. inet_twsk_kill(tw);
  227. inet_twsk_put(tw);
  228. }
  229. EXPORT_SYMBOL(inet_twsk_deschedule_put);
  230. void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
  231. {
  232. /* timeout := RTO * 3.5
  233. *
  234. * 3.5 = 1+2+0.5 to wait for two retransmits.
  235. *
  236. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  237. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  238. * FINs (or previous seqments) are lost (probability of such event
  239. * is p^(N+1), where p is probability to lose single packet and
  240. * time to detect the loss is about RTO*(2^N - 1) with exponential
  241. * backoff). Normal timewait length is calculated so, that we
  242. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  243. * [ BTW Linux. following BSD, violates this requirement waiting
  244. * only for 60sec, we should wait at least for 240 secs.
  245. * Well, 240 consumes too much of resources 8)
  246. * ]
  247. * This interval is not reduced to catch old duplicate and
  248. * responces to our wandering segments living for two MSLs.
  249. * However, if we use PAWS to detect
  250. * old duplicates, we can reduce the interval to bounds required
  251. * by RTO, rather than MSL. So, if peer understands PAWS, we
  252. * kill tw bucket after 3.5*RTO (it is important that this number
  253. * is greater than TS tick!) and detect old duplicates with help
  254. * of PAWS.
  255. */
  256. if (!rearm) {
  257. bool kill = timeo <= 4*HZ;
  258. __NET_INC_STATS(twsk_net(tw), kill ? LINUX_MIB_TIMEWAITKILLED :
  259. LINUX_MIB_TIMEWAITED);
  260. BUG_ON(mod_timer(&tw->tw_timer, jiffies + timeo));
  261. refcount_inc(&tw->tw_dr->tw_refcount);
  262. } else {
  263. mod_timer_pending(&tw->tw_timer, jiffies + timeo);
  264. }
  265. }
  266. EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
  267. /* Remove all non full sockets (TIME_WAIT and NEW_SYN_RECV) for dead netns */
  268. void inet_twsk_purge(struct inet_hashinfo *hashinfo)
  269. {
  270. struct inet_ehash_bucket *head = &hashinfo->ehash[0];
  271. unsigned int ehash_mask = hashinfo->ehash_mask;
  272. struct hlist_nulls_node *node;
  273. unsigned int slot;
  274. struct sock *sk;
  275. for (slot = 0; slot <= ehash_mask; slot++, head++) {
  276. if (hlist_nulls_empty(&head->chain))
  277. continue;
  278. restart_rcu:
  279. cond_resched();
  280. rcu_read_lock();
  281. restart:
  282. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  283. int state = inet_sk_state_load(sk);
  284. if ((1 << state) & ~(TCPF_TIME_WAIT |
  285. TCPF_NEW_SYN_RECV))
  286. continue;
  287. if (refcount_read(&sock_net(sk)->ns.count))
  288. continue;
  289. if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
  290. continue;
  291. if (refcount_read(&sock_net(sk)->ns.count)) {
  292. sock_gen_put(sk);
  293. goto restart;
  294. }
  295. rcu_read_unlock();
  296. local_bh_disable();
  297. if (state == TCP_TIME_WAIT) {
  298. inet_twsk_deschedule_put(inet_twsk(sk));
  299. } else {
  300. struct request_sock *req = inet_reqsk(sk);
  301. inet_csk_reqsk_queue_drop_and_put(req->rsk_listener,
  302. req);
  303. }
  304. local_bh_enable();
  305. goto restart_rcu;
  306. }
  307. /* If the nulls value we got at the end of this lookup is
  308. * not the expected one, we must restart lookup.
  309. * We probably met an item that was moved to another chain.
  310. */
  311. if (get_nulls_value(node) != slot)
  312. goto restart;
  313. rcu_read_unlock();
  314. }
  315. }
  316. EXPORT_SYMBOL_GPL(inet_twsk_purge);