0
votes

I have this NDIS Filter Driver. I try to start in my driver a thread, that will send packet every 10 seconds.

To do that, I use this code:

LARGE_INTEGER TimePrev, TimeNow;
void ThreadedAction()
{
    while(1)
    {
        KeQuerySystemTime(&TimeNow);
        if(NBLtoSend && (TimeNow.QuadPart - TimePrev.QuadPart)>100000000)
        {
            NdisFSendNetBufferLists(NBLtoSend->SourceHandle, NBLtoSend, 0, 0);
            KeQuerySystemTime(&TimePrev);
        }
    }
}

The function started with PsCreateSystemThread in DriverEntry.
But this is not sends my packet.
I try to use this:

void ThreadedAction()
{
    while(1)
    {
        if(NBLtoSend)
        {
            NdisFSendNetBufferLists(NBLtoSend->SourceHandle, NBLtoSend, 0, 0);
        }
    }
}

This code sends my packet non stop.

The following code creates new file with my packet every 10 seconds(CreateFileS is my function), but don't sends my packet:

LARGE_INTEGER TimePrev, TimeNow;
void ThreadedAction()
{
    while(1)
    {
        KeQuerySystemTime(&TimeNow);
        if(NBLtoSend && (TimeNow.QuadPart - TimePrev.QuadPart)>100000000)
        {
            PMDL pmdl = NET_BUFFER_CURRENT_MDL(NET_BUFFER_LIST_FIRST_NB(NBLtoSend));
            CreateFileS(NULL,(char*)MmGetMdlVirtualAddress(pmdl),MmGetMdlByteCount(pmdl));
            NdisFSendNetBufferLists(NBLtoSend->SourceHandle, NBLtoSend, 0, 0);
            KeQuerySystemTime(&TimePrev);
        }
    }
}

Why it's happened, and what can I do to send packet every 10 seconds?

1

1 Answers

0
votes

The NDIS6 programming model is asynchronous. This means when you call NdisFSendNetBufferLists, you actually give up ownership of the NBL. You cannot reuse the NBL until it is returned to your filter via your FilterReturnNetBufferLists callback.

So first off, you need to change the code to look more like this:

PNET_BUFFER_LIST NblToSend;

void FilterReturnNetBufferLists(PNET_BUFFER_LIST NblChain)
{
    for each Nbl in NblChain
    {
        if(Nbl->SourceHandle == MyFilterHandle)
            NblToSend = Nbl;
        else
            NdisFReturnNetBufferLists(Nbl);
    }
}

void ThreadedAction()
{
    while(1)
    {
        if(NBLtoSend)
        {
            PNET_BUFFER_LIST TempNbl = NblToSend;
            NblToSend = NULL;
            NdisFSendNetBufferLists(TempNbl);
        }
    }
}

This way, you honor the NDIS rule: Do not send an NBL again until it's been returned to you.

Next up, the looping is highly inefficient. As implemented, the loop will consume 100% of the CPU, busily counting down the nanoseconds until it's time to send another packet. Instead, use a timer. The system will call into your thread when 10 seconds have elapsed.