Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
16 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // Transmission Control Protocol definitions.
  14. //
  15. #ifndef _TCP_INCLUDED_
  16. #define _TCP_INCLUDED_
  17. #define IP_PROTOCOL_TCP 6
  18. #define DEFAULT_MSS (IPv6_MINIMUM_MTU - sizeof(IPv6Header) - sizeof(TCPHeader))
  19. // Timer stuff. We keep timers as ticks.
  20. #define MS_PER_TICK 100
  21. #define MS_TO_TICKS(m) ((m) / MS_PER_TICK)
  22. #define MIN_RETRAN_TICKS 3
  23. #define DEL_ACK_TICKS 2
  24. // Define MAX_REXMIT_TO to be number of ticks in 2MSL (=240 seconds)
  25. #define MAX_REXMIT_TO ((ushort)FinWait2TO)
  26. #define SWS_TO MS_TO_TICKS(5000)
  27. #define FIN_WAIT2_TO 240
  28. #define PUSH_TO MS_TO_TICKS(500)
  29. #define TCP_MD5_DATA_LENGTH 44
  30. typedef ulong TCP_TIME;
  31. #define MAX_CONN_TO_TICKS 0xffff
  32. #define INFINITE_CONN_TO(t) ((t) == 0)
  33. #define TCP_TIME_TO_TICKS(t) (((t)/MS_PER_TICK)+1)
  34. // Sequence numbers are kept as signed 32 bit quantities, with macros
  35. // defined to do wraparound comparisons on them.
  36. typedef int SeqNum; // A sequence number.
  37. //* Macros for comparions on sequence numbers.
  38. #define SEQ_GT(a, b) (((a) - (b)) > 0)
  39. #define SEQ_GTE(a, b) (((a) - (b)) >= 0)
  40. #define SEQ_LT(a, b) (((a) - (b)) < 0)
  41. #define SEQ_LTE(a, b) (((a) - (b)) <= 0)
  42. #define SEQ_EQ(a, b) ((a) == (b))
  43. // The TCB - transport control block structure. This is the
  44. // structure that contains all of the state for the transport
  45. // connection, including sequence numbers, flow control information,
  46. // pending sends and receives, etc.
  47. #define tcb_signature 0x20424354 // 'TCB '
  48. typedef struct TCB {
  49. struct TCB *tcb_next; // Next pointer in TCB table.
  50. #if DBG
  51. ulong tcb_sig; // Debug signature.
  52. #endif
  53. KSPIN_LOCK tcb_lock;
  54. // Send sequence variables.
  55. SeqNum tcb_senduna; // Sequence number of first unack'd data.
  56. SeqNum tcb_sendnext; // Sequence number of next byte to send.
  57. SeqNum tcb_sendmax; // Max value of sendnext this epoch.
  58. uint tcb_sendwin; // Send window.
  59. uint tcb_unacked; // Total number of bytes of unacked data.
  60. uint tcb_maxwin; // Max send window seen.
  61. uint tcb_cwin; // Congestion window.
  62. uint tcb_ssthresh; // Slow start threshold.
  63. uint tcb_phxsum; // Precomputed pseudo-header xsum.
  64. struct TCPSendReq *tcb_cursend; // Current send in use.
  65. PNDIS_BUFFER tcb_sendbuf; // Current buffer chain being sent.
  66. uint tcb_sendofs; // Offset into start of chain.
  67. uint tcb_sendsize; // Number of bytes unsent in current send.
  68. Queue tcb_sendq; // Queue of send requests.
  69. // Receive sequence variables.
  70. SeqNum tcb_rcvnext; // Next byte we expect to receive.
  71. int tcb_rcvwin; // Receive window we're offering.
  72. SeqNum tcb_rcvwinwatch; // Monitors peer's use of our rcv window.
  73. SeqNum tcb_sendwl1; // Window update sequence number.
  74. SeqNum tcb_sendwl2; // Window update ack number.
  75. struct TCPRcvReq *tcb_currcv; // Current receive buffer.
  76. uint tcb_indicated; // Bytes of data indicated.
  77. uint tcb_flags; // Flags for this TCB.
  78. uint tcb_fastchk; // Fast receive path check field.
  79. uint (*tcb_rcvhndlr)(struct TCB *, uint, IPv6Packet *, uint Size);
  80. // Addressing info.
  81. // NOTE: Do not make the [next 6] invariants non-consecutive. That would
  82. // break the MD5 computation.
  83. union {
  84. struct {
  85. // Addressing info.
  86. IPv6Addr tcb_daddr; // Destination IP address (i.e. our peer's address).
  87. IPv6Addr tcb_saddr; // Source IP address (i.e. one our our addresses).
  88. ulong tcb_dscope_id; // Scope id of destination address (0 if non-scoped).
  89. ulong tcb_sscope_id; // Scope id of source address (0 if non-scoped).
  90. ushort tcb_dport; // Destination port.
  91. ushort tcb_sport; // Source port.
  92. };
  93. uchar tcb_md5data[TCP_MD5_DATA_LENGTH];
  94. };
  95. int tcb_hops; // Hop limit.
  96. uint tcb_refcnt; // Reference count for TCB.
  97. SeqNum tcb_rttseq; // Sequence number being measured for Round Trip Time.
  98. // Retransmit timer information. These are stored as ticks, where by
  99. // default each tick is 100ms.
  100. ushort tcb_smrtt; // Smoothed rtt value.
  101. ushort tcb_delta; // Delta value.
  102. ushort tcb_rexmit; // Retransmit value.
  103. uchar tcb_slowcount; // Count of reasons why we're on the slow path.
  104. uchar tcb_pushtimer; // The 'push' timer.
  105. ushort tcb_mss; // Maximum Segment Size for this connection.
  106. ushort tcb_remmss; // MSS advertised by peer.
  107. // State information.
  108. uchar tcb_state; // State of this TCB.
  109. uchar tcb_rexmitcnt; // Count of rexmits on this TCB.
  110. uchar tcb_pending; // Pending actions on this TCB.
  111. uchar tcb_kacount; // Count of keep alive probes sent.
  112. IP_STATUS tcb_error; // Last error we heard about from IP.
  113. uint tcb_rtt; // Current round trip time TS.
  114. ushort tcb_rexmittimer; // Timer for rexmit.
  115. ushort tcb_delacktimer; // Timer for delayed ack.
  116. uint tcb_defaultwin; // Default rcv. window.
  117. uint tcb_alive; // Keep alive time value.
  118. struct TCPRAHdr *tcb_raq; // Reassembly queue.
  119. struct TCPRcvReq *tcb_rcvhead; // Head of recv. buffer queue.
  120. struct TCPRcvReq *tcb_rcvtail; // Tail of recv. buffer queue.
  121. uint tcb_pendingcnt; // Bytes waiting to be received.
  122. IPv6Packet *tcb_pendhead; // Head of pending receive queue.
  123. IPv6Packet *tcb_pendtail; // Tail of pending receive queue.
  124. struct TCPConnReq *tcb_connreq; // Connection-type request for this connection.
  125. void *tcb_conncontext; // Connection context for this connection.
  126. uint tcb_bcountlow; // Low part of byte count.
  127. uint tcb_bcounthi; // High part of bytecount.
  128. uint tcb_totaltime; // Total number of ticks spent sending.
  129. struct TCB *tcb_aonext; // Next pointer on AddrObj.
  130. struct TCPConn *tcb_conn; // Back pointer to conn for TCB.
  131. Queue tcb_delayq; // Queue linkage for delay queue.
  132. uchar tcb_closereason; // Reason we're closing.
  133. uchar tcb_bhprobecnt; // BH probe count.
  134. ushort tcb_swstimer; // Timer for SWS override.
  135. void *tcb_rcvind; // Receive indication handler.
  136. void *tcb_ricontext; // Receive indication context.
  137. // Miscellaneous info, for IP.
  138. RouteCacheEntry *tcb_rce; // RCE for this connection.
  139. uint tcb_pmtu; // So we know when RCE's PTMU changes.
  140. ulong tcb_security; // So we know when IPsec changes.
  141. struct TCPConnReq *tcb_discwait; // Disc-Wait req., if there is one.
  142. struct TCPRcvReq *tcb_exprcv; // Head of expedited recv. buffer queue.
  143. IPv6Packet *tcb_urgpending; // Urgent data queue.
  144. uint tcb_urgcnt; // Byte count of data on urgent q.
  145. uint tcb_urgind; // Urgent bytes indicated.
  146. SeqNum tcb_urgstart; // Start of urgent data.
  147. SeqNum tcb_urgend; // End of urgent data.
  148. uint tcb_walkcount; // Number of people 'walking' this TCB.
  149. uint tcb_connid; // Cached identifier for this TCB's Conn.
  150. ushort tcb_dupacks; // Number of duplicate acks seen.
  151. ushort tcb_force; // Force send.
  152. } TCB;
  153. //
  154. // Definitions for TCP states.
  155. //
  156. #define TCB_CLOSED 0 // Closed.
  157. #define TCB_LISTEN 1 // Listening.
  158. #define TCB_SYN_SENT 2 // SYN Sent.
  159. #define TCB_SYN_RCVD 3 // SYN received.
  160. #define TCB_ESTAB 4 // Established.
  161. #define TCB_FIN_WAIT1 5 // FIN-WAIT-1
  162. #define TCB_FIN_WAIT2 6 // FIN-WAIT-2
  163. #define TCB_CLOSE_WAIT 7 // Close waiting.
  164. #define TCB_CLOSING 8 // Closing state.
  165. #define TCB_LAST_ACK 9 // Last ack state.
  166. #define TCB_TIME_WAIT 10 // Time wait state.
  167. #define SYNC_STATE(s) ((s) > TCB_SYN_RCVD)
  168. #define GRACEFUL_CLOSED_STATE(s) ((s) >= TCB_LAST_ACK)
  169. #define DATA_RCV_STATE(s) ((s) >= TCB_ESTAB && (s) <= TCB_FIN_WAIT2)
  170. #define DATA_SEND_STATE(s) ((s) == TCB_ESTAB || (s) == TCB_CLOSE_WAIT)
  171. //
  172. // Definitions for TCB flags.
  173. //
  174. #define WINDOW_SET 0x00000001 // Window explictly set.
  175. #define CLIENT_OPTIONS 0x00000002 // Have client IP options on conn.
  176. #define CONN_ACCEPTED 0x00000004 // Connection was accepted.
  177. #define ACTIVE_OPEN 0x00000008 // Connection came from an active open.
  178. #define DISC_NOTIFIED 0x00000010 // Client's been notified of a disconnect.
  179. #define IN_DELAY_Q 0x00000020 // We're in the delayed action Q.
  180. #define RCV_CMPLTING 0x00000040 // We're completeing rcvs.
  181. #define IN_RCV_IND 0x00000080 // We're calling a rcv. indicate handler.
  182. #define NEED_RCV_CMPLT 0x00000100 // We need to have recvs. completed.
  183. #define NEED_ACK 0x00000200 // We need to send an ACK.
  184. #define NEED_OUTPUT 0x00000400 // We need to output.
  185. #define DELAYED_FLAGS (NEED_RCV_CMPLT | NEED_ACK | NEED_OUTPUT)
  186. #define ACK_DELAYED 0x00000800 // We've delayed sending an ACK.
  187. #define PMTU_BH_PROBE 0x00001000 // We're probing for a PMTU BH.
  188. #define BSD_URGENT 0x00002000 // We're using BSD urgent semantics.
  189. #define IN_DELIV_URG 0x00004000 // We're in the DeliverUrgent routine.
  190. #define URG_VALID 0x00008000 // Seen urgent data, and fields are valid.
  191. #define FIN_NEEDED 0x00010000 // We need to send a FIN.
  192. #define NAGLING 0x00020000 // We are using Nagle's algorithm.
  193. #define IN_TCP_SEND 0x00040000 // We're in TCPSend.
  194. #define FLOW_CNTLD 0x00080000 // We've received a zero window from peer.
  195. #define DISC_PENDING 0x00100000 // A disconnect notification is pending.
  196. #define TW_PENDING 0x00200000 // Waiting to finish going to TIME-WAIT.
  197. #define FORCE_OUTPUT 0x00400000 // Output is being forced.
  198. #define FORCE_OUT_SHIFT 22 // Shift to get FORCE_OUTPUT into low bit.
  199. #define SEND_AFTER_RCV 0x00800000 // Need to send after we get out of recv.
  200. #define GC_PENDING 0x01000000 // A graceful close is pending.
  201. #define KEEPALIVE 0x02000000 // Doing keepalives on this TCB.
  202. #define URG_INLINE 0x04000000 // Urgent data to be processed inline.
  203. #define ACCEPT_PENDING 0x08000000 // Sent SYN-ACK before indicating to ULP.
  204. #define FIN_OUTSTANDING 0x10000000 // We've sent a FIN 'recently', i.e.
  205. // since the last retransmit. When
  206. // this flag is set sendnext == sendmax.
  207. #define FIN_OUTS_SHIFT 28 // Shift to FIN_OUTSTANDING bit into low bit.
  208. #define FIN_SENT 0x20000000 // We've sent a FIN that hasn't been ack'd.
  209. // Once this bit has been turned on in
  210. // FIN-WAIT-1 the sequence number of the
  211. // FIN will be sendmax-1.
  212. #define NEED_RST 0x40000000 // We need to send a RST when closing.
  213. #define IN_TCB_TABLE 0x80000000 // TCB is in the TCB table.
  214. //
  215. // The defintion of the 'slow flags'.
  216. // If any of these flags are set we'll be forced off of the fast path.
  217. #define TCP_SLOW_FLAGS (URG_VALID | FLOW_CNTLD | GC_PENDING | TW_PENDING | \
  218. DISC_NOTIFIED | IN_DELIV_URG | FIN_NEEDED | \
  219. FIN_SENT | FIN_OUTSTANDING | DISC_PENDING | \
  220. PMTU_BH_PROBE)
  221. //
  222. // Close reasons.
  223. //
  224. #define TCB_CLOSE_RST 0x80 // Received a RST segment.
  225. #define TCB_CLOSE_ABORTED 0x40 // Had a local abort.
  226. #define TCB_CLOSE_TIMEOUT 0x20 // Connection timed out.
  227. #define TCB_CLOSE_REFUSED 0x10 // Connect attempt was refused.
  228. #define TCB_CLOSE_UNREACH 0x08 // Remote destination unreachable.
  229. #define TCB_CLOSE_SUCCESS 0x01 // Successfull close.
  230. //
  231. // TCB Timer macros.
  232. //
  233. #define START_TCB_TIMER(t, v) (t) = (v)
  234. #define STOP_TCB_TIMER(t) (t) = 0
  235. #define TCB_TIMER_RUNNING(t) ((t) != 0)
  236. // Macro to compute retransmit timeout.
  237. #define REXMIT_TO(t) ((((t)->tcb_smrtt >> 2) + (t)->tcb_delta) >> 1)
  238. //
  239. // Definitons for pending actions. We define a PENDING_ACTION macro that can
  240. // be used to decide whether or not we can proceed with an activity. The only
  241. // pending action we really care about is DELETE - others are low priority and
  242. // can be put off.
  243. //
  244. #define PENDING_ACTION(t) ((t)->tcb_pending & DEL_PENDING)
  245. #define DEL_PENDING 0x01 // Delete is pending.
  246. #define OPT_PENDING 0x02 // Option set is pending.
  247. // Macro to see if a TCB is closing.
  248. #define CLOSING(t) ((t)->tcb_pending & DEL_PENDING)
  249. //
  250. // Structure of a TCP packet header.
  251. //
  252. typedef struct TCPHeader {
  253. ushort tcp_src; // Source port.
  254. ushort tcp_dest; // Destination port.
  255. SeqNum tcp_seq; // Sequence number.
  256. SeqNum tcp_ack; // Ack number.
  257. ushort tcp_flags; // Flags and data offset.
  258. ushort tcp_window; // Window offered.
  259. ushort tcp_xsum; // Checksum.
  260. ushort tcp_urgent; // Urgent pointer.
  261. } TCPHeader;
  262. //
  263. // Definitions for TCP header flags.
  264. //
  265. #define TCP_FLAG_FIN 0x00000100
  266. #define TCP_FLAG_SYN 0x00000200
  267. #define TCP_FLAG_RST 0x00000400
  268. #define TCP_FLAG_PUSH 0x00000800
  269. #define TCP_FLAG_ACK 0x00001000
  270. #define TCP_FLAG_URG 0x00002000
  271. #define TCP_FLAGS_ALL (TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | \
  272. TCP_FLAG_ACK | TCP_FLAG_URG)
  273. //
  274. // Flags in the tcb_fastchk field that are not in the TCP header proper.
  275. // Setting these flags forces us off the fast path.
  276. //
  277. #define TCP_FLAG_SLOW 0x00000001 // Need to be on slow path.
  278. #define TCP_FLAG_IN_RCV 0x00000002 // In recv. path already.
  279. #define TCP_OFFSET_MASK 0xf0
  280. #define TCP_HDR_SIZE(t) (uint)(((*(uchar *)&(t)->tcp_flags) & TCP_OFFSET_MASK) >> 2)
  281. #define MAKE_TCP_FLAGS(o, f) ((f) | ((o) << 4))
  282. //
  283. // TCP Option Identifiers.
  284. //
  285. #define TCP_OPT_EOL 0
  286. #define TCP_OPT_NOP 1
  287. #define TCP_OPT_MSS 2
  288. #define MSS_OPT_SIZE 4
  289. //
  290. // Convenient byte swapped structure for receives.
  291. //
  292. typedef struct TCPRcvInfo {
  293. SeqNum tri_seq; // Sequence number.
  294. SeqNum tri_ack; // Ack number.
  295. uint tri_window; // Window.
  296. uint tri_urgent; // Urgent pointer.
  297. uint tri_flags; // Flags.
  298. } TCPRcvInfo;
  299. //
  300. // General structure, at the start of all command specific request structures.
  301. //
  302. #define tr_signature 0x20205254 // 'TR '
  303. typedef struct TCPReq {
  304. struct Queue tr_q; // Q linkage.
  305. #if DBG
  306. ulong tr_sig;
  307. #endif
  308. RequestCompleteRoutine tr_rtn; // Completion routine.
  309. PVOID tr_context; // User context.
  310. int tr_status; // Final complete status.
  311. } TCPReq;
  312. #define TCP6_TAG '6PCT'
  313. #ifdef POOL_TAGGING
  314. #ifdef ExAllocatePool
  315. #undef ExAllocatePool
  316. #endif
  317. #define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, TCP6_TAG)
  318. #endif // POOL_TAGGING
  319. //
  320. // TCP endpoint context structure allocated for each open of TCP/UDP.
  321. // A pointer to this structure is stored in FileObject->FsContext.
  322. //
  323. typedef struct _TCP_CONTEXT {
  324. union {
  325. HANDLE AddressHandle;
  326. CONNECTION_CONTEXT ConnectionContext;
  327. HANDLE ControlChannel;
  328. } Handle;
  329. ULONG ReferenceCount;
  330. BOOLEAN CancelIrps;
  331. KSPIN_LOCK EndpointLock;
  332. #if DBG
  333. LIST_ENTRY PendingIrpList;
  334. LIST_ENTRY CancelledIrpList;
  335. #endif
  336. KEVENT CleanupEvent;
  337. } TCP_CONTEXT, *PTCP_CONTEXT;
  338. #include "tcpdeb.h"
  339. #endif // _TCP_INCLUDED_