www.pudn.com > IPsec_Src_Instal.rar > Protocol.c


/*++ 
 
	Copyright (c) 1992-2000  Microsoft Corporation 
 
--*/ 
 
#include "precomp.h" 
#include "PktHdr.h" 
#include "hmac.h" 
 
 
 
/* 
	ProtocolBindAdapter performs dynamic binding operations whenever an 
	underlying NIC to which the protocol can bind itself becomes available.  
*/ 
VOID PPBindAdapter 
					( 
						OUT PNDIS_STATUS Status,  
						IN NDIS_HANDLE BindContext,  
						IN PNDIS_STRING	DeviceName, 
						IN PVOID SystemSpecific1,  
						IN PVOID SystemSpecific2 
					) 
{ 
	NDIS_HANDLE						ConfigHandle	= NULL; 
	 
	PNDIS_CONFIGURATION_PARAMETER	Param; 
 
	PADAPT							pAdapt			=	NULL; 
 
	NDIS_STATUS						Sts; 
 
	UINT							MediumIndex; 
	 
	ULONG							TotalSize; 
 
	NDIS_STRING	DeviceStr	=	NDIS_STRING_CONST("UpperBindings"); 
 
 
	do 
	{ 
		// 
		//	Returns a handle for the registry key in which a protocol driver's  
		//	per-adapter information is stored. 
		// 
		NdisOpenProtocolConfiguration(	Status,  
										&ConfigHandle,  
										SystemSpecific1); 
 
 
		if(*Status != NDIS_STATUS_SUCCESS)	 
		{ 
			break; 
		} 
		 
		// 
		//	Returns the value of a named entry of the specified  
		//	type from the registry 
		// 
		NdisReadConfiguration(	Status,  
								&Param,  
								ConfigHandle,  
								&DeviceStr,  
								NdisParameterString); 
 
 
		if(*Status != NDIS_STATUS_SUCCESS)	 
		{ 
			 
			break; 
 
		} 
		 
		// 
        //	Allocate memory for the Adapter structure. This represents  
		//	both the protocol context as well as the adapter structure 
		///	when the miniport is initialized. 
        // 
		TotalSize = sizeof(ADAPT) + Param->ParameterData.StringData.MaximumLength; 
 
		NdisAllocateMemoryWithTag(	&pAdapt,  
									TotalSize, 
									TAG ); 
			 
		if(pAdapt == NULL) 
		{	 
			 
			*Status	=	NDIS_STATUS_RESOURCES; 
			 
			break; 
 
		} 
 
		// 
		//	Initializes Adapter Specific Context to Zero 
		// 
		NdisZeroMemory(pAdapt, TotalSize); 
 
        // 
        // Initialize the adapter structure. We copy in the IM device 
        // name as well, because we may need to use it in a call to 
        // NdisIMCancelInitializeDeviceInstance. The string returned 
        // by NdisReadConfiguration is active (i.e. available) only 
        // for the duration of this call to our BindAdapter handler. 
        // 
        pAdapt->DeviceName.MaximumLength	=	Param->ParameterData.StringData.MaximumLength; 
        pAdapt->DeviceName.Length			=	Param->ParameterData.StringData.Length; 
        pAdapt->DeviceName.Buffer			=	(PWCHAR)((ULONG_PTR)pAdapt + sizeof(ADAPT)); 
         
		NdisMoveMemory(pAdapt->DeviceName.Buffer, 
                       Param->ParameterData.StringData.Buffer, 
                       Param->ParameterData.StringData.MaximumLength); 
		 
				 
		// 
		//	Sets up an event object (during driver initialization) to be used subsequently  
		//	as a synchronization mechanism.  
		// 
		NdisInitializeEvent(&pAdapt->Event); 
 
 
		NdisAllocateSpinLock(&pAdapt->Lock); 
		 
		// 
		//	Allocates and initializes a block of storage for a pool of packet descriptors.  
		// 
		NdisAllocatePacketPoolEx(	Status,  
									&pAdapt->SendPacketPoolHandle,  
									MIN_PACKET_POOL_SIZE,  
									MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,  
									sizeof(SEND_RSVD)); 
 
		if(*Status	!=	NDIS_STATUS_SUCCESS)	 
		{ 
			 
			break; 
 
		} 
			 
		// 
		//	Allocates and initializes a block of storage for a pool of packet descriptors. 
		// 
		NdisAllocatePacketPoolEx(	Status,  
									&pAdapt->RecvPacketPoolHandle,  
									MIN_PACKET_POOL_SIZE,  
									MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,  
									sizeof(RECV_RSVD)); 
			 
		if(*Status	!=	NDIS_STATUS_SUCCESS)	 
		{ 
			 
			break; 
 
		} 
					 
 
	    #define NbrBfrElems  64 
 
		// 
		// Allocate a packet pool for Sends. 
		// 
        NdisAllocateBufferPool(	Status, 
								&pAdapt->hSendBufferPool, 
								NbrBfrElems); 
 
        if (*Status != NDIS_STATUS_SUCCESS) 
        { 
		    break; 
        } 
		 
		// 
        // Allocate a packet pool for Receives. 
		// 
        NdisAllocateBufferPool(	Status, 
								&pAdapt->hRecvBufferPool, 
								NbrBfrElems); 
 
        if (*Status != NDIS_STATUS_SUCCESS) 
        { 
            break; 
        } 
		 
		// 
		//	Sets up a binding between the calling protocol and a  
		//	particular underlying NIC driver. 
		// 
		NdisOpenAdapter(	Status,  
							&Sts,  
							&pAdapt->BindingHandle,  
							&MediumIndex,  
							Medium, 
							sizeof(Medium)/sizeof(NDIS_MEDIUM),  
							ProtocolHandle,  
							pAdapt,  
							DeviceName,  
							0,  
							NULL); 
 
	 
		// 
		//	The requested operation is being handled asynchronously,  
		//	and the PPOpenAdapterComplete function will be called 
		//	when the open is completed.  
		// 
		if(*Status == NDIS_STATUS_PENDING) 
		{ 
			// 
			//	Puts the caller into a wait state until the given event 
			//	is set to the Signaled state or the wait times out.  
			// 
			NdisWaitEvent(&pAdapt->Event, 0); 
		 
			*Status	=	pAdapt->Status; 
 
		}	 
 
		if(*Status != NDIS_STATUS_SUCCESS)	 
		{ 
		 
			break; 
 
		} 
		 
		pAdapt->Medium	=	Medium[MediumIndex]; 
			 
		// 
        //	Now ask NDIS to initialize our miniport (upper) edge. 
        //	Set the flag below to synchronize with a possible call 
        //	to our protocol Unbind handler that may come in before 
        //	our miniport initialization happens. 
        // 
		pAdapt->MiniportInitPending = TRUE; 
         
		NdisInitializeEvent(&pAdapt->MiniportInitEvent); 
 
		// 
		//	Calls the NDIS intermediate driver's MPInitialize function  
		//	to initialize the driver's virtual NIC and to set up state 
		//	information about the driver's virtual NIC for subsequently  
		//	bound protocols.  
	    // 
		*Status		=	NdisIMInitializeDeviceInstanceEx(	DriverHandle,  
															&pAdapt->DeviceName,  
															pAdapt); 
 
        if (*Status != NDIS_STATUS_SUCCESS)	 
		{ 
			 
			break; 
 
		} 
 
	}while(FALSE); 
 
	if (ConfigHandle != NULL) 
	{ 
		 
		NdisCloseConfiguration(ConfigHandle); 
 
	} 
 
	if (*Status != NDIS_STATUS_SUCCESS) 
	{ 
		 
		if (pAdapt != NULL) 
		{ 
 
            if (pAdapt->BindingHandle != NULL) 
            { 
 
                NDIS_STATUS    LocalStatus; 
 
                NdisResetEvent(&pAdapt->Event); 
                 
                NdisCloseAdapter(	&LocalStatus, 
									pAdapt->BindingHandle 
								); 
                 
				pAdapt->BindingHandle = NULL; 
 
                if (LocalStatus == NDIS_STATUS_PENDING) 
                { 
                      
					NdisWaitEvent(&pAdapt->Event, 0); 
                      
					LocalStatus = pAdapt->Status; 
 
                } 
            } 
			 
			if (pAdapt->SendPacketPoolHandle != NULL) 
			{ 
				  
				NdisFreePacketPool(pAdapt->SendPacketPoolHandle); 
 
			} 
 
			if (pAdapt->RecvPacketPoolHandle != NULL) 
			{ 
				 
				NdisFreePacketPool(pAdapt->RecvPacketPoolHandle); 
 
			} 
 
			NdisFreeMemory(pAdapt, 0, 0); 
             
			pAdapt = NULL; 
 
		} 
	 
	} 
 
} 
 
 
/* 
 
	Completes processing of a binding operation for which NdisOpenAdapter returned  
	NDIS_STATUS_PENDING.  
 
*/ 
VOID PPOpenAdapterComplete 
							( 
								IN NDIS_HANDLE ProtocolBindingContext,  
								IN NDIS_STATUS Status,  
								IN NDIS_STATUS OpenErrorStaus 
							) 
{ 
 
	PADAPT pAdapt	=	(PADAPT)ProtocolBindingContext; 
 
	pAdapt->Status	=	Status; 
 
	NdisSetEvent(&pAdapt->Event); 
} 
 
