inet_timewait_sock.c 14 KB

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