tcp.h 29 KB

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