/* 
	Releases all resources the driver allocated for per-binding network I/O operations with  
	one possible exception: the driver-allocated context area designated by the  
	ProtocolBindingContext handle. 
*/ 
VOID PPUnbindAdapter 
						( 
							OUT PNDIS_STATUS Status,  
							IN NDIS_HANDLE ProtocolBindingContext,  
							IN NDIS_HANDLE UnbindContext 
						) 
{ 
	PADAPT			pAdapt			=	(PADAPT)ProtocolBindingContext; 
 
	NDIS_STATUS		LocalStatus; 
 
    NdisAcquireSpinLock(&pAdapt->Lock); 
 
	// 
    //	Set the flag that the miniport below is unbinding, so the request handlers will 
    //	fail any request comming later 
    //  
    pAdapt->UnbindingInProcess = TRUE; 
 
	// 
	//	If a Request is Queued 
	// 
	if(pAdapt->QueuedRequest ==	TRUE) 
	{ 
 
		pAdapt->QueuedRequest = FALSE; 
 
		NdisReleaseSpinLock(&pAdapt->Lock); 
 
		PPRequestComplete(	pAdapt,  
							&pAdapt->Request,  
							NDIS_STATUS_FAILURE 
						 ); 
 
	} 
	else 
	{ 
		 
		NdisReleaseSpinLock(&pAdapt->Lock); 
 
	} 
 
    // 
    //	Check if we had called NdisIMInitializeDeviceInstanceEx and 
    //	we are awaiting a call to MiniportInitialize. 
    // 
    if (pAdapt->MiniportInitPending == TRUE) 
    { 
        // 
        //	Try to cancel the pending IMInit process. 
        // 
        LocalStatus = NdisIMCancelInitializeDeviceInstance(DriverHandle, 
														   &pAdapt->DeviceName 
														  ); 
 
        if (LocalStatus == NDIS_STATUS_SUCCESS) 
        { 
            // 
            //	Successfully cancelled IM Initialization; our 
            //	Miniport Initialize routine will not be called 
            //	for this device. 
            // 
            pAdapt->MiniportInitPending = FALSE; 
     
        } 
        else 
        { 
            // 
            //	Our Miniport Initialize routine will be called 
            //	(may be running on another thread at this time). 
            //	Wait for it to finish. 
            // 
            NdisWaitEvent(&pAdapt->MiniportInitEvent, 0); 
     
        } 
 
    } 
 
    // 
    //	Call NDIS to remove our device-instance. We do most of the work 
    //	inside the HaltHandler. 
    // 
    //	The Handle will be NULL if our miniport Halt Handler has been called or 
    //	if the IM device was never initialized 
    // 
	if(pAdapt->MiniportHandle	!=	NULL) 
	{ 
		// 
		//	The call to NdisIMDeInitializeDeviceInstance causes a call to the  
		//	intermediate driver's MPHalt function after NDIS has told all higher 
		//	level protocols that had bound themselves to the intermediate's  
		//	virtual NIC that they must unbind 
		// 
		*Status		=	NdisIMDeInitializeDeviceInstance(pAdapt->MiniportHandle); 
 
		if(*Status	!=	NDIS_STATUS_SUCCESS) 
		{ 
			 
			*Status	=	NDIS_STATUS_FAILURE; 
 
		} 
	} 
	else 
	{ 
		if(pAdapt->BindingHandle	!=	NULL) 
		{ 
			 
			NdisResetEvent(&pAdapt->Event); 
 
			NdisCloseAdapter(Status, pAdapt->BindingHandle); 
 
			if(*Status	==	NDIS_STATUS_PENDING) 
			{ 
			 
				NdisWaitEvent(&pAdapt->Event, 0); 
 
				*Status	=	pAdapt->Status; 
 
			} 
			 pAdapt->BindingHandle = NULL; 
 
		} 
		else 
		{ 
			 
			*Status	=	NDIS_STATUS_FAILURE; 
 
		} 
 
		NdisFreeMemory(pAdapt,	0, 0); 
	} 
} 
 
 
 
