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