1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved
Abstract:
Monitor Sample driver initialization routines
Environment:
Kernel mode
--*/
#include <ndis.h>
#include <ntddk.h>
#include <wdf.h>
#include <fwpmk.h>
#pragma warning(push)
#pragma warning(disable:4201) // unnamed struct/union
#include <fwpsk.h>
#pragma warning(pop)
#include "ioctl.h"
#include "msnmntr.h"
#include "ctl.h"
#include "notify.h"
//
// Software Tracing Definitions
//
#define WPP_CONTROL_GUIDS \
WPP_DEFINE_CONTROL_GUID(MsnMntrInit,(e7db16bb, 41be, 4c05, b73e, 5feca06f8207), \
WPP_DEFINE_BIT(TRACE_INIT) \
WPP_DEFINE_BIT(TRACE_SHUTDOWN) )
#include "init.tmh"
DEVICE_OBJECT* gWdmDevice;
// ===========================================================================
//
// LOCAL PROTOTYPES
//
// ===========================================================================
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_UNLOAD MonitorEvtDriverUnload;
// We're using what looks like a EVT_WDF_DRIVER_DEVICE_ADD callback, to keep
// this looking like a normal KMDF driver. However, since this is a non-pnp
// driver, it will not be used as a callback; we will call it ourselves at the
// end of DriverEntry. So, do not declare it as a callback.
// The NONPNP sample demonstrates this as well.
NTSTATUS
MonitorEvtDeviceAdd(
_In_ PWDFDEVICE_INIT pInit
);
// ===========================================================================
//
// PUBLIC FUNCTIONS
//
// ===========================================================================
NTSTATUS
DriverEntry(
_In_ DRIVER_OBJECT* driverObject,
_In_ UNICODE_STRING* registryPath
)
/*++
Routine Description:
Main driver entry point. Called at driver load time
Arguments:
driverObject Our driver
registryPath A reg key where we can keep parameters
Return Value:
status of our initialization. A status != STATUS_SUCCESS aborts the
driver load and we don't get called again.
Each component is responsible for logging any error that causes the
driver load to fail.
--*/
{
NTSTATUS status;
WDF_DRIVER_CONFIG config;
WDFDRIVER driver;
PWDFDEVICE_INIT pInit = NULL;
// Request NX Non-Paged Pool when available
ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
//
// This macro is required to initialize software tracing on XP and beyond
// For XP and beyond use the DriverObject as the first argument.
//
WPP_INIT_TRACING(driverObject,registryPath);
DoTraceMessage(TRACE_INIT, "Initializing MonitorSample Driver");
WDF_DRIVER_CONFIG_INIT(&config, WDF_NO_EVENT_CALLBACK);
config.DriverInitFlags |= WdfDriverInitNonPnpDriver;
config.EvtDriverUnload = MonitorEvtDriverUnload;
status = WdfDriverCreate(
driverObject,
registryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
&driver
);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
pInit = WdfControlDeviceInitAllocate(driver, &SDDL_DEVOBJ_SYS_ALL_ADM_ALL);
if (!pInit)
{
status = STATUS_INSUFFICIENT_RESOURCES;
goto cleanup;
}
status = MonitorEvtDeviceAdd(pInit);
cleanup:
if (!NT_SUCCESS(status))
{
DoTraceMessage(TRACE_INIT, "MonitorSample Initialization Failed.");
WPP_CLEANUP(driverObject);
}
return status;
}
NTSTATUS
MonitorEvtDeviceAdd(
_In_ PWDFDEVICE_INIT pInit
)
{
NTSTATUS status;
WDFDEVICE device;
DECLARE_CONST_UNICODE_STRING(ntDeviceName, MONITOR_DEVICE_NAME);
DECLARE_CONST_UNICODE_STRING(symbolicName, MONITOR_SYMBOLIC_NAME);
WdfDeviceInitSetDeviceType(pInit, FILE_DEVICE_NETWORK);
WdfDeviceInitSetCharacteristics(pInit, FILE_DEVICE_SECURE_OPEN, FALSE);
status = WdfDeviceInitAssignName(pInit, &ntDeviceName);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = WdfDeviceCreate(&pInit, WDF_NO_OBJECT_ATTRIBUTES, &device);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = WdfDeviceCreateSymbolicLink(device, &symbolicName);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = MonitorCtlDriverInit(&device);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
gWdmDevice = WdfDeviceWdmGetDeviceObject(device);
status = MonitorCoInitialize(gWdmDevice);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = MonitorNfInitialize(gWdmDevice);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
WdfControlFinishInitializing(device);
cleanup:
// If WdfDeviceCreate was successful, it will set pInit to NULL.
if (pInit)
{
WdfDeviceInitFree(pInit);
}
return status;
}
void
MonitorEvtDriverUnload(
_In_ WDFDRIVER Driver
)
/*++
Routine Description:
Called to indicate that we are being unloaded and to cause an orderly
shutdown
Arguments:
driverObject Our driver
Return Value:
None
--*/
{
DRIVER_OBJECT* driverObject;
MonitorCoUninitialize();
MonitorNfUninitialize();
DoTraceMessage(TRACE_SHUTDOWN, "MonitorSample Driver Shutting Down");
driverObject = WdfDriverWdmGetDriverObject(Driver);
WPP_CLEANUP(driverObject);
}
|