Ejemplo n.º 1
0
/************************************************************
Function used by NDIS to update the VXD when a new MAC driver
is added
************************************************************/
VOID NDIS_API PacketBindAdapter( OUT PNDIS_STATUS Status,
                                 IN  NDIS_HANDLE  BindAdapterContext,
                                 IN  PNDIS_STRING AdapterName,
                                 IN  PVOID        SystemSpecific1,
                                 IN  PVOID        SystemSpecific2 )
{
    PDEVICE_EXTENSION	pde;
    POPEN_INSTANCE		oiNew;
    NDIS_STATUS			nsErrorStatus, nsOpenStatus;
    UINT           		uiMedium;
    UINT           		i;
    PWRAPPER_PROTOCOL_BLOCK				pWPBlock;
    PNDIS_PROTOCOL_CHARACTERISTICS	pNPChar;
    PADAPTER_NAME		AName;
    PWRAPPER_MAC_BLOCK	pWMBlock;
    PNDIS_MAC_CHARACTERISTICS	  pNMChar;
    BYTE                *lpzName;


    TRACE_ENTER( "BindAdapter" );
    pde = GlobalDeviceExtension;
    /*Allocate an element that describe an adapter*/
    NdisAllocateMemory( (PVOID *)&AName, sizeof(ADAPTER_NAME), 0, -1 );
    if ( AName == NULL )
    {
        *Status = NDIS_STATUS_RESOURCES;
        return;
    }

    NdisAllocateMemory( (PVOID *)&oiNew, sizeof( OPEN_INSTANCE ), 0, -1 );
    if ( oiNew == NULL )
    {
        *Status = NDIS_STATUS_RESOURCES;
        return;
    }
    NdisZeroMemory( (PVOID)oiNew, sizeof( OPEN_INSTANCE ) );

    /*Save Binding Context*/
    oiNew->BindAdapterContext = BindAdapterContext;

    /*Save the device handle*/

    oiNew->hDevice = (DWORD) SystemSpecific1;

    /*allocate a pool for the packet headers*/

    NdisAllocatePacketPool( &nsErrorStatus,
                            &(oiNew->PacketPool),
                            TRANSMIT_PACKETS,
                            sizeof(PACKET_RESERVED) );

    IF_TRACE_MSG( "PACKET_RESERVED_b :%lx",sizeof(PACKET_RESERVED));
    if ( nsErrorStatus != NDIS_STATUS_SUCCESS )
    {
        IF_TRACE_MSG( "Failed to allocate packet pool AllocStatus=%x", nsErrorStatus );
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        *Status = NDIS_STATUS_RESOURCES;
        TRACE_LEAVE( "BindAdapter" );
        return;
    }


    /*allocate a pool for the packet data*/

    NdisAllocateBufferPool( &nsErrorStatus,
                            &(oiNew->BufferPool),
                            TRANSMIT_PACKETS );
    if ( nsErrorStatus != NDIS_STATUS_SUCCESS )
    {
        IF_TRACE_MSG( "Failed to allocate packet pool AllocStatus=%x", nsErrorStatus );
        NdisFreePacketPool( oiNew->PacketPool );
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        *Status = NDIS_STATUS_RESOURCES;
        TRACE_LEAVE( "BindAdapter" );
        return;
    }
    NdisAllocateSpinLock( &(oiNew->ResetSpinLock) );
    InitializeListHead( &(oiNew->ResetIrpList) );
    NdisAllocateSpinLock( &(oiNew->RcvQSpinLock) );
    InitializeListHead( &(oiNew->RcvList) );
    NdisAllocateSpinLock( &(oiNew->RequestSpinLock) );
    InitializeListHead( &(oiNew->RequestList) );

    for ( i=0; i<MAX_REQUESTS; i++ )
    {
        InsertTailList( &(oiNew->RequestList), &(oiNew->Requests[i].Reserved.ListElement) );
    }
    oiNew->Status = NDIS_STATUS_PENDING;
    oiNew->BindAdapterContext = BindAdapterContext;

    /*open the MAC driver calling NDIS*/

    oiNew->hDevice=0;
    oiNew->tagProcess=0;

    NdisOpenAdapter( &nsOpenStatus,
                     &nsErrorStatus,
                     &oiNew->AdapterHandle,
                     &uiMedium,
                     MediumArray,
                     NUM_NDIS_MEDIA,
                     pde->NdisProtocolHandle,
                     oiNew,
                     AdapterName,
                     0,
                     NULL );
    IF_TRACE_MSG( "Open Status                   : %lx", nsOpenStatus );
    IF_TRACE_MSG( "Error Status                  : %lx", nsErrorStatus );
    IF_TRACE_MSG( "Completion Status             : %lx", oiNew->Status );
    if ( nsOpenStatus == NDIS_STATUS_PENDING )
    {
        while ( oiNew->Status == NDIS_STATUS_PENDING )
            YieldExecution();
    }
    else
    {
        PacketOpenAdapterComplete( oiNew, nsOpenStatus, nsErrorStatus );
    }

    pWPBlock = ((PWRAPPER_OPEN_BLOCK)(oiNew->AdapterHandle))->ProtocolHandle;
    pNPChar  = &pWPBlock->ProtocolCharacteristics;
    IF_TRACE_MSG( "Protocol                      : %s",  pNPChar->Name.Buffer );
    IF_TRACE_MSG( "Protocol Handle               : %lx", pde->NdisProtocolHandle );
    IF_TRACE_MSG( "PWRAPPER_OPEN_BLOCK           : %lx", oiNew->AdapterHandle );
    IF_TRACE_MSG( "PWRAPPER_PROTOCOL_BLOCK       : %lx", pWPBlock );
    IF_TRACE_MSG( "NDIS_PROTOCOL_CHARACTERISTICS : %lx", pNPChar );
    IF_TRACE_MSG( "Name                          : %lx", &pNPChar->Name );
    IF_TRACE_MSG( "Adapter Name                  : %s",  AdapterName->Buffer );
    *Status = oiNew->Status;

    if ( *Status != NDIS_STATUS_SUCCESS )
    {
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        IF_TRACE( "Bind Operation FAILED!" );
    }
    else
    {
        AName->realnamestr.Length=AdapterName->Length;
        AName->realnamestr.MaximumLength=AdapterName->MaximumLength;
        AName->realnamestr.Buffer=AName->realname;
        for(i=0; i<32; i++)AName->realname[i]=AdapterName->Buffer[i];

        pWMBlock = ((PWRAPPER_OPEN_BLOCK)(oiNew->AdapterHandle))->MacHandle;
        pNMChar  = &pWMBlock->MacCharacteristics;
        lpzName  = pNMChar->Name.Buffer;
        for(i=0; i<32; i++)AName->devicename[i]=lpzName[i];
        InsertTailList( &GlobalDeviceExtension->AdapterNames, &AName->ListElement);

        //close the adapter
        NdisCloseAdapter(&nsErrorStatus,oiNew->AdapterHandle);

        if ( nsErrorStatus == NDIS_STATUS_PENDING )
        {
            while ( oiNew->Status == NDIS_STATUS_PENDING )
                YieldExecution();
        }
        else
        {
            PacketUnbindAdapterComplete( oiNew, nsErrorStatus );
        }
        *Status = oiNew->Status;
        if ( *Status == NDIS_STATUS_SUCCESS )
        {
            //remove this adapter from the list of open adapters
            RemoveEntryList(&(oiNew->ListElement));
            //free the memory
            NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        }
        else
        {
            IF_TRACE( "Close Operation FAILED!" );
        }

    }

    TRACE_LEAVE( "BindAdapter" );
    return;
}
Ejemplo n.º 2
0
DWORD PacketOpen(PNDIS_STRING AdapterName,DWORD dwDDB,DWORD hDevice,PDIOCPARAMETERS pDiocParms)
{

    LARGE_INTEGER		SystemTime;
    __int64				ltime1;
    PDEVICE_EXTENSION	pde;
    POPEN_INSTANCE 		oiNew;
    NDIS_STATUS			nsErrorStatus, nsOpenStatus;
    UINT           	i;
    UINT           	uiMedium;
    NDIS_STRING		NameStr;
    NDIS_STATUS	Status;


    pde = GlobalDeviceExtension;
    /*Allocate an element that describe an adapter*/
    NdisAllocateMemory( (PVOID *)&oiNew, sizeof( OPEN_INSTANCE ), 0, -1 );
    if ( oiNew == NULL )
    {
        return NDIS_STATUS_FAILURE;
    }
    NdisZeroMemory( (PVOID)oiNew, sizeof( OPEN_INSTANCE ) );
    /*allocate a pool for the packet headers*/

    NdisAllocatePacketPool( &nsErrorStatus,
                            &(oiNew->PacketPool),
                            TRANSMIT_PACKETS,
                            sizeof(PACKET_RESERVED) );

    IF_TRACE_MSG( "PACKET_RESERVED_a :%lx",sizeof(PACKET_RESERVED));
    if ( nsErrorStatus != NDIS_STATUS_SUCCESS )
    {
        IF_TRACE_MSG( "Failed to allocate packet pool AllocStatus=%x", nsErrorStatus );
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        TRACE_LEAVE( "BindAdapter" );
        return NDIS_STATUS_FAILURE;
    }


    /*allocate a buffer pool for the packet data*/
    NdisAllocateBufferPool( &nsErrorStatus,
                            &(oiNew->BufferPool),
                            TRANSMIT_PACKETS );
    if ( nsErrorStatus != NDIS_STATUS_SUCCESS )
    {
        IF_TRACE_MSG( "Failed to allocate packet pool AllocStatus=%x", nsErrorStatus );
        NdisFreePacketPool( oiNew->PacketPool );
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        TRACE_LEAVE( "BindAdapter" );
        return NDIS_STATUS_FAILURE;
    }
    NdisAllocateSpinLock( &(oiNew->ResetSpinLock) );
    InitializeListHead( &(oiNew->ResetIrpList) );
    NdisAllocateSpinLock( &(oiNew->RcvQSpinLock) );
    InitializeListHead( &(oiNew->RcvList) );
    NdisAllocateSpinLock( &(oiNew->RequestSpinLock) );
    InitializeListHead( &(oiNew->RequestList) );

    for ( i=0; i<MAX_REQUESTS; i++ )
    {
        InsertTailList( &(oiNew->RequestList), &(oiNew->Requests[i].Reserved.ListElement) );
    }

    oiNew->Status = NDIS_STATUS_PENDING;

    /*initialize the timer variables for this session*/

    SystemTime=GetDate();

    ltime1=((__int64)SystemTime.HighPart*86400);
    ltime1+=(__int64)(SystemTime.LowPart/1000);	//current time from 1980 in seconds
    ltime1+=(__int64)315532800;	//current time from 1970 (Unix format) in seconds
    ltime1*=1193182;
    ltime1+=(SystemTime.LowPart%1000)*1193182/1000; //current time from 1970 in ticks
    ltime1-=QuerySystemTime();	//boot time from 1970 in ticks
    oiNew->StartTime=ltime1;


    oiNew->Dropped=0;		//reset the dropped packets counter
    oiNew->Received=0;		//reset the received packets counter
    oiNew->bpfprogram=NULL;	//set an accept-all filter
    oiNew->bpfprogramlen=0;
    oiNew->BufSize=0;		//set an empty buffer
    oiNew->Buffer=NULL;		//reset the buffer
    oiNew->Bhead=0;
    oiNew->Btail=0;
    oiNew->BLastByte=0;
    oiNew->TimeOut=0;		//reset the timeouts
    oiNew->ReadTimeoutTimer=0;
    oiNew->mode=0;			//set capture mode
    oiNew->Nbytes=0;		//reset the counters
    oiNew->Npackets=0;
    oiNew->hDevice=hDevice;
    oiNew->tagProcess=pDiocParms->tagProcess;
    oiNew->ReadEvent=0;		//reset the read event

    NdisAllocateSpinLock( &(oiNew->CountersLock) );
    /*open the MAC driver calling NDIS*/
    NdisOpenAdapter( &nsOpenStatus,
                     &nsErrorStatus,
                     &oiNew->AdapterHandle,
                     &uiMedium,
                     MediumArray,
                     NUM_NDIS_MEDIA,
                     pde->NdisProtocolHandle,
                     oiNew,
                     AdapterName,
                     0,
                     NULL );

    IF_TRACE_MSG( "Open Status                   : %lx", nsOpenStatus );
    IF_TRACE_MSG( "Error Status                  : %lx", nsErrorStatus );
    IF_TRACE_MSG( "Completion Status             : %lx", oiNew->Status );

    if ( nsOpenStatus == NDIS_STATUS_PENDING )
    {
        while ( oiNew->Status == NDIS_STATUS_PENDING )
            YieldExecution();
    }
    else
    {
        PacketOpenAdapterComplete( oiNew, nsOpenStatus, nsErrorStatus );
    }

    Status = oiNew->Status;
    if ( Status != NDIS_STATUS_SUCCESS )
    {
        NdisFreeMemory( oiNew, sizeof( OPEN_INSTANCE ) ,  0 );
        return NDIS_STATUS_FAILURE;
    }
    else
    {

    }

    TRACE_LEAVE( "BindAdapter" );

    /*return succesfully*/
    return STATUS_SUCCESS;

}
Ejemplo n.º 3
0
/*
 * Opens the specified adapter and binds the protocol to it
 *
 * Arguments
 *		AdapterName		- The name of the adapter with which binding should happen
 *
 * Return Value
 *		Return TRUE if successfully bound otherwise returns FALSE
 *
 */
