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.

354 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. pfd.c
  5. Abstract:
  6. This module contains code for a simple packet-filter driver
  7. Author:
  8. Abolade Gbadegesin (aboladeg) 15-Aug-1999
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. //
  14. // Device- and file-object for the IP driver
  15. //
  16. extern PDEVICE_OBJECT IpDeviceObject = NULL;
  17. extern PFILE_OBJECT IpFileObject = NULL;
  18. //
  19. // Device-object for the filter driver
  20. //
  21. extern PDEVICE_OBJECT PfdDeviceObject = NULL;
  22. //
  23. // FUNCTION PROTOTYPES (alphabetically)
  24. //
  25. NTSTATUS
  26. DriverEntry(
  27. IN PDRIVER_OBJECT DriverObject,
  28. IN PUNICODE_STRING RegistryPath
  29. );
  30. FORWARD_ACTION
  31. PfdFilterPacket(
  32. struct IPHeader UNALIGNED* Header,
  33. PUCHAR Packet,
  34. UINT PacketLength,
  35. UINT ReceivingInterfaceIndex,
  36. UINT SendingInterfaceIndex,
  37. IPAddr ReceivingLinkNextHop,
  38. IPAddr SendingLinkNextHop
  39. );
  40. NTSTATUS
  41. PfdInitializeDriver(
  42. VOID
  43. );
  44. NTSTATUS
  45. PfdSetFilterHook(
  46. BOOLEAN Install
  47. );
  48. VOID
  49. PfdUnloadDriver(
  50. IN PDRIVER_OBJECT DriverObject
  51. );
  52. #ifdef ALLOC_PRAGMA
  53. #pragma alloc_text(INIT, DriverEntry)
  54. #endif
  55. NTSTATUS
  56. DriverEntry(
  57. IN PDRIVER_OBJECT DriverObject,
  58. IN PUNICODE_STRING RegistryPath
  59. )
  60. /*++
  61. Routine Description:
  62. Performs driver-initialization for the filter driver.
  63. Arguments:
  64. Return Value:
  65. STATUS_SUCCESS if initialization succeeded, error code otherwise.
  66. --*/
  67. {
  68. WCHAR DeviceName[] = DD_IP_PFD_DEVICE_NAME;
  69. UNICODE_STRING DeviceString;
  70. LONG i;
  71. OBJECT_ATTRIBUTES ObjectAttributes;
  72. HANDLE ParametersKey;
  73. HANDLE ServiceKey;
  74. NTSTATUS status;
  75. UNICODE_STRING String;
  76. PAGED_CODE();
  77. KdPrint(("DriverEntry\n"));
  78. //
  79. // Create the device's object.
  80. //
  81. RtlInitUnicodeString(&DeviceString, DeviceName);
  82. status =
  83. IoCreateDevice(
  84. DriverObject,
  85. 0,
  86. &DeviceString,
  87. FILE_DEVICE_NETWORK,
  88. FILE_DEVICE_SECURE_OPEN,
  89. FALSE,
  90. &PfdDeviceObject
  91. );
  92. if (!NT_SUCCESS(status)) {
  93. KdPrint(("IoCreateDevice failed (0x%08X)\n", status));
  94. return status;
  95. }
  96. DriverObject->DriverUnload = PfdUnloadDriver;
  97. DriverObject->DriverStartIo = NULL;
  98. //
  99. // Initialize the driver's structures
  100. //
  101. status = PfdInitializeDriver();
  102. return status;
  103. } // DriverEntry
  104. FORWARD_ACTION
  105. PfdFilterPacket(
  106. struct IPHeader UNALIGNED* Header,
  107. PUCHAR Packet,
  108. UINT PacketLength,
  109. UINT ReceivingInterfaceIndex,
  110. UINT SendingInterfaceIndex,
  111. IPAddr ReceivingLinkNextHop,
  112. IPAddr SendingLinkNextHop
  113. )
  114. /*++
  115. Routine Description:
  116. Invoked to determine the fate of each received packet.
  117. Arguments:
  118. none used.
  119. Return Value:
  120. FORWARD_ACTION - indicates whether to forward or drop the given packet.
  121. Environment:
  122. Invoked within the context of reception or transmission.
  123. --*/
  124. {
  125. KdPrint(("PfdFilterPacket\n"));
  126. return FORWARD;
  127. } // PfdFilterPacket
  128. NTSTATUS
  129. PfdInitializeDriver(
  130. VOID
  131. )
  132. /*++
  133. Routine Description:
  134. Performs initialization of the driver's structures.
  135. Arguments:
  136. none.
  137. Return Value:
  138. NTSTATUS - success/error code.
  139. --*/
  140. {
  141. OBJECT_ATTRIBUTES ObjectAttributes;
  142. NTSTATUS status;
  143. UNICODE_STRING UnicodeString;
  144. KdPrint(("PfdInitializeDriver\n"));
  145. //
  146. // Obtain the IP driver device-object
  147. //
  148. RtlInitUnicodeString(&UnicodeString, DD_IP_DEVICE_NAME);
  149. status =
  150. IoGetDeviceObjectPointer(
  151. &UnicodeString,
  152. SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE,
  153. &IpFileObject,
  154. &IpDeviceObject
  155. );
  156. if (!NT_SUCCESS(status)) {
  157. KdPrint(("PfdInitializeDriver: error %X getting IP object\n", status));
  158. return status;
  159. }
  160. ObReferenceObject(IpDeviceObject);
  161. //
  162. // Install the filter-hook routine
  163. //
  164. return PfdSetFilterHook(TRUE);
  165. } // PfdInitializeDriver
  166. NTSTATUS
  167. PfdSetFilterHook(
  168. BOOLEAN Install
  169. )
  170. /*++
  171. Routine Description:
  172. This routine is called to set (Install==TRUE) or clear (Install==FALSE) the
  173. value of the filter-callout function pointer in the IP driver.
  174. Arguments:
  175. Install - indicates whether to install or remove the hook.
  176. Return Value:
  177. NTSTATUS - indicates success/failure
  178. Environment:
  179. The routine assumes the caller is executing at PASSIVE_LEVEL.
  180. --*/
  181. {
  182. IP_SET_FILTER_HOOK_INFO HookInfo;
  183. IO_STATUS_BLOCK IoStatus;
  184. PIRP Irp;
  185. KEVENT LocalEvent;
  186. NTSTATUS status;
  187. KdPrint(("PfdSetFilterHook\n"));
  188. //
  189. // Register (or deregister) as a filter driver
  190. //
  191. HookInfo.FilterPtr = Install ? PfdFilterPacket : NULL;
  192. KeInitializeEvent(&LocalEvent, SynchronizationEvent, FALSE);
  193. Irp =
  194. IoBuildDeviceIoControlRequest(
  195. IOCTL_IP_SET_FILTER_POINTER,
  196. IpDeviceObject,
  197. (PVOID)&HookInfo,
  198. sizeof(HookInfo),
  199. NULL,
  200. 0,
  201. FALSE,
  202. &LocalEvent,
  203. &IoStatus
  204. );
  205. if (!Irp) {
  206. KdPrint(("PfdSetFilterHook: IoBuildDeviceIoControlRequest=0\n"));
  207. return STATUS_UNSUCCESSFUL;
  208. }
  209. status = IoCallDriver(IpDeviceObject, Irp);
  210. if (status == STATUS_PENDING) {
  211. KeWaitForSingleObject(&LocalEvent, Executive, KernelMode, FALSE, NULL);
  212. status = IoStatus.Status;
  213. }
  214. if (!NT_SUCCESS(status)) {
  215. KdPrint(("PfdSetFilterHook: SetFilterPointer=%x\n", status));
  216. }
  217. return status;
  218. } // PfdSetFilterHook
  219. VOID
  220. PfdUnloadDriver(
  221. IN PDRIVER_OBJECT DriverObject
  222. )
  223. /*++
  224. Routine Description:
  225. Performs cleanup for the filter-driver.
  226. Arguments:
  227. DriverObject - reference to the module's driver-object
  228. Return Value:
  229. --*/
  230. {
  231. KdPrint(("PfdUnloadDriver\n"));
  232. //
  233. // Stop translation and clear the periodic timer
  234. //
  235. PfdSetFilterHook(FALSE);
  236. IoDeleteDevice(DriverObject->DeviceObject);
  237. //
  238. // Release references to the IP device object
  239. //
  240. ObDereferenceObject((PVOID)IpFileObject);
  241. ObDereferenceObject(IpDeviceObject);
  242. } // PfdUnloadDriver