inet_timewait_sock.c 14 KB

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