inet_timewait_sock.c 14 KB

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