DrvNetSniffer.cpp revision 1e1273b11e17928ec3c3a8fff45121aa7a169413
/* $Id$ */
/** @file
* DrvNetSniffer - Network sniffer filter driver.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_NAT
#include <iprt/critsect.h>
#include "Pcap.h"
#include "VBoxDD.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*
* @implements PDMINETWORKUP
* @implements PDMINETWORKDOWN
* @implements PDMINETWORKCONFIG
*/
typedef struct DRVNETSNIFFER
{
/** The network interface. */
/** The network interface. */
/** The network config interface.
* @todo this is a main interface and shouldn't be here... */
/** The port we're attached to. */
/** The config port interface we're attached to. */
/** The connector that's attached to us. */
/** The filename. */
char szFilename[RTPATH_MAX];
/** The filehandle. */
/** The lock serializing the file access. */
/** The NanoTS delta we pass to the pcap writers. */
/** Pointer to the driver instance. */
/** For when we're the leaf driver. */
/**
* @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
*/
{
{
rc = VERR_TRY_AGAIN;
return rc;
}
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
*/
{
return VERR_NET_DOWN;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
*/
static DECLCALLBACK(int) drvNetSnifferUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
{
return VERR_NET_DOWN;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
*/
static DECLCALLBACK(int) drvNetSnifferUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
{
return VERR_NET_DOWN;
/* output to sniffer */
else
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
*/
{
LogFlow(("drvNetSnifferUp_EndXmit:\n"));
else
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
*/
static DECLCALLBACK(void) drvNetSnifferUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
{
if (pThis->pIBelowNet)
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnNotifyLinkChanged}
*/
static DECLCALLBACK(void) drvNetSnifferUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
if (pThis->pIBelowNet)
}
/**
* @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
*/
static DECLCALLBACK(int) drvNetSnifferDown_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
{
}
/**
* @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
*/
static DECLCALLBACK(int) drvNetSnifferDown_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
{
/* output to sniffer */
/* pass up */
#if 0
u64TS = RTTimeProgramNanoTS();
#endif
return rc;
}
/**
* @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
*/
{
}
/**
* Gets the current Media Access Control (MAC) address.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pMac Where to store the MAC address.
* @thread EMT
*/
{
}
/**
* Gets the new link state.
*
* @returns The current link state.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
static DECLCALLBACK(PDMNETWORKLINKSTATE) drvNetSnifferDownCfg_GetLinkState(PPDMINETWORKCONFIG pInterface)
{
}
/**
* Sets the new link state.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmState The new link state
* @thread EMT
*/
static DECLCALLBACK(int) drvNetSnifferDownCfg_SetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
{
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* @interface_method_impl{PDMDRVREG,pfnDetach}
*/
{
}
/**
* @interface_method_impl{PDMDRVREG,pfnAttach}
*/
{
/*
* Query the network connector interface.
*/
if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
{
rc = VINF_SUCCESS;
}
else if (RT_SUCCESS(rc))
{
if (pThis->pIBelowNet)
rc = VINF_SUCCESS;
else
{
AssertMsgFailed(("Configuration error: the driver below didn't export the network connector interface!\n"));
}
}
else
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDRVREG,pfnDestruct}
*/
{
{
}
}
/**
* @interface_method_impl{Construct a NAT network transport driver instance,
* PDMDRVREG,pfnDestruct}
*/
static DECLCALLBACK(int) drvNetSnifferConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
{
LogFlow(("drvNetSnifferConstruct:\n"));
/*
* Init the static parts.
*/
/* The pcap file *must* start at time offset 0,0. */
/* IBase */
/* INetworkUp */
/* INetworkDown */
/* INetworkConfig */
/*
* Create the locks.
*/
/*
* Validate the config.
*/
if (CFGMR3GetFirstChild(pCfg))
LogRel(("NetSniffer: Found child config entries -- are you trying to redirect ports?\n"));
/*
* Get the filename.
*/
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
RTStrPrintf(pThis->szFilename, sizeof(pThis->szFilename), "./VBox-%x-%u.pcap", RTProcSelf(), pDrvIns->iInstance);
else
}
else if (RT_FAILURE(rc))
{
return rc;
}
/*
* Query the network port interface.
*/
if (!pThis->pIAboveNet)
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the network port interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Query the network config interface.
*/
if (!pThis->pIAboveConfig)
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the network config interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Query the network connector interface.
*/
if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
else if (RT_SUCCESS(rc))
{
if (!pThis->pIBelowNet)
{
AssertMsgFailed(("Configuration error: the driver below didn't export the network connector interface!\n"));
return VERR_PDM_MISSING_INTERFACE_BELOW;
}
}
else
{
return rc;
}
/*
* Open output file / pipe.
*/
if (RT_FAILURE(rc))
N_("Netsniffer cannot open '%s' for writing. The directory must exist and it must be writable for the current user"), pThis->szFilename);
/*
* Write pcap header.
* Some time has gone by since capturing pThis->StartNanoTS so get the
* current time again.
*/
return VINF_SUCCESS;
}
/**
* Network sniffer filter driver registration record.
*/
const PDMDRVREG g_DrvNetSniffer =
{
/* u32Version */
/* szName */
"NetSniffer",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"Network Sniffer Filter Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
/* cbInstance */
sizeof(DRVNETSNIFFER),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
/* pfnDetach */
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};