ReactOS 0.4.17-dev-966-gf06eace
ioctl.c File Reference
#include "ndisuio.h"
#include <debug.h>
Include dependency graph for ioctl.c:

Go to the source code of this file.

Functions

static NTSTATUS WaitForBind (PIRP Irp, PIO_STACK_LOCATION IrpSp)
 
static NTSTATUS QueryBinding (PIRP Irp, PIO_STACK_LOCATION IrpSp)
 
static NTSTATUS SetAdapterOid (PIRP Irp, PIO_STACK_LOCATION IrpSp)
 
static NTSTATUS QueryAdapterOid (PIRP Irp, PIO_STACK_LOCATION IrpSp)
 
static NTSTATUS OpenDeviceReadWrite (PIRP Irp, PIO_STACK_LOCATION IrpSp)
 
NTSTATUS NTAPI NduDispatchDeviceControl (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 

Function Documentation

◆ NduDispatchDeviceControl()

NTSTATUS NTAPI NduDispatchDeviceControl ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 477 of file ioctl.c.

479{
481 PNDISUIO_OPEN_ENTRY OpenEntry;
482
484
485 /* Handle open IOCTLs first */
486 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode)
487 {
490#if 0
491 case IOCTL_NDISUIO_OPEN_WRITE_DEVICE:
492 return OpenDeviceWrite(Irp, IrpSp);
493#endif
495 return WaitForBind(Irp, IrpSp);
496
498 return QueryBinding(Irp, IrpSp);
499
500 default:
501 /* Fail if this file object has no adapter associated */
502 if (IrpSp->FileObject->FsContext == NULL)
503 {
504 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
505 Irp->IoStatus.Information = 0;
507
509 }
510
511 /* Now handle write IOCTLs */
512 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode)
513 {
515 return SetAdapterOid(Irp, IrpSp);
516
517 default:
518 /* Check that we have read permissions */
519 OpenEntry = IrpSp->FileObject->FsContext2;
520 if (OpenEntry->WriteOnly)
521 {
522 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
523 Irp->IoStatus.Information = 0;
525
527 }
528
529 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode)
530 {
531#if 0
532 case IOCTL_CANCEL_READ:
533 return CancelPacketRead(Irp, IrpSp);
534#endif
535
537 return QueryAdapterOid(Irp, IrpSp);
538
539 default:
540 DPRINT1("Unimplemented\n");
541 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
542 Irp->IoStatus.Information = 0;
545 }
546 }
547 break;
548 }
549}
static PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(PIRP Irp)
#define DPRINT1
Definition: precomp.h:8
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_NOT_IMPLEMENTED
Definition: d3dkmdt.h:42
#define NULL
Definition: types.h:112
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4138
static NTSTATUS QueryAdapterOid(PIRP Irp, PIO_STACK_LOCATION IrpSp)
Definition: ioctl.c:220
static NTSTATUS SetAdapterOid(PIRP Irp, PIO_STACK_LOCATION IrpSp)
Definition: ioctl.c:146
static NTSTATUS QueryBinding(PIRP Irp, PIO_STACK_LOCATION IrpSp)
Definition: ioctl.c:34
static NTSTATUS OpenDeviceReadWrite(PIRP Irp, PIO_STACK_LOCATION IrpSp)
Definition: ioctl.c:294
static NTSTATUS WaitForBind(PIRP Irp, PIO_STACK_LOCATION IrpSp)
Definition: ioctl.c:16
PDEVICE_OBJECT GlobalDeviceObject
Definition: main.c:14
#define ASSERT(a)
Definition: mode.c:44
#define IoCompleteRequest
Definition: irp.c:1272
#define IOCTL_NDISUIO_QUERY_OID_VALUE
Definition: nuiouser.h:16
#define IOCTL_NDISUIO_BIND_WAIT
Definition: nuiouser.h:27
#define IOCTL_NDISUIO_OPEN_DEVICE
Definition: nuiouser.h:12
#define IOCTL_NDISUIO_QUERY_BINDING
Definition: nuiouser.h:23
#define IOCTL_NDISUIO_SET_OID_VALUE
Definition: nuiouser.h:31
PFILE_OBJECT FileObject
Definition: iotypes.h:3171
union _IO_STACK_LOCATION::@1803 Parameters
struct _IO_STACK_LOCATION::@1803::@1804 DeviceIoControl
Definition: ndisuio.h:54
BOOLEAN WriteOnly
Definition: ndisuio.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2061
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by DriverEntry().

◆ OpenDeviceReadWrite()