VOID PPCloseAdapterComplete 
							( 
								IN NDIS_HANDLE ProtocolBindingContext, 
								IN NDIS_STATUS Status 
							) 
{ 
	 
	PADAPT	  pAdapt =(PADAPT)ProtocolBindingContext; 
 
	pAdapt->Status = Status; 
 
	NdisSetEvent(&pAdapt->Event); 
 
} 
 
 
VOID PPUnloadProtocol 
					( 
						VOID 
					) 
{ 
     
	NDIS_STATUS Status; 
 
    if (ProtocolHandle != NULL) 
    { 
 
        NdisDeregisterProtocol(&Status, ProtocolHandle); 
         
		ProtocolHandle = NULL; 
    } 
 
} 
 
VOID PPUnload 
			( 
				IN PDRIVER_OBJECT        DriverObject 
			) 
{ 
     
	PPUnloadProtocol(); 
 
    NdisIMDeregisterLayeredMiniport(DriverHandle); 
   
} 
 
/* 
	ProtocolResetComplete is a required driver function that  
	completes a protocol-initiated reset operation for which NdisReset  
	returned NDIS_STATUS_PENDING.  
 
*/ 
VOID PPResetComplete 
						( 
							IN NDIS_HANDLE ProtocolBindingContext, 
							IN NDIS_STATUS Status 
						) 
{ 
	 
} 
 
 
/* 
		Completes the processing of a protocol-initiated query or set for which  
		NdisRequest returned NDIS_STATUS_PENDING.  
 
*/ 
 
VOID PPRequestComplete 
						( 
							IN NDIS_HANDLE ProtocolBindingContext,  
							IN PNDIS_REQUEST NdisRequest,  
							IN NDIS_STATUS	Status 
						) 
{ 
	 
	PADAPT		pAdapt	=	(PADAPT)ProtocolBindingContext; 
 
	NDIS_OID	Oid		=	pAdapt->Request.DATA.SET_INFORMATION.Oid; 
 
	pAdapt->OutstandingRequests	=	FALSE; 
 
    // 
    //	Complete the Set or Query, and fill in the buffer for OID_PNP_CAPABILITIES,  
	//	if need be. 
    // 
	switch	(NdisRequest->RequestType) 
	{ 
	 
	case NdisRequestQueryInformation: 
        // 
        //	We never pass OID_PNP_QUERY_POWER down. 
        // 
        if ((Oid == OID_PNP_CAPABILITIES) && (Status == NDIS_STATUS_SUCCESS)) 
        { 
			// 
			//	Postprocess a request for OID_PNP_CAPABILITIES that was forwarded 
			//  down to the underlying miniport, and has been completed by it. 
			// 
			MPQueryPNPCapabilities(pAdapt, &Status); 
 
        } 
 
        *pAdapt->BytesReadOrWritten = NdisRequest->DATA.QUERY_INFORMATION.BytesWritten; 
        *pAdapt->BytesNeeded		= NdisRequest->DATA.QUERY_INFORMATION.BytesNeeded; 
 
        if ((Oid == OID_GEN_MAC_OPTIONS) && (Status == NDIS_STATUS_SUCCESS)) 
        { 
            // 
            //	Remove the no-loopback bit from mac-options. In essence we are 
            //	telling NDIS that we can handle loopback. We don't, but the 
            //	interface below us does. If we do not do this, then loopback 
            //	processing happens both below us and above us. This is wasteful 
            //	at best and if Netmon is running, it will see multiple copies 
            //	of loopback packets when sniffing above us. 
            // 
            //	Only the lowest miniport is a stack of layered miniports should 
            //	ever report this bit set to NDIS. 
            // 
			*(PULONG)NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer &= ~NDIS_MAC_OPTION_NO_LOOPBACK; 
        } 
 
		// 
		// 修改MTU值 
		// 将MTU改为1500-16 
		// 
 		if ((Oid == OID_GEN_MAXIMUM_FRAME_SIZE  
			|| Oid == OID_GEN_MAXIMUM_TOTAL_SIZE)  
			&& (Status == NDIS_STATUS_SUCCESS)) 
        {			 
			 *(PULONG)NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer = ETH_MAX_DATA_SIZE - 16; 
        } 
 
        // 
		//	Indicates that a prior call to MPQueryInformation, which returned  
		//	NDIS_STATUS_PENDING, has completed. 
		// 
		NdisMQueryInformationComplete(	pAdapt->MiniportHandle, 
										Status); 
        break; 
 
	case NdisRequestSetInformation: 
 
		*pAdapt->BytesReadOrWritten = NdisRequest->DATA.SET_INFORMATION.BytesRead; 
         
		*pAdapt->BytesNeeded		= NdisRequest->DATA.SET_INFORMATION.BytesNeeded; 
 
	    // 
		//	Indicates that a prior call to MPSetInformation, which returned  
		//	NDIS_STATUS_PENDING, has completed. 
		//		 
		NdisMSetInformationComplete(	pAdapt->MiniportHandle, 
										Status); 
        break; 
 
      default: 
 
        break; 
    } 
     
} 
 