BOOL PKTOpenAdapter (PNDIS_STRING pAdapterName)
{
	POPEN_INSTANCE	pOI;
	NDIS_STATUS		nsOpen;
	NDIS_STATUS		nsError;
	SYSTEMTIME		SystemTime;
	FILETIME		FileTime;
	LARGE_INTEGER	liSysTime;
	LARGE_INTEGER	TimeFreq;
	LARGE_INTEGER	PTime;
	UINT			uiMedium;


	// Check if an instance is already opened
	if (g_pDeviceExtension->pOpenInstance) {
		SetLastError (ERROR_ALREADY_INITIALIZED);
		return FALSE;
	}

	// Time initialization
	GetSystemTime (&SystemTime);
	if (! (SystemTimeToFileTime (&SystemTime, &FileTime) &&
		QueryPerformanceCounter (&PTime) && QueryPerformanceFrequency (&TimeFreq))) {
		return FALSE;
	}
	NdisMoveMemory (&liSysTime, &FileTime, sizeof (LARGE_INTEGER));
	
	
	
	// allocate an instance that describes the adapter
	NdisAllocateMemory (&pOI, sizeof (OPEN_INSTANCE), 0, NDIS_ADDR_M1);
	if (pOI == NULL) {
		return FALSE;
	}
	NdisZeroMemory (pOI, sizeof (OPEN_INSTANCE));

	// init struct variables
	pOI->bpfprogram			= NULL;		//set an accept-all filter
	pOI->bpfprogramlen		= 0;
	pOI->BufSize			= 0;		//set an empty buffer
	pOI->Buffer				= NULL;		//reset the buffer
	pOI->Bhead				= 0;
	pOI->Btail				= 0;
	pOI->BLastByte			= 0;
	pOI->TimeOut			= 0;		//reset the timeouts


	// create the shared name read event
	pOI->ReadEvent = CreateEvent (NULL, FALSE, FALSE, SH_EVENT_NAME);
	if (pOI->ReadEvent == NULL) {
		NdisFreeMemory (pOI, sizeof (OPEN_INSTANCE), 0);
		return FALSE;
	}

	// set time values
	pOI->StartTime.QuadPart = (((liSysTime.QuadPart) % 10000000) * TimeFreq.QuadPart)/10000000;
	liSysTime.QuadPart = liSysTime.QuadPart / 10000000 - 11644473600;
	pOI->StartTime.QuadPart += (liSysTime.QuadPart) * TimeFreq.QuadPart - PTime.QuadPart;


	// allocate pool for the packet headers
	NdisAllocatePacketPool (&nsError, &(pOI->PacketPool), 
		TRANSMIT_PACKETS, sizeof (PACKET_RESERVED));
	if (nsError != NDIS_STATUS_SUCCESS) {
		CloseHandle (pOI->ReadEvent);
		NdisFreeMemory (pOI, sizeof (OPEN_INSTANCE), 0);
		return FALSE;
	}


	// allocate buffer pool for the packet data
	NdisAllocateBufferPool (&nsError, &(pOI->BufferPool), TRANSMIT_PACKETS);
	if (nsError != NDIS_STATUS_SUCCESS) {
		CloseHandle (pOI->ReadEvent);
		NdisFreePacketPool (pOI->PacketPool);
		NdisFreeMemory (pOI, sizeof (OPEN_INSTANCE), 0);
		return FALSE;
	}

	// set status pending
	pOI->Status = NDIS_STATUS_PENDING;

	// open the MAC driver
	NdisOpenAdapter (&nsOpen, &nsError, &pOI->AdapterHandle, &uiMedium, MediumArray,
		NUM_NDIS_MEDIA, g_pDeviceExtension->NdisProtocolHandle, pOI, pAdapterName, 0, NULL);


	if (nsOpen == NDIS_STATUS_PENDING) {
		SuspendExecution (pOI);
	} else {
		PacketOpenAdapterComplete (pOI, nsOpen, nsError);
	}

	// free the packet instance if not successful
	if (pOI->Status != NDIS_STATUS_SUCCESS) {
		CloseHandle (pOI->ReadEvent);
		NdisFreeMemory (pOI, sizeof (OPEN_INSTANCE), 0);
		return FALSE;
	}

	return TRUE;
}