static NTSTATUS OpenDeviceReadWrite ( PIRP  Irp,
PIO_STACK_LOCATION  IrpSp 
)
static

Definition at line 294 of file ioctl.c.

295{
298 ULONG NameLength;
300 PNDISUIO_ADAPTER_CONTEXT AdapterContext;
301 PNDISUIO_OPEN_ENTRY OpenEntry;
303
304 NameLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
305 if (NameLength != 0)
306 {
307 DeviceName.MaximumLength = DeviceName.Length = NameLength;
308 DeviceName.Buffer = Irp->AssociatedIrp.SystemBuffer;
309
310 /* The bound name is a counted string with no terminator, so
311 * drop one the caller may have included before matching */
312 if (DeviceName.Length >= sizeof(WCHAR) &&
313 DeviceName.Buffer[DeviceName.Length / sizeof(WCHAR) - 1] == UNICODE_NULL)
314 {
315 DeviceName.Length -= sizeof(WCHAR);
316 }
317
318 /* Check if this already has a context */
319 AdapterContext = FindAdapterContextByName(&DeviceName);
320 if (AdapterContext != NULL)
321 {
322 DPRINT("Binding file object 0x%x to device %wZ\n", FileObject, &AdapterContext->DeviceName);
323
324 /* Reference the adapter context */
325 KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
326 if (AdapterContext->OpenCount != 0)
327 {
328 /* An open for read-write is exclusive,
329 * so we can't have any other open handles */
330 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
332 }
333 else
334 {
335 /* Add a reference */
336 ReferenceAdapterContext(AdapterContext);
338 }
339 }
340 else
341 {
342 /* Invalid device name */
344 }
345
346 /* Check that the bind succeeded */
347 if (NT_SUCCESS(Status))
348 {
349 OpenEntry = ExAllocatePool(NonPagedPool, sizeof(*OpenEntry));
350 if (OpenEntry)
351 {
352 /* Set the file object pointer */
353 OpenEntry->FileObject = FileObject;
354
355 /* Set the permissions */
356 OpenEntry->WriteOnly = FALSE;
357
358 /* Associate this FO with the adapter */
359 FileObject->FsContext = AdapterContext;
360 FileObject->FsContext2 = OpenEntry;
361
362 /* Add it to the adapter's list */
363 InsertTailList(&AdapterContext->OpenEntryList,
364 &OpenEntry->ListEntry);
365
366 /* Success */
367 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
369 }
370 else
371 {
372 /* Remove the reference we added */
373 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
376 }
377 }
378 }
379 else
380 {
381 /* Invalid device name */
383 }
384
385 Irp->IoStatus.Status = Status;
386 Irp->IoStatus.Information = 0;
387
389
390 return Status;
391}
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
VOID DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PNDISUIO_OPEN_ENTRY OpenEntry)
Definition: misc.c:149
PNDISUIO_ADAPTER_CONTEXT FindAdapterContextByName(PNDIS_STRING DeviceName)
Definition: misc.c:115
VOID ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
Definition: misc.c:142
#define InsertTailList(ListHead, Entry)
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define NonPagedPool
Definition: env_spec_w32.h:307
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
Status
Definition: gdiplustypes.h:24
#define UNICODE_NULL
short WCHAR
Definition: pedump.c:58
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
LIST_ENTRY OpenEntryList
Definition: ndisuio.h:30
KSPIN_LOCK Spinlock
Definition: ndisuio.h:50
UNICODE_STRING DeviceName
Definition: ndisuio.h:44
LIST_ENTRY ListEntry
Definition: ndisuio.h:62
PFILE_OBJECT FileObject
Definition: ndisuio.h:56
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:550
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3281
* PFILE_OBJECT
Definition: iotypes.h:1998
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778

Referenced by NduDispatchDeviceControl().

◆ QueryAdapterOid()

static NTSTATUS QueryAdapterOid ( PIRP  Irp,
PIO_STACK_LOCATION  IrpSp 
)
static

Definition at line 220 of file ioctl.c.