/* 
	NDIS calls PPStatus when an underlying connectionless NIC driver  
	calls NdisMIndicateStatus to report a change in its hardware status. 
	 
	NDIS_STATUS_MEDIA_DISCONNECT:When a miniport senses that the network  
	connection has been lost, it should call NdisMIndicateStatus  
	with NDIS_STATUS_MEDIA_DISCONNECT.  
	 
	NDIS_STATUS_MEDIA_CONNECT:When the connection is restored, the miniport 
	should call NdisMIndicateStatus with NDIS_STATUS_MEDIA_CONNECT. 
 
	Intermediate Driver indicates status up to the higher level driver(s)  
	by calling NdisMIndicateStatus only if the status indication causes  
	the intermediate driver to change its internal state in a way that  
	affects the operation of its MPXxx functions. 
 
 
*/ 
 
VOID PPStatus 
				( 
					IN NDIS_HANDLE ProtocolBindingContext, 
					IN NDIS_STATUS GeneralStatus, 
					IN PVOID StatusBuffer, 
					IN UINT StatusBufferSize 
				) 
{ 
    PADAPT      pAdapt = (PADAPT)ProtocolBindingContext; 
 
    // 
    //	Pass up this indication only if the upper edge miniport is initialized 
    //	and powered on. Also ignore indications that might be sent by the lower 
    //	miniport when it isn't at D0. 
    // 
    if	((  pAdapt->MiniportHandle  !=  NULL  )					  && 
         (  pAdapt->MiniportDeviceState	==	NdisDeviceStateD0  )  && 
         (  pAdapt->ProtocolDeviceState	==	NdisDeviceStateD0  ) 
		)     
    { 
        if	((  GeneralStatus == NDIS_STATUS_MEDIA_CONNECT  )		  || 
			 (  GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT  )  
			) 
        { 
             
            pAdapt->LastIndicatedStatus = GeneralStatus; 
         
		} 
         
		// 
		//	Indicates changes in the status to higher-level NDIS drivers. 
		// 
		NdisMIndicateStatus(	pAdapt->MiniportHandle, 
								GeneralStatus, 
								StatusBuffer, 
								StatusBufferSize); 
    } 
    else 
    { 
        if	((  pAdapt->MiniportHandle  !=  NULL  ) &&  
			 ((  GeneralStatus  ==  NDIS_STATUS_MEDIA_CONNECT  )||  
              (  GeneralStatus  ==  NDIS_STATUS_MEDIA_DISCONNECT  ) 
			 ) 
			) 
        { 
             
			pAdapt->LatestUnIndicateStatus = GeneralStatus; 
         
		} 
    } 
     
} 
 
 
/* 
	Completes a status-change operation initiated when the underlying driver  
	called NdisMIndicateStatus. 
 
	The underlying miniport's call to NdisMIndicateStatusComplete causes NDIS 
	to call the PPStatusComplete function. 
*/ 
 
 
 
VOID PPStatusComplete 
				( 
					IN NDIS_HANDLE ProtocolBindingContext 
				) 
{ 
	PADAPT	  pAdapt =(PADAPT)ProtocolBindingContext; 
 
	if	(	pAdapt->MiniportHandle	!=	NULL				&& 
			pAdapt->MiniportDeviceState	==	NdisDeviceStateD0	&& 
			pAdapt->ProtocolDeviceState	==	NdisDeviceStateD0  
		)	 
	{ 
		  NdisMIndicateStatusComplete(pAdapt->MiniportHandle); 
	} 
} 
 
 
 
 
/* 
	PPSendComplete completes the operation of previous NdisSend or  
	NdisSendPackets , which returns NDIS_STATUS_PENDING 
 
	Completion of a send operation usually implies that the underlying  
	NIC driver actually has transmitted the given packet over the network. 
 
	The underlying driver's call to NdisMSendComplete or NdisMWanSendComplete  
	causes NDIS to call the PPSendComplete function.  
 
	Consequently, PPSendComplete can either release these resources or  
	prepare them for reuse in a subsequent call to NdisSendPackets or NdisSend. 
 
 
*/ 
VOID PPSendComplete 
					( 
						IN NDIS_HANDLE ProtocolBindingContext,  
						IN PNDIS_PACKET Packet,  
						IN NDIS_STATUS Status 
					) 
{ 
 
	PADAPT            pAdapt = (PADAPT)ProtocolBindingContext; 
     
	PNDIS_PACKET      Pkt;  
    NDIS_HANDLE       PoolHandle; 
 
    PSEND_RSVD        SendRsvd; 
	 
	SendRsvd	=	(PSEND_RSVD)(Packet->ProtocolReserved); 
	 
	Pkt			=	SendRsvd->OriginalPkt; 
	 
	// 
	//	Copies per-packet information from a packet returned up by a  
	//  lower miniport driver into a new packet that is to be returned  
	//	to an upper-level driver.  
	//	Per-packet information is contained in an NDIS_PACKET_EXTENSION structure  
	// 
	//NdisIMCopySendCompletePerPacketInfo (Pkt, Packet); 
	 
 
	// 
	// If New NDIS_BUFFER used 
	// 
	if (TRUE  ==  SendRsvd->bNewBuffer)                
	{     
		PNDIS_BUFFER	NewNdisBfr; 
		 
		PUCHAR			pVA; 
		UINT			VA_len; 
		 
		pIPHdr			pIPH; 
		pAHHdr			pAH; 
		 
		USHORT			IPHdrl; 
 
 
		// 
		//	Returns information about a given packet.  
		// 
		NdisQueryPacket(	Packet,                     
							NULL, 
							NULL, 
							&NewNdisBfr,            
							NULL 
							); 
 
		// 
		//	Retrieves the size of the range, and optionally  
		//	its base virtual address, from a buffer descriptor. 
		// 
		NdisQueryBufferSafe(	NewNdisBfr,            
								&pVA,                  // Payload address. 
								&VA_len,               // Payload length. 
								LowPagePriority 
								); 
 
		// 
		//	Returns the given buffer descriptor to the free list.  
		// 
		NdisFreeBuffer(NewNdisBfr);          
 
/*		pIPH = (pIPHdr)(pVA + sizeof(EthHdr)); 
 
		IPHdrl = pIPH->IPHdrLen * 4; 
 
		pAH = (pAHHdr)(pVA + sizeof(EthHdr)+ IPHdrl); 
 
		VA_len = pAH->NewVa;		*/ 
		// 
		//	Recycle memory used for payload. 
		// 
		NdisFreeMemory(	pVA,		//	VirtualAddress                   
                        VA_len,		//	Length 
						0			//	MemoryFlags:0 - Non-Paged Pool 
						); 
	}     
 
	// 
	//	Releases a driver-allocated packet descriptor and returns it to  
	//	the free list when the caller is running at IRQL DISPATCH_LEVEL.  
	// 
	NdisDprFreePacket(Packet); 
 
    // 
	//	Returns the packet and final status of a completed send request  
	//	for which the driver previously returned NDIS_STATUS_PENDING.  
	// 
	NdisMSendComplete(	pAdapt->MiniportHandle, 
						Pkt, 
						Status); 
	// 
    //	Decrease the outstanding send count 
    // 
    ADAPT_DECR_PENDING_SENDS(pAdapt); 
}        
 
 
/* 
	PPTransferDataComplete is a required driver function if the driver might  
	bind itself to an underlying connectionless NIC driver that does not  
	indicate full-packet receives with NdisMIndicateReceivePacket.  
 
	Completes the processing of a protocol-initiated transfer-data request  
	for which NdisTransferData returned NDIS_STATUS_PENDING.  
 
*/ 
 
