Source code of Windows XP (NT5)
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.

218 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. pool.h
  5. Abstract:
  6. This header contains declarations for the management of the NAT's pools
  7. of addresses and ports.
  8. Author:
  9. Abolade Gbadegesin (t-abolag) 12-July-1997
  10. Revision History:
  11. --*/
  12. #ifndef _NAT_POOL_H_
  13. #define _NAT_POOL_H_
  14. //
  15. // forward declaration
  16. //
  17. struct _NAT_INTERFACE;
  18. #define PNAT_INTERFACE struct _NAT_INTERFACE*
  19. //
  20. // Structure: NAT_FREE_ADDRESS
  21. //
  22. // Represents a range of free addresses
  23. // Each interface with address-ranges holds an array of this structure,
  24. // which keep track of which addresses are in use and which are free.
  25. //
  26. typedef struct _NAT_FREE_ADDRESS {
  27. ULONG StartAddress;
  28. ULONG EndAddress;
  29. ULONG SubnetMask;
  30. PRTL_BITMAP Bitmap;
  31. } NAT_FREE_ADDRESS, *PNAT_FREE_ADDRESS;
  32. //
  33. // Structure: NAT_USED_ADDRESS
  34. //
  35. // Represents an address which is in use.
  36. //
  37. // Each address is an entry on its interface's list of in-use addresses
  38. // from the address pool. In addition to the address pool, entries are made
  39. // for each binding on the interface (i.e. each local address).
  40. //
  41. // Each address is also included in the interface's splay tree of addresses,
  42. // sorted on 'PrivateAddress'.
  43. //
  44. // Any address which is mapped statically to a private-address will have
  45. // the flag NAT_POOL_FLAG_STATIC set, and the field 'Mapping' will point
  46. // to the entry in the interface's configuration for the static address-mapping.
  47. //
  48. // When a session cannot be assigned a unique address, an in-use address
  49. // may be used for the session if the interface has port-translation enabled.
  50. // In this event, the field 'ReferenceCount' is incremented.
  51. //
  52. // Each in-use address is initialized with ranges for free UDP and TCP ports
  53. // (stored in network order). NextPortToTry is used to keep track of where
  54. // to start the search for an unconflicting port the next time an allocation
  55. // is requested; this is also in network order.
  56. //
  57. typedef struct _NAT_USED_ADDRESS {
  58. RTL_SPLAY_LINKS SLink;
  59. LIST_ENTRY Link;
  60. ULONG64 Key;
  61. ULONG PrivateAddress;
  62. ULONG PublicAddress;
  63. struct _NAT_USED_ADDRESS* SharedAddress;
  64. PIP_NAT_ADDRESS_MAPPING AddressMapping;
  65. ULONG Flags;
  66. ULONG ReferenceCount;
  67. USHORT StartPort;
  68. USHORT EndPort;
  69. USHORT NextPortToTry;
  70. } NAT_USED_ADDRESS, *PNAT_USED_ADDRESS;
  71. #define MAKE_USED_ADDRESS_KEY(priv,pub) \
  72. ((ULONG64)(((ULONG64)(priv) << 32) | (ULONG)(pub)))
  73. //
  74. // Used-list entry is deleted
  75. //
  76. #define NAT_POOL_FLAG_DELETED 0x80000000
  77. #define NAT_POOL_DELETED(a) \
  78. ((a)->Flags & NAT_POOL_FLAG_DELETED)
  79. //
  80. // Used-list entry is for a static mapping
  81. //
  82. #define NAT_POOL_FLAG_STATIC 0x00000001
  83. #define NAT_POOL_STATIC(a) \
  84. ((a)->Flags & NAT_POOL_FLAG_STATIC)
  85. //
  86. // Used-list entry is for an interface's binding (i.e. local address)
  87. //
  88. #define NAT_POOL_FLAG_BINDING 0x00000008
  89. #define NAT_POOL_BINDING(a) \
  90. ((a)->Flags & NAT_POOL_FLAG_BINDING)
  91. //
  92. // Used-list entry is a placeholder for a shared address
  93. //
  94. #define NAT_POOL_FLAG_PLACEHOLDER 0x00000010
  95. #define NAT_POOL_PLACEHOLDER(a) \
  96. ((a)->Flags & NAT_POOL_FLAG_PLACEHOLDER)
  97. //
  98. // Macro for obtaining a placeholder's shared-address
  99. //
  100. #define PLACEHOLDER_TO_ADDRESS(a) \
  101. ((a) = NAT_POOL_PLACEHOLDER(a) ? (a)->SharedAddress : (a))
  102. //
  103. // POOL MANAGEMENT ROUTINES
  104. //
  105. NTSTATUS
  106. NatAcquireEndpointFromAddressPool(
  107. PNAT_INTERFACE Interfacep,
  108. ULONG64 PrivateKey,
  109. ULONG64 RemoteKey,
  110. ULONG PublicAddress,
  111. USHORT PreferredPort,
  112. BOOLEAN AllowAnyPort,
  113. PNAT_USED_ADDRESS* AddressAcquired,
  114. PUSHORT PortAcquired
  115. );
  116. NTSTATUS
  117. NatAcquireFromAddressPool(
  118. PNAT_INTERFACE Interfacep,
  119. ULONG PrivateAddress,
  120. ULONG PublicAddress OPTIONAL,
  121. PNAT_USED_ADDRESS* AddressAcquired
  122. );
  123. NTSTATUS
  124. NatAcquireFromPortPool(
  125. PNAT_INTERFACE Interfacep,
  126. PNAT_USED_ADDRESS Addressp,
  127. UCHAR Protocol,
  128. USHORT PreferredPort,
  129. PUSHORT PortAcquired
  130. );
  131. NTSTATUS
  132. NatCreateAddressPool(
  133. PNAT_INTERFACE Interfacep
  134. );
  135. NTSTATUS
  136. NatCreateStaticPortMapping(
  137. PNAT_INTERFACE Interfacep,
  138. PIP_NAT_PORT_MAPPING PortMapping
  139. );
  140. NTSTATUS
  141. NatDeleteAddressPool(
  142. PNAT_INTERFACE Interfacep
  143. );
  144. NTSTATUS
  145. NatDereferenceAddressPoolEntry(
  146. PNAT_INTERFACE Interfacep,
  147. PNAT_USED_ADDRESS AddressToRelease
  148. );
  149. PNAT_USED_ADDRESS
  150. NatLookupAddressPoolEntry(
  151. PNAT_USED_ADDRESS Root,
  152. ULONG PrivateAddress,
  153. ULONG PublicAddress,
  154. PNAT_USED_ADDRESS* InsertionPoint
  155. );
  156. PNAT_USED_ADDRESS
  157. NatLookupStaticAddressPoolEntry(
  158. PNAT_INTERFACE Interfacep,
  159. ULONG PublicAddress,
  160. BOOLEAN RequireInboundSessions
  161. );
  162. //
  163. // VOID
  164. // NatReferenceAddressPoolEntry(
  165. // PNAT_USED_ADDRESS Addressp
  166. // );
  167. //
  168. #define \
  169. NatReferenceAddressPoolEntry( \
  170. _Addressp \
  171. ) \
  172. (NAT_INTERFACE_DELETED(_Addressp) \
  173. ? FALSE \
  174. : (InterlockedIncrement(&(_Addressp)->ReferenceCount), TRUE))
  175. #undef PNAT_INTERFACE
  176. #endif // _NAT_POOL_H_