adapter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * TAP-Windows -- A kernel driver to provide virtual tap
  3. * device functionality on Windows.
  4. *
  5. * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
  6. *
  7. * This source code is Copyright (C) 2002-2014 OpenVPN Technologies, Inc.,
  8. * and is released under the GPL version 2 (see below).
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program (see the file COPYING included with this
  21. * distribution); if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #ifndef __TAP_ADAPTER_CONTEXT_H_
  25. #define __TAP_ADAPTER_CONTEXT_H_
  26. #include "tap.h"
  27. // Memory allocation tags.
  28. #define TAP_ADAPTER_TAG ((ULONG)'ApaT') // "TapA
  29. #define TAP_RX_NBL_TAG ((ULONG)'RpaT') // "TapR
  30. #define TAP_RX_INJECT_BUFFER_TAG ((ULONG)'IpaT') // "TapI
  31. #define TAP_MAX_NDIS_NAME_LENGTH 64 // 38 character GUID string plus extra..
  32. // TAP receive indication NBL flag definitions.
  33. #define TAP_RX_NBL_FLAGS NBL_FLAGS_MINIPORT_RESERVED
  34. #define TAP_RX_NBL_FLAGS_CLEAR_ALL(_NBL) ((_NBL)->Flags &= ~TAP_RX_NBL_FLAGS)
  35. #define TAP_RX_NBL_FLAG_SET(_NBL, _F) ((_NBL)->Flags |= ((_F) & TAP_RX_NBL_FLAGS))
  36. #define TAP_RX_NBL_FLAG_CLEAR(_NBL, _F) ((_NBL)->Flags &= ~((_F) & TAP_RX_NBL_FLAGS))
  37. #define TAP_RX_NBL_FLAG_TEST(_NBL, _F) (((_NBL)->Flags & ((_F) & TAP_RX_NBL_FLAGS)) != 0)
  38. #define TAP_RX_NBL_FLAGS_IS_P2P 0x00001000
  39. #define TAP_RX_NBL_FLAGS_IS_INJECTED 0x00002000
  40. // MSDN Ref: http://msdn.microsoft.com/en-us/library/windows/hardware/ff560490(v=vs.85).aspx
  41. typedef
  42. enum _TAP_MINIPORT_ADAPTER_STATE
  43. {
  44. // The Halted state is the initial state of all adapters. When an
  45. // adapter is in the Halted state, NDIS can call the driver's
  46. // MiniportInitializeEx function to initialize the adapter.
  47. MiniportHaltedState,
  48. // In the Shutdown state, a system shutdown and restart must occur
  49. // before the system can use the adapter again.
  50. MiniportShutdownState,
  51. // In the Initializing state, a miniport driver completes any
  52. //operations that are required to initialize an adapter.
  53. MiniportInitializingState,
  54. // Entering the Paused state...
  55. MiniportPausingState,
  56. // In the Paused state, the adapter does not indicate received
  57. // network data or accept send requests.
  58. MiniportPausedState,
  59. // In the Running state, a miniport driver performs send and
  60. // receive processing for an adapter.
  61. MiniportRunning,
  62. // In the Restarting state, a miniport driver completes any
  63. // operations that are required to restart send and receive
  64. // operations for an adapter.
  65. MiniportRestartingState
  66. } TAP_MINIPORT_ADAPTER_STATE, *PTAP_MINIPORT_ADAPTER_STATE;
  67. //
  68. // Each adapter managed by this driver has a TapAdapter struct.
  69. // ------------------------------------------------------------
  70. // Since there is a one-to-one relationship between adapter instances
  71. // and device instances this structure is the device extension as well.
  72. //
  73. typedef struct _TAP_ADAPTER_CONTEXT
  74. {
  75. LIST_ENTRY AdapterListLink;
  76. volatile LONG RefCount;
  77. NDIS_HANDLE MiniportAdapterHandle;
  78. NDIS_SPIN_LOCK AdapterLock; // Lock for protection of state and outstanding sends and recvs
  79. //
  80. // All fields that are protected by the AdapterLock are included
  81. // in the Locked structure to remind us to take the Lock
  82. // before accessing them :)
  83. //
  84. struct
  85. {
  86. TAP_MINIPORT_ADAPTER_STATE AdapterState;
  87. } Locked;
  88. BOOLEAN ResetInProgress;
  89. //
  90. // NetCfgInstanceId as UNICODE_STRING
  91. // ----------------------------------
  92. // This a GUID string provided by NDIS that identifies the adapter instance.
  93. // An example is:
  94. //
  95. // NetCfgInstanceId={410EB49D-2381-4FE7-9B36-498E22619DF0}
  96. //
  97. // Other names are derived from NetCfgInstanceId. For example, MiniportName:
  98. //
  99. // MiniportName=\DEVICE\{410EB49D-2381-4FE7-9B36-498E22619DF0}
  100. //
  101. NDIS_STRING NetCfgInstanceId;
  102. WCHAR NetCfgInstanceIdBuffer[TAP_MAX_NDIS_NAME_LENGTH];
  103. # define MINIPORT_INSTANCE_ID(a) ((a)->NetCfgInstanceIdAnsi.Buffer)
  104. ANSI_STRING NetCfgInstanceIdAnsi; // Used occasionally
  105. ULONG MtuSize; // 1500 byte (typical)
  106. // TRUE if adapter should always be "connected" even when device node
  107. // is not open by a userspace process.
  108. //
  109. // FALSE if connection state is application controlled.
  110. BOOLEAN MediaStateAlwaysConnected;
  111. // TRUE if device is "connected".
  112. BOOLEAN LogicalMediaState;
  113. NDIS_DEVICE_POWER_STATE CurrentPowerState;
  114. BOOLEAN AllowNonAdmin;
  115. MACADDR PermanentAddress; // From registry, if available
  116. MACADDR CurrentAddress;
  117. // Device registration parameters from NdisRegisterDeviceEx.
  118. NDIS_STRING DeviceName;
  119. WCHAR DeviceNameBuffer[TAP_MAX_NDIS_NAME_LENGTH];
  120. NDIS_STRING LinkName;
  121. WCHAR LinkNameBuffer[TAP_MAX_NDIS_NAME_LENGTH];
  122. NDIS_HANDLE DeviceHandle;
  123. PDEVICE_OBJECT DeviceObject;
  124. BOOLEAN TapDeviceCreated; // WAS: m_TapIsRunning
  125. PFILE_OBJECT TapFileObject; // Exclusive access
  126. BOOLEAN TapFileIsOpen; // WAS: m_TapOpens
  127. LONG TapFileOpenCount; // WAS: m_NumTapOpens
  128. // Cancel-Safe read IRP queue.
  129. TAP_IRP_CSQ PendingReadIrpQueue;
  130. // Queue containing TAP packets representing host send NBs. These are
  131. // waiting to be read by user-mode application.
  132. TAP_PACKET_QUEUE SendPacketQueue;
  133. // NBL pool for making TAP receive indications.
  134. NDIS_HANDLE ReceiveNblPool;
  135. volatile LONG ReceiveNblInFlightCount;
  136. #define TAP_WAIT_POLL_LOOP_TIMEOUT 3000 // 3 seconds
  137. NDIS_EVENT ReceiveNblInFlightCountZeroEvent;
  138. /*
  139. // Info for point-to-point mode
  140. BOOLEAN m_tun;
  141. IPADDR m_localIP;
  142. IPADDR m_remoteNetwork;
  143. IPADDR m_remoteNetmask;
  144. ETH_HEADER m_TapToUser;
  145. ETH_HEADER m_UserToTap;
  146. ETH_HEADER m_UserToTap_IPv6; // same as UserToTap but proto=ipv6
  147. */
  148. // Info for DHCP server masquerade
  149. /*
  150. BOOLEAN m_dhcp_enabled;
  151. IPADDR m_dhcp_addr;
  152. ULONG m_dhcp_netmask;
  153. IPADDR m_dhcp_server_ip;
  154. BOOLEAN m_dhcp_server_arp;
  155. MACADDR m_dhcp_server_mac;
  156. ULONG m_dhcp_lease_time;
  157. UCHAR m_dhcp_user_supplied_options_buffer[DHCP_USER_SUPPLIED_OPTIONS_BUFFER_SIZE];
  158. ULONG m_dhcp_user_supplied_options_buffer_len;
  159. BOOLEAN m_dhcp_received_discover;
  160. ULONG m_dhcp_bad_requests;
  161. */
  162. // Multicast list. Fixed size.
  163. ULONG ulMCListSize;
  164. UCHAR MCList[TAP_MAX_MCAST_LIST][MACADDR_SIZE];
  165. ULONG PacketFilter;
  166. ULONG ulLookahead;
  167. //
  168. // Statistics
  169. // -------------------------------------------------------------------------
  170. //
  171. // Packet counts
  172. ULONG64 FramesRxDirected;
  173. ULONG64 FramesRxMulticast;
  174. ULONG64 FramesRxBroadcast;
  175. ULONG64 FramesTxDirected;
  176. ULONG64 FramesTxMulticast;
  177. ULONG64 FramesTxBroadcast;
  178. // Byte counts
  179. ULONG64 BytesRxDirected;
  180. ULONG64 BytesRxMulticast;
  181. ULONG64 BytesRxBroadcast;
  182. ULONG64 BytesTxDirected;
  183. ULONG64 BytesTxMulticast;
  184. ULONG64 BytesTxBroadcast;
  185. // Count of transmit errors
  186. ULONG TxAbortExcessCollisions;
  187. ULONG TxLateCollisions;
  188. ULONG TxDmaUnderrun;
  189. ULONG TxLostCRS;
  190. ULONG TxOKButDeferred;
  191. ULONG OneRetry;
  192. ULONG MoreThanOneRetry;
  193. ULONG TotalRetries;
  194. ULONG TransmitFailuresOther;
  195. // Count of receive errors
  196. ULONG RxCrcErrors;
  197. ULONG RxAlignmentErrors;
  198. ULONG RxResourceErrors;
  199. ULONG RxDmaOverrunErrors;
  200. ULONG RxCdtFrames;
  201. ULONG RxRuntErrors;
  202. #if PACKET_TRUNCATION_CHECK
  203. LONG m_RxTrunc, m_TxTrunc;
  204. #endif
  205. BOOLEAN m_InterfaceIsRunning;
  206. LONG m_Rx, m_RxErr;
  207. NDIS_MEDIUM m_Medium;
  208. // Help to tear down the adapter by keeping
  209. // some state information on allocated
  210. // resources.
  211. BOOLEAN m_CalledAdapterFreeResources;
  212. BOOLEAN m_RegisteredAdapterShutdownHandler;
  213. } TAP_ADAPTER_CONTEXT, *PTAP_ADAPTER_CONTEXT;
  214. FORCEINLINE
  215. LONG
  216. tapAdapterContextReference(
  217. __in PTAP_ADAPTER_CONTEXT Adapter
  218. )
  219. {
  220. LONG refCount = NdisInterlockedIncrement(&Adapter->RefCount);
  221. ASSERT(refCount>1); // Cannot dereference a zombie.
  222. return refCount;
  223. }
  224. VOID
  225. tapAdapterContextFree(
  226. __in PTAP_ADAPTER_CONTEXT Adapter
  227. );
  228. FORCEINLINE
  229. LONG
  230. tapAdapterContextDereference(
  231. IN PTAP_ADAPTER_CONTEXT Adapter
  232. )
  233. {
  234. LONG refCount = NdisInterlockedDecrement(&Adapter->RefCount);
  235. ASSERT(refCount >= 0);
  236. if (!refCount)
  237. {
  238. tapAdapterContextFree(Adapter);
  239. }
  240. return refCount;
  241. }
  242. VOID
  243. tapAdapterAcquireLock(
  244. __in PTAP_ADAPTER_CONTEXT Adapter,
  245. __in BOOLEAN DispatchLevel
  246. );
  247. VOID
  248. tapAdapterReleaseLock(
  249. __in PTAP_ADAPTER_CONTEXT Adapter,
  250. __in BOOLEAN DispatchLevel
  251. );
  252. // Returns with added reference on adapter context.
  253. PTAP_ADAPTER_CONTEXT
  254. tapAdapterContextFromDeviceObject(
  255. __in PDEVICE_OBJECT DeviceObject
  256. );
  257. BOOLEAN
  258. tapAdapterReadAndWriteReady(
  259. __in PTAP_ADAPTER_CONTEXT Adapter
  260. );
  261. NDIS_STATUS
  262. tapAdapterSendAndReceiveReady(
  263. __in PTAP_ADAPTER_CONTEXT Adapter
  264. );
  265. ULONG
  266. tapGetNetBufferFrameType(
  267. __in PNET_BUFFER NetBuffer
  268. );
  269. ULONG
  270. tapGetNetBufferCountsFromNetBufferList(
  271. __in PNET_BUFFER_LIST NetBufferList,
  272. __inout_opt PULONG TotalByteCount // Of all linked NBs
  273. );
  274. // Prototypes for standard NDIS miniport entry points
  275. MINIPORT_SET_OPTIONS AdapterSetOptions;
  276. MINIPORT_INITIALIZE AdapterCreate;
  277. MINIPORT_HALT AdapterHalt;
  278. MINIPORT_UNLOAD TapDriverUnload;
  279. MINIPORT_PAUSE AdapterPause;
  280. MINIPORT_RESTART AdapterRestart;
  281. MINIPORT_OID_REQUEST AdapterOidRequest;
  282. MINIPORT_SEND_NET_BUFFER_LISTS AdapterSendNetBufferLists;
  283. MINIPORT_RETURN_NET_BUFFER_LISTS AdapterReturnNetBufferLists;
  284. MINIPORT_CANCEL_SEND AdapterCancelSend;
  285. MINIPORT_CHECK_FOR_HANG AdapterCheckForHangEx;
  286. MINIPORT_RESET AdapterReset;
  287. MINIPORT_DEVICE_PNP_EVENT_NOTIFY AdapterDevicePnpEventNotify;
  288. MINIPORT_SHUTDOWN AdapterShutdownEx;
  289. MINIPORT_CANCEL_OID_REQUEST AdapterCancelOidRequest;
  290. #endif // __TAP_ADAPTER_CONTEXT_H_