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.

453 lines
11 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. tunmp.h
  5. Abstract:
  6. Data structures, defines and function prototypes for TUNMP.
  7. Environment:
  8. Kernel mode only.
  9. Revision History:
  10. alid 10/22/2001 Created
  11. --*/
  12. #ifndef __TUNMP__H
  13. #define __TUNMP__H
  14. typedef struct _TUN_MEDIA_INFO
  15. {
  16. ULONG MaxFrameLen;
  17. UINT MacHeaderLen;
  18. ULONG LinkSpeed;
  19. } TUN_MEDIA_INFO, *PTUN_MEDIA_INFO;
  20. extern const TUN_MEDIA_INFO MediaParams[];
  21. extern NDIS_HANDLE NdisWrapperHandle;
  22. //internal device name and size
  23. #define DEVICE_NAME L"\\Device\\Tun"
  24. //device name visible to users
  25. #define SYMBOLIC_NAME L"\\GLOBAL??\\Tun"
  26. extern LONG GlobalDeviceInstanceNumber;
  27. extern NDIS_SPIN_LOCK TunGlobalLock;
  28. extern LIST_ENTRY TunAdapterList;
  29. #define TUN_CARD_ADDRESS "\02\0TUN\01"
  30. #define TUN_MAX_MULTICAST_ADDRESS 16
  31. #define TUN_MAC_ADDR_LEN 6
  32. #define TUN_MAX_LOOKAHEAD 256
  33. //
  34. // The Open Context represents an open of our device object.
  35. // We allocate this on processing a BindAdapter from NDIS,
  36. // and free it when all references (see below) to it are gone.
  37. //
  38. // Binding/unbinding to an NDIS device:
  39. //
  40. // On processing a BindAdapter call from NDIS, we set up a binding
  41. // to the specified NDIS device (miniport). This binding is
  42. // torn down when NDIS asks us to Unbind by calling
  43. // our UnbindAdapter handler.
  44. //
  45. // Receiving data:
  46. //
  47. // While an NDIS binding exists, read IRPs are queued on this
  48. // structure, to be processed when packets are received.
  49. // If data arrives in the absense of a pended read IRP, we
  50. // queue it, to the extent of one packet, i.e. we save the
  51. // contents of the latest packet received. We fail read IRPs
  52. // received when no NDIS binding exists (or is in the process
  53. // of being torn down).
  54. //
  55. // Sending data:
  56. //
  57. // Write IRPs are used to send data. Each write IRP maps to
  58. // a single NDIS packet. Packet send-completion is mapped to
  59. // write IRP completion. We use NDIS 5.1 CancelSend to support
  60. // write IRP cancellation. Write IRPs that arrive when we don't
  61. // have an active NDIS binding are failed.
  62. //
  63. // Reference count:
  64. //
  65. // The following are long-lived references:
  66. // OPEN_DEVICE ioctl (goes away on processing a Close IRP)
  67. // Pended read IRPs
  68. // Queued received packets
  69. // Uncompleted write IRPs (outstanding sends)
  70. // Existence of NDIS binding
  71. //
  72. typedef struct _TUN_ADAPTER
  73. {
  74. LIST_ENTRY Link; // Link into global list
  75. ULONG Flags; // State information
  76. ULONG RefCount;
  77. TUN_LOCK Lock;
  78. NDIS_MEDIUM Medium;
  79. PFILE_OBJECT pFileObject; // Set on OPEN_DEVICE
  80. NDIS_HANDLE MiniportHandle;
  81. ULONG MaxLookAhead;
  82. ULONG PacketFilter;
  83. ULONG MediumLinkSpeed;
  84. ULONG MediumMinPacketLen;
  85. ULONG MediumMaxPacketLen;
  86. UINT MediumMacHeaderLen;
  87. ULONG MediumMaxFrameLen;
  88. UCHAR PermanentAddress[TUN_MAC_ADDR_LEN];
  89. UCHAR CurrentAddress[TUN_MAC_ADDR_LEN];
  90. ULONG SendPackets; // number of frames that are sent
  91. ULONG RcvPackets; // number of frames that are received
  92. ULONG RcvBytes;
  93. ULONG SendBytes;
  94. ULONG XmitError;
  95. ULONG XmitErrorNoReadIrps;
  96. ULONG RcvError;
  97. ULONG RcvNoBuffer;
  98. NDIS_HANDLE SendPacketPool;
  99. ULONG MacOptions;
  100. LIST_ENTRY PendedWrites; // pended Write IRPs
  101. ULONG PendedSendCount;// number of outstanding Write IRPs
  102. LIST_ENTRY PendedReads; // pended Read IRPs
  103. ULONG PendedReadCount;// number of outstanding Read IRPs
  104. LIST_ENTRY RecvPktQueue; // queued rcv packets
  105. ULONG RecvPktCount; // number of outstanding NDIS Send requests
  106. PDEVICE_OBJECT DeviceObject;
  107. NDIS_HANDLE NdisDeviceHandle;
  108. ULONG DeviceInstanceNumber;
  109. NDIS_DEVICE_POWER_STATE PowerState;
  110. NDIS_STRING MiniportName;
  111. PNDIS_EVENT HaltEvent;
  112. ULONG mc_sig; // Signature for sanity
  113. } TUN_ADAPTER, *PTUN_ADAPTER;
  114. #define mc_signature 'nuTN'
  115. #define TUN_SET_FLAG(_M, _F) ((_M)->Flags |= (_F))
  116. #define TUN_CLEAR_FLAG(_M, _F) ((_M)->Flags &= ~(_F))
  117. #define TUN_TEST_FLAG(_M, _F) (((_M)->Flags & (_F)) != 0)
  118. #define TUN_TEST_FLAGS(_M, _F) (((_M)->Flags & (_F)) == (_F))
  119. //
  120. // Definitions for Flags above.
  121. //
  122. #define TUN_ADAPTER_ACTIVE 0x00000001 // packet filter is non zero
  123. #define TUN_ADAPTER_OPEN 0x00000002 // app has an open handle
  124. #define TUN_MEDIA_CONNECTED 0x00000004
  125. #define TUN_COMPLETE_REQUEST 0x00000008
  126. #define TUN_ADAPTER_OFF 0x00000010
  127. #define TUN_ADAPTER_CANT_HALT 0x00000020
  128. //
  129. // Send packet pool bounds
  130. //
  131. #define MIN_SEND_PACKET_POOL_SIZE 20
  132. #define MAX_SEND_PACKET_POOL_SIZE 400
  133. //
  134. // MiniportReserved in written packets. We save a pointer to the IRP
  135. // that generated the send.
  136. //
  137. // The RefCount is used to determine when to free the packet back
  138. // to its pool. It is used to synchronize between a thread completing
  139. // a send and a thread attempting to cancel a send.
  140. //
  141. typedef struct _TUN_SEND_PACKET_RSVD
  142. {
  143. PIRP pIrp;
  144. ULONG RefCount;
  145. } TUN_SEND_PACKET_RSVD, *PTUN_SEND_PACKET_RSVD;
  146. //
  147. // Receive packet pool bounds
  148. //
  149. #define MIN_RECV_PACKET_POOL_SIZE 4
  150. #define MAX_RECV_PACKET_POOL_SIZE 20
  151. //
  152. // Max receive packets we allow to be queued up
  153. //
  154. //1 check to see if this value is good enough
  155. #define MAX_RECV_QUEUE_SIZE 10
  156. //
  157. // MiniportReserved in received packets: we link these
  158. // packets up in a queue waiting for Read IRPs.
  159. //
  160. typedef struct _TUN_RECV_PACKET_RSVD
  161. {
  162. LIST_ENTRY Link;
  163. } TUN_RECV_PACKET_RSVD, *PTUN_RECV_PACKET_RSVD;
  164. #define TUN_ALLOC_TAG 'untN'
  165. //
  166. // Prototypes.
  167. //
  168. NTSTATUS
  169. DriverEntry(
  170. IN PDRIVER_OBJECT pDriverObject,
  171. IN PUNICODE_STRING pRegistryPath
  172. );
  173. VOID
  174. TunUnload(
  175. IN PDRIVER_OBJECT DriverObject
  176. );
  177. NTSTATUS
  178. TunOpen(
  179. IN PDEVICE_OBJECT pDeviceObject,
  180. IN PIRP pIrp
  181. );
  182. NTSTATUS
  183. TunClose(
  184. IN PDEVICE_OBJECT pDeviceObject,
  185. IN PIRP pIrp
  186. );
  187. NTSTATUS
  188. TunCleanup(
  189. IN PDEVICE_OBJECT pDeviceObject,
  190. IN PIRP pIrp
  191. );
  192. NTSTATUS
  193. TunIoControl(
  194. IN PDEVICE_OBJECT pDeviceObject,
  195. IN PIRP pIrp
  196. );
  197. VOID
  198. TunMpRefAdapter(
  199. IN PTUN_ADAPTER pAdapter
  200. );
  201. VOID
  202. TunMpDerefAdapter(
  203. IN PTUN_ADAPTER pAdapter
  204. );
  205. #if DBG
  206. VOID
  207. TunMpDbgRefAdapter(
  208. IN PTUN_ADAPTER pAdapter,
  209. IN ULONG FileNumber,
  210. IN ULONG LineNumber
  211. );
  212. VOID
  213. TunMpDbgDerefAdapter(
  214. IN PTUN_ADAPTER pAdapter,
  215. IN ULONG FileNumber,
  216. IN ULONG LineNumber
  217. );
  218. #endif // DBG
  219. VOID
  220. TunPnPEvent(
  221. IN NDIS_HANDLE MiniportAdapterContext,
  222. IN NDIS_DEVICE_PNP_EVENT DevicePnPEvent,
  223. IN PVOID InformationBuffer,
  224. IN ULONG InformationBufferLength
  225. );
  226. VOID
  227. TunWaitForPendingIO(
  228. IN PTUN_ADAPTER pAdapter,
  229. IN BOOLEAN DoCancelReads
  230. );
  231. PTUN_ADAPTER
  232. TunLookupDevice(
  233. IN PUCHAR pBindingInfo,
  234. IN ULONG BindingInfoLength
  235. );
  236. NDIS_STATUS
  237. TunQueryOidValue(
  238. IN PTUN_ADAPTER pAdapter,
  239. OUT PVOID pDataBuffer,
  240. IN ULONG BufferLength,
  241. OUT PULONG pBytesWritten
  242. );
  243. NDIS_STATUS
  244. TunSetOidValue(
  245. IN PTUN_ADAPTER pAdapter,
  246. OUT PVOID pDataBuffer,
  247. IN ULONG BufferLength
  248. );
  249. NTSTATUS
  250. TunRead(
  251. IN PDEVICE_OBJECT pDeviceObject,
  252. IN PIRP pIrp
  253. );
  254. VOID
  255. TunCancelRead(
  256. IN PDEVICE_OBJECT pDeviceObject,
  257. IN PIRP pIrp
  258. );
  259. VOID
  260. TunServiceReads(
  261. IN PTUN_ADAPTER pAdapter
  262. );
  263. INT
  264. TunReceivePacket(
  265. IN NDIS_HANDLE ProtocolBindingContext,
  266. IN PNDIS_PACKET pNdisPacket
  267. );
  268. VOID
  269. TunShutdownBinding(
  270. IN PTUN_ADAPTER pAdapter
  271. );
  272. VOID
  273. TunCancelPendingReads(
  274. IN PTUN_ADAPTER pAdapter
  275. );
  276. VOID
  277. TunFlushReceiveQueue(
  278. IN PTUN_ADAPTER pAdapter
  279. );
  280. NTSTATUS
  281. TunWrite(
  282. IN PDEVICE_OBJECT pDeviceObject,
  283. IN PIRP pIrp
  284. );
  285. //
  286. // Tun miniport entry points
  287. //
  288. NDIS_STATUS
  289. TunMpInitialize(
  290. OUT PNDIS_STATUS OpenErrorStatus,
  291. OUT PUINT SelectedMediumIndex,
  292. IN PNDIS_MEDIUM MediumArray,
  293. IN UINT MediumArraySize,
  294. IN NDIS_HANDLE MiniportAdapterHandle,
  295. IN NDIS_HANDLE ConfigurationContext
  296. );
  297. VOID
  298. TunMpHalt(
  299. IN NDIS_HANDLE MiniportAdapterContext
  300. );
  301. VOID
  302. TunMpShutdown(
  303. IN NDIS_HANDLE MiniportAdapterContext
  304. );
  305. NDIS_STATUS
  306. TunMpSetInformation(
  307. IN NDIS_HANDLE MiniportAdapterContext,
  308. IN NDIS_OID Oid,
  309. IN PVOID InformationBuffer,
  310. IN ULONG InformationBufferLength,
  311. OUT PULONG BytesRead,
  312. OUT PULONG BytesNeeded
  313. );
  314. NDIS_STATUS
  315. TunMpQueryInformation(
  316. IN NDIS_HANDLE MiniportAdapterContext,
  317. IN NDIS_OID Oid,
  318. IN PVOID InformationBuffer,
  319. IN ULONG InformationBufferLength,
  320. OUT PULONG BytesWritten,
  321. OUT PULONG BytesNeeded
  322. );
  323. VOID
  324. TunMpReturnPacket(
  325. IN NDIS_HANDLE pMiniportAdapterContext,
  326. IN PNDIS_PACKET pNdisPacket
  327. );
  328. VOID
  329. TunMpSendPackets(
  330. IN NDIS_HANDLE MiniportAdapterContext,
  331. IN PPNDIS_PACKET PacketArray,
  332. IN UINT NumberOfPackets
  333. );
  334. NTSTATUS
  335. TunFOpen(
  336. IN PDEVICE_OBJECT pDeviceObject,
  337. IN PIRP pIrp
  338. );
  339. NTSTATUS
  340. TunFClose(
  341. IN PDEVICE_OBJECT pDeviceObject,
  342. IN PIRP pIrp
  343. );
  344. NTSTATUS
  345. TunFCleanup(
  346. IN PDEVICE_OBJECT pDeviceObject,
  347. IN PIRP pIrp
  348. );
  349. NTSTATUS
  350. TunFIoControl(
  351. IN PDEVICE_OBJECT pDeviceObject,
  352. IN PIRP pIrp
  353. );
  354. NDIS_STATUS
  355. TunMpCreateDevice(
  356. IN PTUN_ADAPTER pAdapter
  357. );
  358. #endif // __TUNMP__H