Hi guys,
I'm programming a firewall and so far I have complete the driver coding, but I don't know How to send IRPs from user applicatio to the dirver?
I have used IoCreateSymbolicLink to establish the connection between the usermode and kernelmode but I don't know how to send information from the user application downto the driver. I have searched in google in days but without a real results.
So any one can help me I will be very grateful.

this is my code:

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath)
{
    UNICODE_STRING DeviceName,Win32Device;
    PDEVICE_OBJECT DeviceObject = NULL;
    NTSTATUS status;
    unsigned i;

    DbgPrint("NetFilter: Driver loaded");

    RtlInitUnicodeString(&DeviceName,L"\\Device\\NetFilter0");
    RtlInitUnicodeString(&Win32Device,L"\\DosDevices\\NetFilter0");

    for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
        DriverObject->MajorFunction[i] = NetFilterDefaultHandler;

    DriverObject->MajorFunction[IRP_MJ_CREATE]         = NetFilterCreateClose;
    DriverObject->MajorFunction[IRP_MJ_CLOSE]          = NetFilterCreateClose;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDispatch;

    DriverObject->DriverUnload = NetFilterUnload;
    status = IoCreateDevice(DriverObject,
                            0,
                            &DeviceName,
                            FILE_DEVICE_UNKNOWN,
                            0,
                            FALSE,
                            &DeviceObject);
    if (!NT_SUCCESS(status))
        return status;
    if (!DeviceObject)
        return STATUS_UNEXPECTED_IO_ERROR;

    DeviceObject->Flags |= DO_DIRECT_IO;
    DeviceObject->AlignmentRequirement = FILE_WORD_ALIGNMENT;
    status = IoCreateSymbolicLink(&Win32Device, &DeviceName);
    if (!NT_SUCCESS(status)) DbgPrint("NetFilter Error: Faild to establish connection");
    DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

    return STATUS_SUCCESS;
}

void NetFilterUnload(IN PDRIVER_OBJECT DriverObject)
{
    UNICODE_STRING Win32Device;

    DbgPrint("NetFilter: Driver unloaded");

    RtlInitUnicodeString(&Win32Device,L"\\DosDevices\\NetFilter0");
    IoDeleteSymbolicLink(&Win32Device);
    IoDeleteDevice(DriverObject->DeviceObject);
}

NTSTATUS NetFilterCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}

NTSTATUS NetFilterDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return Irp->IoStatus.Status;
}

NTSTATUS DrvDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    DbgPrint("NetFilter: Entring DrvDispatch Function");
    PIO_STACK_LOCATION  irpStack;
    PVOID               ioBuffer;
    ULONG               inputBufferLength;
    ULONG               outputBufferLength;
    ULONG               ioControlCode;
    NTSTATUS            ntStatus;

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

    irpStack = IoGetCurrentIrpStackLocation(Irp);

    ioBuffer           = Irp->AssociatedIrp.SystemBuffer;
    inputBufferLength  = irpStack->Parameters.DeviceIoControl.InputBufferLength;
    outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;

    switch (irpStack->MajorFunction)
    {
    case IRP_MJ_CREATE:

        DbgPrint("DrvFltIp.SYS: IRP_MJ_CREATE\n");

        break;

    case IRP_MJ_CLOSE:

        DbgPrint("DrvFltIp.SYS: IRP_MJ_CLOSE\n");

        break;

    case IRP_MJ_DEVICE_CONTROL:

        DbgPrint("DrvFltIp.SYS: IRP_MJ_DEVICE_CONTROL\n");

        ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;

        switch (ioControlCode)
        {
            case START_IP_HOOK:
            {
                SetFilterFunction(cbFilterFunction);

                break;
            }

            case STOP_IP_HOOK:
            {
                SetFilterFunction(NULL);

                break;
            }

            case ADD_FILTER:
            {
                if(inputBufferLength == sizeof(IPFilter))
                {
                    IPFilter *nf;

                    nf = (IPFilter *)ioBuffer;

                    AddFilterToList(nf);
                }

                break;
            }

            case CLEAR_FILTER:
            {
                ClearFilterList();

                break;
            }

            default:
                Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;

                DbgPrint("DrvFltIp.SYS: unknown IRP_MJ_DEVICE_CONTROL\n");

                break;
        }

        break;
    }

    ntStatus = Irp->IoStatus.Status;

    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return ntStatus;
}

Recommended Answers

All 6 Replies

Kernel mode drivers do not normally export functions. Instead, DriverEntry is responsible to report to user mode, the addresses of functions which need to be exposed to the system, using the DRIVER_OBJECT structure.

The IRP_MJ_DEVICE_CONTROL is the IRP used when a usermode program sends an IOCTL to a driver with the DeviceIoControl function.

Dear BobS0327, what function must I use to send signals to the driver??????
If you have some examples please give it to me!!! :)

nullptr provided a link to a lot of good tutorials. This specific, basic tutorial from the link provided does have all the info you should need.

Please read my question again, I know how to establish the connection between the driver and the user, but I don't know how to send signals from the user to the driver!!!
All these tutorials shows how to program a driver, I already know about that.

From tutorial 1 - "As you notice, you simply need to open the DOS Device Name using \.\<DosName>. You could even open \Device\<Nt Device Name> using the same method. You will then create a handle to the device and you can call WriteFile, ReadFile, CloseHandle, DeviceIoControl!"

Which part are you not understanding?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.