Leaked source code of windows server 2003
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.

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