VOID PPTransferDataComplete 
							( 
								IN NDIS_HANDLE  ProtocolBindingContext, 
								IN PNDIS_PACKET	Packet, 
								IN NDIS_STATUS	Status, 
								IN UINT			BytesTransferred 
							) 
{ 
 
	PADAPT	  pAdapt	=	(PADAPT)ProtocolBindingContext; 
 
	if(pAdapt->MiniportHandle) 
	{ 
 
		// 
		//	Whenever MPTransferData returns NDIS_STATUS_PENDING for a  
		//	given packet, the driver must call NdisMTransferDataComplete  
		//	when it has finished copying the requested data into that  
		//	packet or must fail the request.  
		// 
		NdisMTransferDataComplete(	pAdapt->MiniportHandle,  
									Packet,  
									Status,  
									BytesTransferred); 
 
	} 
} 
 
 
/* 
	Copies the indicate packet from the underlying Miniport 
 
*/ 
NDIS_STATUS PPReceive 
				( 
					IN NDIS_HANDLE ProtocolBindingContext,  
					IN NDIS_HANDLE MacReceiveContext,  
					IN PVOID HeaderBuffer, 
					IN UINT HeaderBufferSize,  
					IN PVOID LookAheadBuffer,  
					IN UINT LookAheadBufferSize,  
					IN UINT PacketSize 
				) 
{ 
    PADAPT            pAdapt = (PADAPT)ProtocolBindingContext; 
    PNDIS_PACKET      MyPacket, Packet; 
    NDIS_STATUS       Status = NDIS_STATUS_SUCCESS; 
	BOOLEAN			  bDecision;	 
 
    if ((!pAdapt->MiniportHandle) || (pAdapt->MiniportDeviceState > NdisDeviceStateD0)) 
    { 
        Status = NDIS_STATUS_FAILURE; 
    } 
    else do 
    { 
        // 
        // Get at the packet, if any, indicated up by the miniport below. 
        // 
        Packet = NdisGetReceivedPacket(pAdapt->BindingHandle, MacReceiveContext); 
        if (Packet != NULL) 
        { 
            // 
            // The miniport below did indicate up a packet. Use information 
            // from that packet to construct a new packet to indicate up. 
            // 
                // 
                // Make our packet point to data from the original 
                // packet. NOTE: this works only because we are 
                // indicating a receive directly from the context of 
                // our receive indication. If we need to queue this 
                // packet and indicate it from another thread context, 
                // we will also have to allocate a new buffer and copy 
                // over the packet contents, OOB data and per-packet 
                // information. This is because the packet data 
                // is available only for the duration of this 
                // receive indication call. 
                // 
	            // 
	            // Get a packet off the pool and indicate that up 
		        // 
			    NdisDprAllocatePacket(&Status, 
                                &MyPacket, 
                                pAdapt->RecvPacketPoolHandle); 
 
 
				if (Status == NDIS_STATUS_SUCCESS) 
				{ 
					PRECV_RSVD            RecvRsvd; 
					PNDIS_BUFFER          NewNdisBfr; 
 
					RecvRsvd = (PRECV_RSVD)(MyPacket->MiniportReserved); 
					RecvRsvd->OriginalPkt = Packet; 
					 
					MyPacket->Private.Head = Packet->Private.Head; 
					MyPacket->Private.Tail = Packet->Private.Tail; 
 
					// 
					// Get the original packet (it could be the same packet as the 
					// one received or a different one based on the number of layered 
					// miniports below) and set it on the indicated packet so the OOB 
					// data is visible correctly at protocols above. 
					// 
					NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet)); 
					NDIS_SET_PACKET_HEADER_SIZE(MyPacket, HeaderBufferSize); 
 
					// 
					// Copy packet flags. 
					// 
					NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet); 
 
					// 
					// Force protocols above to make a copy if they want to hang 
					// on to data in this packet. This is because we are in our 
					// Receive handler (not ReceivePacket) and we can't return a 
					// ref count from here. 
					// 
					NDIS_SET_PACKET_STATUS(MyPacket, NDIS_STATUS_RESOURCES); 
 
					// 
					// By setting NDIS_STATUS_RESOURCES, we also know that we can reclaim 
					// this packet as soon as the call to NdisMIndicateReceivePacket 
					// returns. 
					// 
 
					NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1); 
 
					// 
					// Reclaim the indicated packet. Since we had set its status 
					// to NDIS_STATUS_RESOURCES, we are guaranteed that protocols 
					// above are done with it. 
					// 
					NdisDprFreePacket(MyPacket); 
	 
		            break; 
			    } 
 
		} 
		else 
		{ 
 
			// 
			// The miniport below us uses the old-style (not packet) 
            // receive indication. Fall through. 
            // 
        } 
 
        // 
        // Fall through if the miniport below us has either not 
        // indicated a packet or we could not allocate one 
        // 
        pAdapt->IndicateRcvComplete = TRUE; 
        switch (pAdapt->Medium) 
        { 
            case NdisMedium802_3: 
            case NdisMediumWan: 
                NdisMEthIndicateReceive(pAdapt->MiniportHandle, 
                                             MacReceiveContext, 
                                             HeaderBuffer, 
                                             HeaderBufferSize, 
                                             LookAheadBuffer, 
                                             LookAheadBufferSize, 
                                             PacketSize); 
                break; 
 
            default: 
                break; 
        } 
 
    } while(FALSE); 
 
    return Status; 
} 
	 
 
 
