VBoxNetFlt.c revision 9c70f50b7838559639fe35e9bab6b26fbe75705b
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VBoxNetFlt - Network Filter Driver (Host), Common Code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2008-2009 Sun Microsystems, Inc.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * additional information or have any questions.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @page pg_netflt VBoxNetFlt - Network Interface Filter
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This is a kernel module that attaches to a real interface on the host
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * and filters and injects packets.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * In the big picture we're one of the three trunk interface on the internal
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * network, the one named "NIC Filter Driver": @image html Networking_Overview.gif
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @section sec_netflt_msc Locking / Sequence Diagrams
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This secion contains a few sequence diagrams describing the problematic
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * transitions of a host interface filter instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The thing that makes it all a bit problematic is that multiple events may
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * happen at the same time, and that we have to be very careful to avoid
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * deadlocks caused by mixing our locks with the ones in the host kernel.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The main events are receive, send, async send completion, disappearance of
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * the host networking interface and it's reappearance. The latter two events
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * are can be caused by driver unloading/loading or the device being physical
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * unplugged (e.g. a USB network device).
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The strategy for dealing with these issues are:
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * - Use a simple state machine.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * - Require the user (IntNet) to serialize all its calls to us,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * while at the same time not owning any lock used by any of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * the callbacks we might call on receive and async send completion.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * - Make sure we're 100% idle before disconnecting, and have a
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * disconnected status on both sides to fend off async calls.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * - Protect the host specific interface handle and the state variables
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * using a spinlock.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @subsection subsec_netflt_msc_dis_rel Disconnect from the network and release
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM, IntNet, NetFlt, Kernel, Wire;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="pkt0 to wire", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel->Wire [label="pkt0 to wire", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * --- [label="Suspending the trunk interface"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Wire->Kernel [label="pkt1 - racing us", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel=>>NetFlt [label="pkt1 - racing us", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>>IntNet [label="pkt1 recv - blocks", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Mark Trunk Suspended"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pfnSetActive(false)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Mark inactive (atomic)"];
a118ea84f7f14ee4c253a7f584f0b592caf74c4fvboxsync * IntNet<<NetFlt;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pfnWaitForIdle(forever)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>>NetFlt [label="pkt1 to host", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>>Kernel [label="pkt1 to host", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel<-Wire [label="pkt0 on wire", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="pkt0 on wire", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<=IntNet [label="Lock Net, free SG, Unlock Net", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<-NetFlt [label="idle", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="idle (pfnWaitForIdle)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Wire->Kernel [label="pkt2", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel=>>NetFlt [label="pkt2", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>>Kernel [label="pkt2 to host", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM->IntNet [label="pkt3", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> drop", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * --- [label="The trunk interface is idle now, disconnect it"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlink Trunk"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pfnDisconnectAndRelease"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="iflt_detach"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<=Kernel [label="iff_detached"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt>>Kernel [label="iff_detached"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="iflt_detach"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Release"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="pfnDisconnectAndRelease"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @subsection subsec_netflt_msc_hif_rm Host Interface Removal
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The ifnet_t (pIf) is a tricky customer as any reference to it can potentially
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * race the filter detaching. The simple way of solving it on Darwin is to guard
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * all access to the pIf member with a spinlock. The other host systems will
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * probably have similar race conditions, so the spinlock is a generic thing.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM, IntNet, NetFlt, Kernel;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="ifnet_reference w/ spinlock", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="ifnet_reference", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="pkt0 to wire (blocks)", linecolor="green", textcolor="green" ];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * --- [label="The host interface is being disconnected"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel->NetFlt [label="iff_detached"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="ifnet_release w/ spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="ifnet_release"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="fDisconnectedFromHost=true"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt>>Kernel [label="iff_detached"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="dropped", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Acquire spinlock", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="pIf=NULL", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Release spinlock", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @subsection subsec_netflt_msc_hif_rm Host Interface Rediscovery
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The rediscovery is performed when we receive a send request and a certain
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * period have elapsed since the last attempt, i.e. we're polling it. We
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * synchronize the rediscovery with disconnection from the internal network
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * by means of the pfnWaitForIdle call, so no special handling is required.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM2, VM1, IntNet, NetFlt, Kernel, Wire;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * --- [label="Rediscovery conditions are not met"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM1->IntNet [label="pkt0"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pkt0 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="pkt0 to wire (dropped)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * --- [label="Rediscovery conditions"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM1->IntNet [label="pkt1"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pkt1 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="fRediscoveryPending=true w/ spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="ifnet_find_by_name"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="ifnet_find_by_name (success)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VM2->IntNet [label="pkt2", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet=>NetFlt [label="pkt2 to wire", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="!pIf || fRediscoveryPending (w/ spinlock)", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="pkt2 to wire (dropped)", linecolor="red", textcolor="red"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="iflt_attach"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="iflt_attach (success)"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Acquire spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Set pIf and update flags"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>NetFlt [label="Release spinlock"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt=>Kernel [label="pkt1 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Kernel->Wire [label="pkt1 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NetFlt<<Kernel [label="pkt1 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IntNet<<NetFlt [label="pkt1 to wire"];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Defined Constants And Macros *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ( (PVBOXNETFLTINS)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETFLTINS, MyPort)) )
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncAssertCompileMemberSize(VBOXNETFLTINS, enmState, sizeof(uint32_t));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Sets the enmState member atomically.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Used for all updates.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param enmNewState The new value.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncDECLINLINE(void) vboxNetFltSetState(PVBOXNETFLTINS pThis, VBOXNETFTLINSSTATE enmNewState)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Gets the enmState member atomically.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Used for all reads.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns The enmState value.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncDECLINLINE(VBOXNETFTLINSSTATE) vboxNetFltGetState(PVBOXNETFLTINS pThis)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync return (VBOXNETFTLINSSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Finds a instance by its name, the caller does the locking.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns Pointer to the instance by the given name. NULL if not found.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pGlobals The globals.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszName The name of the instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic PVBOXNETFLTINS vboxNetFltFindInstanceLocked(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Finds a instance by its name, will request the mutex.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * No reference to the instance is retained, we're assuming the caller to
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * already have one but just for some reason doesn't have the pointer to it.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns Pointer to the instance by the given name. NULL if not found.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pGlobals The globals.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszName The name of the instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncDECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pRet = vboxNetFltFindInstanceLocked(pGlobals, pszName);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Unlinks an instance from the chain.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pGlobals The globals.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pToUnlink The instance to unlink.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic void vboxNetFltUnlinkLocked(PVBOXNETFLTGLOBALS pGlobals, PVBOXNETFLTINS pToUnlink)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Performs interface rediscovery if it was disconnected from the host.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns true if successfully rediscovered and connected, false if not.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pThis The instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic bool vboxNetFltMaybeRediscovered(PVBOXNETFLTINS pThis)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Rediscovered already? Time to try again?
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync fRediscovered = !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && !ASMAtomicUoReadBool(&pThis->fRediscoveryPending)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && Now - ASMAtomicUoReadU64(&pThis->NanoTSLastRediscovery) > UINT64_C(5000000000); /* 5 sec */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASMAtomicWriteBool(&pThis->fRediscoveryPending, true);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Call the OS specific code to do the job.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Update the state when the call returns, that is everything except for
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * the fDisconnectedFromHost flag which the OS specific code shall set.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync fRediscovered = vboxNetFltOsMaybeRediscovered(pThis);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync Assert(!fRediscovered || !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, RTTimeNanoTS());
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync ASMAtomicWriteBool(&pThis->fRediscoveryPending, false);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @copydoc INTNETTRUNKIFPORT::pfnXmit
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic DECLCALLBACK(int) vboxNetFltPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Input validation.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Do a busy retain and then make sure we're connected to the interface
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * before invoking the OS specific code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync if ( !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic DECLCALLBACK(bool) vboxNetFltPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Input validation.
int rc;
return rc;
return !fActive;
#ifdef VBOXNETFLT_STATIC_CONFIG
int rc;
#ifdef VBOXNETFLT_STATIC_CONFIG
if (fBusy)
if (!cRefs)
if (!cRefs)
if (fBusy)
static int vboxNetFltConnectIt(PVBOXNETFLTINS pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
int rc;
#ifdef VBOXNETFLT_STATIC_CONFIG
return rc;
static int vboxNetFltNewInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PINTNETTRUNKSWPORT pSwitchPort,
int rc;
if (!pNew)
return VERR_INTNET_FLT_IF_FAILED;
#ifdef VBOXNETFLT_STATIC_CONFIG
return rc;
return rc;
return rc;
#ifdef VBOXNETFLT_STATIC_CONFIG
DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void *pvContext)
int rc;
while (pCur)
switch (enmState)
return VINF_ALREADY_INITIALIZED;
LogRel(("VBoxNetFlt: Huh? An instance of '%s' already exists! [pCur=%p cRefs=%d fDfH=%RTbool enmState=%d]\n",
return VERR_INTNET_FLT_IF_BUSY;
# ifdef RT_STRICT
return rc;
static DECLCALLBACK(int) vboxNetFltFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
int rc;
LogFlow(("vboxNetFltFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
if (pCur)
#ifdef VBOXNETFLT_STATIC_CONFIG
if (pCur)
return rc;
#ifdef VBOXNETFLT_STATIC_CONFIG
NULL,
ppIfPort);
return rc;
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
static DECLCALLBACK(void *) vboxNetFltQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, SupDrvFactory));
#ifdef LOG_ENABLED
return NULL;
return fRc;
int rc;
return VERR_WRONG_ORDER;
return VERR_WRONG_ORDER;
return rc;
int rc;
rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
return rc;
return rc;
return rc;
return rc;
return rc;
return rc;
return rc;