221{
222 PNDISUIO_ADAPTER_CONTEXT AdapterContext = IrpSp->FileObject->FsContext;
223 PNDISUIO_QUERY_OID QueryOidRequest;
225 ULONG RequestLength;
227
228 Irp->IoStatus.Information = 0;
229
230 QueryOidRequest = Irp->AssociatedIrp.SystemBuffer;
231 RequestLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
232 if (QueryOidRequest && RequestLength >= sizeof(NDIS_OID))
233 {
234 /* Setup the NDIS request */
236 Request.DATA.QUERY_INFORMATION.Oid = QueryOidRequest->Oid;
237 Request.DATA.QUERY_INFORMATION.InformationBufferLength = RequestLength - sizeof(NDIS_OID);
238 if (Request.DATA.QUERY_INFORMATION.InformationBufferLength != 0)
239 {
240 Request.DATA.QUERY_INFORMATION.InformationBuffer = QueryOidRequest->Data;
241 }
242 else
243 {
244 Request.DATA.QUERY_INFORMATION.InformationBuffer = NULL;
245 }
246 Request.DATA.QUERY_INFORMATION.BytesWritten = 0;
247
248 DPRINT("Querying OID 0x%x on adapter %wZ\n", QueryOidRequest->Oid, &AdapterContext->DeviceName);
249
250 /* Dispatch the request */
252 AdapterContext->BindingHandle,
253 &Request);
254
255 /* Wait for the request */
257 {
258 KeWaitForSingleObject(&AdapterContext->AsyncEvent,
259 Executive,
261 FALSE,
262 NULL);
263 Status = AdapterContext->AsyncStatus;
264 }
265
269 {
271 }
272 else if (Status == NDIS_STATUS_SUCCESS)
273 {
274 Irp->IoStatus.Information = sizeof(NDIS_OID) + Request.DATA.QUERY_INFORMATION.BytesWritten;
275 }
276
277 DPRINT("Final request status: 0x%x (%d)\n", Status, Irp->IoStatus.Information);
278 }
279 else
280 {
281 /* Bad parameters */
283 }
284
285 Irp->IoStatus.Status = Status;
286
288
289 return Status;
290}
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define NDIS_STATUS_PENDING
Definition: ndis.h:347
#define NDIS_STATUS_INVALID_LENGTH
Definition: ndis.h:485
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
#define NDIS_STATUS_BUFFER_TOO_SHORT
Definition: ndis.h:487
#define NDIS_STATUS_BUFFER_OVERFLOW
Definition: ndis.h:464
_In_opt_ NDIS_HANDLE _In_opt_ NDIS_HANDLE _Inout_ PNDIS_REQUEST NdisRequest
Definition: ndis.h:1573
@ NdisRequestQueryInformation
Definition: ndis.h:790
#define KernelMode
Definition: asm.h:38
int NDIS_STATUS
Definition: ntddndis.h:496
ULONG NDIS_OID
Definition: ntddndis.h:249
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
NDIS_STATUS AsyncStatus
Definition: ndisuio.h:22
NDIS_HANDLE BindingHandle
Definition: ndisuio.h:26
UCHAR Data[sizeof(ULONG)]
Definition: nuiouser.h:38
NDIS_OID Oid
Definition: nuiouser.h:37
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
@ Executive
Definition: ketypes.h:467

Referenced by NduDispatchDeviceControl().

◆ QueryBinding()

static NTSTATUS QueryBinding ( PIRP  Irp,
PIO_STACK_LOCATION  IrpSp 
)
static

Definition at line 34 of file ioctl.c.

35{
36 PNDISUIO_ADAPTER_CONTEXT AdapterContext = NULL;
37 PNDISUIO_QUERY_BINDING QueryBinding = Irp->AssociatedIrp.SystemBuffer;
38 ULONG BindingLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
40 PLIST_ENTRY CurrentEntry;
42 ULONG i;
44
45 if (QueryBinding && BindingLength >= sizeof(NDISUIO_QUERY_BINDING))
46 {
48 i = 0;
49 CurrentEntry = GlobalAdapterList.Flink;
50 while (CurrentEntry != &GlobalAdapterList)
51 {
52 if (i == QueryBinding->BindingIndex)
53 {
54 AdapterContext = CONTAINING_RECORD(CurrentEntry, NDISUIO_ADAPTER_CONTEXT, ListEntry);
55 break;
56 }
57 i++;
58 CurrentEntry = CurrentEntry->Flink;
59 }
61 if (AdapterContext)
62 {
63 DPRINT("Query binding for index %d is adapter %wZ\n", i, &AdapterContext->DeviceName);
65 if (AdapterContext->DeviceName.Length <= BindingLength - BytesCopied)
66 {
67 QueryBinding->DeviceNameOffset = BytesCopied;
68 QueryBinding->DeviceNameLength = AdapterContext->DeviceName.Length;
69 RtlCopyMemory((PUCHAR)QueryBinding + QueryBinding->DeviceNameOffset,
70 AdapterContext->DeviceName.Buffer,
71 QueryBinding->DeviceNameLength);
72 BytesCopied += AdapterContext->DeviceName.Length;
73
74 /* FIXME: Copy description too */
75 QueryBinding->DeviceDescrOffset = BytesCopied;
76 QueryBinding->DeviceDescrLength = 0;
77
78 /* Successful */
80 }
81 else
82 {
83 /* Not enough buffer space */
85 }
86 }
87 else
88 {
89 /* Invalid index */
91 }
92 }
93 else
94 {
95 /* Invalid parameters */
97 }
98
99 Irp->IoStatus.Status = Status;
100 Irp->IoStatus.Information = BytesCopied;
101
103
104 return Status;
105}
LIST_ENTRY GlobalAdapterList
Definition: main.c:17
KSPIN_LOCK GlobalAdapterListLock
Definition: main.c:16
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_In_ UINT _In_ UINT _In_ PNDIS_PACKET _In_ UINT _Out_ PUINT BytesCopied
Definition: ndis.h:3171
#define STATUS_NO_MORE_ENTRIES
Definition: ntstatus.h:285
struct _NDISUIO_QUERY_BINDING NDISUIO_QUERY_BINDING
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
unsigned char * PUCHAR
Definition: typedefs.h:53

