Redirecting IOCTLs

Hello All,

I want to redirect the IOCTLs I receive on one device object to another one. I found an article on OSR website regarding the redirection of CREATE requests. My question is that is it possible to redirect every IOCTL request we receive for some device object to another device object.

What method should I choose. The one given on the article on OSR website (which make some changes in the FILE OBJECT) or shall I use IoBuildDeviceIoControlRequest to redirect my requests.

I can also post portions of my code if required.

Thanks

Perhaps a bit more explanation of what you are trying to accomplish
would help. What sort of devices are we talking about? What is the
relationship between your driver and the device you want to redirect
requests to? In some cases, where for example you are acting as a mux
device in between two stacks of devices, you simply have to allocate
enough IRP stack space to begin with and then forward the IOCTL to the
appropriate lower stack. In other cases you do indeed need to build your
own IO request, send it to the target device, and then complete the
original request as part of the completion handling for the new request.
IoBuildDeviceIoControlRequest may work, assuming that you can meet the
IRQL <= APC_LEVEL restriction.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@yahoo.com
Sent: Monday, May 07, 2007 12:47 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Redirecting IOCTLs

Hello All,

I want to redirect the IOCTLs I receive on one device object to another
one. I found an article on OSR website regarding the redirection of
CREATE requests. My question is that is it possible to redirect every
IOCTL request we receive for some device object to another device
object.

What method should I choose. The one given on the article on OSR website
(which make some changes in the FILE OBJECT) or shall I use
IoBuildDeviceIoControlRequest to redirect my requests.

I can also post portions of my code if required.

Thanks


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Hello All,

Thanks for the reply. The problem is that we are creating a virtual device aganist our original drive. For example we have

created X: (virtual device) aganist F: (actual hard disk partition). Now we want to redirect every device control request for

X: to F: That is we want to write a function aganist IRP_MJ_DEVICE_CONTROL that will redirect the control requests for X: to

F:

Below is some portion of the code:

// Driver Entry
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SampleDeviceControl;

NTSTATUS
SampleDeviceControl(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
{
PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
PIO_STACK_LOCATION currentIrpStack = IoGetCurrentIrpStackLocation(Irp);
ULONG command = currentIrpStack->Parameters.DeviceIoControl.IoControlCode;
NTSTATUS status = STATUS_SUCCESS;
KEVENT event;
PIRP OwnIoctlIrp;
PVOID InputBuffer, OutputBuffer;

status = IoAcquireRemoveLock(&deviceExtension->RemoveLock, (PVOID)42);
if (!NT_SUCCESS(status)) {
DbgPrint(“=== DTSC4DeviceControl FAILURE(1) ===\n”);
DbgPrint(“Acquire RemoveLock failed\n”);
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return status;
}

if(currentIrpStack->Parameters.DeviceIoControl.InputBufferLength){
InputBuffer = Irp->AssociatedIrp.SystemBuffer;
OutputBuffer = NULL;
}
else{
InputBuffer = NULL;
OutputBuffer = Irp->AssociatedIrp.SystemBuffer;
}

KeInitializeEvent(&event, /*SynchronizationEvent*/NotificationEvent, FALSE);
OwnIoctlIrp = IoBuildDeviceIoControlRequest (
command,
deviceExtension->StoragetDeviceObject,
InputBuffer,
currentIrpStack->Parameters.DeviceIoControl.InputBufferLength,
OutputBuffer,
currentIrpStack->Parameters.DeviceIoControl.OutputBufferLength,
FALSE, // External
&event,
&Irp->IoStatus);

if (NULL == OwnIoctlIrp) {

Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;

IoCompleteRequest(Irp,IO_NO_INCREMENT);

IoReleaseRemoveLock(&deviceExtension->RemoveLock, (PVOID)42);
return STATUS_INSUFFICIENT_RESOURCES;
}

status = IoCallDriver(deviceExtension->StoragetDeviceObject, OwnIoctlIrp);

if (status == STATUS_PENDING){
DbgPrint(“=== DTSC4DeviceControl FAILURE(3) ===\n”);
KeWaitForSingleObject(
&event,
Executive,
KernelMode,
FALSE,
NULL);
}

IoCompleteRequest(Irp,IO_NO_INCREMENT);

IoReleaseRemoveLock(&deviceExtension->RemoveLock, (PVOID)42);

return STATUS_SUCCESS;
}

Thanks