none
How to modify the data in MDL? RRS feed

  • Question

  • Hi all,

    I am trying to modify the data in MDL.  I would like to change the 802.11 Header to my own header. My custom header size is lesser than default header. I don't want to disturb the packet data in Netbuffer. So here my requirement is to shrink NetBufferDataLength. How can I achieve this?

    here I used following code: : 

    for(NetBuffer=NET_BUFFER_LIST_FIRST_NB (NetBufferLists); NetBuffer != NULL; NetBuffer = NET_BUFFER_NEXT_NB (NetBuffer)) { // here test buffer is Modified data buffer. NdisChangeNetBuffer(NetBuffer,testBuffer, Buffersize); } /* I am Trying observe the modified data here in following code:*/

    /***** Converted Data ***************/ for(NetBuffer=NET_BUFFER_LIST_FIRST_NB (NetBufferLists); NetBuffer != NULL; NetBuffer = NET_BUFFER_NEXT_NB (NetBuffer)) { dataLength = NET_BUFFER_DATA_LENGTH (NetBuffer); DbgPrint("Converted dataLength= %d", dataLength); // here expecting a modified datalength but not happening tempData=(PUCHAR)NdisAllocateMemoryWithTagPriority( FilterDriverHandle, (UINT)dataLength, Tag, NormalPoolPriority ); NdisQueryNetBuffer(NetBuffer, tempData, &BytesCopied); DbgPrint("Bytes copied in converted data= %d", BytesCopied); DbgPrint("Converted Data");

    for(count=0;count<BytesCopied;count++)

    { DbgPrint("%d---> 0x%x \t",count, tempData[count]); } NdisFreeMemory((PVOID)tempData, dataLength, 0); tempData = NULL; } /**************************************************************/ NDIS_STATUS NdisChangeNetBuffer( IN PNET_BUFFER NetBufferToSend, IN PUCHAR Modified_data, IN ULONG BufferLength ) { // Here Modified data is a buffer having OwnHeader + Packetdata // BufferLength is the whole dataLength of OwnHeader+Packetdata NDIS_STATUS ndisStatus = NDIS_STATUS_SUCCESS; PNET_BUFFER CurrentNetBuffer; ULONG MdlOffset; PMDL Mdl, MdlNext; ULONG dataLength; ULONG exit = 0; PMDL NewMdl = NULL; ULONG ArrayOffset=0; SIZE_T MdlByteCount, Remain, CopySize; do { CurrentNetBuffer = NetBufferToSend; Mdl = NET_BUFFER_FIRST_MDL (CurrentNetBuffer); MdlOffset = NET_BUFFER_DATA_OFFSET (CurrentNetBuffer); // // Skip the required offset bytes in the MDL chain. // while ((Mdl != NULL) && (MdlOffset >= (MdlByteCount = MmGetMdlByteCount(Mdl)))) { MdlOffset -= MdlByteCount; Mdl = Mdl->Next; } for (Remain = BufferLength; Mdl && (Remain > 0); Mdl = Mdl->Next) { PUCHAR SysVa = NULL ; MdlNext = Mdl->Next; // // Skip zero length MDLs. // MdlByteCount = MmGetMdlByteCount (Mdl); if (MdlByteCount == 0) { continue; } // // Compute the amount to transfer this time. // ASSERT (MdlOffset < MdlByteCount); MdlByteCount -= MdlOffset; CopySize = min (Remain, MdlByteCount); // // Perform the transfer, either directly or using our reserved PTE. // if(MdlNext!=NULL) { SysVa = MmGetSystemAddressForMdlSafe (Mdl, NormalPagePriority); } if(MdlNext == NULL) { NewMdl = NdisAllocateMdl(FilterDriverHandle, (PVOID)(Modified_data+ArrayOffset), CopySize); } else { RtlCopyMemory ( SysVa + MdlOffset, Modified_data + ArrayOffset, CopySize); } MdlOffset = 0; ArrayOffset += CopySize; Remain -= CopySize; if(MdlNext == NULL) { Mdl = NewMdl; } } } while(exit); return ndisStatus; } NTSTATUS NICCopyMdlToBuffer( IN PMDL Mdl, IN SIZE_T MdlOffset, //IN PVOID Buffer, IN PUCHAR Buffer, IN SIZE_T BytesToCopy, OUT SIZE_T* BytesCopied ) { SIZE_T MdlByteCount, Remain, CopySize; // // This routine copies data from an MDL chain to a flat buffer // A maximum of BytesToCopy bytes will be copied. // The actual number of bytes copied is returned in BytesCopied. // *BytesCopied = 0; // // Skip the required offset bytes in the MDL chain. // while ((Mdl != NULL) && (MdlOffset >= (MdlByteCount = MmGetMdlByteCount(Mdl)))) { MdlOffset -= MdlByteCount; Mdl = Mdl->Next; } // // While there is remaining data to be copied, and we have MDLs to walk... // for (Remain = BytesToCopy; Mdl && (Remain > 0); Mdl = Mdl->Next) { PUCHAR SysVa; // // Skip zero length MDLs. // DbgPrint("Address of Mdl in copy to buffer from mdl =%p", Mdl); MdlByteCount = MmGetMdlByteCount (Mdl); DbgPrint("Mdl Byte Count in copy to buffer from mdl= %d", MdlByteCount); DbgPrint("Remain in copy to buffer from mdl= %d", Remain); if (MdlByteCount == 0) { continue; } // // Compute the amount to transfer this time. // ASSERT (MdlOffset < MdlByteCount); MdlByteCount -= MdlOffset; CopySize = min (Remain, MdlByteCount); DbgPrint("Copy size in copy to buffer from mdl = %d" , CopySize); // // Perform the transfer, either directly or using our reserved PTE. // SysVa = MmGetSystemAddressForMdlSafe (Mdl, NormalPagePriority); if (SysVa == NULL) { //ATH_DEBUG_PRINTF (DBG_MP_RECV, ATH_DEBUG_TRC, ("SysVa Not Valid!!! Strange\n")); return STATUS_INSUFFICIENT_RESOURCES; } else { RtlCopyMemory (Buffer, SysVa + MdlOffset, CopySize); } MdlOffset = 0; Buffer += CopySize; Remain -= CopySize; } *BytesCopied = BytesToCopy - Remain; return STATUS_SUCCESS; } NDIS_STATUS NdisQueryNetBuffer( IN PNET_BUFFER NetBufferToSend, OUT PUCHAR pDest, //driver buffer got from malloc OUT PULONG BytesCopied ) { NDIS_STATUS ndisStatus = NDIS_STATUS_SUCCESS; PNET_BUFFER CurrentNetBuffer; ULONG offset; PMDL firstMdl; ULONG dataLength; ULONG exit = 0; do { *BytesCopied = 0; CurrentNetBuffer = NetBufferToSend; dataLength = NET_BUFFER_DATA_LENGTH (CurrentNetBuffer); firstMdl = NET_BUFFER_FIRST_MDL (CurrentNetBuffer); offset = NET_BUFFER_DATA_OFFSET (CurrentNetBuffer); ndisStatus = NICCopyMdlToBuffer (firstMdl, (SIZE_T)offset, pDest, (SIZE_T)dataLength, (SIZE_T*)BytesCopied); } while(exit); return ndisStatus; }

    Here I am expecting a new dataLength as lesser value than actual value in converted data. But that is not happing. It is still pointing to original MDL? Why? Where i did mistake?

    Thanks.



    • Edited by Sathish308 Tuesday, August 18, 2015 7:25 AM
    Tuesday, August 18, 2015 6:47 AM