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.

303 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. if.h
  5. Abstract:
  6. This file contains declarations for interface management.
  7. Author:
  8. Abolade Gbadegesin (t-abolag) 12-July-1997
  9. Revision History:
  10. --*/
  11. #ifndef _NAT_IF_H_
  12. #define _NAT_IF_H_
  13. //
  14. // Structure: NAT_ADDRESS
  15. //
  16. // This structure holds an address in an interface's list of binding-addresses.
  17. //
  18. typedef struct _NAT_ADDRESS {
  19. ULONG Address;
  20. ULONG Mask;
  21. ULONG NegatedClassMask;
  22. } NAT_ADDRESS, *PNAT_ADDRESS;
  23. struct _NAT_INTERFACE;
  24. //
  25. // Structure: NAT_INTERFACE
  26. //
  27. // Holds configuration/operational information for a NAT interface.
  28. //
  29. // Synchronization on an interface makes use of an interface-list lock
  30. // ('InterfaceLock'), a per-interface reference count, and a per-interface
  31. // spin-lock:
  32. //
  33. // Acquiring a reference to an interface guarantees the interface's existence;
  34. // acquiring the interface's spin-lock guarantees the interface's consistency.
  35. //
  36. // To acquire a reference, first acquire the interface-list lock;
  37. // to traverse the interface-list, first acquire the interface-list lock.
  38. //
  39. // An interface's spin-lock can only be acquired if
  40. // (a) a reference to the interface has been acquired, or
  41. // (b) the interface-list lock is currently held.
  42. // Note that holding the list lock alone does not guarantee consistency.
  43. //
  44. // Each session being associated with an interface is linked into its
  45. // of mappings ('MappingList'). Access to this list of mappings must also
  46. // be synchronized. This is achieved using 'InterfaceMappingLock', which
  47. // must be acquired before modifying any interface's list of mappings.
  48. // See 'MAPPING.H' for further details.
  49. //
  50. // N.B. On the rare occasions when 'MappingLock' must be held at the same time
  51. // as one of 'InterfaceLock', 'EditorLock', and 'DirectorLock', 'MappingLock'
  52. // must always be acquired first.
  53. //
  54. typedef struct _NAT_INTERFACE {
  55. LIST_ENTRY Link;
  56. ULONG ReferenceCount;
  57. KSPIN_LOCK Lock;
  58. ULONG Index; // read-only
  59. PFILE_OBJECT FileObject; // read-only
  60. //
  61. // Configuration information
  62. //
  63. PIP_NAT_INTERFACE_INFO Info;
  64. ULONG Flags;
  65. ULONG AddressRangeCount;
  66. PIP_NAT_ADDRESS_RANGE AddressRangeArray;
  67. ULONG PortMappingCount;
  68. PIP_NAT_PORT_MAPPING PortMappingArray;
  69. ULONG AddressMappingCount;
  70. PIP_NAT_ADDRESS_MAPPING AddressMappingArray;
  71. ULONG IcmpFlags;
  72. USHORT MTU;
  73. //
  74. // Binding information
  75. //
  76. ULONG AddressCount; // read-only
  77. PNAT_ADDRESS AddressArray; // read-only
  78. //
  79. // Operational information
  80. //
  81. ULONG NoStaticMappingExists; // interlocked-access only
  82. ULONG FreeMapCount;
  83. PNAT_FREE_ADDRESS FreeMapArray;
  84. PNAT_USED_ADDRESS UsedAddressTree;
  85. LIST_ENTRY UsedAddressList;
  86. LIST_ENTRY TicketList;
  87. LIST_ENTRY MappingList;
  88. //
  89. // Statistical information
  90. //
  91. IP_NAT_INTERFACE_STATISTICS Statistics;
  92. } NAT_INTERFACE, *PNAT_INTERFACE;
  93. //
  94. // Flags
  95. //
  96. #define NAT_INTERFACE_BOUNDARY(Interface) \
  97. ((Interface)->Flags & IP_NAT_INTERFACE_FLAGS_BOUNDARY)
  98. #define NAT_INTERFACE_NAPT(Interface) \
  99. ((Interface)->Flags & IP_NAT_INTERFACE_FLAGS_NAPT)
  100. #define NAT_INTERFACE_FW(Interface) \
  101. ((Interface)->Flags & IP_NAT_INTERFACE_FLAGS_FW)
  102. #define NAT_INTERFACE_FLAGS_DELETED 0x80000000
  103. #define NAT_INTERFACE_DELETED(Interface) \
  104. ((Interface)->Flags & NAT_INTERFACE_FLAGS_DELETED)
  105. #define NAT_INTERFACE_ALLOW_ICMP(Interface, MessageCode) \
  106. ((Interface)->IcmpFlags & (1 << MessageCode))
  107. //
  108. // Defines the depth of the lookaside list for allocating ICMP mappings
  109. //
  110. #define ICMP_LOOKASIDE_DEPTH 10
  111. //
  112. // Defines the depth of the lookaside list for allocating IP mappings
  113. //
  114. #define IP_LOOKASIDE_DEPTH 10
  115. //
  116. // Minimum interface MTU
  117. //
  118. #define MIN_VALID_MTU 68
  119. //
  120. // GLOBAL DATA DECLARATIONS
  121. //
  122. extern ULONG FirewalledInterfaceCount;
  123. extern CACHE_ENTRY InterfaceCache[CACHE_SIZE];
  124. extern ULONG InterfaceCount;
  125. extern LIST_ENTRY InterfaceList;
  126. extern KSPIN_LOCK InterfaceLock;
  127. extern KSPIN_LOCK InterfaceMappingLock;
  128. //
  129. // INTERFACE MANAGEMENT ROUTINES
  130. //
  131. VOID
  132. NatCleanupInterface(
  133. IN PNAT_INTERFACE Interfacep
  134. );
  135. NTSTATUS
  136. NatConfigureInterface(
  137. IN PIP_NAT_INTERFACE_INFO InterfaceInfo,
  138. IN PFILE_OBJECT FileObject
  139. );
  140. NTSTATUS
  141. NatCreateInterface(
  142. IN PIP_NAT_CREATE_INTERFACE CreateInterface,
  143. IN PFILE_OBJECT FileObject
  144. );
  145. VOID
  146. NatDeleteAnyAssociatedInterface(
  147. PFILE_OBJECT FileObject
  148. );
  149. NTSTATUS
  150. NatDeleteInterface(
  151. IN ULONG Index,
  152. IN PFILE_OBJECT FileObject
  153. );
  154. //
  155. // BOOLEAN
  156. // NatDereferenceInterface(
  157. // PNAT_INTERFACE Interfacep
  158. // );
  159. //
  160. #define \
  161. NatDereferenceInterface( \
  162. _Interfacep \
  163. ) \
  164. (InterlockedDecrement(&(_Interfacep)->ReferenceCount) \
  165. ? TRUE \
  166. : (NatCleanupInterface(_Interfacep), FALSE))
  167. VOID
  168. NatInitializeInterfaceManagement(
  169. VOID
  170. );
  171. PIP_NAT_ADDRESS_MAPPING
  172. NatLookupAddressMappingOnInterface(
  173. IN PNAT_INTERFACE Interfacep,
  174. IN ULONG PublicAddress
  175. );
  176. //
  177. // PNAT_INTERFACE
  178. // NatLookupCachedInterface(
  179. // IN ULONG Index,
  180. // IN OUT PNAT_INTERFACE Interfacep
  181. // );
  182. //
  183. #define \
  184. NatLookupCachedInterface( \
  185. _Index, \
  186. _Interfacep \
  187. ) \
  188. ((((_Interfacep) = InterlockedProbeCache(InterfaceCache, (_Index))) && \
  189. (_Interfacep)->Index == (_Index) && \
  190. !NAT_INTERFACE_DELETED((_Interfacep))) \
  191. ? (_Interfacep) \
  192. : (((_Interfacep) = NatLookupInterface((_Index), NULL)) \
  193. ? (InterlockedUpdateCache(InterfaceCache,(_Index),(_Interfacep)), \
  194. (_Interfacep)) \
  195. : NULL))
  196. PNAT_INTERFACE
  197. NatLookupInterface(
  198. IN ULONG Index,
  199. OUT PLIST_ENTRY* InsertionPoint OPTIONAL
  200. );
  201. PIP_NAT_PORT_MAPPING
  202. NatLookupPortMappingOnInterface(
  203. IN PNAT_INTERFACE Interfacep,
  204. IN UCHAR Protocol,
  205. IN USHORT PublicPort
  206. );
  207. VOID
  208. NatMappingAttachInterface(
  209. PNAT_INTERFACE Interfacep,
  210. PVOID InterfaceContext,
  211. PNAT_DYNAMIC_MAPPING Mapping
  212. );
  213. VOID
  214. NatMappingDetachInterface(
  215. PNAT_INTERFACE Interfacep,
  216. PVOID InterfaceContext,
  217. PNAT_DYNAMIC_MAPPING Mapping
  218. );
  219. NTSTATUS
  220. NatQueryInformationInterface(
  221. IN ULONG Index,
  222. IN PIP_NAT_INTERFACE_INFO InterfaceInfo,
  223. IN PULONG Size
  224. );
  225. NTSTATUS
  226. NatQueryStatisticsInterface(
  227. IN ULONG Index,
  228. IN PIP_NAT_INTERFACE_STATISTICS InterfaceStatistics
  229. );
  230. //
  231. // BOOLEAN
  232. // NatReferenceInterface(
  233. // PNAT_INTERFACE Interfacep
  234. // );
  235. //
  236. #define \
  237. NatReferenceInterface( \
  238. _Interfacep \
  239. ) \
  240. (NAT_INTERFACE_DELETED(_Interfacep) \
  241. ? FALSE \
  242. : (InterlockedIncrement(&(_Interfacep)->ReferenceCount), TRUE))
  243. VOID
  244. NatResetInterface(
  245. IN PNAT_INTERFACE Interfacep
  246. );
  247. VOID
  248. NatShutdownInterfaceManagement(
  249. VOID
  250. );
  251. #endif // _NAT_IF_H_