//Completes postprocessing of one or more preceding receive indications from a NIC driver.  
 
VOID PPReceiveComplete 
						( 
							IN NDIS_HANDLE ProtocolBindingContext 
						) 
{ 
    PADAPT        pAdapt =(PADAPT)ProtocolBindingContext; 
 
	if (((pAdapt->MiniportHandle != NULL) 
                && (pAdapt->MiniportDeviceState > NdisDeviceStateD0)) 
                && (pAdapt->IndicateRcvComplete)) 
    { 
        switch (pAdapt->Medium) 
        { 
            case NdisMedium802_3: 
            case NdisMediumWan: 
                NdisMEthIndicateReceiveComplete(pAdapt->MiniportHandle); 
                break; 
 
            default: 
                break; 
        } 
    } 
 
    pAdapt->IndicateRcvComplete = FALSE; 
} 
 
 
INT			PPReceivePacket 
							( 
								IN NDIS_HANDLE  ProtocolBindingContext, 
								IN PNDIS_PACKET  Packet 
							) 
{ 
    PADAPT              pAdapt =(PADAPT)ProtocolBindingContext; 
 
    NDIS_STATUS         Status; 
    PNDIS_PACKET        MyPacket; 
 
	BOOLEAN				bDecision; 
 
 
	BOOLEAN             Remaining; 
 
	ULONG				MACAddrComp; 
	ULONG				EncPayloadl; 
	ULONG				UnEncPayloadl; 
 
	#define				szPayloadCopy	ETH_MAX_PACKET_SIZE 
	char				PayloadCopy[szPayloadCopy]; 
 
	pEthHdr             pETH; 
 
	PUCHAR				pUnencPayload	=	NULL; 
 
	UCHAR	digest[16]; 
 
	int i; 
 
	DbgPrint("PPRecievePacket"); 
 
 
	GetPktPayload(	Packet, 
					PayloadCopy, 
					szPayloadCopy, 
					&EncPayloadl 
					); 
    pETH = (pEthHdr)PayloadCopy;                      // Point to ethernet header. 
 
    ETH_COMPARE_NETWORK_ADDRESSES_EQ(pETH->SrcMAC, pAdapt->MACAddress, &MACAddrComp); 
 
    if (0==MACAddrComp)                               // A loopback? 
      { 
       return 0;                                      // Drop packet. 
      } 
 
	//---------------------------------------- 
            Status = FilterPacket(                    // See if packet to be dropped. 
                                  pAdapt, 
                                  Packet, 
                                  NULL, 
                                  FALSE,               // Send action. 
                                  &bDecision 
                                 ); 
 
            if ( 
                NDIS_STATUS_SUCCESS==Status           // Everything OK? 
                  && 
                TRUE!=bDecision                       // Packet to be dropped? 
               ) 
              { 
					goto Done; 
					 
              } 
 
	//------------------------------------------ 
			{ 
				PUCHAR	pHmacPayload = NULL; 
 
				pIPHdr	pHmacIPH; 
 
				pAHHdr  pHmacAH; 
 
				ULONG	IPHdrl; 
 
				ULONG	NewPayloadl = EncPayloadl; 
 
				UCHAR	key[]="Prakash"; 
 
				NdisZeroMemory(digest,16); 
 
				Status  = NdisAllocateMemoryWithTag(&pHmacPayload, 
											EncPayloadl - sizeof(EthHdr), 
											TAG 
											); 
				if( NDIS_STATUS_SUCCESS != Status ) 
				{ 
					goto Done; 
				} 
 
				NdisMoveMemory(	pHmacPayload, 
								PayloadCopy + sizeof(EthHdr), 
								NewPayloadl - sizeof(EthHdr) 
								); 
 
				pHmacIPH = (pIPHdr)pHmacPayload; 
 
				IPHdrl = pHmacIPH->IPHdrLen * 4; 
 
				pHmacIPH->TTL = 0; 
				pHmacIPH->FragmentationSummary = 0; 
				pHmacIPH->Checksum = 0; 
 
				pHmacAH = (pAHHdr)(pHmacPayload + IPHdrl); 
 
				NdisZeroMemory(pHmacAH->Auth, 16); 
 
				hmac_md5(pHmacPayload,NewPayloadl - sizeof(EthHdr),key,7,digest); 
 
				DbgPrint("Digest Gen Recv"); 
 
				for(i = 0; i<16 ;i++) 
				{ 
 
					DbgPrint("%x",digest[i]); 
				} 
 
			} 
 
//PPBuildOrigPkt 
	{ 
		pIPHdr		pIPH; 
 
		pAHHdr		pAH; 
		 
		USHORT		IPHdrl; 
 
		ULONG		NewPayloadl; 
 
		UCHAR       digest1[16]; 
 
		int i; 
 
		pIPH = (pIPHdr)(PayloadCopy + sizeof(EthHdr)); 
 
		IPHdrl = pIPH->IPHdrLen * 4; 
		 
		NewPayloadl = EncPayloadl - sizeof(AHHdr); 
 
		pAH = (pAHHdr)(PayloadCopy + sizeof(EthHdr) + IPHdrl); 
 
		//NewPayloadl = EncPayloadl; 
 
		NdisMoveMemory(digest1, pAH->Auth, 16); 
 
 
 
		DbgPrint("Digest Recv"); 
 
		for(i = 0; i<16 ;i++) 
		{ 
 
			DbgPrint("%x",pAH->Auth[i]); 
		} 
 
 
		for(i=0;i<16;i++) 
		{ 
			if(pAH->Auth[i] != digest[i]) 
			{ 
				DbgPrint("Dropped"); 
 
				Status = NDIS_GET_PACKET_STATUS(Packet); 
		 
				//return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);		 
				 
				  return 0; 
 
			} 
		 
		} 
 
 
	 
		Status = NdisAllocateMemoryWithTag(	&pUnencPayload, 
											NewPayloadl, 
											TAG 
										); 
 
		if (NDIS_STATUS_SUCCESS != Status) 
		{ 
		   goto Done; 
		} 
 
		UnEncPayloadl = NewPayloadl; 
 
		NdisMoveMemory( pUnencPayload, 
						PayloadCopy, 
						sizeof(EthHdr) + IPHdrl 
					  ); 
 
		NdisMoveMemory( pUnencPayload + sizeof(EthHdr) + IPHdrl, 
						PayloadCopy	  + sizeof(EthHdr) + IPHdrl + sizeof(AHHdr), 
						EncPayloadl   - sizeof(EthHdr) - IPHdrl - sizeof(AHHdr) 
					  ); 
 
		pIPH = (pIPHdr)(pUnencPayload + sizeof(EthHdr)); 
 
		pIPH->TotalLength = (USHORT)RtlUlongByteSwap((NewPayloadl - sizeof(EthHdr))<<16); 
 
		pIPH->Checksum = 0;                                  
 
		pIPH->Checksum = GetIPChecksum((PUSHORT)pIPH, IPHdrl); 
 
 
 
	} 
 
Done: 
 
    if ((!pAdapt->MiniportHandle) || (pAdapt->MiniportDeviceState > NdisDeviceStateD0)) 
    { 
          return 0; 
    } 
 
 
    // 
    // Get a packet off the pool and indicate that up 
    // 
    NdisDprAllocatePacket(&Status, 
                           &MyPacket, 
                           pAdapt->RecvPacketPoolHandle); 
 
    if (Status == NDIS_STATUS_SUCCESS) 
    { 
        PRECV_RSVD            RecvRsvd; 
        PNDIS_BUFFER          NewNdisBfr; 
 
        RecvRsvd = (PRECV_RSVD)(MyPacket->MiniportReserved); 
        RecvRsvd->OriginalPkt = Packet; 
 
        // 
		//	Have unencapsulated payload? 
		// 
		if(  NULL != pUnencPayload  ) 
		{ 
           NdisAllocateBuffer(	&Status,                // Build new buffer descriptor. 
								&NewNdisBfr, 
								pAdapt->hRecvBufferPool, 
								pUnencPayload, 
								UnEncPayloadl 
								); 
 
           if(  NDIS_STATUS_SUCCESS  !=  Status  ) 
		   { 
			    
              return 0; 
           } 
 
           RecvRsvd->bNewBuffer = TRUE;               // Show new NDIS_BUFFER used. 
 
           NdisChainBufferAtFront(	MyPacket,          // Chain new buffer descriptor to new packet. 
									NewNdisBfr 
									); 
		}                                           // End 'if' found desired packet. 
        else 
		{ 
			RecvRsvd->bNewBuffer = FALSE;              // Original NDIS_BUFFER used. 
 
			MyPacket->Private.Head = Packet->Private.Head; 
			MyPacket->Private.Tail = Packet->Private.Tail; 
		} 
 
		// 
        // Get the original packet (it could be the same packet as the one 
        // received or a different one based on the number of layered miniports 
        // below) and set it on the indicated packet so the OOB data is visible 
        // correctly to protocols above us. 
        // 
        NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet)); 
 
        // 
        // Set Packet Flags 
        // 
        NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet); 
 
        Status = NDIS_GET_PACKET_STATUS(Packet); 
 
        NDIS_SET_PACKET_STATUS(MyPacket, Status); 
        NDIS_SET_PACKET_HEADER_SIZE(MyPacket, NDIS_GET_PACKET_HEADER_SIZE(Packet)); 
 
        NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1); 
 
        // 
        // Check if we had indicated up the packet with NDIS_STATUS_RESOURCES 
        // NOTE -- do not use NDIS_GET_PACKET_STATUS(MyPacket) for this since 
        // it might have changed! Use the value saved in the local variable. 
        // 
        if (Status == NDIS_STATUS_RESOURCES) 
        { 
        
			// 
			//	Have unencapsulated payload?             
			// 
			if(  NULL !=  pUnencPayload  ) 
			{ 
				 
				NdisFreeBuffer(NewNdisBfr);           // Recycle buffer descriptor.                               
 
				NdisFreeMemory(	pUnencPayload,         // Recycle memory. 
								UnEncPayloadl, 
								0 
								); 
 
				pUnencPayload = NULL; 
			} 
 
			// 
            // Our ReturnPackets handler will not be called for this packet. 
            // We should reclaim it right here. 
            // 
            NdisDprFreePacket(MyPacket); 
        } 
 
        return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0); 
    } 
    else 
    { 
 
        if (  NULL  !=  pUnencPayload)                // Have unencapsulated payload? 
		{ 
          NdisFreeMemory( pUnencPayload,              // Recycle memory. 
                          UnEncPayloadl, 
                          0 
						  ); 
		} 
 
 
		// 
        // We are out of packets. Silently drop it. 
        // 
        return(0); 
    } 
} 
 
 
/* 
	Plug and Play event or a Power Management event is handled 
*/ 
 
