动NET_BUFFER还算行,动NET_BUFFER_LIST估计也可以,消耗就更大了。还有种方法就是换MDL,或者链在MdlChain后面, 然后改CurrentMdl,不过这种方法估计要自己维护个结构,看哪个Mdl是自己分配的,或者把相关的数据放到NET_BUFFER的MiniportReserved里,但原来的包在complete的时候要恢复的完全一样,想想还是替换NET_BUFFER最简单。
ndislwf可能还算较新的东西,网上相关的东西不多,希望这个例子能有帮助
晕 代码高亮的快疯了,就这样算了
 
   1: VOID

   2: FilterSendNetBufferListsComplete(

   3:         IN  NDIS_HANDLE         FilterModuleContext,

   4:         IN  PNET_BUFFER_LIST    NetBufferLists,

   5:         IN  ULONG               SendCompleteFlags

   6:         )

   7: /*++

   8:  

   9: Routine Description:

  10: 

  11:     SendNetBufferListComplete 

  12: 

  13: Arguments:

  14: 

  15:       

  16:      

  17: Return Value:

  18: 

  19:      NONE

  20:  

  21: --*/

  22: {

  23:     PMS_FILTER          pFilter = (PMS_FILTER)FilterModuleContext;

  24:     //ULONG              NumOfSendCompletes = 0;

  25:     //BOOLEAN            DispatchLevel;

  26:     //PNET_BUFFER_LIST   CurrNbl;

  27:  

  28:     PNET_BUFFER_LIST    pNetBufList = NULL;

  29:     PNET_BUFFER_LIST    pNextNetBufList = NULL;

  30:  

  31:     PMDL                pMdl = NULL;

  32:     PNDISPROT_ETH_HEADER pEthHeader = NULL;

  33:     ULONG               ulOffset = 0;

  34:     ULONG               ulBufferLength = 0;

  35:     PNET_BUFFER         pCurrentNetBuffer = NULL;

  36:     PNET_BUFFER         pPreviousNetBuffer = NULL;

  37:     PNET_BUFFER         pOriginalNetBuffer = NULL;

  38:     ULONG               ulTotalLength = 0;

  39:    

  40:     DEBUGP(DL_TRACE, ("===>SendNBLComplete, NetBufferList: %p.\n", NetBufferLists));

  41:  

  42:     //

  43:     // if necessary, undo any modifications to the NetBufferList thate were performed "on the way down"

  44:     //

  45:     //if (pFilter->TrackSends)

  46:     //{

  47:     //    CurrNbl = NetBufferLists;

  48:     //    while (CurrNbl)

  49:     //    {

  50:     //        NumOfSendCompletes++;

  51:     //        CurrNbl = NET_BUFFER_LIST_NEXT_NBL(CurrNbl);

  52:     //        

  53:     //    }

  54:     //    DispatchLevel = NDIS_TEST_SEND_AT_DISPATCH_LEVEL(SendCompleteFlags);

  55:     //    FILTER_ACQUIRE_LOCK(&pFilter->Lock, DispatchLevel);

  56:     //    pFilter->OutstandingSends -= NumOfSendCompletes;

  57:     //    FILTER_LOG_SEND_REF(2, pFilter, PrevNbl, pFilter->OutstandingSends);        

  58:     //    FILTER_RELEASE_LOCK(&pFilter->Lock, DispatchLevel);

  59:     //}

  60:  

  61:  

  62:     pNetBufList = NetBufferLists;

  63:  

  64:     while (pNetBufList != NULL)

  65:     {

  66:         pNextNetBufList = NET_BUFFER_LIST_NEXT_NBL(pNetBufList);

  67:  

  68:         for (pCurrentNetBuffer = NET_BUFFER_LIST_FIRST_NB(pNetBufList);

  69:             pCurrentNetBuffer != NULL;

  70:             pCurrentNetBuffer = NET_BUFFER_NEXT_NB(pCurrentNetBuffer)) 

  71:         {

  72:             pMdl = NET_BUFFER_CURRENT_MDL(pCurrentNetBuffer);

  73:             ulTotalLength = NET_BUFFER_DATA_LENGTH(pCurrentNetBuffer);

  74:             ulOffset = NET_BUFFER_CURRENT_MDL_OFFSET(pCurrentNetBuffer);

  75:  

  76:             do 

  77:             {

  78:                 ASSERT(pMdl != NULL);

  79:                 if (pMdl)

  80:                 {

  81:                     NdisQueryMdl(

  82:                         pMdl,

  83:                         &pEthHeader,

  84:                         &ulBufferLength,

  85:                         NormalPagePriority);

  86:                 }

  87:                 if (NULL == pEthHeader)

  88:                 {

  89:                     //

  90:                     //  The system is low on resources. Set up to handle failure

  91:                     //  below.

  92:                     //

  93:                     ulBufferLength = 0;

  94:                     break;

  95:                 }

  96:  

  97:                 if (0 == ulBufferLength)

  98:                 {

  99:                     break;

 100:                 }

 101:  

 102:                 ASSERT(ulBufferLength > ulOffset);

 103:  

 104:                 ulBufferLength -= ulOffset;

 105:                 pEthHeader = (PNDISPROT_ETH_HEADER)((PUCHAR)pEthHeader + ulOffset);

 106:  

 107:                 if (ulBufferLength < sizeof(NDISPROT_ETH_HEADER))

 108:                 {

 109:                     DEBUGP(DL_WARN,

 110:                         ("ReceiveNetBufferList: runt nbl %p, first buffer length %d\n",

 111:                         pNetBufList, ulBufferLength));

 112:  

 113:                     break;

 114:                 }

 115:  

 116:                 //

 117:                 //  Check the EtherType. If the Ether type indicates presence of

 118:                 //  a tag, then the "real" Ether type is 4 bytes further down.

 119:                 //

 120:                 if (pEthHeader->EthType == NPROT_8021P_TAG_TYPE)

 121:                 {

 122:                     USHORT UNALIGNED *pEthType;

 123:  

 124:                     if (ulBufferLength < (sizeof(NDISPROT_ETH_HEADER) + 4))

 125:                     {

 126:                         break;

 127:                     }

 128:  

 129:                     pEthType = (USHORT UNALIGNED *)((PUCHAR)&pEthHeader->EthType + 4);

 130:  

 131:                     if (*pEthType != NPROT_ETH_TYPE)

 132:                     {

 133:                         break;

 134:                     }

 135:                 }

 136:                 else if (pEthHeader->EthType != NPROT_ETH_TYPE)

 137:                 {

 138:                     break;

 139:                 }

 140:  

 141:                 //

 142:                 //  to do

 143:                 //

 144:  

 145:                 if (NdisGetPoolFromNetBuffer(pCurrentNetBuffer) == pFilter->NetBufferPool)

 146:                 {

 147:                     KdBreakPoint();

 148:  

 149:                     pOriginalNetBuffer = NET_BUFFER_LIST_MINIPORT_RESERVED(pCurrentNetBuffer)[0];

 150:  

 151:                     if (pCurrentNetBuffer == NET_BUFFER_LIST_FIRST_NB(pNetBufList))

 152:                     {

 153:                         NET_BUFFER_LIST_FIRST_NB(pNetBufList) = pOriginalNetBuffer;

 154:                     }

 155:                     else

 156:                     {

 157:                         pPreviousNetBuffer->Next = pOriginalNetBuffer;

 158:                     }

 159:  

 160:                     pOriginalNetBuffer->Next = pCurrentNetBuffer->Next;

 161:  

 162:  

 163:                     NdisFreeNetBuffer(pCurrentNetBuffer);

 164:  

 165:                     pCurrentNetBuffer = pOriginalNetBuffer;

 166:                 }

 167:  

 168:             } while (FALSE);

 169:  

 170:             pPreviousNetBuffer = pCurrentNetBuffer;

 171:         }

 172:  

 173:  

 174:         pNetBufList = pNextNetBufList;

 175:     }

 176:  

 177:  

 178:  

 179:     NdisFSendNetBufferListsComplete(pFilter->FilterHandle, NetBufferLists, SendCompleteFlags);

 180:  

 181:     DEBUGP(DL_TRACE, ("<===SendNBLComplete.\n"));

 182: }

 

   1: VOID

   2: FilterSendNetBufferLists(

   3:         IN  NDIS_HANDLE         FilterModuleContext,

   4:         IN  PNET_BUFFER_LIST    NetBufferLists,

   5:         IN  NDIS_PORT_NUMBER    PortNumber,

   6:         IN  ULONG               SendFlags

   7:         )

   8: /*++

   9:  

  10: Routine Description:

  11: 

  12:     Send Net Buffer List handler

  13:     This function is an optional function for filter drivers. If provided, NDIS

  14:     will call this function to transmit a linked list of NetBuffers, described by a 

  15:     NetBuferList, over the network. If this handler is NULL, NDIS will skip calling

  16:     this fitler when sending a NetBufferList and will call the next lower fitler 

  17:     in the stack with a non_NULL FilterSendNetBufferList handleror the miniport driver.

  18:     A filter that doesn't provide a FilerSendNetBufferList handler can not initiate a 

  19:     send o its own.

  20: 

  21: Arguments:

  22: 

  23:     FilterModuleContext: Pointer to our filter context area.

  24:     NetBufferLists: Pointer to a List of NetBufferLists.

  25:     PortNumber - Port Number to which this send is targetted

  26:     SendFlags-  Specifies if the call is at DISPATCH_LEVEL                     

  27:   

  28: 

  29: Return Value:

  30:  

  31:     NDIS_STATUS_SUCCESS: 

  32:     NDIS_STATUS_PENDING:

  33:     NDIS_STATUS_INVALID_PACKET:

  34:     NDIS_STATUS_RESOURCES:

  35:     NDIS_STATUS_FAILURE:

  36: 

  37: 

  38: NOTE: The filter will act like a passthru filter.       

  39:  

  40: --*/

  41: {

  42:     NDIS_STATUS         Status = NDIS_STATUS_FAILURE;

  43:     PMS_FILTER          pFilter = (PMS_FILTER)FilterModuleContext;

  44:     //PNET_BUFFER_LIST    CurrNbl;

  45:     //BOOLEAN             DispatchLevel;

  46:     //BOOLEAN             bFalse = FALSE;

  47:  

  48:     PNET_BUFFER_LIST    pNetBufList = NULL;

  49:     PNET_BUFFER_LIST    pNextNetBufList = NULL;

  50:  

  51:     PMDL                pMdl = NULL;

  52:     PNDISPROT_ETH_HEADER pEthHeader = NULL;

  53:     ULONG               ulOffset = 0;

  54:     ULONG               ulBufferLength = 0;

  55:     PNET_BUFFER         pCurrentNetBuffer = NULL;

  56:     PNET_BUFFER         pPreviousNetBuffer = NULL;

  57:     PNET_BUFFER         pNewNetBuffer = NULL;

  58:     ULONG               ulTotalLength = 0;

  59:     

  60:     DEBUGP(DL_TRACE, ("===>SendNetBufferList: NBL = %p.\n", NetBufferLists));

  61:  

  62: //    do

  63: //    {

  64: //

  65: //       DispatchLevel = NDIS_TEST_SEND_AT_DISPATCH_LEVEL(SendFlags);

  66: //#if DBG

  67: //        //

  68: //        // we should never get packets to send if we are not in running state

  69: //        //

  70: //        

  71: //        FILTER_ACQUIRE_LOCK(&pFilter->Lock, DispatchLevel);

  72: //        //

  73: //        // If the filter is not in running state, fail the send

  74: //        // 

  75: //        if (pFilter->State != FilterRunning)

  76: //        {

  77: //            FILTER_RELEASE_LOCK(&pFilter->Lock, DispatchLevel);

  78: //            

  79: //            CurrNbl = NetBufferLists;

  80: //            while (CurrNbl)

  81: //            {

  82: //                NET_BUFFER_LIST_STATUS(CurrNbl) = NDIS_STATUS_PAUSED;

  83: //                CurrNbl = NET_BUFFER_LIST_NEXT_NBL(CurrNbl);

  84: //            }

  85: //            NdisFSendNetBufferListsComplete(pFilter->FilterHandle, 

  86: //                        NetBufferLists, 

  87: //                        DispatchLevel ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0);

  88: //            break;

  89: //            

  90: //        }

  91: //        FILTER_RELEASE_LOCK(&pFilter->Lock, DispatchLevel);

  92: //#endif

  93: //        if (pFilter->TrackSends)

  94: //        {

  95: //            FILTER_ACQUIRE_LOCK(&pFilter->Lock, DispatchLevel);

  96: //            CurrNbl = NetBufferLists;

  97: //            while (CurrNbl)

  98: //            {

  99: //                pFilter->OutstandingSends++;

 100: //                FILTER_LOG_SEND_REF(1, pFilter, CurrNbl, pFilter->OutstandingSends);

 101: //                

 102: //                CurrNbl = NET_BUFFER_LIST_NEXT_NBL(CurrNbl);

 103: //            }

 104: //            FILTER_RELEASE_LOCK(&pFilter->Lock, DispatchLevel);

 105: //        }

 106: //        //

 107: //        // If necessary, queue the NetBufferList in a local structure for later processing

 108: //        //

 109: //        NdisFSendNetBufferLists(pFilter->FilterHandle, NetBufferLists, PortNumber, SendFlags);

 110: //        

 111: //        

 112: //    }

 113: //    while (bFalse);

 114:  

 115:     KdBreakPoint();

 116:  

 117:  

 118:     pNetBufList = NetBufferLists;

 119:  

 120:     while (pNetBufList != NULL)

 121:     {

 122:         pNextNetBufList = NET_BUFFER_LIST_NEXT_NBL(pNetBufList);

 123:  

 124:         for (pCurrentNetBuffer = NET_BUFFER_LIST_FIRST_NB(pNetBufList);

 125:             pCurrentNetBuffer != NULL;

 126:             pCurrentNetBuffer = NET_BUFFER_NEXT_NB(pCurrentNetBuffer)) 

 127:         {

 128:             pMdl = NET_BUFFER_CURRENT_MDL(pCurrentNetBuffer);

 129:             ulTotalLength = NET_BUFFER_DATA_LENGTH(pCurrentNetBuffer);

 130:             ulOffset = NET_BUFFER_CURRENT_MDL_OFFSET(pCurrentNetBuffer);

 131:  

 132:             do 

 133:             {

 134:                 ASSERT(pMdl != NULL);

 135:                 if (pMdl)

 136:                 {

 137:                     NdisQueryMdl(

 138:                         pMdl,

 139:                         &pEthHeader,

 140:                         &ulBufferLength,

 141:                         NormalPagePriority);

 142:                 }

 143:                 if (NULL == pEthHeader)

 144:                 {

 145:                     //

 146:                     //  The system is low on resources. Set up to handle failure

 147:                     //  below.

 148:                     //

 149:                     ulBufferLength = 0;

 150:                     break;

 151:                 }

 152:  

 153:                 if (0 == ulBufferLength)

 154:                 {

 155:                     break;

 156:                 }

 157:  

 158:                 ASSERT(ulBufferLength > ulOffset);

 159:  

 160:                 ulBufferLength -= ulOffset;

 161:                 pEthHeader = (PNDISPROT_ETH_HEADER)((PUCHAR)pEthHeader + ulOffset);

 162:  

 163:                 if (ulBufferLength < sizeof(NDISPROT_ETH_HEADER))

 164:                 {

 165:                     DEBUGP(DL_WARN,

 166:                         ("ReceiveNetBufferList: runt nbl %p, first buffer length %d\n",

 167:                             pNetBufList, ulBufferLength));

 168:  

 169:                     break;

 170:                 }

 171:  

 172:                 //

 173:                 //  Check the EtherType. If the Ether type indicates presence of

 174:                 //  a tag, then the "real" Ether type is 4 bytes further down.

 175:                 //

 176:                 if (pEthHeader->EthType == NPROT_8021P_TAG_TYPE)

 177:                 {

 178:                     USHORT UNALIGNED *pEthType;

 179:  

 180:                     if (ulBufferLength < (sizeof(NDISPROT_ETH_HEADER) + 4))

 181:                     {

 182:                         break;

 183:                     }

 184:  

 185:                     pEthType = (USHORT UNALIGNED *)((PUCHAR)&pEthHeader->EthType + 4);

 186:  

 187:                     if (*pEthType != NPROT_ETH_TYPE)

 188:                     {

 189:                         break;

 190:                     }

 191:                 }

 192:                 else if (pEthHeader->EthType != NPROT_ETH_TYPE)

 193:                 {

 194:                     break;

 195:                 }

 196:  

 197:                 //

 198:                 //  to do

 199:                 //

 200:  

 201:                 //PrintDataBuffer((PUCHAR)pEthHeader, ulBufferLength);

 202:                 DbgPrint("Packet Length 0x%d,  pMdl->ByteCount 0x%x,  pMdl->Next 0x%x\n", ulBufferLength, pMdl->ByteCount, pMdl->Next);

 203:  

 204:                 Status = CopyNetBuffer(pFilter,pCurrentNetBuffer, &pNewNetBuffer);

 205:                 if (NDIS_STATUS_SUCCESS != Status)

 206:                 {

 207:                     break;

 208:                 }

 209:  

 210:                 if (pCurrentNetBuffer == NET_BUFFER_LIST_FIRST_NB(pNetBufList))

 211:                 {

 212:                     NET_BUFFER_LIST_FIRST_NB(pNetBufList) = pNewNetBuffer;

 213:                 }

 214:                 else

 215:                 {

 216:                     pPreviousNetBuffer->Next = pNewNetBuffer;

 217:                 }

 218:  

 219:                 pNewNetBuffer->Next = pCurrentNetBuffer->Next;

 220:                 NET_BUFFER_LIST_MINIPORT_RESERVED(pNewNetBuffer)[0] = pCurrentNetBuffer;

 221:  

 222:  

 223:                 pCurrentNetBuffer = pNewNetBuffer;

 224:  

 225:             } while (FALSE);

 226:  

 227:             pPreviousNetBuffer = pCurrentNetBuffer;

 228:         }

 229:  

 230:  

 231:         pNetBufList = pNextNetBufList;

 232:     }

 233:  

 234:  

 235:  

 236:     NdisFSendNetBufferLists(pFilter->FilterHandle, NetBufferLists, PortNumber, SendFlags);

 237:     

 238:     DEBUGP(DL_TRACE, ("<===SendNetBufferList. \n"));

 239: }


评论

该日志第一篇评论

发表评论

评论也有版权!