DrvNetSniffer.cpp revision 5b465a7c1237993faf8bb50120d247f3f0319ada
/* $Id$ */
/** @file
* DrvNetSniffer - Network sniffer filter driver.
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_NAT
#include <iprt/critsect.h>
#include "Pcap.h"
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*
* @implements PDMINETWORKCONNECTOR
* @implements PDMINETWORKPORT
* @implements PDMINETWORKCONFIG
*/
typedef struct DRVNETSNIFFER
{
/** The network interface. */
/** The network interface. */
/** The network config interface. */
/** 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. */
/** Converts a pointer to NAT::INetworkConnector to a PDRVNETSNIFFER. */
#define PDMINETWORKCONNECTOR_2_DRVNETSNIFFER(pInterface) ( (PDRVNETSNIFFER)((uintptr_t)pInterface - RT_OFFSETOF(DRVNETSNIFFER, INetworkConnector)) )
/** Converts a pointer to NAT::INetworkPort to a PDRVNETSNIFFER. */
#define PDMINETWORKPORT_2_DRVNETSNIFFER(pInterface) ( (PDRVNETSNIFFER)((uintptr_t)pInterface - RT_OFFSETOF(DRVNETSNIFFER, INetworkPort)) )
/** Converts a pointer to NAT::INetworkConfig to a PDRVNETSNIFFER. */
#define PDMINETWORKCONFIG_2_DRVNETSNIFFER(pInterface) ( (PDRVNETSNIFFER)((uintptr_t)pInterface - RT_OFFSETOF(DRVNETSNIFFER, INetworkConfig)) )
/**
* Send data to the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf Data to send.
* @param cb Number of bytes to send.
* @thread EMT
*/
static DECLCALLBACK(int) drvNetSnifferSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
{
/* output to sniffer */
/* pass down */
if (pThis->pConnector)
{
#if 0
u64TS = RTTimeProgramNanoTS();
#endif
return rc;
}
return VINF_SUCCESS;
}
/**
* Set promiscuous mode.
*
* This is called when the promiscuous mode is set. This means that there doesn't have
* to be a mode change when it's called.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
* @thread EMT
*/
static DECLCALLBACK(void) drvNetSnifferSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
if (pThis->pConnector)
}
/**
* Notification on link status changes.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmLinkState The new link state.
* @thread EMT
*/
static DECLCALLBACK(void) drvNetSnifferNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
if (pThis->pConnector)
}
/**
* This must be called before the pfnRecieve() method is called.
*
* @returns Number of bytes the device can receive now.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
static DECLCALLBACK(int) drvNetSnifferWaitReceiveAvail(PPDMINETWORKPORT pInterface, RTMSINTERVAL cMillies)
{
}
/**
* Receive data from the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf The available data.
* @param cb Number of bytes available in the buffer.
* @thread EMT
*/
static DECLCALLBACK(int) drvNetSnifferReceive(PPDMINETWORKPORT pInterface, const void *pvBuf, size_t cb)
{
/* output to sniffer */
/* pass up */
#if 0
u64TS = RTTimeProgramNanoTS();
#endif
return rc;
}
/**
* 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
*/
{
}
/**
* 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) drvNetSnifferSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
{
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* Detach a driver instance.
*
* @param pDrvIns The driver instance.
* @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
*/
{
}
/**
* Attach a driver instance.
*
* @returns VBox status code.
* @param pDrvIns The driver instance.
* @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
*/
{
/*
* Query the network connector interface.
*/
if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
else if (RT_SUCCESS(rc))
{
if (!pThis->pConnector)
{
AssertMsgFailed(("Configuration error: the driver below didn't export the network connector interface!\n"));
return VERR_PDM_MISSING_INTERFACE_BELOW;
}
}
else
{
return rc;
}
return VINF_SUCCESS;
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
{
}
}
/**
* Construct a NAT network transport driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
static DECLCALLBACK(int) drvNetSnifferConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
{
LogFlow(("drvNetSnifferConstruct:\n"));
/*
* Validate the config.
*/
LogRel(("NetSniffer: Found child config entries -- are you trying to redirect ports?\n"));
/*
* Init the static parts.
*/
/* The pcap file *must* start at time offset 0,0. */
/* IBase */
/* INetworkConnector */
/* INetworkPort */
/* INetworkConfig */
/*
* 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.
*/
{
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.
*/
{
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->pConnector)
{
AssertMsgFailed(("Configuration error: the driver below didn't export the network connector interface!\n"));
return VERR_PDM_MISSING_INTERFACE_BELOW;
}
}
else
{
return rc;
}
/*
* Create the lock.
*/
if (RT_FAILURE(rc))
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 is done since capturing pThis->StartNanoTS so capture 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 */
};