NDIS_STATUS PPPnpEvent 
						( 
							IN NDIS_HANDLE		ProtocolBindingContext,  
							IN PNET_PNP_EVENT	pNetPnPEvent 
						) 
{ 
    PADAPT            pAdapt  =(PADAPT)ProtocolBindingContext; 
    NDIS_STATUS       Status  = NDIS_STATUS_SUCCESS; 
 
 
    switch (pNetPnPEvent->NetEvent) 
    { 
        case NetEventSetPower: 
			Status = PtPnPNetEventSetPower(pAdapt, pNetPnPEvent); 
            break; 
 
         case NetEventReconfigure: 
            Status = PtPnPNetEventReconfigure(pAdapt, pNetPnPEvent); 
            break; 
 
         default: 
			Status = NDIS_STATUS_SUCCESS; 
			break; 
	} 
    return Status; 
} 
 
NDIS_STATUS	PtPnPNetEventReconfigure 
							( 
								IN PADAPT            pAdapt, 
								IN PNET_PNP_EVENT    pNetPnPEvent 
							) 
{ 
    NDIS_STATUS    ReconfigStatus = NDIS_STATUS_SUCCESS; 
    NDIS_STATUS    ReturnStatus = NDIS_STATUS_SUCCESS; 
 
    do 
    { 
        // 
        // Is this is a global reconfiguration notification ? 
        // 
        if (pAdapt == NULL) 
        { 
            // 
            // An important event that causes this notification to us is if 
            // one of our upper-edge miniport instances was enabled after being 
            // disabled earlier, e.g. from Device Manager in Win2000. Note that 
            // NDIS calls this because we had set up an association between our 
            // miniport and protocol entities by calling NdisIMAssociateMiniport. 
            // 
            // Since we would have torn down the lower binding for that miniport, 
            // we need NDIS' assistance to re-bind to the lower miniport. The 
            // call to NdisReEnumerateProtocolBindings does exactly that. 
            // 
            NdisReEnumerateProtocolBindings (ProtocolHandle);         
            break; 
        } 
 
        ReconfigStatus = NDIS_STATUS_SUCCESS; 
 
    } while(FALSE); 
 
    return ReconfigStatus; 
} 
	 