Referenced by NduDispatchDeviceControl(), OpenAdapterHandle(), and QueryBinding().

◆ SetAdapterOid()

static NTSTATUS SetAdapterOid ( PIRP  Irp,
PIO_STACK_LOCATION  IrpSp 
)
static

Definition at line 146 of file ioctl.c.

147{
148 PNDISUIO_ADAPTER_CONTEXT AdapterContext = IrpSp->FileObject->FsContext;
149 PNDISUIO_SET_OID SetOidRequest;
151 ULONG RequestLength;
153
154 Irp->IoStatus.Information = 0;
155
156 SetOidRequest = Irp->AssociatedIrp.SystemBuffer;
157 RequestLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
158 if (SetOidRequest && RequestLength >= sizeof(NDIS_OID))
159 {
160 /* Setup the NDIS request */
162 Request.DATA.SET_INFORMATION.Oid = SetOidRequest->Oid;
163 Request.DATA.SET_INFORMATION.InformationBufferLength = RequestLength - sizeof(NDIS_OID);
164 if (Request.DATA.SET_INFORMATION.InformationBufferLength != 0)
165 {
166 Request.DATA.SET_INFORMATION.InformationBuffer = SetOidRequest->Data;
167 }
168 else
169 {
170 Request.DATA.SET_INFORMATION.InformationBuffer = NULL;
171 }
172 Request.DATA.SET_INFORMATION.BytesRead = 0;
173
174 DPRINT("Setting OID 0x%x on adapter %wZ\n", SetOidRequest->Oid, &AdapterContext->DeviceName);
175
176 /* Dispatch the request */
178 AdapterContext->BindingHandle,
179 &Request);
180
181 /* Wait for the request */
183 {
184 KeWaitForSingleObject(&AdapterContext->AsyncEvent,
185 Executive,
187 FALSE,
188 NULL);
189 Status = AdapterContext->AsyncStatus;
190 }
191
192 /* Return the bytes read */
195 {
197 }
198 else if (Status == NDIS_STATUS_SUCCESS)
199 {
200 Irp->IoStatus.Information = sizeof(NDIS_OID) + Request.DATA.SET_INFORMATION.BytesRead;
201 }
202
203 DPRINT("Final request status: 0x%x (%d)\n", Status, Irp->IoStatus.Information);
204 }
205 else
206 {
207 /* Bad parameters */
209 }
210
211 Irp->IoStatus.Status = Status;
212
214
215 return Status;
216}
@ NdisRequestSetInformation
Definition: ndis.h:791
NDIS_OID Oid
Definition: nuiouser.h:44
UCHAR Data[sizeof(ULONG)]
Definition: nuiouser.h:45

Referenced by NduDispatchDeviceControl().

◆ WaitForBind()

static NTSTATUS WaitForBind ( PIRP  Irp,
PIO_STACK_LOCATION  IrpSp 
)
static

Definition at line 16 of file ioctl.c.

17{
18 /* I've seen several code samples that use this IOCTL but there's
19 * no official documentation on it. I'm just implementing it as a no-op
20 * right now because I don't see any reason we need it. We handle an open
21 * and bind just fine with IRP_MJ_CREATE and IOCTL_NDISUIO_OPEN_DEVICE */
22 DPRINT("Wait for bind complete\n");
23
24 Irp->IoStatus.Status = STATUS_SUCCESS;
25 Irp->IoStatus.Information = 0;
26
28
29 return STATUS_SUCCESS;
30}

Referenced by NduDispatchDeviceControl().