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.

899 lines
34 KiB

  1. /** Microsoft LAN Manager **/
  2. /** Copyright(c) Microsoft Corp., 1990-2000 **/
  3. /********************************************************************/
  4. /* :ts=4 */
  5. #ifndef _TCP_INCLUDED_
  6. #define _TCP_INCLUDED_
  7. #include "tcpinfo.h"
  8. //** TCP.H - TCP definitions.
  9. //
  10. // This file contains the definitions of TCP protocol specific options, such
  11. // as the sequence numbers and TCB.
  12. //
  13. #define PROTOCOL_TCP 6
  14. #define MIN_LOCAL_MSS 88
  15. #define MAX_REMOTE_MSS 536
  16. //* Timer stuff. We keep timers as ticks.
  17. #define MS_PER_TICK 100
  18. #define MS_TO_TICKS(m) ((m) / MS_PER_TICK)
  19. #define MIN_RETRAN_TICKS 3
  20. #define DEL_ACK_TICKS 2
  21. #define MAX_DEL_ACK_TICKS 6
  22. #define MIN_INITIAL_RTT 3
  23. // Define MAX_REXMIT_TO to be number of ticks in 2MSL
  24. #define MAX_REXMIT_TO ((ushort)FinWait2TO)
  25. #define SWS_TO MS_TO_TICKS(5000)
  26. #define PUSH_TO MS_TO_TICKS(500)
  27. typedef ulong TCP_TIME;
  28. #define MAX_CONN_TO_TICKS 0xffff
  29. #define INFINITE_CONN_TO(t) ((t) == 0)
  30. #define TCP_TIME_TO_TICKS(t) (((t)/MS_PER_TICK)+1)
  31. // Sequence numbers are kept as signed 32 bit quantities, with macros
  32. // defined to do wraparound comparisons on them.
  33. typedef int SeqNum; // A sequence number.
  34. //* Macros for comparions on sequence numbers.
  35. #define SEQ_GT(a, b) ((SeqNum)((a) - (b)) > 0)
  36. #define SEQ_GTE(a, b) ((SeqNum)((a) - (b)) >= 0)
  37. #define SEQ_LT(a, b) ((SeqNum)((a) - (b)) < 0)
  38. #define SEQ_LTE(a, b) ((SeqNum)((a) - (b)) <= 0)
  39. #define SEQ_EQ(a, b) ((a) == (b))
  40. #define TS_LT(a, b) (((a) - (b)) < 0)
  41. #define TS_LTE(a, b) (((a) - (b)) <= 0)
  42. #define TS_GTE(a, b) (((a) - (b)) >= 0)
  43. #define TCPTIME_LTE(a, b) ((int)((a) - (b)) <= 0)
  44. #define TCPTIME_LT(a, b) ((int)((a) - (b)) < 0)
  45. #define TIMWAITTABLE 1 //turn on timed wait TCB table changes
  46. #define IRPFIX 1 //turn on quick Irp to Conn find
  47. #if DBG && !MILLEN
  48. #ifndef REFERENCE_DEBUG
  49. #define REFERENCE_DEBUG 1
  50. #endif
  51. #else // DBG && !MILLEN
  52. #ifndef REFERENCE_DEBUG
  53. #define REFERENCE_DEBUG 0
  54. #endif
  55. #endif // DBG && !MILLEN
  56. #if REFERENCE_DEBUG
  57. // Reference history structure.
  58. //
  59. #define MAX_REFERENCE_HISTORY 64
  60. typedef struct _TCP_REFERENCE_HISTORY {
  61. uchar *File;
  62. uint Line;
  63. void *Caller;
  64. uint Count;
  65. } TCP_REFERENCE_HISTORY;
  66. #endif // REFERENCE_DEBUG
  67. //* The TCB - transport control block structure. This is the
  68. // structure that contains all of the state for the transport
  69. // connection, including sequence numbers, flow control information,
  70. // pending sends and receives, etc.
  71. #define tcb_signature 0x20424354 // 'TCB '
  72. #define twtcb_signature 0x22424354 // 'TCB2'
  73. #define syntcb_signature 0x23424354 // 'TCB3'
  74. typedef struct TWTCB {
  75. #if DBG
  76. ulong twtcb_sig;
  77. #endif
  78. Queue twtcb_link;
  79. // Do not reorder or move apart the following address fields.
  80. union {
  81. struct twtcb_addrinfo {
  82. IPAddr twtcb_daddr; // Destination IP address.
  83. IPAddr twtcb_saddr; // Source IP address.
  84. ushort twtcb_dport; // Destination port.
  85. ushort twtcb_sport; // Source port.
  86. };
  87. TCPAddrInfo twtcb_addrbytes;
  88. };
  89. Queue twtcb_TWQueue; // Place to hang all the timed_waits
  90. ushort twtcb_delta;
  91. ushort twtcb_rexmittimer;
  92. SeqNum twtcb_rcvnext;
  93. SeqNum twtcb_sendnext;
  94. #if DBG
  95. uint twtcb_flags;
  96. #endif
  97. } TWTCB;
  98. typedef struct SYNTCB {
  99. #if DBG
  100. ulong syntcb_sig; // Debug signature.
  101. #endif
  102. Queue syntcb_link; // Next pointer in TCB table.
  103. DEFINE_LOCK_STRUCTURE(syntcb_lock)
  104. // Do not reorder or move apart the following address fields.
  105. union {
  106. struct syntcb_addrinfo {
  107. IPAddr syntcb_daddr; // Destination IP address.
  108. IPAddr syntcb_saddr; // Source IP address.
  109. ushort syntcb_dport; // Destination port.
  110. ushort syntcb_sport; // Source port.
  111. };
  112. TCPAddrInfo syntcb_addrbytes;
  113. };
  114. // State information.
  115. uchar syntcb_state; // State of this TCB.
  116. uchar syntcb_rexmitcnt; // Count of rexmits on this TCB.
  117. // Highly used receive sequence variables.
  118. ushort syntcb_mss; // MSS for this connection.
  119. ushort syntcb_remmss; // MSS advertised by peer.
  120. uchar syntcb_tcpopts; // rfc 1323 and 2018 options holder
  121. uchar syntcb_ttl;
  122. SeqNum syntcb_rcvnext; // Next byte we expect to receive.
  123. // Send sequence variables.
  124. SeqNum syntcb_sendnext; // Sequence number of next byte to send.
  125. uint syntcb_sendwin; // Send window.
  126. uint syntcb_flags; // Flags for this TCB.
  127. uint syntcb_refcnt; // Reference count for TCB.
  128. ushort syntcb_rexmit; // Rexmit value.
  129. ushort syntcb_rexmittimer;// Timer for rexmit.
  130. uint syntcb_defaultwin; // Default rcv. window.
  131. short syntcb_sndwinscale;// send window scale
  132. short syntcb_rcvwinscale;// receive window scale
  133. int syntcb_tsrecent; // time stamp recent
  134. int syntcb_tsupdatetime; // Time when tsrecent was updated
  135. uint syntcb_walkcount;
  136. uint syntcb_partition;
  137. } SYNTCB;
  138. #if TRACE_EVENT
  139. typedef struct WMIData {
  140. ulong wmi_context; // PID
  141. ulong wmi_size; // num Bytes successfully sent.
  142. IPAddr wmi_destaddr; // Remote IPAddr.
  143. IPAddr wmi_srcaddr; // Local IPAddr.
  144. ushort wmi_destport; // Remote port.
  145. ushort wmi_srcport; // Local Port.
  146. } WMIData;
  147. #endif
  148. // We will have 7 timers in TCP, and integrate all their processing
  149. typedef enum {
  150. RXMIT_TIMER = 0,
  151. DELACK_TIMER,
  152. PUSH_TIMER,
  153. SWS_TIMER,
  154. ACD_TIMER,
  155. CONN_TIMER,
  156. KA_TIMER,
  157. NUM_TIMERS
  158. } TCP_TIMER_TYPE;
  159. #define NO_TIMER NUM_TIMERS
  160. typedef struct TCB {
  161. #if DBG
  162. ulong tcb_sig; // Debug signature.
  163. #endif
  164. struct TCB *tcb_next; // Next pointer in TCB table.
  165. DEFINE_LOCK_STRUCTURE(tcb_lock)
  166. uint tcb_refcnt; // Reference count for TCB.
  167. // Do not reorder or move apart the following address fields.
  168. union {
  169. struct tcb_addrinfo {
  170. IPAddr tcb_daddr; // Destination IP address.
  171. IPAddr tcb_saddr; // Source IP address.
  172. ushort tcb_dport; // Destination port.
  173. ushort tcb_sport; // Source port.
  174. };
  175. TCPAddrInfo tcb_addrbytes;
  176. };
  177. #if TRACE_EVENT
  178. ulong tcb_cpcontext;
  179. #endif
  180. // State information.
  181. uchar tcb_state; // State of this TCB.
  182. uchar tcb_rexmitcnt; // Count of rexmits on this TCB.
  183. uchar tcb_pending; // Pending actions on this TCB.
  184. uchar tcb_kacount; // Count of keep alive probes sent.
  185. // Highly used receive sequence variables.
  186. ushort tcb_mss; // MSS for this connection.
  187. ushort tcb_remmss; // MSS advertised by peer.
  188. SeqNum tcb_rcvnext; // Next byte we expect to receive.
  189. int tcb_rcvwin; // Receive window we're offering.
  190. // Send sequence variables.
  191. SeqNum tcb_senduna; // Sequence number of first unack'd data.
  192. SeqNum tcb_sendnext; // Sequence number of next byte to send.
  193. SeqNum tcb_sendmax; // Max value of sendnext this epoch.
  194. uint tcb_sendwin; // Send window.
  195. uint tcb_unacked; // Total number of bytes of unacked data.
  196. uint tcb_maxwin; // Max send window seen.
  197. uint tcb_cwin; // Congestion window.
  198. uint tcb_ssthresh; // Slow start threshold.
  199. uint tcb_phxsum; // Precomputed pseudo-header xsum.
  200. struct TCPSendReq *tcb_cursend; // Current send in use.
  201. PNDIS_BUFFER tcb_sendbuf; // Current buffer chain being sent.
  202. uint tcb_sendofs; // Offset into start of chain.
  203. uint tcb_sendsize; // Number of bytes unsent in current send.
  204. Queue tcb_sendq; // Queue of send requests.
  205. // Less highly used receive sequence variables.
  206. SeqNum tcb_sendwl1; // Window update sequence number.
  207. SeqNum tcb_sendwl2; // Window update ack number.
  208. struct TCPRcvReq *tcb_currcv; // Current receive buffer.
  209. uint tcb_indicated; // Bytes of data indicated.
  210. uint tcb_flags; // Flags for this TCB.
  211. uint tcb_fastchk; // Fast receive path check field.
  212. uint (*tcb_rcvhndlr)(struct TCB *, uint, struct IPRcvBuf *, uint Size);
  213. SeqNum tcb_rttseq; // Sequence number being measured for RTT.
  214. ushort tcb_rexmit; // Rexmit value.
  215. // Retransmit timer information. These are stored as ticks, where by
  216. // default each tick is 100ms.
  217. ushort tcb_smrtt; // Smoothed rtt value.
  218. ushort tcb_delta; // Delta value.
  219. uchar tcb_slowcount; // Count of reasons why we're on the slow path.
  220. uchar tcb_closereason; // Reason we're closing.
  221. IP_STATUS tcb_error; // Last error we heard about from IP.
  222. uint tcb_rtt; // Current round trip time TS.
  223. uint tcb_defaultwin; // Default rcv. window.
  224. struct TCPRAHdr *tcb_raq; // Reassembly queue.
  225. struct TCPRcvReq *tcb_rcvhead; // Head of recv. buffer queue.
  226. struct TCPRcvReq *tcb_rcvtail; // Tail of recv. buffer queue.
  227. uint tcb_pendingcnt; // Bytes waiting to be received.
  228. struct IPRcvBuf *tcb_pendhead; // Head of pending recv. queue.
  229. struct IPRcvBuf *tcb_pendtail; // Tail of pending recv. queue.
  230. struct TCPConnReq *tcb_connreq; // Connection-type request for
  231. // this connection.
  232. void *tcb_conncontext; // Connection context for this
  233. // connection.
  234. uint tcb_bcountlow; // Low part of byte count.
  235. uint tcb_bcounthi; // High part of bytecount.
  236. uint tcb_totaltime; // Total number of ticks spent
  237. // sending.
  238. struct TCPConn *tcb_conn; // Back pointer to conn for TCB.
  239. Queue tcb_delayq; // Queue linkage for delay queue.
  240. void *tcb_rcvind; // Receive indication handler.
  241. union {
  242. void *tcb_ricontext; // Receive indication context.
  243. struct TCB *tcb_aonext; // Next pointer on AddrObj.
  244. };
  245. // Miscellaneous info, for IP.
  246. IPOptInfo tcb_opt; // Option information.
  247. RouteCacheEntry *tcb_rce; // RCE for this connection.
  248. struct TCPConnReq *tcb_discwait; // Disc-Wait req., if there is one.
  249. struct TCPAbortReq *tcb_abortreq; // Abort req., if there is one.
  250. struct TCPRcvReq *tcb_exprcv; // Head of expedited recv. buffer
  251. // queue.
  252. struct IPRcvBuf *tcb_urgpending; // Urgent data queue.
  253. uint tcb_urgcnt; // Byte count of data on urgent q.
  254. uint tcb_urgind; // Urgent bytes indicated.
  255. SeqNum tcb_urgstart; // Start of urgent data.
  256. SeqNum tcb_urgend; // End of urgent data.
  257. short tcb_walkcount; // Count of number of people
  258. // 'walking' this TCB.
  259. short tcb_unusedpendbuf; // Tracks the number of bytes
  260. // wasted while using slist buffers.
  261. ushort tcb_dup; // For Fast recovery algorithm
  262. ushort tcb_force : 1; // Force next send after fast send
  263. ushort tcb_tcpopts : 3;// rfc 1323 and 2018 options holder
  264. ushort tcb_moreflag : 3;
  265. ushort tcb_allowedoffloads : 9; // Types of allowed offloads.
  266. struct SACKSendBlock *tcb_SackBlock;// Sacks which needs to be sent
  267. struct SackListEntry *tcb_SackRcvd; // Sacks which needs to be processed
  268. short tcb_sndwinscale;// send window scale
  269. short tcb_rcvwinscale;// receive window scale
  270. int tcb_tsrecent; // time stamp recent
  271. SeqNum tcb_lastack; // ack number in the last segment sent
  272. int tcb_tsupdatetime; // Time when tsrecent was updated
  273. void *tcb_chainedrcvind; //for chained receives
  274. void *tcb_chainedrcvcontext;
  275. #if GPC
  276. ULONG tcb_GPCCachedIF;
  277. ULONG tcb_GPCCachedLink;
  278. struct RouteTableEntry *tcb_GPCCachedRTE;
  279. #endif
  280. #if DBG
  281. uint tcb_LargeSend; // Counts the number of outstanding
  282. // large-send transmit-requests
  283. #endif
  284. uint tcb_partition;
  285. uint tcb_connid;
  286. // ACK behavior
  287. uchar tcb_delackticks;
  288. uchar tcb_numdelacks;
  289. uchar tcb_rcvdsegs;
  290. uchar tcb_bhprobecnt; // BH probe count.
  291. // Timer wheel parameters
  292. // The first two are one logical group called wheel state.
  293. // They indicate which slot in the timer wheel the TCB is in,
  294. // and it's linkage in the timer slot queue.
  295. Queue tcb_timerwheelq;
  296. ushort tcb_timerslot : 12;
  297. // These three variables are another logical group called timer
  298. // state. They indicate the state of timers active on the TCB,
  299. // tcb_timertime maintains the time at which the earliest timer
  300. // will fire, and tcb_timertype maintains the earliest timer's
  301. // type.
  302. // To see why this whole thing is important, see comments after
  303. // the TIMER_WHEEL structure definition.
  304. ushort tcb_timertype : 4;
  305. uint tcb_timertime;
  306. uint tcb_timer[NUM_TIMERS];
  307. #if REFERENCE_DEBUG
  308. uint tcb_refhistory_index;
  309. TCP_REFERENCE_HISTORY tcb_refhistory[MAX_REFERENCE_HISTORY];
  310. #endif //REFERENCE_DEBUG
  311. } TCB;
  312. #define TIMER_WHEEL_SIZE 511
  313. #define DUMMY_SLOT TIMER_WHEEL_SIZE
  314. #define MAX_TIME_VALUE 0xffffffff
  315. // The Timer wheel structure definition has:
  316. // tw_timerslot: An array of queues, one for each timer slot.
  317. // tw_lock: A lock protecting the complete timer wheel.
  318. // (Contention is reduced by having as many timer
  319. // wheels as partitions in the system).
  320. // tw_starttick: Indicates the first time tick that has to
  321. // be looked at.
  322. // For e.g., if a timer routine looked at all
  323. // TCBs firing on or before tick 5 in a pass,
  324. // it would set tw_starttick to 6, and that is
  325. // the point it would start processing from on
  326. // the next pass.
  327. typedef struct CACHE_ALIGN _Timer_Wheel {
  328. Queue tw_timerslot[TIMER_WHEEL_SIZE];
  329. uint tw_starttick;
  330. CTELock tw_lock;
  331. } TIMER_WHEEL, *PTIMER_WHEEL;
  332. C_ASSERT(sizeof(TIMER_WHEEL) % MAX_CACHE_LINE_SIZE == 0);
  333. C_ASSERT(__alignof(TIMER_WHEEL) == MAX_CACHE_LINE_SIZE);
  334. // The first two functions operate on timer state (see comments in definition
  335. // of TCB for meaning of timer state). StopTCBTimerR and StartTCBTimerR
  336. // operate on the timer state (tcb_timertype, tcb_timertime, tcb_timer)
  337. // atomically. A call to either of these functions will always leave wheel
  338. // state consistent.
  339. extern void StopTCBTimerR(TCB *StopTCB, TCP_TIMER_TYPE TimerType);
  340. extern BOOLEAN StartTCBTimerR(TCB *StartTCB, TCP_TIMER_TYPE TimerType, uint TimerValue);
  341. // The following functions operate on the TCB's wheel state (tcb_timerwheelq
  342. // and tcb_timerslot). A call to any of these functions changes the value
  343. // of both the variables in such a way that they are consistent.
  344. extern void InsertIntoTimerWheel(TCB *InsertTCB, ushort Slot);
  345. extern void RemoveFromTimerWheel(TCB *RemoveTCB);
  346. extern void RemoveAndInsertIntoTimerWheel(TCB *RemInsTCB, ushort Slot);
  347. // The job of the inline functions below, is to sort-of act as glue, and they ensure
  348. // that the wheel state and timer state of a TCB are in tandem with each
  349. // other.
  350. // STOP_TCB_TIMER_R modifies the timer state but never
  351. // does anything to the wheel state. This means that the TCB will remain
  352. // where it was in the timer wheel, and the timer routine will eventually
  353. // bring the wheel state in conformance with the timer state.
  354. extern void STOP_TCB_TIMER_R(TCB *Tcb, TCP_TIMER_TYPE Type);
  355. // START_TCB_TIMER_R modifies the timer state and only modifies
  356. // wheel state if the timer that was started was earlier than all the
  357. // other timers on that TCB. This is in accordance with the lazy evaluation
  358. // strategy.
  359. extern void START_TCB_TIMER_R(TCB *Tcb, TCP_TIMER_TYPE Type, uint Value);
  360. #define COMPUTE_SLOT(Time) ((Time) % TIMER_WHEEL_SIZE)
  361. #define TCB_TIMER_FIRED_R(tcb, type, time) \
  362. ((tcb->tcb_timer[type]) && (tcb->tcb_timer[type] == time))
  363. #define TCB_TIMER_RUNNING_R(tcb, type) (tcb->tcb_timer[type] != 0)
  364. //* Definitions for TCP states.
  365. #define TCB_CLOSED 0 // Closed.
  366. #define TCB_LISTEN 1 // Listening.
  367. #define TCB_SYN_SENT 2 // SYN Sent.
  368. #define TCB_SYN_RCVD 3 // SYN received.
  369. #define TCB_ESTAB 4 // Established.
  370. #define TCB_FIN_WAIT1 5 // FIN-WAIT-1
  371. #define TCB_FIN_WAIT2 6 // FIN-WAIT-2
  372. #define TCB_CLOSE_WAIT 7 // Close waiting.
  373. #define TCB_CLOSING 8 // Closing state.
  374. #define TCB_LAST_ACK 9 // Last ack state.
  375. #define TCB_TIME_WAIT 10 // Time wait state.
  376. #define SYNC_STATE(s) ((s) > TCB_SYN_RCVD)
  377. #define SYNC_RCVD_STATE(s) ((s) > TCB_SYN_SENT)
  378. #define GRACEFUL_CLOSED_STATE(s) ((s) >= TCB_LAST_ACK)
  379. #define DATA_RCV_STATE(s) ((s) >= TCB_ESTAB && (s) <= TCB_FIN_WAIT2)
  380. #define DATA_SEND_STATE(s) ((s) == TCB_ESTAB || (s) == TCB_CLOSE_WAIT)
  381. #define CONN_STATE(TcbState) ((TcbState) + 1)
  382. C_ASSERT(TCP_CONN_CLOSED == TCB_CLOSED + 1);
  383. C_ASSERT(TCP_CONN_LISTEN == TCB_LISTEN + 1);
  384. C_ASSERT(TCP_CONN_SYN_SENT == TCB_SYN_SENT + 1);
  385. C_ASSERT(TCP_CONN_SYN_RCVD == TCB_SYN_RCVD + 1);
  386. C_ASSERT(TCP_CONN_ESTAB == TCB_ESTAB + 1);
  387. C_ASSERT(TCP_CONN_FIN_WAIT1 == TCB_FIN_WAIT1 + 1);
  388. C_ASSERT(TCP_CONN_FIN_WAIT2 == TCB_FIN_WAIT2 + 1);
  389. C_ASSERT(TCP_CONN_CLOSE_WAIT == TCB_CLOSE_WAIT + 1);
  390. C_ASSERT(TCP_CONN_CLOSING == TCB_CLOSING + 1);
  391. C_ASSERT(TCP_CONN_LAST_ACK == TCB_LAST_ACK + 1);
  392. C_ASSERT(TCP_CONN_TIME_WAIT == TCB_TIME_WAIT + 1);
  393. //* Definitions for flags.
  394. #define WINDOW_SET 0x00000001 // Window explictly set.
  395. #define CLIENT_OPTIONS 0x00000002 // Have client IP options on conn.
  396. #define CONN_ACCEPTED 0x00000004 // Connection was accepted.
  397. #define ACTIVE_OPEN 0x00000008 // Connection came from an active
  398. // open.
  399. #define DISC_NOTIFIED 0x00000010 // Client has been notified of a
  400. // disconnect.
  401. #define IN_DELAY_Q 0x00000020 // We're in the delayed action Q.
  402. #define RCV_CMPLTING 0x00000040 // We're completeing rcvs.
  403. #define IN_RCV_IND 0x00000080 // We're calling a rcv. indicate
  404. // handler.
  405. #define NEED_RCV_CMPLT 0x00000100 // We need to have recvs. completed.
  406. #define NEED_ACK 0x00000200 // We need to send an ACK.
  407. #define NEED_OUTPUT 0x00000400 // We need to output.
  408. #define DELAYED_FLAGS (NEED_RCV_CMPLT | NEED_ACK | NEED_OUTPUT)
  409. #define ACK_DELAYED 0x00000800 // We've delayed sending an ACK.
  410. #define PMTU_BH_PROBE 0x00001000 // We're probing for a PMTU BH.
  411. #define BSD_URGENT 0x00002000 // We're using BSD urgent semantics.
  412. #define IN_DELIV_URG 0x00004000 // We're in the DeliverUrgent routine.
  413. #define URG_VALID 0x00008000 // We've seen urgent data, and
  414. // the urgent data fields are valid.
  415. #define FIN_NEEDED 0x00010000 // We need to send a FIN.
  416. #define NAGLING 0x00020000 // We are using Nagle's algorithm.
  417. #define IN_TCP_SEND 0x00040000 // We're in TCPSend.
  418. #define FLOW_CNTLD 0x00080000 // We've received a zero window
  419. // from our peer.
  420. #define DISC_PENDING 0x00100000 // A disconnect notification is
  421. // pending.
  422. #define TW_PENDING 0x00200000 // We're waiting to finish going
  423. // to TIME-WAIT.
  424. #define FORCE_OUTPUT 0x00400000 // Output is being forced.
  425. #define FORCE_OUT_SHIFT 22 // Shift to get FORCE_OUTPUT into
  426. // low bit.
  427. #define SEND_AFTER_RCV 0x00800000 // We need to send after we get out
  428. // of recv.
  429. #define GC_PENDING 0x01000000 // A graceful close is pending.
  430. #define KEEPALIVE 0x02000000 // Doing keepalives on this TCB.
  431. #define URG_INLINE 0x04000000 // Urgent data to be processed
  432. // inline.
  433. #define SCALE_CWIN 0x08000000 // Increment CWin proportionally to
  434. // amount of data acknowledged.
  435. #define FIN_OUTSTANDING 0x10000000 // We've sent a FIN 'recently', i.e.
  436. // since the last retransmit. When
  437. // this flag is set sendnext ==
  438. // sendmax.
  439. #define FIN_OUTS_SHIFT 28 // Shift to FIN_OUTSTANDING bit into
  440. // low bit.
  441. #define FIN_SENT 0x20000000 // We've sent a FIN that hasn't
  442. // been acknowledged. Once this
  443. // bit has been turned on in
  444. // FIN-WAIT-1 the sequence number
  445. // of the FIN will be sendmax-1.
  446. #define NEED_RST 0x40000000 // We need to send a RST when
  447. // closing.
  448. #define IN_TCB_TABLE 0x80000000 // TCB is in the TCB table.
  449. #define IN_TWTCB_TABLE 0x80000000
  450. #define IN_TWQUEUE 0x00000001
  451. // N.B. SYNTCB flags share the same bits as TCB flags.
  452. #define IN_SYNTCB_TABLE IN_TCB_TABLE
  453. #define SYNTCB_SHARED_FLAGS (CONN_ACCEPTED | WINDOW_SET)
  454. //* The defintion of the 'slow flags'. If any of these flags are set we'll
  455. // be forced off of the fast path.
  456. #define TCP_SLOW_FLAGS (URG_VALID | FLOW_CNTLD | GC_PENDING | \
  457. TW_PENDING | DISC_NOTIFIED | IN_DELIV_URG | \
  458. FIN_NEEDED | FIN_SENT | FIN_OUTSTANDING | \
  459. DISC_PENDING | PMTU_BH_PROBE)
  460. //* Close reasons.
  461. #define TCB_CLOSE_RST 0x80 // Received a RST segment.
  462. #define TCB_CLOSE_ABORTED 0x40 // Had a local abort.
  463. #define TCB_CLOSE_TIMEOUT 0x20 // Connection timed out.
  464. #define TCB_CLOSE_REFUSED 0x10 // Connect attempt was refused.
  465. #define TCB_CLOSE_UNREACH 0x08 // Remote destination unreachable.
  466. #define TCB_CLOSE_SUCCESS 0x01 // Successfull close.
  467. //* TCB Timer macros.
  468. #define START_TCB_TIMER(t, v) (t) = (v)
  469. #define STOP_TCB_TIMER(t) (t) = 0
  470. #define TCB_TIMER_RUNNING(t) ((t) != 0)
  471. // Macro to compute retransmit timeout.
  472. #define REXMIT_TO(t) ((((t)->tcb_smrtt >> 2) + (t)->tcb_delta) >> 1)
  473. //* Definitons for pending actions. We define a PENDING_ACTION macro
  474. // that can be used to decide whether or not we can proceed with an
  475. // activity. The only pending action we really care about is DELETE - others
  476. // are low priority and can be put off.
  477. #define PENDING_ACTION(t) ((t)->tcb_pending & DEL_PENDING)
  478. #define DEL_PENDING 0x01 // Delete is pending.
  479. #define OPT_PENDING 0x02 // Option set is pending.
  480. #define FREE_PENDING 0x04 // Can be freed
  481. #define RST_PENDING 0x08 // RST-indication is pending.
  482. //* Macro to see if a TCB is closing.
  483. #define CLOSING(t) ((t)->tcb_pending & DEL_PENDING)
  484. //* Structure of a TCP packet header.
  485. struct TCPHeader {
  486. ushort tcp_src; // Source port.
  487. ushort tcp_dest; // Destination port.
  488. SeqNum tcp_seq; // Sequence number.
  489. SeqNum tcp_ack; // Ack number.
  490. ushort tcp_flags; // Flags and data offset.
  491. ushort tcp_window; // Window offered.
  492. ushort tcp_xsum; // Checksum.
  493. ushort tcp_urgent; // Urgent pointer.
  494. };
  495. typedef struct TCPHeader TCPHeader;
  496. //* Definitions for header flags.
  497. #define TCP_FLAG_FIN 0x00000100
  498. #define TCP_FLAG_SYN 0x00000200
  499. #define TCP_FLAG_RST 0x00000400
  500. #define TCP_FLAG_PUSH 0x00000800
  501. #define TCP_FLAG_ACK 0x00001000
  502. #define TCP_FLAG_URG 0x00002000
  503. #define TCP_FLAGS_ALL (TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | \
  504. TCP_FLAG_ACK | TCP_FLAG_URG)
  505. //* Flags in the tcb_fastchk field that are not in the TCP header proper.
  506. // Setting these flags forces us off the fast path.
  507. #define TCP_FLAG_SLOW 0x00000001 // Need to be on slow path.
  508. #define TCP_FLAG_IN_RCV 0x00000002 // In recv. path already.
  509. #define TCP_FLAG_FASTREC 0x00000004 // This is used to mark tcb
  510. #define TCP_FLAG_SEND_AND_DISC 0x00000008
  511. // former tcb_flag2 flags, now in tcb_fastchk
  512. #define TCP_FLAG_ACCEPT_PENDING 0x00000010
  513. #define TCP_FLAG_REQUEUE_FROM_SEND_AND_DISC 0x00000020
  514. #define TCP_FLAG_RST_WHILE_SYN 0x00000040 // Valid RST was received while
  515. // establishing outboud connct.
  516. #define TCP_OFFSET_MASK 0xf0
  517. #define TCP_HDR_SIZE(t) (uint)(((*(uchar *)&(t)->tcp_flags) & TCP_OFFSET_MASK) >> 2)
  518. #define MAKE_TCP_FLAGS(o, f) ((f) | ((o) << 4))
  519. #define TCP_OPT_EOL 0
  520. #define TCP_OPT_NOP 1
  521. #define TCP_OPT_MSS 2
  522. #define MSS_OPT_SIZE 4
  523. #define TCP_SACK_PERMITTED_OPT 4
  524. #define SACK_PERMITTED_OPT_SIZE 2 // SACK "permitted" option size, in SYN segments
  525. #define TCP_FLAG_SACK 0x00000004
  526. #define TCP_OPT_SACK 5 // Sack option
  527. #define ALIGNED_TS_OPT_SIZE 12
  528. #define TCP_OPT_WS 3 // Window scale option
  529. #define TCP_OPT_TS 8 // Time stamp option
  530. #define WS_OPT_SIZE 3
  531. #define TS_OPT_SIZE 10
  532. #define TCP_MAXWIN 65535 // maximum unscaled window size
  533. #define TCP_MAX_SCALED_WIN 0x3fffffff // maximum window with scaling
  534. #define TCP_MAX_WINSHIFT 14 // Maximum shift allowed
  535. #define TCP_MAX_SCALED_WIN 0x3fffffff // maximum window with scaling.
  536. #define TCP_FLAG_WS 0x00000001 // Flags in tcb_options for ws and ts
  537. #define TCP_FLAG_TS 0x00000002
  538. #define PAWS_IDLE 24*24*60*60*100 // Paws idle time - 24 days
  539. //* Convenient byte swapped structure for receives.
  540. struct TCPRcvInfo {
  541. SeqNum tri_seq; // Sequence number.
  542. SeqNum tri_ack; // Ack number.
  543. uint tri_window; // Window.
  544. uint tri_urgent; // Urgent pointer.
  545. uint tri_flags; // Flags.
  546. };
  547. typedef struct TCPRcvInfo TCPRcvInfo;
  548. //* General structure, at the start of all command specific request structures.
  549. #define tr_signature 0x20205254 // 'TR '
  550. struct TCPReq {
  551. #if DBG
  552. ulong tr_sig;
  553. #endif
  554. struct Queue tr_q; // Q linkage.
  555. CTEReqCmpltRtn tr_rtn; // Completion routine.
  556. PVOID tr_context; // User context.
  557. int tr_status; // Final complete status.
  558. };
  559. typedef struct TCPReq TCPReq;
  560. // structures to support SACK
  561. struct SackSeg {
  562. SeqNum begin;
  563. SeqNum end;
  564. };
  565. typedef struct SackSeg SackSeg;
  566. // Maximum 4 sack entries can be sent
  567. // so, size sack send block acordingly
  568. struct SACKSendBlock {
  569. uchar Mask[4];
  570. SackSeg Block[4];
  571. };
  572. typedef struct SACKSendBlock SACKSendBlock;
  573. // list of received sack entries
  574. struct SackListEntry {
  575. struct SackListEntry *next;
  576. SeqNum begin;
  577. SeqNum end;
  578. };
  579. typedef struct SackListEntry SackListEntry;
  580. struct ReservedPortListEntry {
  581. struct ReservedPortListEntry *next;
  582. ushort UpperRange;
  583. ushort LowerRange;
  584. };
  585. typedef struct ReservedPortListEntry ReservedPortListEntry;
  586. #ifdef POOL_TAGGING
  587. #ifdef ExAllocatePool
  588. #undef ExAllocatePool
  589. #endif
  590. #ifdef CTEAllocMemN
  591. #undef CTEAllocMemN
  592. #endif
  593. #define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, 'tPCT')
  594. #ifndef CTEAllocMem
  595. #error "CTEAllocMem is not already defined - will override tagging"
  596. #else
  597. #undef CTEAllocMem
  598. #endif
  599. #ifdef CTEAllocMemBoot
  600. #undef CTEAllocMemBoot
  601. #endif
  602. #if MILLEN
  603. #define CTEAllocMem(size) ExAllocatePoolWithTag(NonPagedPool, size, 'tPCT')
  604. #define CTEAllocMemN(size,tag) ExAllocatePoolWithTag(NonPagedPool, size, tag)
  605. #define CTEAllocMemLow(size,tag) ExAllocatePoolWithTag(NonPagedPool, size, tag)
  606. #define CTEAllocMemBoot(size) ExAllocatePoolWithTag(NonPagedPool, size, 'tPCT')
  607. #else // MILLEN
  608. #define CTEAllocMem(size) ExAllocatePoolWithTagPriority(NonPagedPool, size, 'tPCT', NormalPoolPriority)
  609. #define CTEAllocMemN(size,tag) ExAllocatePoolWithTagPriority(NonPagedPool, size, tag,NormalPoolPriority)
  610. #define CTEAllocMemLow(size,tag) ExAllocatePoolWithTagPriority(NonPagedPool, size, tag,LowPoolPriority)
  611. #define CTEAllocMemBoot(size) ExAllocatePoolWithTag(NonPagedPool, size, 'tPCT')
  612. #endif // !MILLEN
  613. #endif // POOL_TAGGING
  614. #if TRACE_EVENT
  615. #define _WMIKM_
  616. #include "evntrace.h"
  617. #include "wmikm.h"
  618. #include "wmistr.h"
  619. #define EVENT_TRACE_GROUP_TCPIP 0x0600
  620. #define EVENT_TRACE_GROUP_UDPIP 0x0800
  621. typedef VOID (*PTDI_DATA_REQUEST_NOTIFY_ROUTINE)(
  622. IN ULONG EventType,
  623. IN PVOID DataBlock,
  624. IN ULONG Size,
  625. IN PETHREAD Thread);
  626. extern PTDI_DATA_REQUEST_NOTIFY_ROUTINE TCPCPHandlerRoutine;
  627. typedef struct _CPTRACE_DATABLOCK {
  628. IPAddr saddr;
  629. IPAddr daddr;
  630. ushort sport;
  631. ushort dport;
  632. uint size;
  633. HANDLE cpcontext;
  634. } CPTRACE_DATABLOCK, *PCPTRACE_BLOCK;
  635. #endif
  636. //
  637. // TCP endpoint context structure allocated for each open of TCP/UDP.
  638. // A pointer to this structure is stored in FileObject->FsContext.
  639. //
  640. typedef struct _TCP_CONTEXT {
  641. union {
  642. HANDLE AddressHandle;
  643. CONNECTION_CONTEXT ConnectionContext;
  644. HANDLE ControlChannel;
  645. } Handle;
  646. ULONG ReferenceCount;
  647. BOOLEAN CancelIrps;
  648. BOOLEAN Cleanup;
  649. #if DBG
  650. LIST_ENTRY PendingIrpList;
  651. LIST_ENTRY CancelledIrpList;
  652. #endif
  653. KEVENT CleanupEvent;
  654. UINT_PTR Conn;
  655. PIRP Irp;
  656. DEFINE_LOCK_STRUCTURE(EndpointLock)
  657. } TCP_CONTEXT, *PTCP_CONTEXT;
  658. #define MAJOR_TDI_VERSION 2
  659. #define MINOR_TDI_VERSION 0
  660. extern HANDLE DgHeaderPool;
  661. //* Definition of an AO request structure. There structures are used only for
  662. // queuing delete and option get/set requests.
  663. #define aor_signature 0x20524F41
  664. typedef struct AORequest {
  665. struct AODeleteRequest {
  666. #if DBG
  667. ulong aor_sig;
  668. #endif
  669. CTEReqCmpltRtn aor_rtn; // Request complete routine for
  670. // this request.
  671. PVOID aor_context; // Request context.
  672. uint aor_type; // Request type.
  673. };
  674. struct AORequest *aor_next; // Next pointer in chain.
  675. uint aor_id; // ID for the request.
  676. uint aor_length; // Length of buffer.
  677. void *aor_buffer; // Buffer for this request.
  678. } AORequest;
  679. //
  680. // Values of aor_type
  681. //
  682. #define AOR_TYPE_GET_OPTIONS 1
  683. #define AOR_TYPE_SET_OPTIONS 2
  684. #define AOR_TYPE_REVALIDATE_MCAST 3
  685. #define AOR_TYPE_DELETE 4
  686. #define AOR_TYPE_CONNECT 5
  687. #define AOR_TYPE_DISCONNECT 6
  688. extern AORequest *AORequestBlockPtr;
  689. #include "tcpdeb.h"
  690. #if REFERENCE_DEBUG
  691. uint
  692. TcpReferenceTCB (
  693. IN TCB *RefTCB,
  694. IN uchar *File,
  695. IN uint Line
  696. );
  697. uint
  698. TcpDereferenceTCB (
  699. IN TCB *DerefTCB,
  700. IN uchar *File,
  701. IN uint Line
  702. );
  703. #define REFERENCE_TCB(_a) TcpReferenceTCB((_a), (PUCHAR)__FILE__, __LINE__)
  704. #define DEREFERENCE_TCB(_a) TcpDereferenceTCB((_a), (PUCHAR)__FILE__, __LINE__)
  705. #else // REFERENCE_DEBUG
  706. #define REFERENCE_TCB(_a) ++(_a)->tcb_refcnt
  707. #define DEREFERENCE_TCB(_a) --(_a)->tcb_refcnt
  708. #endif // REFERENCE_DEBUG
  709. #endif // _TCP_INCLUDED_