NDIS_STATUS	PtPnPNetEventSetPower 
							( 
								IN PADAPT            pAdapt, 
								IN PNET_PNP_EVENT    pNetPnPEvent 
							) 
{ 
    PNDIS_DEVICE_POWER_STATE       pDeviceState  =(PNDIS_DEVICE_POWER_STATE)(pNetPnPEvent->Buffer); 
    NDIS_DEVICE_POWER_STATE        PrevDeviceState = pAdapt->ProtocolDeviceState;   
    NDIS_STATUS                    Status; 
    NDIS_STATUS                    ReturnStatus; 
 
    ReturnStatus = NDIS_STATUS_SUCCESS; 
 
    // 
    // Set the Internal Device State, this blocks all new sends or receives 
    // 
    NdisAcquireSpinLock(&pAdapt->Lock); 
    pAdapt->ProtocolDeviceState = *pDeviceState; 
 
    // 
    // Check if the miniport below is going to a low power state. 
    // 
    if (pAdapt->ProtocolDeviceState > NdisDeviceStateD0) 
    { 
        // 
        // If the miniport below is going to standby, fail all incoming requests 
        // 
        if (PrevDeviceState == NdisDeviceStateD0) 
        { 
            pAdapt->StandingBy = TRUE; 
        } 
 
        NdisReleaseSpinLock(&pAdapt->Lock); 
 
        // 
        // Wait for outstanding sends and requests to complete. 
        // 
        while (pAdapt->OutstandingSends != 0) 
        { 
            NdisMSleep(2); 
        } 
 
        while (pAdapt->OutstandingRequests == TRUE) 
        { 
            // 
            // sleep till outstanding requests complete 
            // 
            NdisMSleep(2); 
        } 
 
        // 
        // If the below miniport is going to low power state, complete the queued request 
        // 
        NdisAcquireSpinLock(&pAdapt->Lock); 
        if (pAdapt->QueuedRequest) 
        { 
            pAdapt->QueuedRequest = FALSE; 
            NdisReleaseSpinLock(&pAdapt->Lock); 
            PPRequestComplete(pAdapt, &pAdapt->Request, NDIS_STATUS_FAILURE); 
        } 
        else 
        { 
            NdisReleaseSpinLock(&pAdapt->Lock); 
        } 
             
    } 
    else 
    { 
        // 
        // If the physical miniport is powering up (from Low power state to D0),  
        // clear the flag 
        // 
        if (PrevDeviceState > NdisDeviceStateD0) 
        { 
            pAdapt->StandingBy = FALSE; 
        } 
        // 
        // The device below is being turned on. If we had a request 
        // pending, send it down now. 
        // 
        if (pAdapt->QueuedRequest == TRUE) 
        { 
            pAdapt->QueuedRequest = FALSE; 
         
            pAdapt->OutstandingRequests = TRUE; 
            NdisReleaseSpinLock(&pAdapt->Lock); 
 
            NdisRequest(&Status, 
                        pAdapt->BindingHandle, 
                        &pAdapt->Request); 
 
            if (Status != NDIS_STATUS_PENDING) 
            { 
                PPRequestComplete(pAdapt, 
                                  &pAdapt->Request, 
                                  Status); 
                 
            } 
        } 
        else 
        { 
            NdisReleaseSpinLock(&pAdapt->Lock); 
        } 
    } 
 
    return ReturnStatus; 
}