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.

243 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. NsPacket.h
  5. Abstract:
  6. Declarations for IpSec NAT shim packet handling routines
  7. Author:
  8. Jonathan Burstein (jonburs) 10-July-2001
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #pragma once
  14. typedef enum
  15. {
  16. NsInboundDirection = 0,
  17. NsOutboundDirection,
  18. NsMaximumDirection
  19. } IPSEC_NATSHIM_DIRECTION, *PIPSEC_NATSHIM_DIRECTION;
  20. //
  21. // Structure: NS_PACKET_CONTEXT
  22. //
  23. // This structure holds context information for a packet as it is
  24. // passed through the processing code. The majority of packet parsing
  25. // and verification is done when this structure is filled out.
  26. //
  27. typedef struct _NS_PACKET_CONTEXT
  28. {
  29. IPHeader UNALIGNED *pIpHeader;
  30. ULONG ulSourceAddress;
  31. ULONG ulDestinationAddress;
  32. USHORT usSourcePort;
  33. USHORT usDestinationPort;
  34. union {
  35. TCP_HEADER UNALIGNED *pTcpHeader;
  36. UDP_HEADER UNALIGNED *pUdpHeader;
  37. ICMP_HEADER UNALIGNED *pIcmpHeader;
  38. PVOID pvProtocolHeader;
  39. };
  40. ULONG ulProtocolHeaderLength;
  41. UCHAR ucProtocol;
  42. } NS_PACKET_CONTEXT, *PNS_PACKET_CONTEXT;
  43. //
  44. // Forward Declarations
  45. //
  46. struct _NS_CONNECTION_ENTRY;
  47. #define PNS_CONNECTION_ENTRY struct _NS_CONNECTION_ENTRY*
  48. //
  49. // Functional signature macro
  50. //
  51. #define PACKET_ROUTINE(Name) \
  52. NTSTATUS \
  53. Name( \
  54. PNS_CONNECTION_ENTRY pConnection, \
  55. PNS_PACKET_CONTEXT pContext \
  56. );
  57. typedef PACKET_ROUTINE((FASTCALL*PNS_PACKET_ROUTINE));
  58. //
  59. // Prototypes: NS_PACKET_ROUTINE
  60. //
  61. // These routines are called for each packet that matches a
  62. // connection entry. During connection entry initialization
  63. // the PacketRoutine fileds are filled in based on the specifics
  64. // of the connnection.
  65. //
  66. // By using separate routines in this manner it will never be
  67. // necessary to branch on such things as protocol, path, or whether
  68. // or not remote port translation is needed on the main packet
  69. // processing path. Such decisions are made only during connection
  70. // entry creation.
  71. //
  72. PACKET_ROUTINE(FASTCALL NsInboundTcpPacketRoutine)
  73. PACKET_ROUTINE(FASTCALL NsOutboundTcpPacketRoutine)
  74. PACKET_ROUTINE(FASTCALL NsInboundUdpPacketRoutine)
  75. PACKET_ROUTINE(FASTCALL NsOutboundUdpPacketRoutine)
  76. PACKET_ROUTINE(FASTCALL NsInboundTcpTranslatePortPacketRoutine)
  77. PACKET_ROUTINE(FASTCALL NsOutboundTcpTranslatePortPacketRoutine)
  78. PACKET_ROUTINE(FASTCALL NsInboundUdpTranslatePortPacketRoutine)
  79. PACKET_ROUTINE(FASTCALL NsOutboundUdpTranslatePortPacketRoutine)
  80. //
  81. // Checksum manipulation macros
  82. //
  83. //
  84. // Fold carry-bits of a checksum into the low-order word
  85. //
  86. #define CHECKSUM_FOLD(xsum) \
  87. (xsum) = (USHORT)(xsum) + ((xsum) >> 16); \
  88. (xsum) += ((xsum) >> 16)
  89. //
  90. // Sum the words of a 32-bit value into a checksum
  91. //
  92. #define CHECKSUM_LONG(xsum,l) \
  93. (xsum) += (USHORT)(l) + (USHORT)((l) >> 16)
  94. //
  95. // Transfer a checksum to or from the negated format sent on the network
  96. //
  97. #define CHECKSUM_XFER(dst,src) \
  98. (dst) = (USHORT)~(src)
  99. //
  100. // Update the checksum field 'x' using standard variables 'ulChecksum' and
  101. // 'ulChecksumDelta'
  102. //
  103. #define CHECKSUM_UPDATE(x) \
  104. CHECKSUM_XFER(ulChecksum, (x)); \
  105. ulChecksum += ulChecksumDelta; \
  106. CHECKSUM_FOLD(ulChecksum); \
  107. CHECKSUM_XFER((x), ulChecksum)
  108. //
  109. // Function Prototypes
  110. //
  111. __forceinline
  112. NTSTATUS
  113. NsBuildPacketContext(
  114. IPHeader UNALIGNED *pIpHeader,
  115. PVOID pvProtocolHeader,
  116. ULONG ulProtocolHeaderLength,
  117. PNS_PACKET_CONTEXT pContext
  118. )
  119. {
  120. if (NULL == pIpHeader)
  121. {
  122. return STATUS_INVALID_PARAMETER;
  123. }
  124. pContext->pIpHeader = pIpHeader;
  125. pContext->ulSourceAddress = pIpHeader->iph_src;
  126. pContext->ulDestinationAddress = pIpHeader->iph_dest;
  127. pContext->ulProtocolHeaderLength = ulProtocolHeaderLength;
  128. pContext->ucProtocol = pIpHeader->iph_protocol;
  129. switch (pContext->ucProtocol)
  130. {
  131. case NS_PROTOCOL_ICMP:
  132. {
  133. if (NULL == pvProtocolHeader
  134. || ulProtocolHeaderLength < FIELD_OFFSET(ICMP_HEADER, EncapsulatedIpHeader))
  135. {
  136. return STATUS_INVALID_PARAMETER;
  137. }
  138. pContext->pIcmpHeader = pvProtocolHeader;
  139. break;
  140. }
  141. case NS_PROTOCOL_TCP:
  142. {
  143. if (NULL == pvProtocolHeader
  144. || ulProtocolHeaderLength < sizeof(TCP_HEADER))
  145. {
  146. return STATUS_INVALID_PARAMETER;
  147. }
  148. pContext->pTcpHeader = pvProtocolHeader;
  149. pContext->usSourcePort = pContext->pTcpHeader->SourcePort;
  150. pContext->usDestinationPort = pContext->pTcpHeader->DestinationPort;
  151. break;
  152. }
  153. case NS_PROTOCOL_UDP:
  154. {
  155. if (NULL == pvProtocolHeader
  156. || ulProtocolHeaderLength < sizeof(UDP_HEADER))
  157. {
  158. return STATUS_INVALID_PARAMETER;
  159. }
  160. pContext->pUdpHeader = pvProtocolHeader;
  161. pContext->usSourcePort = pContext->pUdpHeader->SourcePort;
  162. pContext->usDestinationPort = pContext->pUdpHeader->DestinationPort;
  163. break;
  164. }
  165. default:
  166. {
  167. pContext->pvProtocolHeader = pvProtocolHeader;
  168. break;
  169. }
  170. }
  171. return STATUS_SUCCESS;
  172. } // NsBuildPacketContext
  173. NTSTATUS
  174. NsInitializePacketManagement(
  175. VOID
  176. );
  177. NTSTATUS
  178. NsProcessOutgoingPacket(
  179. IPHeader UNALIGNED *pIpHeader,
  180. PVOID pvProtocolHeader,
  181. ULONG ulProtocolHeaderSize,
  182. PVOID *ppvIpSecContext
  183. );
  184. NTSTATUS
  185. NsProcessIncomingPacket(
  186. IPHeader UNALIGNED *pIpHeader,
  187. PVOID pvProtocolHeader,
  188. ULONG ulProtocolHeaderSize,
  189. PVOID pvIpSecContext
  190. );
  191. VOID
  192. NsShutdownPacketManagement(
  193. VOID
  194. );
  195. #undef PNS_CONNECTION_ENTRY