inet_timewait_sock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. printk(KERN_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. struct hlist_node *node;
  195. unsigned int killed;
  196. int ret;
  197. /* NOTE: compare this to previous version where lock
  198. * was released after detaching chain. It was racy,
  199. * because tw buckets are scheduled in not serialized context
  200. * in 2.3 (with netfilter), and with softnet it is common, because
  201. * soft irqs are not sequenced.
  202. */
  203. killed = 0;
  204. ret = 0;
  205. rescan:
  206. inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
  207. __inet_twsk_del_dead_node(tw);
  208. spin_unlock(&twdr->death_lock);
  209. __inet_twsk_kill(tw, twdr->hashinfo);
  210. #ifdef CONFIG_NET_NS
  211. NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
  212. #endif
  213. inet_twsk_put(tw);
  214. killed++;
  215. spin_lock(&twdr->death_lock);
  216. if (killed > INET_TWDR_TWKILL_QUOTA) {
  217. ret = 1;
  218. break;
  219. }
  220. /* While we dropped twdr->death_lock, another cpu may have
  221. * killed off the next TW bucket in the list, therefore
  222. * do a fresh re-read of the hlist head node with the
  223. * lock reacquired. We still use the hlist traversal
  224. * macro in order to get the prefetches.
  225. */
  226. goto rescan;
  227. }
  228. twdr->tw_count -= killed;
  229. #ifndef CONFIG_NET_NS
  230. NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
  231. #endif
  232. return ret;
  233. }
  234. void inet_twdr_hangman(unsigned long data)
  235. {
  236. struct inet_timewait_death_row *twdr;
  237. unsigned int need_timer;
  238. twdr = (struct inet_timewait_death_row *)data;
  239. spin_lock(&twdr->death_lock);
  240. if (twdr->tw_count == 0)
  241. goto out;
  242. need_timer = 0;
  243. if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
  244. twdr->thread_slots |= (1 << twdr->slot);
  245. schedule_work(&twdr->twkill_work);
  246. need_timer = 1;
  247. } else {
  248. /* We purged the entire slot, anything left? */
  249. if (twdr->tw_count)
  250. need_timer = 1;
  251. twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
  252. }
  253. if (need_timer)
  254. mod_timer(&twdr->tw_timer, jiffies + twdr->period);
  255. out:
  256. spin_unlock(&twdr->death_lock);
  257. }
  258. EXPORT_SYMBOL_GPL(inet_twdr_hangman);
  259. void inet_twdr_twkill_work(struct work_struct *work)
  260. {
  261. struct inet_timewait_death_row *twdr =
  262. container_of(work, struct inet_timewait_death_row, twkill_work);
  263. int i;
  264. BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
  265. (sizeof(twdr->thread_slots) * 8));
  266. while (twdr->thread_slots) {
  267. spin_lock_bh(&twdr->death_lock);
  268. for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
  269. if (!(twdr->thread_slots & (1 << i)))
  270. continue;
  271. while (inet_twdr_do_twkill_work(twdr, i) != 0) {
  272. if (need_resched()) {
  273. spin_unlock_bh(&twdr->death_lock);
  274. schedule();
  275. spin_lock_bh(&twdr->death_lock);
  276. }
  277. }
  278. twdr->thread_slots &= ~(1 << i);
  279. }
  280. spin_unlock_bh(&twdr->death_lock);
  281. }
  282. }
  283. EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
  284. /* These are always called from BH context. See callers in
  285. * tcp_input.c to verify this.
  286. */
  287. /* This is for handling early-kills of TIME_WAIT sockets. */
  288. void inet_twsk_deschedule(struct inet_timewait_sock *tw,
  289. struct inet_timewait_death_row *twdr)
  290. {
  291. spin_lock(&twdr->death_lock);
  292. if (inet_twsk_del_dead_node(tw)) {
  293. inet_twsk_put(tw);
  294. if (--twdr->tw_count == 0)
  295. del_timer(&twdr->tw_timer);
  296. }
  297. spin_unlock(&twdr->death_lock);
  298. __inet_twsk_kill(tw, twdr->hashinfo);
  299. }
  300. EXPORT_SYMBOL(inet_twsk_deschedule);
  301. void inet_twsk_schedule(struct inet_timewait_sock *tw,
  302. struct inet_timewait_death_row *twdr,
  303. const int timeo, const int timewait_len)
  304. {
  305. struct hlist_head *list;
  306. int slot;
  307. /* timeout := RTO * 3.5
  308. *
  309. * 3.5 = 1+2+0.5 to wait for two retransmits.
  310. *
  311. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  312. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  313. * FINs (or previous seqments) are lost (probability of such event
  314. * is p^(N+1), where p is probability to lose single packet and
  315. * time to detect the loss is about RTO*(2^N - 1) with exponential
  316. * backoff). Normal timewait length is calculated so, that we
  317. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  318. * [ BTW Linux. following BSD, violates this requirement waiting
  319. * only for 60sec, we should wait at least for 240 secs.
  320. * Well, 240 consumes too much of resources 8)
  321. * ]
  322. * This interval is not reduced to catch old duplicate and
  323. * responces to our wandering segments living for two MSLs.
  324. * However, if we use PAWS to detect
  325. * old duplicates, we can reduce the interval to bounds required
  326. * by RTO, rather than MSL. So, if peer understands PAWS, we
  327. * kill tw bucket after 3.5*RTO (it is important that this number
  328. * is greater than TS tick!) and detect old duplicates with help
  329. * of PAWS.
  330. */
  331. slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
  332. spin_lock(&twdr->death_lock);
  333. /* Unlink it, if it was scheduled */
  334. if (inet_twsk_del_dead_node(tw))
  335. twdr->tw_count--;
  336. else
  337. atomic_inc(&tw->tw_refcnt);
  338. if (slot >= INET_TWDR_RECYCLE_SLOTS) {
  339. /* Schedule to slow timer */
  340. if (timeo >= timewait_len) {
  341. slot = INET_TWDR_TWKILL_SLOTS - 1;
  342. } else {
  343. slot = DIV_ROUND_UP(timeo, twdr->period);
  344. if (slot >= INET_TWDR_TWKILL_SLOTS)
  345. slot = INET_TWDR_TWKILL_SLOTS - 1;
  346. }
  347. tw->tw_ttd = jiffies + timeo;
  348. slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
  349. list = &twdr->cells[slot];
  350. } else {
  351. tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
  352. if (twdr->twcal_hand < 0) {
  353. twdr->twcal_hand = 0;
  354. twdr->twcal_jiffie = jiffies;
  355. twdr->twcal_timer.expires = twdr->twcal_jiffie +
  356. (slot << INET_TWDR_RECYCLE_TICK);
  357. add_timer(&twdr->twcal_timer);
  358. } else {
  359. if (time_after(twdr->twcal_timer.expires,
  360. jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
  361. mod_timer(&twdr->twcal_timer,
  362. jiffies + (slot << INET_TWDR_RECYCLE_TICK));
  363. slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
  364. }
  365. list = &twdr->twcal_row[slot];
  366. }
  367. hlist_add_head(&tw->tw_death_node, list);
  368. if (twdr->tw_count++ == 0)
  369. mod_timer(&twdr->tw_timer, jiffies + twdr->period);
  370. spin_unlock(&twdr->death_lock);
  371. }
  372. EXPORT_SYMBOL_GPL(inet_twsk_schedule);
  373. void inet_twdr_twcal_tick(unsigned long data)
  374. {
  375. struct inet_timewait_death_row *twdr;
  376. int n, slot;
  377. unsigned long j;
  378. unsigned long now = jiffies;
  379. int killed = 0;
  380. int adv = 0;
  381. twdr = (struct inet_timewait_death_row *)data;
  382. spin_lock(&twdr->death_lock);
  383. if (twdr->twcal_hand < 0)
  384. goto out;
  385. slot = twdr->twcal_hand;
  386. j = twdr->twcal_jiffie;
  387. for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
  388. if (time_before_eq(j, now)) {
  389. struct hlist_node *node, *safe;
  390. struct inet_timewait_sock *tw;
  391. inet_twsk_for_each_inmate_safe(tw, node, safe,
  392. &twdr->twcal_row[slot]) {
  393. __inet_twsk_del_dead_node(tw);
  394. __inet_twsk_kill(tw, twdr->hashinfo);
  395. #ifdef CONFIG_NET_NS
  396. NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
  397. #endif
  398. inet_twsk_put(tw);
  399. killed++;
  400. }
  401. } else {
  402. if (!adv) {
  403. adv = 1;
  404. twdr->twcal_jiffie = j;
  405. twdr->twcal_hand = slot;
  406. }
  407. if (!hlist_empty(&twdr->twcal_row[slot])) {
  408. mod_timer(&twdr->twcal_timer, j);
  409. goto out;
  410. }
  411. }
  412. j += 1 << INET_TWDR_RECYCLE_TICK;
  413. slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
  414. }
  415. twdr->twcal_hand = -1;
  416. out:
  417. if ((twdr->tw_count -= killed) == 0)
  418. del_timer(&twdr->tw_timer);
  419. #ifndef CONFIG_NET_NS
  420. NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
  421. #endif
  422. spin_unlock(&twdr->death_lock);
  423. }
  424. EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
  425. void inet_twsk_purge(struct inet_hashinfo *hashinfo,
  426. struct inet_timewait_death_row *twdr, int family)
  427. {
  428. struct inet_timewait_sock *tw;
  429. struct sock *sk;
  430. struct hlist_nulls_node *node;
  431. unsigned int slot;
  432. for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
  433. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  434. restart_rcu:
  435. rcu_read_lock();
  436. restart:
  437. sk_nulls_for_each_rcu(sk, node, &head->twchain) {
  438. tw = inet_twsk(sk);
  439. if ((tw->tw_family != family) ||
  440. atomic_read(&twsk_net(tw)->count))
  441. continue;
  442. if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
  443. continue;
  444. if (unlikely((tw->tw_family != family) ||
  445. atomic_read(&twsk_net(tw)->count))) {
  446. inet_twsk_put(tw);
  447. goto restart;
  448. }
  449. rcu_read_unlock();
  450. local_bh_disable();
  451. inet_twsk_deschedule(tw, twdr);
  452. local_bh_enable();
  453. inet_twsk_put(tw);
  454. goto restart_rcu;
  455. }
  456. /* If the nulls value we got at the end of this lookup is
  457. * not the expected one, we must restart lookup.
  458. * We probably met an item that was moved to another chain.
  459. */
  460. if (get_nulls_value(node) != slot)
  461. goto restart;
  462. rcu_read_unlock();
  463. }
  464. }
  465. EXPORT_SYMBOL_GPL(inet_twsk_purge);