tcp.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. tcp.h
  5. Abstract:
  6. This header contains internal definitions for the TCP implementation.
  7. Author:
  8. Evan Green 10-Apr-2013
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. //
  14. // --------------------------------------------------------------------- Macros
  15. //
  16. //
  17. // This macro evaluates whether two sequence numbers are in descending order,
  18. // taking wrapping into account.
  19. //
  20. #define TCP_SEQUENCE_GREATER_THAN(_Sequence1, _Sequence2) \
  21. (((LONG)((_Sequence1) - (_Sequence2))) > 0)
  22. //
  23. // This macro evaluates whether two sequence numbers are in ascending order,
  24. // taking wrapping into account.
  25. //
  26. #define TCP_SEQUENCE_LESS_THAN(_Sequence1, _Sequence2) \
  27. (((LONG)((_Sequence1) - (_Sequence2))) < 0)
  28. //
  29. // This macro updates the socket's retry expiration end time and increments the
  30. // retry wait period.
  31. //
  32. #define TCP_UPDATE_RETRY_TIME(_Socket) \
  33. (_Socket)->RetryTime = KeGetRecentTimeCounter(); \
  34. (_Socket)->RetryTime += KeConvertMicrosecondsToTimeTicks( \
  35. (_Socket)->RetryWaitPeriod * \
  36. MICROSECONDS_PER_MILLISECOND); \
  37. \
  38. (_Socket)->RetryWaitPeriod *= 2;
  39. //
  40. // This macro sets the default timeout expiration time in the socket.
  41. //
  42. #define TCP_SET_DEFAULT_TIMEOUT(_Socket) \
  43. (_Socket)->TimeoutEnd = KeGetRecentTimeCounter() + \
  44. (HlQueryTimeCounterFrequency() * \
  45. TCP_DEFAULT_TIMEOUT);
  46. //
  47. // This macro determines whether or not the TCP state is a SYN retry state.
  48. //
  49. #define TCP_IS_SYN_RETRY_STATE(_TcpState) \
  50. (((_TcpState) == TcpStateSynSent) || \
  51. ((_TcpState) == TcpStateSynReceived))
  52. //
  53. // This macro determins whether or not the TCP state is a FIN retry state.
  54. //
  55. #define TCP_IS_FIN_RETRY_STATE(_TcpState) \
  56. (((_TcpState) == TcpStateFinWait1) || \
  57. ((_TcpState) == TcpStateClosing) || \
  58. ((_TcpState) == TcpStateLastAcknowledge))
  59. //
  60. // This macro determines whether or not the TCP state is a keep alive state.
  61. //
  62. #define TCP_IS_KEEP_ALIVE_STATE(_TcpState) \
  63. (((_TcpState) == TcpStateEstablished) || \
  64. ((_TcpState) == TcpStateFinWait2) || \
  65. ((_TcpState) == TcpStateCloseWait))
  66. //
  67. // ---------------------------------------------------------------- Definitions
  68. //
  69. //
  70. // Define the allocation tag used by the TCP socket protocol.
  71. //
  72. #define TCP_ALLOCATION_TAG 0x21706354 // '!pcT'
  73. //
  74. // Define the default maximum segment size, in bytes.
  75. //
  76. #define TCP_DEFAULT_MAX_SEGMENT_SIZE 576
  77. //
  78. // Define the initial default round trip time, in milliseconds.
  79. //
  80. #define TCP_DEFAULT_ROUND_TRIP_TIME MILLISECONDS_PER_SECOND
  81. #define TCP_ROUND_TRIP_TIMEOUT_FACTOR 2
  82. //
  83. // Define the numerator and denominator for the fraction of the new round trip
  84. // sample that is added to the estimate. The spec indicates that this should be
  85. // somewhere betwen 0.1 and 0.2. Using a denominator that's a power of 2 means
  86. // the compiler can optimize this to a shift.
  87. //
  88. #define TCP_ROUND_TRIP_SAMPLE_NUMERATOR 2
  89. #define TCP_ROUND_TRIP_SAMPLE_DENOMINATOR 16
  90. //
  91. // Define TCP's periodic timer interval, in microseconds.
  92. //
  93. #define TCP_TIMER_PERIOD (250 * MICROSECONDS_PER_MILLISECOND)
  94. //
  95. // Define the length in seconds of the default timeout. This is used as a
  96. // timeout in the time-wait state and when waiting for a SYN or FIN to be
  97. // acknowledges.
  98. //
  99. #define TCP_DEFAULT_TIMEOUT 60
  100. //
  101. // Define the amount of time to wait (in milliseconds) before resending any
  102. // packet, whether it be a zero window probe, SYN, or FIN.
  103. //
  104. #define TCP_INITIAL_RETRY_WAIT_PERIOD 500
  105. //
  106. // Define the maximum amount of time to wait (in milliseconds) before sending
  107. // a packet just to probe for a non-zero window size.
  108. //
  109. #define TCP_WINDOW_WAIT_PERIOD_MAX (120 * MILLISECONDS_PER_SECOND)
  110. //
  111. // Define the number of duplicate ACKs that must come in to signal packet loss.
  112. //
  113. #define TCP_DUPLICATE_ACK_THRESHOLD 3
  114. //
  115. // Define the default receive minimum size, in bytes.
  116. //
  117. #define TCP_DEFAULT_RECEIVE_MINIMUM 1
  118. //
  119. // Define the default send buffer size.
  120. //
  121. #define TCP_DEFAULT_SEND_BUFFER_SIZE (16 * _1KB)
  122. //
  123. // Define the default send minimum size, in bytes.
  124. //
  125. #define TCP_DEFAULT_SEND_MINIMUM 1
  126. //
  127. // Define the default window size.
  128. //
  129. #define TCP_DEFAULT_WINDOW_SIZE (64 * _1KB)
  130. //
  131. // Define the default window scale.
  132. //
  133. #define TCP_DEFAULT_WINDOW_SCALE 8
  134. //
  135. // Define the maximum window size.
  136. //
  137. #define TCP_MAXIMUM_WINDOW_SIZE (_1GB - 1)
  138. //
  139. // Define the mask for the TCP window.
  140. //
  141. #define TCP_WINDOW_MASK MAX_USHORT
  142. //
  143. // Define the minimum window size.
  144. //
  145. #define TCP_MINIMUM_WINDOW_SIZE 256
  146. //
  147. // Define the maximum window scale. A maximum window scale of 14, prevents the
  148. // window from being greater than or equal to 1GB, giving sequence numbers
  149. // enough space to avoid ambiguity between old and new data.
  150. //
  151. #define TCP_MAXIMUM_WINDOW_SCALE 14
  152. //
  153. // Define how often packets are retransmitted, in microseconds.
  154. //
  155. #define TCP_TRANSMIT_RETRY_INTERVAL MICROSECONDS_PER_SECOND
  156. //
  157. // Define how many times a packet is resent before the worst is assumed.
  158. //
  159. #define TCP_RETRANSMIT_COUNT 10
  160. //
  161. // Define the time, in seconds, to wait after a connection goes idle before
  162. // sending the first keep alive probe.
  163. //
  164. #define TCP_DEFAULT_KEEP_ALIVE_TIMEOUT 3600
  165. //
  166. // Define the time, in seconds, between sending keep alive messages on an idle
  167. // connection.
  168. //
  169. #define TCP_DEFAULT_KEEP_ALIVE_PERIOD 60
  170. //
  171. // Define the number of keep alive probes to be sent before the connection is
  172. // reset.
  173. //
  174. #define TCP_DEFAULT_KEEP_ALIVE_PROBE_LIMIT 5
  175. //
  176. // Define TCP header flags.
  177. //
  178. #define TCP_HEADER_FLAG_FIN 0x01
  179. #define TCP_HEADER_FLAG_SYN 0x02
  180. #define TCP_HEADER_FLAG_RESET 0x04
  181. #define TCP_HEADER_FLAG_PUSH 0x08
  182. #define TCP_HEADER_FLAG_ACKNOWLEDGE 0x10
  183. #define TCP_HEADER_FLAG_URGENT 0x20
  184. //
  185. // The keep alive flag is not a real TCP header flag.
  186. //
  187. #define TCP_HEADER_FLAG_KEEP_ALIVE 0x80
  188. #define TCP_HEADER_LENGTH_MASK 0xF0
  189. #define TCP_HEADER_LENGTH_SHIFT 4
  190. //
  191. // Define the TCP option types.
  192. //
  193. #define TCP_OPTION_END 0
  194. #define TCP_OPTION_NOP 1
  195. #define TCP_OPTION_MAXIMUM_SEGMENT_SIZE 2
  196. #define TCP_OPTION_WINDOW_SCALE 3
  197. //
  198. // Define TCP option sizes.
  199. //
  200. #define TCP_OPTION_NOP_SIZE 1
  201. #define TCP_OPTION_MSS_SIZE 4
  202. #define TCP_OPTION_WINDOW_SCALE_SIZE 3
  203. //
  204. // Define the TCP receive segment flags. The first six bits matche up with the
  205. // TCP header flags.
  206. //
  207. #define TCP_RECEIVE_SEGMENT_FLAG_FIN TCP_HEADER_FLAG_FIN
  208. #define TCP_RECEIVE_SEGMENT_FLAG_SYN TCP_HEADER_FLAG_SYN
  209. #define TCP_RECEIVE_SEGMENT_FLAG_RESET TCP_HEADER_FLAG_RESET
  210. #define TCP_RECEIVE_SEGMENT_FLAG_PUSH TCP_HEADER_FLAG_PUSH
  211. #define TCP_RECEIVE_SEGMENT_FLAG_ACKNOWLEDGE TCP_HEADER_FLAG_ACKNOWLEDGE
  212. #define TCP_RECEIVE_SEGMENT_FLAG_URGENT TCP_HEADER_FLAG_URGENT
  213. #define TCP_RECEIVE_SEGMENT_HEADER_FLAG_MASK \
  214. (TCP_RECEIVE_SEGMENT_FLAG_FIN | \
  215. TCP_RECEIVE_SEGMENT_FLAG_SYN | \
  216. TCP_RECEIVE_SEGMENT_FLAG_RESET | \
  217. TCP_RECEIVE_SEGMENT_FLAG_PUSH | \
  218. TCP_RECEIVE_SEGMENT_FLAG_ACKNOWLEDGE)
  219. //
  220. // Define the TCP send segment flags. The first six bits matche up with the TCP
  221. // header flags.
  222. //
  223. #define TCP_SEND_SEGMENT_FLAG_FIN TCP_HEADER_FLAG_FIN
  224. #define TCP_SEND_SEGMENT_FLAG_SYN TCP_HEADER_FLAG_SYN
  225. #define TCP_SEND_SEGMENT_FLAG_RESET TCP_HEADER_FLAG_RESET
  226. #define TCP_SEND_SEGMENT_FLAG_PUSH TCP_HEADER_FLAG_PUSH
  227. #define TCP_SEND_SEGMENT_FLAG_ACKNOWLEDGE TCP_HEADER_FLAG_ACKNOWLEDGE
  228. #define TCP_SEND_SEGMENT_FLAG_URGENT TCP_HEADER_FLAG_URGENT
  229. #define TCP_SEND_SEGMENT_HEADER_FLAG_MASK \
  230. (TCP_SEND_SEGMENT_FLAG_FIN | \
  231. TCP_SEND_SEGMENT_FLAG_SYN | \
  232. TCP_SEND_SEGMENT_FLAG_RESET | \
  233. TCP_SEND_SEGMENT_FLAG_PUSH | \
  234. TCP_SEND_SEGMENT_FLAG_ACKNOWLEDGE | \
  235. TCP_SEND_SEGMENT_FLAG_URGENT)
  236. //
  237. // Define the TCP socket flags.
  238. //
  239. #define TCP_SOCKET_FLAG_RECEIVE_FINAL_SEQUENCE_VALID 0x00000001
  240. #define TCP_SOCKET_FLAG_SEND_FINAL_SEQUENCE_VALID 0x00000002
  241. #define TCP_SOCKET_FLAG_SEND_FIN_WITH_DATA 0x00000004
  242. #define TCP_SOCKET_FLAG_SEND_ACKNOWLEDGE 0x00000008
  243. #define TCP_SOCKET_FLAG_CONNECTION_RESET 0x00000010
  244. #define TCP_SOCKET_FLAG_IN_FAST_RECOVERY 0x00000020
  245. #define TCP_SOCKET_FLAG_LINGER_ENABLED 0x00000040
  246. #define TCP_SOCKET_FLAG_KEEP_ALIVE 0x00000080
  247. #define TCP_SOCKET_FLAG_URGENT_INLINE 0x00000100
  248. #define TCP_SOCKET_FLAG_RECEIVE_MISSING_SEGMENTS 0x00000200
  249. #define TCP_SOCKET_FLAG_NO_DELAY 0x00000400
  250. #define TCP_SOCKET_FLAG_WINDOW_SCALING 0x00000800
  251. //
  252. // ------------------------------------------------------ Data Type Definitions
  253. //
  254. //
  255. // Define the ioctl numbers that can be sent to a TCP socket. These have to
  256. // match with the values in the C library header <sys/ioctl.h>.
  257. //
  258. typedef enum _TCP_USER_CONTROL_CODE {
  259. TcpUserControlAtUrgentMark = 0x7300,
  260. } TCP_USER_CONTROL_CODE, *PTCP_USER_CONTROL_CODE;
  261. //
  262. // Define the various TCP connection states.
  263. //
  264. // Invalid - The socket should never be in this state.
  265. //
  266. // Initialized - This is a brand new socket that is neither listening nor
  267. // connected.
  268. //
  269. // Listening - Represents waiting for a connection request from any remote host.
  270. //
  271. // SynSent - Represents waiting for a matched connection request after having
  272. // sent a connection request.
  273. //
  274. // SynReceived - Represents waiting for a confirmation connection request
  275. // acknowledgment after having both received and sent a connection request.
  276. //
  277. // Established - Represents an open connection, data can be both sent and
  278. // received.
  279. //
  280. // FinWait1 - Represents waiting for a connection termination request from the
  281. // remote host, or an acknowledgment of the connection termition request
  282. // previously sent.
  283. //
  284. // FinWait2 - Represents waiting for a connection termination request from the
  285. // remote host.
  286. //
  287. // CloseWait - Represents waiting for a connection termination request from the
  288. // local user.
  289. //
  290. // Closing - Represents waiting for a connection termination request
  291. // acknowledgment from the remote host.
  292. //
  293. // LastAcknowledge - Represents waiting for an acknowledgment of the connection
  294. // termination request previously sent to the remote host (which includes
  295. // an acknowledgment of its connection termination request.
  296. //
  297. // TimeWait - Represents waiting for enough time to pass to be sure the remote
  298. // host received the acnkowledgment of its connection termination request.
  299. // This prevents a stray FIN+ACK still stuck in the network from ruining
  300. // the next connection to use this host/port combination when it arrives.
  301. //
  302. // Closed - Represents a completely shut down connection.
  303. //
  304. typedef enum _TCP_STATE {
  305. TcpStateInvalid,
  306. TcpStateInitialized,
  307. TcpStateListening,
  308. TcpStateSynSent,
  309. TcpStateSynReceived,
  310. TcpStateEstablished,
  311. TcpStateFinWait1,
  312. TcpStateFinWait2,
  313. TcpStateCloseWait,
  314. TcpStateClosing,
  315. TcpStateLastAcknowledge,
  316. TcpStateTimeWait,
  317. TcpStateClosed
  318. } TCP_STATE, *PTCP_STATE;
  319. /*++
  320. Structure Description:
  321. This structure defines a TCP data socket.
  322. Members:
  323. NetSocket - Stores the common core networking parameters.
  324. ListEntry - Stores pointers to the previous and next sockets on the global
  325. list.
  326. State - Stores the connection state of the socket.
  327. Flags - Stores a bitmask of TCP flags. See TCP_SOCKET_FLAG_* for
  328. definitions.
  329. SendInitialSequence - Stores the random offset that the sequence numbers
  330. started at for this socket.
  331. SendUnacknowledgedSequence - Stores the first unacknowledged sequence
  332. number, representing data that was sent but not acknowledged.
  333. SendNextBufferSequence - Stores the sequence number of the next byte
  334. accepted into the send buffer.
  335. SendNextNetworkSequence - Stores the sequence number of the next byte to
  336. be sent out of the network. This may be different than the next buffer
  337. sequence if data has been accepted into the send buffer but not yet
  338. actually sent out on the wire.
  339. SendMaxSegmentSize - Stores the maximum segment size for outgoing packets.
  340. SendWindowScale - Stores the number of bits to shift the window size left
  341. by for incoming packets.
  342. SendWindowSize - Stores the size of the window of data that can be sent out
  343. to the remote host.
  344. SendWindowUpdateSequence - Stores the sequence number of the packet when the
  345. send window was last updated. This is used to prevent old packets from
  346. updating the send window.
  347. SendWindowUpdateAcknowledge - Stores the acnknowledge number of the most
  348. recent packet used to update the send window. This is used to prevent
  349. old packets from updating the send window.
  350. SendBufferTotalSize - Stores the total size in the send buffer, in bytes.
  351. SendBufferFreeSize - Stores the number of free bytes size in the send
  352. buffer.
  353. SendFinalSequence - Stores the outgoing sequence number of the sent or
  354. soon-to-be-sent FIN.
  355. PreviousAcknowledgeNumber - Stores the most recently received acknowledge
  356. number.
  357. DuplicateAcknowledgeCount - Stores the number of duplicate acknowledges
  358. that have come in. A value of 1 means two packets with the same
  359. acknowledge number have come in.
  360. ReceiveWindowTotalSize - Stores the total size of the local receive window,
  361. in bytes.
  362. ReceiveWindowFreeSize - Stores the current size of the local receive window,
  363. in bytes.
  364. ReceiveWindowScale - Stores the number of bits by which the window size
  365. must be shifted before being put into the header.
  366. ReceiveMinimum - Stores the minimum number of bytes that must be received
  367. before this socket becomes readable.
  368. ReceiveInitialSequence - Stores the random initial sequence number
  369. provided by the remote host.
  370. ReceiveUnreadSequence - Stores the sequence number of the first unread
  371. byte. This should be at the head of the received segment list.
  372. ReceiveNextSequence - Stores the next sequence number expected to be
  373. received from the remote host (the value to fill in the acknowledgment
  374. number).
  375. ReceiveFinalSequence - Stores the sequence number in which the FIN was
  376. sent.
  377. ReceiveSegmentOffset - Stores the offset in bytes into the first segment
  378. where the next user receive call will read from.
  379. ReceiveMaxSegmentSize - Stores the maximum segment size of packets received
  380. by the TCP socket.
  381. Lock - Store a pointer to a queued lock used to synchronize access to
  382. various parts of the structure.
  383. ReceivedSegmentList - Stores the head of the list of recieved segments that
  384. have not yet been read by the user. This list contains objects of type
  385. TCP_RECEIVED_SEGMENT, and is in order by sequence number.
  386. OutgoingSegmentList - Stores the list of segments that have either not yet
  387. been sent or have been sent but not acknowledged.
  388. FreeSegmentList - Stores the head of the list of segments that can be
  389. reused for send and receive.
  390. IncomingConnectionList - Stores the head of the list of incoming
  391. connections. This list only applies to a listening socket.
  392. IncomingConnectionCount - Stores the number of elements that are on the
  393. incoming connection list.
  394. SlowStartThreshold - Stores the threshold value for the congestion window.
  395. If the congestion window size is less than or equal to this value, then
  396. Slow Start is used. Otherwise, Congestion Avoidance is used.
  397. CongestionWindowSize - Stores the current size of the congestion window.
  398. FastRecoveryEndSequence - Stores the sequence number that when acknowledged
  399. will transition congestion control out of Fast Recovery back into
  400. Congestion Avoidance mode.
  401. RoundTripTime - Stores the latest estimate for the round trip time.
  402. TimeoutEnd - Stores the ending time, in time counter ticks, of the current
  403. timeout period. Depending on the state this could be the time-wait
  404. timeout, the SYN resend timeout, or the packet retransmit timeout.
  405. These three uses are mutually exclusive, so the timeout end can be
  406. safely shared.
  407. RetryTime - Stores the time, in time counter ticks, when the socket will
  408. retry sending a packet. Depending on the state, this could be a probe
  409. despite a zero window size, a resend of the SYN packet, or a resend of
  410. the FIN packet. These there uses are mutually exclusive, so the retry
  411. time can be safely shared.
  412. KeepAliveTime - Stores the time, in time counter ticks, when the socket
  413. will probe the remote host with a keep alive message.
  414. KeepAliveTimeout - Stores the time, in seconds, to wait after the
  415. connection goes idle before sending a keep alive probe.
  416. KeepAlivePeriod - Stores the time, in seconds, between sending keep alive
  417. probes on an idle connection.
  418. KeepAliveProbeLimit - Stores the number of keep alive probes to send before
  419. resetting the connection.
  420. KeepAliveProbeCount - Stores the current number of keep alive probes that
  421. have been sent without reply.
  422. RetryWaitPeriod - Stores the time in milliseconds for the socket to wait
  423. until it sends its next retry packet. This could be a probe on zero
  424. window, another SYN packet, or another FIN packet. These there uses are
  425. mutually exclusive, so the period can be safely shared.
  426. LingerTimeout - Stores the time, in milliseconds, that the socket will wait
  427. for all the data to be sent on close before forcefully closing the
  428. connection.
  429. SendTimeout - Stores the maximum time, in milliseconds, for the socket to
  430. wait until send buffer space becomes available.
  431. ReceiveTimeout - Stores the maximum time, in milliseconds, for the socket
  432. to wait until data is available to receive.
  433. ShutdownTypes - Stores a mask of the shutdown types that have occurred.
  434. See SOCKET_SHUTDOWN_* definitions.
  435. OutOfBandData - Stores a single urgent byte, or -1 if the urgent data is
  436. not valid.
  437. SegmentAllocationSize - Stores the allocation size for each of the send and
  438. receive TCP segments, including enough size for the header and data.
  439. --*/
  440. typedef struct _TCP_SOCKET {
  441. NET_SOCKET NetSocket;
  442. LIST_ENTRY ListEntry;
  443. TCP_STATE State;
  444. ULONG Flags;
  445. ULONG SendInitialSequence;
  446. ULONG SendUnacknowledgedSequence;
  447. ULONG SendNextBufferSequence;
  448. ULONG SendNextNetworkSequence;
  449. ULONG SendMaxSegmentSize;
  450. ULONG SendWindowScale;
  451. ULONG SendWindowSize;
  452. ULONG SendWindowUpdateSequence;
  453. ULONG SendWindowUpdateAcknowledge;
  454. ULONG SendBufferTotalSize;
  455. ULONG SendBufferFreeSize;
  456. ULONG SendFinalSequence;
  457. ULONG PreviousAcknowledgeNumber;
  458. ULONG DuplicateAcknowledgeCount;
  459. ULONG ReceiveWindowTotalSize;
  460. ULONG ReceiveWindowFreeSize;
  461. ULONG ReceiveWindowScale;
  462. ULONG ReceiveMinimum;
  463. ULONG ReceiveInitialSequence;
  464. ULONG ReceiveUnreadSequence;
  465. ULONG ReceiveNextSequence;
  466. ULONG ReceiveFinalSequence;
  467. ULONG ReceiveSegmentOffset;
  468. ULONG ReceiveMaxSegmentSize;
  469. PQUEUED_LOCK Lock;
  470. LIST_ENTRY ReceivedSegmentList;
  471. LIST_ENTRY OutgoingSegmentList;
  472. LIST_ENTRY FreeSegmentList;
  473. LIST_ENTRY IncomingConnectionList;
  474. ULONG IncomingConnectionCount;
  475. ULONG SlowStartThreshold;
  476. ULONG CongestionWindowSize;
  477. ULONG FastRecoveryEndSequence;
  478. ULONGLONG RoundTripTime;
  479. ULONGLONG TimeoutEnd;
  480. ULONGLONG RetryTime;
  481. ULONGLONG KeepAliveTime;
  482. ULONG KeepAliveTimeout;
  483. ULONG KeepAlivePeriod;
  484. ULONG KeepAliveProbeLimit;
  485. ULONG KeepAliveProbeCount;
  486. ULONG RetryWaitPeriod;
  487. ULONG LingerTimeout;
  488. ULONG SendTimeout;
  489. ULONG ReceiveTimeout;
  490. ULONG ShutdownTypes;
  491. LONG OutOfBandData;
  492. ULONG SegmentAllocationSize;
  493. } TCP_SOCKET, *PTCP_SOCKET;
  494. /*++
  495. Structure Description:
  496. This structure stores information about an incoming TCP connection.
  497. Members:
  498. ListEntry - Stores pointers to the next and previous incoming connections.
  499. IoHandle - Stores a pointer to the I/O handle for the connection.
  500. --*/
  501. typedef struct _TCP_INCOMING_CONNECTION {
  502. LIST_ENTRY ListEntry;
  503. PIO_HANDLE IoHandle;
  504. } TCP_INCOMING_CONNECTION, *PTCP_INCOMING_CONNECTION;
  505. /*++
  506. Structure Description:
  507. This structure stores information common to all TCP segment types.
  508. Members:
  509. ListEntry - Stores a pointer to the next and previous segments.
  510. --*/
  511. typedef struct _TCP_SEGMENT_HEADER {
  512. LIST_ENTRY ListEntry;
  513. } TCP_SEGMENT_HEADER, *PTCP_SEGMENT_HEADER;
  514. /*++
  515. Structure Description:
  516. This structure stores information about a received segment. The data comes
  517. after this structure.
  518. Members:
  519. Header - Stores information common to all TCP segment types.
  520. SequenceNumber - Stores the byte offset into the stream where this buffer
  521. belongs.
  522. Length - Stores the length of the data, in bytes.
  523. NextSequence - Stores the sequence number after this segment. Nearly all
  524. of the time this is the same as SequenceNumber + Length, but in the
  525. extremely rare cases where an out-of-band byte was pulled out, the
  526. length will be one shy of the next sequence.
  527. Flags - Stores a bitmaks of flags for the incoming TCP segment. See TCP_
  528. RECEIVE_SEGMENT_FLAG_* for definitions;
  529. --*/
  530. typedef struct _TCP_RECEIVED_SEGMENT {
  531. TCP_SEGMENT_HEADER Header;
  532. ULONG SequenceNumber;
  533. ULONG Length;
  534. ULONG NextSequence;
  535. ULONG Flags;
  536. } TCP_RECEIVED_SEGMENT, *PTCP_RECEIVED_SEGMENT;
  537. /*++
  538. Structure Description:
  539. This structure stores information about an outgoing TCP segment. The data
  540. comes immediately after this structure.
  541. Members:
  542. Header - Stores information common to all TCP segment types.
  543. SequenceNumber - Stores the byte offset into the stream where this buffer
  544. belongs.
  545. LastSendTime - Stores the performance counter value the last time this
  546. packet was sent.
  547. TimeoutInterval - Stores the number of time counter ticks from the last send
  548. time when this packet is considered timed out and needs to be resent or
  549. otherwise acted on.
  550. SendAttemptCount - Stores the number of times this packet has been sent off
  551. without getting acknowledged.
  552. Length - Stores the length of the data, in bytes.
  553. Offset - Stores the offset in bytes from the beginning of the segment to
  554. resend due to a partial ACK.
  555. Flags - Stores a bitmask of flags for the outgoing TCP segment. See
  556. TCP_SEND_SEGMENT_FLAG_* for definitions.
  557. --*/
  558. typedef struct _TCP_SEND_SEGMENT {
  559. TCP_SEGMENT_HEADER Header;
  560. ULONG SequenceNumber;
  561. ULONGLONG LastSendTime;
  562. ULONGLONG TimeoutInterval;
  563. ULONG SendAttemptCount;
  564. ULONG Length;
  565. ULONG Offset;
  566. ULONG Flags;
  567. } TCP_SEND_SEGMENT, *PTCP_SEND_SEGMENT;
  568. /*++
  569. Structure Description:
  570. This structure defines a TCP packet protocol header.
  571. Members:
  572. SourcePort - Stores the source port number of the packet.
  573. DestinationPort - Stores the port number of this packet's destination.
  574. SequenceNumber - Stores the position of this data within the stream.
  575. AcknowledgmentNumber - Stores the next sequence number that the sender
  576. expects to receive. This field is only valid if the ACK flag is on,
  577. which it always is once a connection is established.
  578. HeaderLength - Stores the length of the header, in 32-bit words.
  579. Flags - Stores a bitfield of flags used to relay control information
  580. between two peers.
  581. WindowSize - Stores the size of the advertised window of data the socket
  582. can receive from the other host.
  583. Checksum - Stores the checksum of the header and data.
  584. NonUrgentOffset - Stores the offset within the data where the non-urgent
  585. data begins. This field is only used if the urgent flag is set. RFC793
  586. is inconsistent as to whether this field points to the last urgent
  587. octet or the first non-urgent octet. RFC1122 attempted to clarify this
  588. as the last urgent octet, but all of today's implementations
  589. maintained the opposite semantics. Stick with tradition to be
  590. consistent with everyone else. RFC6093 sums this all up.
  591. --*/
  592. typedef struct _TCP_HEADER {
  593. USHORT SourcePort;
  594. USHORT DestinationPort;
  595. ULONG SequenceNumber;
  596. ULONG AcknowledgmentNumber;
  597. UCHAR HeaderLength;
  598. UCHAR Flags;
  599. USHORT WindowSize;
  600. USHORT Checksum;
  601. USHORT NonUrgentOffset;
  602. } PACKED TCP_HEADER, *PTCP_HEADER;
  603. //
  604. // -------------------------------------------------------------------- Globals
  605. //
  606. extern BOOL NetTcpDebugPrintCongestionControl;
  607. //
  608. // -------------------------------------------------------- Function Prototypes
  609. //
  610. VOID
  611. NetpTcpPrintSocketEndpoints (
  612. PTCP_SOCKET Socket,
  613. BOOL Transmit
  614. );
  615. /*++
  616. Routine Description:
  617. This routine prints the socket local and remote addresses.
  618. Arguments:
  619. Socket - Supplies a pointer to the socket whose addresses should be printed.
  620. Transmit - Supplies a boolean indicating if the print is requested for a
  621. transmit (TRUE) or receive (FALSE).
  622. Return Value:
  623. None.
  624. --*/
  625. VOID
  626. NetpTcpRetransmit (
  627. PTCP_SOCKET Socket
  628. );
  629. /*++
  630. Routine Description:
  631. This routine immediately transmits the oldest pending packet. This routine
  632. assumes the socket lock is already held.
  633. Arguments:
  634. Socket - Supplies a pointer to the socket whose segment should be
  635. retransmitted.
  636. Return Value:
  637. None.
  638. --*/
  639. //
  640. // Congestion control routines
  641. //
  642. VOID
  643. NetpTcpCongestionInitializeSocket (
  644. PTCP_SOCKET Socket
  645. );
  646. /*++
  647. Routine Description:
  648. This routine initializes the congestion control portion of the TCP socket.
  649. Arguments:
  650. Socket - Supplies a pointer to the socket to initialize.
  651. Return Value:
  652. None.
  653. --*/
  654. VOID
  655. NetpTcpCongestionConnectionEstablished (
  656. PTCP_SOCKET Socket
  657. );
  658. /*++
  659. Routine Description:
  660. This routine is called when a socket moves to the Established state.
  661. Arguments:
  662. Socket - Supplies a pointer to the socket to initialize.
  663. Return Value:
  664. None.
  665. --*/
  666. ULONG
  667. NetpTcpGetSendWindowSize (
  668. PTCP_SOCKET Socket
  669. );
  670. /*++
  671. Routine Description:
  672. This routine determines the current available window of data that can be
  673. sent, taking into account both the receiver's window and the congestion
  674. window.
  675. Arguments:
  676. Socket - Supplies a pointer to the socket whose send window should be
  677. returned.
  678. Return Value:
  679. Returns one beyond the highest sequence number that can currently be sent.
  680. --*/
  681. VOID
  682. NetpTcpCongestionAcknowledgeReceived (
  683. PTCP_SOCKET Socket,
  684. ULONG AcknowledgeNumber
  685. );
  686. /*++
  687. Routine Description:
  688. This routine is called when an acknowledge (duplicate or not) comes in.
  689. This routine assumes the socket lock is already held.
  690. Arguments:
  691. Socket - Supplies a pointer to the socket that just got an acknowledge.
  692. AcknowledgeNumber - Supplies the acknowledge number that came in.
  693. Return Value:
  694. None.
  695. --*/
  696. VOID
  697. NetpTcpProcessNewRoundTripTimeSample (
  698. PTCP_SOCKET Socket,
  699. ULONGLONG RoundTripTicks
  700. );
  701. /*++
  702. Routine Description:
  703. This routine is called when a new round trip time sample arrives.
  704. Arguments:
  705. Socket - Supplies a pointer to the socket.
  706. RoundTripTicks - Supplies the most recent sample of round trip time, in
  707. time counter ticks.
  708. Return Value:
  709. None.
  710. --*/
  711. VOID
  712. NetpTcpGetTransmitTimeoutInterval (
  713. PTCP_SOCKET Socket,
  714. PTCP_SEND_SEGMENT Segment
  715. );
  716. /*++
  717. Routine Description:
  718. This routine sets the timeout duration for a transmitted packet.
  719. Arguments:
  720. Socket - Supplies a pointer to the socket.
  721. Segment - Supplies a pointer to the segment whose timeout interval needs to
  722. be set.
  723. Return Value:
  724. None. Upon completion, the segment's timeout interval is expected to be
  725. filled in by this routine.
  726. --*/
  727. VOID
  728. NetpTcpTransmissionTimeout (
  729. PTCP_SOCKET Socket,
  730. PTCP_SEND_SEGMENT Segment
  731. );
  732. /*++
  733. Routine Description:
  734. This routine is called when an acknowledge is not recieved for a sent
  735. packet in a timely manner (the packet timed out).
  736. Arguments:
  737. Socket - Supplies a pointer to the socket.
  738. Segment - Supplies a pointer to the segment that timed out.
  739. Return Value:
  740. None.
  741. --*/