Skip to content

Commit

Permalink
New device structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Aug 11, 2019
1 parent 15765ba commit 545c677
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 176 deletions.
8 changes: 4 additions & 4 deletions Common/WpcapNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@
//

// Used in packetWin7\Dll and the driver
#define NPF_DEVICE_NAMES_PREFIX NPF_DRIVER_NAME "_" ///< (AAA) packet.dll
#define NPF_DEVICE_NAMES_PREFIX_WIDECHAR NPF_DRIVER_NAME_WIDECHAR L"_" ///< (AAA) used by the NPF driver, that does not accept the TEXT(a) macro correctly.
#define NPF_DEVICE_NAMES_PREFIX_WIFI NPF_DEVICE_NAMES_PREFIX "WIFI_"
#define NPF_DEVICE_NAMES_PREFIX_WIDECHAR_WIFI NPF_DEVICE_NAMES_PREFIX_WIDECHAR L"WIFI_"
#define NPF_DEVICE_NAMES_PREFIX NPF_DRIVER_NAME "\\" ///< (AAA) packet.dll
#define NPF_DEVICE_NAMES_PREFIX_WIDECHAR NPF_DRIVER_NAME_WIDECHAR L"\\" ///< (AAA) used by the NPF driver, that does not accept the TEXT(a) macro correctly.
#define NPF_DEVICE_NAMES_PREFIX_WIFI NPF_DEVICE_NAMES_PREFIX "\\WIFI_"
#define NPF_DEVICE_NAMES_PREFIX_WIDECHAR_WIFI NPF_DEVICE_NAMES_PREFIX_WIDECHAR L"\\WIFI_"

