inet_timewait_sock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * Definitions for a generic INET TIMEWAIT sock
  7. *
  8. * From code originally in net/tcp.h
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _INET_TIMEWAIT_SOCK_
  16. #define _INET_TIMEWAIT_SOCK_
  17. #include <linux/config.h>
  18. #include <linux/ip.h>
  19. #include <linux/list.h>
  20. #include <linux/types.h>
  21. #include <net/sock.h>
  22. #include <net/tcp_states.h>
  23. #include <asm/atomic.h>
  24. #if (BITS_PER_LONG == 64)
  25. #define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 8
  26. #else
  27. #define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 4
  28. #endif
  29. struct inet_bind_bucket;
  30. struct inet_hashinfo;
  31. /*
  32. * This is a TIME_WAIT sock. It works around the memory consumption
  33. * problems of sockets in such a state on heavily loaded servers, but
  34. * without violating the protocol specification.
  35. */
  36. struct inet_timewait_sock {
  37. /*
  38. * Now struct sock also uses sock_common, so please just
  39. * don't add nothing before this first member (__tw_common) --acme
  40. */
  41. struct sock_common __tw_common;
  42. #define tw_family __tw_common.skc_family
  43. #define tw_state __tw_common.skc_state
  44. #define tw_reuse __tw_common.skc_reuse
  45. #define tw_bound_dev_if __tw_common.skc_bound_dev_if
  46. #define tw_node __tw_common.skc_node
  47. #define tw_bind_node __tw_common.skc_bind_node
  48. #define tw_refcnt __tw_common.skc_refcnt
  49. #define tw_prot __tw_common.skc_prot
  50. volatile unsigned char tw_substate;
  51. /* 3 bits hole, try to pack */
  52. unsigned char tw_rcv_wscale;
  53. /* Socket demultiplex comparisons on incoming packets. */
  54. /* these five are in inet_sock */
  55. __u16 tw_sport;
  56. __u32 tw_daddr __attribute__((aligned(INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES)));
  57. __u32 tw_rcv_saddr;
  58. __u16 tw_dport;
  59. __u16 tw_num;
  60. /* And these are ours. */
  61. __u8 tw_ipv6only:1;
  62. /* 31 bits hole, try to pack */
  63. int tw_hashent;
  64. int tw_timeout;
  65. unsigned long tw_ttd;
  66. struct inet_bind_bucket *tw_tb;
  67. struct hlist_node tw_death_node;
  68. };
  69. static inline void inet_twsk_add_node(struct inet_timewait_sock *tw,
  70. struct hlist_head *list)
  71. {
  72. hlist_add_head(&tw->tw_node, list);
  73. }
  74. static inline void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
  75. struct hlist_head *list)
  76. {
  77. hlist_add_head(&tw->tw_bind_node, list);
  78. }
  79. static inline int inet_twsk_dead_hashed(const struct inet_timewait_sock *tw)
  80. {
  81. return tw->tw_death_node.pprev != NULL;
  82. }
  83. static inline void inet_twsk_dead_node_init(struct inet_timewait_sock *tw)
  84. {
  85. tw->tw_death_node.pprev = NULL;
  86. }
  87. static inline void __inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  88. {
  89. __hlist_del(&tw->tw_death_node);
  90. inet_twsk_dead_node_init(tw);
  91. }
  92. static inline int inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  93. {
  94. if (inet_twsk_dead_hashed(tw)) {
  95. __inet_twsk_del_dead_node(tw);
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. #define inet_twsk_for_each(tw, node, head) \
  101. hlist_for_each_entry(tw, node, head, tw_node)
  102. #define inet_twsk_for_each_inmate(tw, node, jail) \
  103. hlist_for_each_entry(tw, node, jail, tw_death_node)
  104. #define inet_twsk_for_each_inmate_safe(tw, node, safe, jail) \
  105. hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
  106. static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
  107. {
  108. return (struct inet_timewait_sock *)sk;
  109. }
  110. static inline u32 inet_rcv_saddr(const struct sock *sk)
  111. {
  112. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  113. inet_sk(sk)->rcv_saddr : inet_twsk(sk)->tw_rcv_saddr;
  114. }
  115. static inline void inet_twsk_put(struct inet_timewait_sock *tw)
  116. {
  117. if (atomic_dec_and_test(&tw->tw_refcnt)) {
  118. #ifdef SOCK_REFCNT_DEBUG
  119. printk(KERN_DEBUG "%s timewait_sock %p released\n",
  120. tw->tw_prot->name, tw);
  121. #endif
  122. kmem_cache_free(tw->tw_prot->twsk_slab, tw);
  123. }
  124. }
  125. extern struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  126. const int state);
  127. extern void __inet_twsk_kill(struct inet_timewait_sock *tw,
  128. struct inet_hashinfo *hashinfo);
  129. extern void __inet_twsk_hashdance(struct inet_timewait_sock *tw,
  130. struct sock *sk,
  131. struct inet_hashinfo *hashinfo);
  132. #endif /* _INET_TIMEWAIT_SOCK_ */