// Used in packetWin7\Dll
#define FAKE_NDISWAN_ADAPTER_NAME "\\Device\\" NPF_DRIVER_NAME "_GenericDialupAdapter" ///< (CCC) Name of a fake ndiswan adapter that is always available on 2000/XP/2003, used to capture NCP/LCP packets
Expand Down
66 changes: 48 additions & 18 deletions packetWin7/npf/npf/Openclos.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,15 @@ NPF_OpenAdapter(
DeviceExtension = DeviceObject->DeviceExtension;

IrpSp = IoGetCurrentIrpStackLocation(Irp);

// Find the head adapter of the global array.
pFiltMod = NPF_GetFilterModuleByAdapterName(&DeviceExtension->AdapterName, DeviceExtension->Dot11);
pFiltMod = NPF_GetFilterModuleByAdapterName(&IrpSp->FileObject->FileName);

if (pFiltMod == NULL)
{
// Can't find the adapter from the global open array.
TRACE_MESSAGE2(PACKET_DEBUG_LOUD,
"NPF_GetOpenByAdapterName error, pFiltMod=NULL, AdapterName=%ws, Dot11=%d",
DeviceExtension->AdapterName.Buffer,
DeviceExtension->Dot11);
TRACE_MESSAGE1(PACKET_DEBUG_LOUD,
"NPF_GetOpenByAdapterName error, pFiltMod=NULL, AdapterName=%ws",
IrpSp->FileObject->FileName.Buffer);

Irp->IoStatus.Status = STATUS_NDIS_INTERFACE_NOT_FOUND;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
Expand All @@ -263,8 +261,8 @@ NPF_OpenAdapter(
if (NPF_StartUsingBinding(pFiltMod) == FALSE)
{
TRACE_MESSAGE1(PACKET_DEBUG_LOUD,
"NPF_StartUsingBinding error, AdapterName=%ws",
DeviceExtension->AdapterName.Buffer);
"NPF_GetOpenByAdapterName error, AdapterName=%ws",
IrpSp->FileObject->FileName.Buffer);

Irp->IoStatus.Status = STATUS_NDIS_OPEN_FAILED;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
Expand All @@ -286,20 +284,20 @@ NPF_OpenAdapter(
#ifdef HAVE_WFP_LOOPBACK_SUPPORT
TRACE_MESSAGE3(PACKET_DEBUG_LOUD,
"Opening the device %ws, BindingContext=%p, Loopback=%u",
DeviceExtension->AdapterName.Buffer,
IrpSp->FileObject->FileName.Buffer,
Open,
pFiltMod->Loopback);
#else
TRACE_MESSAGE2(PACKET_DEBUG_LOUD,
"Opening the device %ws, BindingContext=%p, Loopback=<Not supported>",
DeviceExtension->AdapterName.Buffer,
IrpSp->FileObject->FileName.Buffer,
Open);
#endif

#ifdef HAVE_WFP_LOOPBACK_SUPPORT
if (pFiltMod->Loopback)
{
if (g_LoopbackDevObj != NULL && g_WFPEngineHandle == INVALID_HANDLE_VALUE)
if (g_WFPEngineHandle == INVALID_HANDLE_VALUE)
{
TRACE_MESSAGE1(PACKET_DEBUG_LOUD, "g_LoopbackDevObj=%p, init WSK injection handles and register callouts", g_LoopbackDevObj);
// Use Windows Filtering Platform (WFP) to capture loopback packets, also help WSK take care of loopback packet sending.
Expand All @@ -309,7 +307,7 @@ NPF_OpenAdapter(
goto NPF_OpenAdapter_End;
}

Status = NPF_RegisterCallouts(g_LoopbackDevObj);
Status = NPF_RegisterCallouts(DeviceObject);
if (!NT_SUCCESS(Status))
{
if (g_WFPEngineHandle != INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -1293,16 +1291,46 @@ NPF_EqualAdapterName(

//-------------------------------------------------------------------

#define UNICODE_CONTAINS(a, b, byteoffset) (sizeof(b) == RtlCompareMemory(a.Buffer + byteoffset/sizeof(WCHAR), b, sizeof(b)))
PNPCAP_FILTER_MODULE
NPF_GetFilterModuleByAdapterName(
PNDIS_STRING pAdapterName,
BOOLEAN Dot11
PNDIS_STRING pAdapterName
)
{
PSINGLE_LIST_ENTRY Curr = NULL;
PNPCAP_FILTER_MODULE pFiltMod = NULL;
size_t i = 0;
size_t shrink_by = 0;
BOOLEAN Dot11 = FALSE;
NDIS_STRING BaseName;
TRACE_ENTER();

BaseName.MaximumLength = pAdapterName->MaximumLength;
BaseName.Buffer = ExAllocatePoolWithTag(NonPagedPool, BaseName.MaximumLength, 'GFBN');
if (BaseName.Buffer == NULL) {
IF_LOUD(DbgPrint("NPF_GetFilterModuleByAdapterName: failed to allocate BaseName.Buffer\n");)
TRACE_EXIT();
return NULL;
}
RtlCopyUnicodeString(&BaseName, pAdapterName);

// strip off leading backslashes
while (shrink_by < BaseName.Length && BaseName.Buffer[shrink_by] == L'\\') {
shrink_by++;
}

// Check for WIFI_ prefix and strip it
if (UNICODE_CONTAINS(BaseName, NPF_DEVICE_NAMES_PREFIX_WIDECHAR_WIFI, i)) {
shrink_by += sizeof(NPF_DEVICE_NAMES_PREFIX_WIDECHAR)/sizeof(WCHAR) - 1;
Dot11 = TRUE;
}

// Do the strip
for (i=shrink_by; i < pAdapterName->Length/sizeof(WCHAR) && (i - shrink_by)*sizeof(WCHAR) < BaseName.MaximumLength; i++) {
BaseName.Buffer[i - shrink_by] = pAdapterName->Buffer[i];
}
BaseName.Length = BaseName.Length - shrink_by*sizeof(WCHAR);

NdisAcquireSpinLock(&g_FilterArrayLock);
for (Curr = g_arrFiltMod.Next; Curr != NULL; Curr = Curr->Next)
{
Expand All @@ -1312,10 +1340,11 @@ NPF_GetFilterModuleByAdapterName(
continue;
}

if (NPF_EqualAdapterName(&pFiltMod->AdapterName, pAdapterName) && pFiltMod->Dot11 == Dot11)
if (pFiltMod->Dot11 == Dot11 && NPF_EqualAdapterName(&pFiltMod->AdapterName, &BaseName))
{
NPF_StopUsingBinding(pFiltMod);
NdisReleaseSpinLock(&g_FilterArrayLock);
ExFreePoolWithTag(BaseName.Buffer, 'GFBN');
return pFiltMod;
}
else
Expand All @@ -1324,6 +1353,7 @@ NPF_GetFilterModuleByAdapterName(
}
}
NdisReleaseSpinLock(&g_FilterArrayLock);
ExFreePoolWithTag(BaseName.Buffer, 'GFBN');

TRACE_EXIT();
return NULL;
Expand Down Expand Up @@ -1514,10 +1544,10 @@ NPF_CreateFilterModule(

pFiltMod->MaxFrameSize = 0;

pFiltMod->AdapterName.Buffer = ExAllocatePoolWithTag(NonPagedPool, AdapterName->MaximumLength, 'NPCA');
pFiltMod->AdapterName.MaximumLength = AdapterName->MaximumLength;
pFiltMod->AdapterName.MaximumLength = AdapterName->MaximumLength - devicePrefix.Length;
pFiltMod->AdapterName.Buffer = ExAllocatePoolWithTag(NonPagedPool, pFiltMod->AdapterName.MaximumLength, 'NPCA');
pFiltMod->AdapterName.Length = 0;
RtlCopyUnicodeString(&pFiltMod->AdapterName, AdapterName);
RtlAppendUnicodeToString(&pFiltMod->AdapterName, AdapterName->Buffer + devicePrefix.Length / sizeof(WCHAR));

//
//allocate the spinlock for the OID requests
Expand Down
Loading

0 comments on commit 545c677

Please sign in to comment.