VBoxNetFlt.c revision 47a160d35dbc42c8c817c9b48be21dca11d329d6
c74832c7184337c330041742d88e6dacaa07b378vboxsync/* $Id$ */
c74832c7184337c330041742d88e6dacaa07b378vboxsync/** @file
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * VBoxNetFlt - Network Filter Driver (Host), Common Code.
d192cc72774b02a0df2fdd46350d418236ac09aevboxsync */
d192cc72774b02a0df2fdd46350d418236ac09aevboxsync
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync/*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * available from http://www.virtualbox.org. This file is free software;
c74832c7184337c330041742d88e6dacaa07b378vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c74832c7184337c330041742d88e6dacaa07b378vboxsync * General Public License (GPL) as published by the Free Software
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c74832c7184337c330041742d88e6dacaa07b378vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
c74832c7184337c330041742d88e6dacaa07b378vboxsync * additional information or have any questions.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/** @page pg_netflt VBoxNetFlt - Network Interface Filter
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * This is a kernel module that attaches to a real interface on the host
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * and filters and injects packets.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * In the big picture we're one of the three trunk interface on the internal
c74832c7184337c330041742d88e6dacaa07b378vboxsync * network, the one named "NIC Filter Driver": @image html Networking_Overview.gif
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @section sec_netflt_msc Locking / Sequence Diagrams
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * This secion contains a few sequence diagrams describing the problematic
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * transitions of a host interface filter instance.
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync *
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * The thing that makes it all a bit problematic is that multiple events may
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * happen at the same time, and that we have to be very careful to avoid
c74832c7184337c330041742d88e6dacaa07b378vboxsync * deadlocks caused by mixing our locks with the ones in the host kernel.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * The main events are receive, send, async send completion, disappearance of
c74832c7184337c330041742d88e6dacaa07b378vboxsync * the host networking interface and it's reappearance. The latter two events
c74832c7184337c330041742d88e6dacaa07b378vboxsync * are can be caused by driver unloading/loading or the device being physical
c74832c7184337c330041742d88e6dacaa07b378vboxsync * unplugged (e.g. a USB network device).
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * The strategy for dealing with these issues are:
c74832c7184337c330041742d88e6dacaa07b378vboxsync * - Use a simple state machine.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * - Require the user (IntNet) to serialize all its calls to us,
c74832c7184337c330041742d88e6dacaa07b378vboxsync * while at the same time not owning any lock used by any of the
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * the callbacks we might call on receive and async send completion.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * - Make sure we're 100% idle before disconnecting, and have a
c74832c7184337c330041742d88e6dacaa07b378vboxsync * disconnected status on both sides to fend off async calls.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * - Protect the host specific interface handle and the state variables
c74832c7184337c330041742d88e6dacaa07b378vboxsync * using a spinlock.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @subsection subsec_netflt_msc_dis_rel Disconnect from the network and release
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @msc
c74832c7184337c330041742d88e6dacaa07b378vboxsync * VM, IntNet, NetFlt, Kernel, Wire;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt=>Kernel [label="pkt0 to wire", linecolor="green", textcolor="green"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Kernel->Wire [label="pkt0 to wire", linecolor="green", textcolor="green"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * --- [label="Suspending the trunk interface"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Lock Network"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Wire->Kernel [label="pkt1 - racing us", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Kernel=>>NetFlt [label="pkt1 - racing us", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt=>>IntNet [label="pkt1 recv - blocks", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Mark Trunk Suspended"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Unlock Network"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>NetFlt [label="pfnSetActive(false)"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt=>NetFlt [label="Mark inactive (atomic)"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet<<NetFlt;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>NetFlt [label="pfnWaitForIdle(forever)"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>>NetFlt [label="pkt1 to host", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt=>>Kernel [label="pkt1 to host", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Kernel<-Wire [label="pkt0 on wire", linecolor="green", textcolor="green"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt<<Kernel [label="pkt0 on wire", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * IntNet<<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * IntNet<<=IntNet [label="Lock Net, free SG, Unlock Net", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * NetFlt<-NetFlt [label="idle", linecolor="green", textcolor="green"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync *
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * IntNet<<NetFlt [label="idle (pfnWaitForIdle)"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync *
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Wire->Kernel [label="pkt2", linecolor="red", textcolor="red"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Kernel=>>NetFlt [label="pkt2", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>>Kernel [label="pkt2 to host", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * VM->IntNet [label="pkt3", linecolor="green", textcolor="green"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>IntNet [label="Route packet -> drop", linecolor="green", textcolor="green" ];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync *
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * --- [label="The trunk interface is idle now, disconnect it"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Lock Network"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Unlink Trunk"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Unlock Network"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>NetFlt [label="pfnDisconnectAndRelease"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * NetFlt=>Kernel [label="iflt_detach"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * NetFlt<<=Kernel [label="iff_detached"];
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * NetFlt>>Kernel [label="iff_detached"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * NetFlt<<Kernel [label="iflt_detach"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Release"];
0c34933fc8f84dd5183d1897881bbc7683d24541vboxsync * IntNet<<NetFlt [label="pfnDisconnectAndRelease"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @endmsc
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync *
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @subsection subsec_netflt_msc_hif_rm Host Interface Removal
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * The ifnet_t (pIf) is a tricky customer as any reference to it can potentially
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * race the filter detaching. The simple way of solving it on Darwin is to guard
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * all access to the pIf member with a spinlock. The other host systems will
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * probably have similar race conditions, so the spinlock is a generic thing.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @msc
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * VM, IntNet, NetFlt, Kernel;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="ifnet_reference w/ spinlock", linecolor="green", textcolor="green" ];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt<<Kernel [label="ifnet_reference", linecolor="green", textcolor="green" ];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="pkt0 to wire (blocks)", linecolor="green", textcolor="green" ];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * --- [label="The host interface is being disconnected"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * Kernel->NetFlt [label="iff_detached"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="ifnet_release w/ spinlock"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt<<Kernel [label="ifnet_release"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="fDisconnectedFromHost=true"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt>>Kernel [label="iff_detached"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt<<Kernel [label="dropped", linecolor="green", textcolor="green"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Acquire spinlock", linecolor="green", textcolor="green"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * NetFlt<<Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="pIf=NULL", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * NetFlt=>NetFlt [label="Release spinlock", linecolor="green", textcolor="green"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * IntNet<<NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green"];
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync *
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync * @endmsc
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @subsection subsec_netflt_msc_hif_rm Host Interface Rediscovery
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * The rediscovery is performed when we receive a send request and a certain
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * period have elapsed since the last attempt, i.e. we're polling it. We
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync * synchronize the rediscovery with disconnection from the internal network
c74832c7184337c330041742d88e6dacaa07b378vboxsync * by means of the pfnWaitForIdle call, so no special handling is required.
a6e58d30b4d856cf924fda15cd743fed386fff93vboxsync *
509c0d9c2c05b40f3f07f79e44c06c9395c1b293vboxsync * @msc
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * VM2, VM1, IntNet, NetFlt, Kernel, Wire;
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * --- [label="Rediscovery conditions are not met"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * VM1->IntNet [label="pkt0"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>IntNet [label="Lock Network"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * IntNet=>IntNet [label="Route packet -> wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>IntNet [label="Unlock Network"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>NetFlt [label="pkt0 to wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet<<NetFlt [label="pkt0 to wire (dropped)"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * --- [label="Rediscovery conditions"];
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * VM1->IntNet [label="pkt1"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Lock Network"];
509c0d9c2c05b40f3f07f79e44c06c9395c1b293vboxsync * IntNet=>IntNet [label="Route packet -> wire"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Unlock Network"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>NetFlt [label="pkt1 to wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="fRediscoveryPending=true w/ spinlock"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="ifnet_find_by_name"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt<<Kernel [label="ifnet_find_by_name (success)"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * VM2->IntNet [label="pkt2", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Lock Network", linecolor="red", textcolor="red"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * IntNet=>IntNet [label="Route packet -> wire", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>IntNet [label="Unlock Network", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet=>NetFlt [label="pkt2 to wire", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="!pIf || fRediscoveryPending (w/ spinlock)", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet<<NetFlt [label="pkt2 to wire (dropped)", linecolor="red", textcolor="red"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="iflt_attach"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt<<Kernel [label="iflt_attach (success)"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Acquire spinlock"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * NetFlt=>NetFlt [label="Set pIf and update flags"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>NetFlt [label="Release spinlock"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt=>Kernel [label="pkt1 to wire"];
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Kernel->Wire [label="pkt1 to wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * NetFlt<<Kernel [label="pkt1 to wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync * IntNet<<NetFlt [label="pkt1 to wire"];
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @endmsc
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/*******************************************************************************
4fbca3751fe239da5934c23a783c3422618336e8vboxsync* Header Files *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync*******************************************************************************/
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include "VBoxNetFltInternal.h"
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <VBox/sup.h>
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <VBox/log.h>
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <VBox/err.h>
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <iprt/assert.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync#include <iprt/string.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync#include <iprt/spinlock.h>
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <iprt/uuid.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync#include <iprt/mem.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync#include <iprt/time.h>
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#include <iprt/semaphore.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync#include <iprt/thread.h>
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/*******************************************************************************
c74832c7184337c330041742d88e6dacaa07b378vboxsync* Defined Constants And Macros *
c74832c7184337c330041742d88e6dacaa07b378vboxsync*******************************************************************************/
c74832c7184337c330041742d88e6dacaa07b378vboxsync#define IFPORT_2_VBOXNETFLTINS(pIfPort) \
c74832c7184337c330041742d88e6dacaa07b378vboxsync ( (PVBOXNETFLTINS)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETFLTINS, MyPort)) )
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsyncAssertCompileMemberSize(VBOXNETFLTINS, enmState, sizeof(uint32_t));
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Sets the enmState member atomically.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Used for all updates.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pThis The instance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param enmNewState The new value.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncDECLINLINE(void) vboxNetFltSetState(PVBOXNETFLTINS pThis, VBOXNETFTLINSSTATE enmNewState)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Gets the enmState member atomically.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Used for all reads.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @returns The enmState value.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pThis The instance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncDECLINLINE(VBOXNETFTLINSSTATE) vboxNetFltGetState(PVBOXNETFLTINS pThis)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync return (VBOXNETFTLINSSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Finds a instance by its name, the caller does the locking.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @returns Pointer to the instance by the given name. NULL if not found.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pGlobals The globals.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pszName The name of the instance.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncstatic PVBOXNETFLTINS vboxNetFltFindInstanceLocked(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync PVBOXNETFLTINS pCur;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync if (!strcmp(pszName, pCur->szName))
c74832c7184337c330041742d88e6dacaa07b378vboxsync return pCur;
c74832c7184337c330041742d88e6dacaa07b378vboxsync return NULL;
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Finds a instance by its name, will request the mutex.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * No reference to the instance is retained, we're assuming the caller to
c74832c7184337c330041742d88e6dacaa07b378vboxsync * already have one but just for some reason doesn't have the pointer to it.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @returns Pointer to the instance by the given name. NULL if not found.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pGlobals The globals.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pszName The name of the instance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncDECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync PVBOXNETFLTINS pRet;
c74832c7184337c330041742d88e6dacaa07b378vboxsync int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertRCReturn(rc, NULL);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync pRet = vboxNetFltFindInstanceLocked(pGlobals, pszName);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = RTSemFastMutexRelease(pGlobals->hFastMtx);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertRC(rc);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync return pRet;
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Unlinks an instance from the chain.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pGlobals The globals.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pToUnlink The instance to unlink.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncstatic void vboxNetFltUnlinkLocked(PVBOXNETFLTGLOBALS pGlobals, PVBOXNETFLTINS pToUnlink)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync if (pGlobals->pInstanceHead == pToUnlink)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync pGlobals->pInstanceHead = pToUnlink->pNext;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync else
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync {
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync PVBOXNETFLTINS pCur;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync if (pCur->pNext == pToUnlink)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync {
4fbca3751fe239da5934c23a783c3422618336e8vboxsync pCur->pNext = pToUnlink->pNext;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync break;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync }
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(pCur);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync }
4fbca3751fe239da5934c23a783c3422618336e8vboxsync pToUnlink->pNext = NULL;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync * Performs interface rediscovery if it was disconnected from the host.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync * @returns true if successfully rediscovered and connected, false if not.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pThis The instance.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsyncstatic bool vboxNetFltMaybeRediscovered(PVBOXNETFLTINS pThis)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync uint64_t Now = RTTimeNanoTS();
4fbca3751fe239da5934c23a783c3422618336e8vboxsync bool fRediscovered;
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync bool fDoIt;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Rediscovered already? Time to try again?
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync fRediscovered = !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync fDoIt = !fRediscovered
4fbca3751fe239da5934c23a783c3422618336e8vboxsync && !ASMAtomicUoReadBool(&pThis->fRediscoveryPending)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync && Now - ASMAtomicUoReadU64(&pThis->NanoTSLastRediscovery) > UINT64_C(5000000000); /* 5 sec */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync if (fDoIt)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync ASMAtomicWriteBool(&pThis->fRediscoveryPending, true);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Call the OS specific code to do the job.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Update the state when the call returns, that is everything except for
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * the fDisconnectedFromHost flag which the OS specific code shall set.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (fDoIt)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync {
c74832c7184337c330041742d88e6dacaa07b378vboxsync fRediscovered = vboxNetFltOsMaybeRediscovered(pThis);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(!fRediscovered || !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost));
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, RTTimeNanoTS());
4fbca3751fe239da5934c23a783c3422618336e8vboxsync ASMAtomicWriteBool(&pThis->fRediscoveryPending, false);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync if (fRediscovered)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync vboxNetFltPortOsSetActive(pThis, pThis->fActive);
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync }
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync return fRediscovered;
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync#ifdef RT_WITH_W64_UNWIND_HACK
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# if defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# define NETFLT_DECL_CALLBACK(type) DECLASM(DECLHIDDEN(type))
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync# define NETFLT_CALLBACK(_n) netfltNtWrap##_n
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortXmit)(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous)(PINTNETTRUNKIFPORT pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortGetMacAddress)(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsHostMac)(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortWaitForIdle)(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortSetActive)(PINTNETTRUNKIFPORT pIfPort, bool fActive);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease)(PINTNETTRUNKIFPORT pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRetain)(PINTNETTRUNKIFPORT pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRelease)(PINTNETTRUNKIFPORT pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# else
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# error "UNSUPPORTED (RT_WITH_W64_UNWIND_HACK)"
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# endif
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#else
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# define NETFLT_DECL_CALLBACK(type) static DECLCALLBACK(type)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync# define NETFLT_CALLBACK(_n) _n
4fbca3751fe239da5934c23a783c3422618336e8vboxsync#endif
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @copydoc INTNETTRUNKIFPORT::pfnXmit
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncNETFLT_DECL_CALLBACK(int) vboxNetFltPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync int rc = VINF_SUCCESS;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Input validation.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertPtr(pThis);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync AssertPtr(pSG);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync Assert(pThis->fActive);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Do a busy retain and then make sure we're connected to the interface
c74832c7184337c330041742d88e6dacaa07b378vboxsync * before invoking the OS specific code.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltRetain(pThis, true /* fBusy */);
c74832c7184337c330041742d88e6dacaa07b378vboxsync if ( !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)
c74832c7184337c330041742d88e6dacaa07b378vboxsync || vboxNetFltMaybeRediscovered(pThis))
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = vboxNetFltPortOsXmit(pThis, pSG, fDst);
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltRelease(pThis, true /* fBusy */);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync return rc;
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncNETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Input validation.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync AssertPtr(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->fActive);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Ask the OS specific code.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync return vboxNetFltPortOsIsPromiscuous(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncNETFLT_DECL_CALLBACK(void) vboxNetFltPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Input validation.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
d59a43b735abea2db17caa9b5661d2f5118f0e02vboxsync AssertPtr(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->fActive);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * Forward the question to the OS specific code.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltPortOsGetMacAddress(pThis, pMac);
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @copydoc INTNETTRUNKIFPORT::pfnIsHostMac
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncNETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsHostMac(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac)
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Input validation.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertPtr(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(pThis->fActive);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Ask the OS specific code.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync return vboxNetFltPortOsIsHostMac(pThis, pMac);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(int) vboxNetFltPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync int rc;
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Input validation.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync AssertPtr(pThis);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertReturn(!pThis->fActive, VERR_INVALID_STATE);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync /*
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Go to sleep on the semaphore after checking the busy count.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync vboxNetFltRetain(pThis, false /* fBusy */);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync rc = VINF_SUCCESS;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync while (pThis->cBusy && RT_SUCCESS(rc))
4fbca3751fe239da5934c23a783c3422618336e8vboxsync rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync vboxNetFltRelease(pThis, false /* fBusy */);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync return rc;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @copydoc INTNETTRUNKIFPORT::pfnSetActive
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncNETFLT_DECL_CALLBACK(bool) vboxNetFltPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * Input validation.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertPtr(pThis);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertPtr(pThis->pGlobals);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, false);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * We're assuming that the caller is serializing the calls, so we don't
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * have to be extremely careful here. Just update first and then call
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * the OS specific code, the update must be serialized for various reasons.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync if (ASMAtomicReadBool(&pThis->fActive) != fActive)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync {
4fbca3751fe239da5934c23a783c3422618336e8vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync ASMAtomicWriteBool(&pThis->fActive, fActive);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync vboxNetFltPortOsSetActive(pThis, fActive);
c74832c7184337c330041742d88e6dacaa07b378vboxsync }
c74832c7184337c330041742d88e6dacaa07b378vboxsync else
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync fActive = !fActive;
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync return !fActive;
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync}
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync/**
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync */
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsyncNETFLT_DECL_CALLBACK(void) vboxNetFltPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync{
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync /*
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync * Serious paranoia.
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertPtr(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertPtr(pThis->pGlobals);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync Assert(pThis->szName[0]);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->fActive);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync Assert(!pThis->fRediscoveryPending);
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync Assert(!pThis->cBusy);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Disconnect and release it.
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
b5d837811bf21f30a31748bbbcb28ee562bb2355vboxsync
6dea6d87ed79bc0994d314fed1c90431091e8820vboxsync vboxNetFltOsDisconnectIt(pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pThis->pSwitchPort = NULL;
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync#ifdef VBOXNETFLT_STATIC_CONFIG
b5d837811bf21f30a31748bbbcb28ee562bb2355vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
6dea6d87ed79bc0994d314fed1c90431091e8820vboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Unconnected);
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync#endif
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync vboxNetFltRelease(pThis, false /* fBusy */);
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync}
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync/**
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync * Destroy a device that has been disconnected from the switch.
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync *
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync * @returns true if the instance is destroyed, false otherwise.
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync * @param pThis The instance to be destroyed. This is
b5d837811bf21f30a31748bbbcb28ee562bb2355vboxsync * no longer valid when this function returns.
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync */
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsyncstatic bool vboxNetFltDestroyInstance(PVBOXNETFLTINS pThis)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
b5d837811bf21f30a31748bbbcb28ee562bb2355vboxsync PVBOXNETFLTGLOBALS pGlobals = pThis->pGlobals;
c74832c7184337c330041742d88e6dacaa07b378vboxsync uint32_t cRefs = ASMAtomicUoReadU32((uint32_t volatile *)&pThis->cRefs);
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync int rc;
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync LogFlow(("vboxNetFltDestroyInstance: pThis=%p (%s)\n", pThis, pThis->szName));
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync /*
e7925b345f17e5bd9f0c1cf3540b7d8573ec274fvboxsync * Validate the state.
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync */
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync#ifdef VBOXNETFLT_STATIC_CONFIG
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert( vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync || vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync#else
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting);
c74832c7184337c330041742d88e6dacaa07b378vboxsync#endif
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->fActive);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->fRediscoveryPending);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->cRefs);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->cBusy);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->pSwitchPort);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * Make sure the state is 'disconnecting' / 'destroying' and let the OS
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * specific code do its part of the cleanup outside the mutex.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#ifdef VBOXNETFLT_STATIC_CONFIG
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroying);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#else
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
c74832c7184337c330041742d88e6dacaa07b378vboxsync#endif
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTSemFastMutexRelease(pGlobals->hFastMtx);
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync vboxNetFltOsDeleteInstance(pThis);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Unlink the instance and free up its resources.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroyed);
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltUnlinkLocked(pGlobals, pThis);
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTSemFastMutexRelease(pGlobals->hFastMtx);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTSemEventDestroy(pThis->hEventIdle);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pThis->hEventIdle = NIL_RTSEMEVENT;
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTSpinlockDestroy(pThis->hSpinlock);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pThis->hSpinlock = NIL_RTSPINLOCK;
c74832c7184337c330041742d88e6dacaa07b378vboxsync RTMemFree(pThis);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync return true;
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Releases a reference to the specified instance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * This method will destroy the instance when the count reaches 0.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * It will also take care of decrementing the counter and idle wakeup.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pThis The instance.
cad8876b46f9e366c4a1007a40c27ca1df078950vboxsync * @param fBusy Whether the busy counter should be decremented too.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncDECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync uint32_t cRefs;
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * Paranoid Android.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertPtr(pThis);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
c74832c7184337c330041742d88e6dacaa07b378vboxsync AssertPtr(pThis->pGlobals);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->szName[0]);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Work the busy counter.
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (fBusy)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync {
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync cRefs = ASMAtomicDecU32(&pThis->cBusy);
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (!cRefs)
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync {
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync int rc = RTSemEventSignal(pThis->hEventIdle);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync AssertRC(rc);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync }
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync else
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(cRefs < UINT32_MAX / 2);
c74832c7184337c330041742d88e6dacaa07b378vboxsync }
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync * The object reference counting.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync cRefs = ASMAtomicDecU32(&pThis->cRefs);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync if (!cRefs)
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync vboxNetFltDestroyInstance(pThis);
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync else
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(cRefs < UINT32_MAX / 2);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync}
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync/**
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * @copydoc INTNETTRUNKIFPORT::pfnRetain
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsyncNETFLT_DECL_CALLBACK(void) vboxNetFltPortRelease(PINTNETTRUNKIFPORT pIfPort)
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync{
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync vboxNetFltRelease(pThis, false /* fBusy */);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync/**
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync * Retains a reference to the specified instance and a busy reference too.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync *
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pThis The instance.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param fBusy Whether the busy counter should be incremented as well.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncDECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy)
c74832c7184337c330041742d88e6dacaa07b378vboxsync{
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync uint32_t cRefs;
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync /*
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * Paranoid Android.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync AssertPtr(pThis);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
3e9c5c3e44de15c28695c7b570bc2551639187e3vboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync AssertPtr(pThis->pGlobals);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(pThis->szName[0]);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync /*
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync * Retain the object.
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync cRefs = ASMAtomicIncU32(&pThis->cRefs);
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(cRefs > 1 && cRefs < UINT32_MAX / 2);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Work the busy counter.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsync if (fBusy)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync {
4fbca3751fe239da5934c23a783c3422618336e8vboxsync cRefs = ASMAtomicIncU32(&pThis->cBusy);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync Assert(cRefs > 0 && cRefs < UINT32_MAX / 2);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync }
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync NOREF(cRefs);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
8b90eb0585fa16024709ca374c69f1eb5d5a5a7cvboxsync * @copydoc INTNETTRUNKIFPORT::pfnRetain
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsyncNETFLT_DECL_CALLBACK(void) vboxNetFltPortRetain(PINTNETTRUNKIFPORT pIfPort)
4fbca3751fe239da5934c23a783c3422618336e8vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync vboxNetFltRetain(pThis, false /* fBusy */);
4fbca3751fe239da5934c23a783c3422618336e8vboxsync}
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync/**
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * Connects the instance to the specified switch port.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Called while owning the lock. We're ASSUMING that the internal
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * networking code is already owning an recursive mutex, so, there
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * will be no deadlocks when vboxNetFltOsConnectIt calls back into
c74832c7184337c330041742d88e6dacaa07b378vboxsync * it for setting preferences.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync * @returns VBox status code.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pThis The instance.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param pSwitchPort The port on the internal network 'switch'.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync * @param ppIfPort Where to return our port interface.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync */
4fbca3751fe239da5934c23a783c3422618336e8vboxsyncstatic int vboxNetFltConnectIt(PVBOXNETFLTINS pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync{
4fbca3751fe239da5934c23a783c3422618336e8vboxsync int rc;
4fbca3751fe239da5934c23a783c3422618336e8vboxsync
4fbca3751fe239da5934c23a783c3422618336e8vboxsync /*
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Validate state.
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync */
6b98f92a0c2c5dd90d8496ee553e5763c4debd49vboxsync Assert(!pThis->fActive);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(!pThis->fRediscoveryPending);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(!pThis->cBusy);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync#ifdef VBOXNETFLT_STATIC_CONFIG
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync#else
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Initializing);
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync#endif
120ee2736ed70b5ce8b0b4dd73cc4f8b4b9416c1vboxsync
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync /*
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync * Do the job.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Note that we're calling the os stuff while owning the semaphore here.
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync */
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync pThis->pSwitchPort = pSwitchPort;
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync rc = vboxNetFltOsConnectIt(pThis);
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync if (RT_SUCCESS(rc))
c74832c7184337c330041742d88e6dacaa07b378vboxsync {
c74832c7184337c330041742d88e6dacaa07b378vboxsync vboxNetFltSetState(pThis, kVBoxNetFltInsState_Connected);
c74832c7184337c330041742d88e6dacaa07b378vboxsync *ppIfPort = &pThis->MyPort;
c74832c7184337c330041742d88e6dacaa07b378vboxsync }
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync else
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync pThis->pSwitchPort = NULL;
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync Assert(!pThis->fActive);
c74832c7184337c330041742d88e6dacaa07b378vboxsync return rc;
c74832c7184337c330041742d88e6dacaa07b378vboxsync}
fe813b3594039ba864493438e78ee0e7132bc445vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync
a40c05ca69e15a5efdd0796ef52e26ec0b1bc4d2vboxsync/**
c74832c7184337c330041742d88e6dacaa07b378vboxsync * Creates a new instance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync *
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync * The new instance will be in the suspended state in a dynamic config and in
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * the inactive in a static one.
6f8a1ec7a08590986f176ea072ad499630fe5b6evboxsync *
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * Called without owning the lock, but will request is several times.
4fbca3751fe239da5934c23a783c3422618336e8vboxsync *
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @returns VBox status code.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pGlobals The globals.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pszName The instance name.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pSwitchPort The port on the switch that we're connected with (dynamic only).
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param fNoPromisc Do not attempt going into promiscuous mode.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param pvContext Context argument for vboxNetFltOsInitInstance.
c74832c7184337c330041742d88e6dacaa07b378vboxsync * @param ppIfPort Where to store the pointer to our port interface (dynamic only).
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsyncstatic int vboxNetFltNewInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PINTNETTRUNKSWPORT pSwitchPort,
c74832c7184337c330041742d88e6dacaa07b378vboxsync bool fNoPromisc, void *pvContext, PINTNETTRUNKIFPORT *ppIfPort)
2c8ee291fb75c4a6f05df160f5d67f4e9ef1cabcvboxsync{
c74832c7184337c330041742d88e6dacaa07b378vboxsync /*
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync * Allocate and initialize a new instance before requesting the mutex.
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync int rc;
c74832c7184337c330041742d88e6dacaa07b378vboxsync size_t const cchName = strlen(pszName);
c74832c7184337c330041742d88e6dacaa07b378vboxsync PVBOXNETFLTINS pNew = (PVBOXNETFLTINS)RTMemAllocZ(RT_OFFSETOF(VBOXNETFLTINS, szName[cchName + 1]));
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (!pNew)
c74832c7184337c330041742d88e6dacaa07b378vboxsync return VERR_INTNET_FLT_IF_FAILED;
3cf2cf6eea0401233eece976db8cf31909f1f955vboxsync pNew->pNext = NULL;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnRetain = NETFLT_CALLBACK(vboxNetFltPortRetain);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnRelease = NETFLT_CALLBACK(vboxNetFltPortRelease);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnDisconnectAndRelease= NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnSetActive = NETFLT_CALLBACK(vboxNetFltPortSetActive);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnWaitForIdle = NETFLT_CALLBACK(vboxNetFltPortWaitForIdle);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnGetMacAddress = NETFLT_CALLBACK(vboxNetFltPortGetMacAddress);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnIsHostMac = NETFLT_CALLBACK(vboxNetFltPortIsHostMac);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnIsPromiscuous = NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.pfnXmit = NETFLT_CALLBACK(vboxNetFltPortXmit);
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->pSwitchPort = NULL;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->pGlobals = pGlobals;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->hSpinlock = NIL_RTSPINLOCK;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->enmState = kVBoxNetFltInsState_Initializing;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->fActive = false;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->fDisconnectedFromHost = false;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->fRediscoveryPending = false;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->fDisablePromiscuous = fNoPromisc;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->NanoTSLastRediscovery = INT64_MAX;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->cRefs = 1;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->cBusy = 0;
c74832c7184337c330041742d88e6dacaa07b378vboxsync pNew->hEventIdle = NIL_RTSEMEVENT;
c74832c7184337c330041742d88e6dacaa07b378vboxsync memcpy(pNew->szName, pszName, cchName + 1);
c74832c7184337c330041742d88e6dacaa07b378vboxsync
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = RTSpinlockCreate(&pNew->hSpinlock);
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (RT_SUCCESS(rc))
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync {
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync rc = RTSemEventCreate(&pNew->hEventIdle);
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync if (RT_SUCCESS(rc))
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync {
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync rc = vboxNetFltOsPreInitInstance(pNew);
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync if (RT_SUCCESS(rc))
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync {
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync /*
f01175bdcb5e36a1a89fca88f45d7ee7fb034ed6vboxsync * Insert the instance into the chain, checking for
c74832c7184337c330041742d88e6dacaa07b378vboxsync * duplicates first of course (race).
c74832c7184337c330041742d88e6dacaa07b378vboxsync */
c74832c7184337c330041742d88e6dacaa07b378vboxsync rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
c74832c7184337c330041742d88e6dacaa07b378vboxsync if (RT_SUCCESS(rc))
c74832c7184337c330041742d88e6dacaa07b378vboxsync {
if (!vboxNetFltFindInstanceLocked(pGlobals, pszName))
{
pNew->pNext = pGlobals->pInstanceHead;
pGlobals->pInstanceHead = pNew;
RTSemFastMutexRelease(pGlobals->hFastMtx);
/*
* Call the OS specific initialization code.
*/
rc = vboxNetFltOsInitInstance(pNew, pvContext);
RTSemFastMutexRequest(pGlobals->hFastMtx);
if (RT_SUCCESS(rc))
{
#ifdef VBOXNETFLT_STATIC_CONFIG
/*
* Static instances are unconnected at birth.
*/
Assert(!pSwitchPort);
pNew->enmState = kVBoxNetFltInsState_Unconnected;
RTSemFastMutexRelease(pGlobals->hFastMtx);
*ppIfPort = &pNew->MyPort;
return rc;
#else /* !VBOXNETFLT_STATIC_CONFIG */
/*
* Connect it as well, the OS specific bits has to be done outside
* the lock as they may call back to into intnet.
*/
rc = vboxNetFltConnectIt(pNew, pSwitchPort, ppIfPort);
if (RT_SUCCESS(rc))
{
RTSemFastMutexRelease(pGlobals->hFastMtx);
Assert(*ppIfPort == &pNew->MyPort);
return rc;
}
/* Bail out (failed). */
vboxNetFltOsDeleteInstance(pNew);
#endif /* !VBOXNETFLT_STATIC_CONFIG */
}
vboxNetFltUnlinkLocked(pGlobals, pNew);
}
else
rc = VERR_INTNET_FLT_IF_BUSY;
RTSemFastMutexRelease(pGlobals->hFastMtx);
}
}
RTSemEventDestroy(pNew->hEventIdle);
}
RTSpinlockDestroy(pNew->hSpinlock);
}
RTMemFree(pNew);
return rc;
}
#ifdef VBOXNETFLT_STATIC_CONFIG
/**
* Searches for the NetFlt instance by its name and creates the new one if not found.
*
* @returns VBox status code.
* @retval VINF_SUCCESS and *ppInstance if a new instance was created.
* @retval VINF_ALREADY_INITIALIZED and *ppInstance if an instance already exists.
*
* @param pGlobal Pointer to the globals.
* @param pszName The instance name.
* @param ppInstance Where to return the instance pointer on success.
* @param pvContext Context which needs to be passed along to vboxNetFltOsInitInstance.
*/
DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void *pvContext)
{
PINTNETTRUNKIFPORT pIfPort;
PVBOXNETFLTINS pCur;
VBOXNETFTLINSSTATE enmState;
int rc;
*ppInstance = NULL;
rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
AssertRCReturn(rc, rc);
/*
* Look for an existing instance in the list.
*
* There might be an existing one in the list if the driver was unbound
* while it was connected to an internal network. We're running into
* a destruction race that is a bit similar to the one in
* vboxNetFltFactoryCreateAndConnect, only the roles are reversed
* and we're not in a position to back down. Instead of backing down
* we'll delay a bit giving the other thread time to complete the
* destructor.
*/
pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
while (pCur)
{
uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
if (cRefs > 1)
{
enmState = vboxNetFltGetState(pCur);
switch (enmState)
{
case kVBoxNetFltInsState_Unconnected:
case kVBoxNetFltInsState_Connected:
case kVBoxNetFltInsState_Disconnecting:
if (pCur->fDisconnectedFromHost)
{
/* Wait for it to exit the transitional disconnecting
state. It might otherwise be running the risk of
upsetting the OS specific code... */
/** @todo This reconnect stuff should be serialized correctly for static
* devices. Shouldn't it? In the dynamic case we're using the INTNET
* outbound thrunk lock, but that doesn't quite cut it here, or does
* it? We could either transition to initializing or make a callback
* while owning the mutext here... */
if (enmState == kVBoxNetFltInsState_Disconnecting)
{
do
{
RTSemFastMutexRelease(pGlobals->hFastMtx);
RTThreadSleep(2); /* (2ms) */
RTSemFastMutexRequest(pGlobals->hFastMtx);
enmState = vboxNetFltGetState(pCur);
}
while (enmState == kVBoxNetFltInsState_Disconnecting);
AssertMsg(enmState == kVBoxNetFltInsState_Unconnected, ("%d\n", enmState));
Assert(pCur->fDisconnectedFromHost);
}
RTSemFastMutexRelease(pGlobals->hFastMtx);
*ppInstance = pCur;
return VINF_ALREADY_INITIALIZED;
}
/* fall thru */
default:
{
bool fDfH = pCur->fDisconnectedFromHost;
RTSemFastMutexRelease(pGlobals->hFastMtx);
vboxNetFltRelease(pCur, false /* fBusy */);
LogRel(("VBoxNetFlt: Huh? An instance of '%s' already exists! [pCur=%p cRefs=%d fDfH=%RTbool enmState=%d]\n",
pszName, pCur, cRefs - 1, fDfH, enmState));
*ppInstance = NULL;
return VERR_INTNET_FLT_IF_BUSY;
}
}
}
/* Zero references, it's being destroyed. Delay a bit so the destructor
can finish its work and try again. (vboxNetFltNewInstance will fail
with duplicate name if we don't.) */
# ifdef RT_STRICT
Assert(cRefs == 1);
enmState = vboxNetFltGetState(pCur);
AssertMsg( enmState == kVBoxNetFltInsState_Unconnected
|| enmState == kVBoxNetFltInsState_Disconnecting
|| enmState == kVBoxNetFltInsState_Destroyed, ("%d\n", enmState));
# endif
ASMAtomicDecU32(&pCur->cRefs);
RTSemFastMutexRelease(pGlobals->hFastMtx);
RTThreadSleep(2); /* (2ms) */
rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
AssertRCReturn(rc, rc);
/* try again */
pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
}
RTSemFastMutexRelease(pGlobals->hFastMtx);
/*
* Try create a new instance.
* (fNoPromisc is overridden in the vboxNetFltFactoryCreateAndConnect path, so pass true here.)
*/
rc = vboxNetFltNewInstance(pGlobals, pszName, NULL, true /* fNoPromisc */, pvContext, &pIfPort);
if (RT_SUCCESS(rc))
*ppInstance = IFPORT_2_VBOXNETFLTINS(pIfPort);
else
*ppInstance = NULL;
return rc;
}
#endif /* VBOXNETFLT_STATIC_CONFIG */
/**
* @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
*/
static DECLCALLBACK(int) vboxNetFltFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
PINTNETTRUNKSWPORT pSwitchPort, uint32_t fFlags,
PINTNETTRUNKIFPORT *ppIfPort)
{
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
PVBOXNETFLTINS pCur;
int rc;
LogFlow(("vboxNetFltFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
Assert(pGlobals->cFactoryRefs > 0);
AssertMsgReturn(!(fFlags & ~(INTNETTRUNKFACTORY_FLAG_NO_PROMISC)),
("%#x\n", fFlags), VERR_INVALID_PARAMETER);
/*
* Static: Find instance, check if busy, connect if not.
* Dynamic: Check for duplicate / busy interface instance.
*/
rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
AssertRCReturn(rc, rc);
//#if defined(VBOX_TAPMINIPORT) && defined(RT_OS_WINDOWS)
// /* temporary hack to pick up the first adapter */
// pCur = pGlobals->pInstanceHead; /** @todo Don't for get to remove this temporary hack... :-) */
//#else
pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
//#endif
if (pCur)
{
#ifdef VBOXNETFLT_STATIC_CONFIG
/* Try grab a reference. If the count had already reached zero we're racing the
destructor code and must back down. */
uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
if (cRefs > 1)
{
if (vboxNetFltGetState(pCur) == kVBoxNetFltInsState_Unconnected)
{
pCur->fDisablePromiscuous = !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC);
rc = vboxNetFltConnectIt(pCur, pSwitchPort, ppIfPort);
if (RT_SUCCESS(rc))
pCur = NULL; /* Don't release it, reference given to the caller. */
}
else
rc = VERR_INTNET_FLT_IF_BUSY;
}
else
{
Assert(cRefs == 1);
ASMAtomicDecU32(&pCur->cRefs);
pCur = NULL; /* nothing to release */
rc = VERR_INTNET_FLT_IF_NOT_FOUND;
}
RTSemFastMutexRelease(pGlobals->hFastMtx);
if (pCur)
vboxNetFltRelease(pCur, false /* fBusy */);
#else
rc = VERR_INTNET_FLT_IF_BUSY;
RTSemFastMutexRelease(pGlobals->hFastMtx);
#endif
LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
return rc;
}
RTSemFastMutexRelease(pGlobals->hFastMtx);
#ifdef VBOXNETFLT_STATIC_CONFIG
rc = VERR_INTNET_FLT_IF_NOT_FOUND;
#else
/*
* Dynamically create a new instance.
*/
rc = vboxNetFltNewInstance(pGlobals,
pszName,
pSwitchPort,
!!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC),
NULL,
ppIfPort);
#endif
LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
return rc;
}
/**
* @copydoc INTNETTRUNKFACTORY::pfnRelease
*/
static DECLCALLBACK(void) vboxNetFltFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
{
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
Assert(cRefs >= 0); NOREF(cRefs);
LogFlow(("vboxNetFltFactoryRelease: cRefs=%d (new)\n", cRefs));
}
/**
* Implements the SUPDRV component factor interface query method.
*
* @returns Pointer to an interface. NULL if not supported.
*
* @param pSupDrvFactory Pointer to the componet factory registration structure.
* @param pSession The session - unused.
* @param pszInterfaceUuid The factory interface id.
*/
static DECLCALLBACK(void *) vboxNetFltQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
{
PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, SupDrvFactory));
/*
* Convert the UUID strings and compare them.
*/
RTUUID UuidReq;
int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
if (RT_SUCCESS(rc))
{
if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
{
ASMAtomicIncS32(&pGlobals->cFactoryRefs);
return &pGlobals->TrunkFactory;
}
#ifdef LOG_ENABLED
/* log legacy queries */
/* else if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_V1_UUID_STR))
Log(("VBoxNetFlt: V1 factory query\n"));
*/
else
Log(("VBoxNetFlt: unknown factory interface query (%s)\n", pszInterfaceUuid));
#endif
}
else
Log(("VBoxNetFlt: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
return NULL;
}
/**
* Checks whether the VBoxNetFlt wossname can be unloaded.
*
* This will return false if someone is currently using the module.
*
* @returns true if it's relatively safe to unload it, otherwise false.
* @param pGlobals Pointer to the globals.
*/
DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals)
{
int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
bool fRc = !pGlobals->pInstanceHead
&& pGlobals->cFactoryRefs <= 0;
RTSemFastMutexRelease(pGlobals->hFastMtx);
AssertRC(rc);
return fRc;
}
/**
* Try to close the IDC connection to SUPDRV if established.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_WRONG_ORDER if we're busy.
*
* @param pGlobals Pointer to the globals.
*
* @sa vboxNetFltTryDeleteIdcAndGlobals()
*/
DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals)
{
int rc;
Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
/*
* Check before trying to deregister the factory.
*/
if (!vboxNetFltCanUnload(pGlobals))
return VERR_WRONG_ORDER;
if (!pGlobals->fIDCOpen)
rc = VINF_SUCCESS;
else
{
/*
* Disconnect from SUPDRV and check that nobody raced us,
* reconnect if that should happen.
*/
rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
AssertRC(rc);
if (!vboxNetFltCanUnload(pGlobals))
{
rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
AssertRC(rc);
return VERR_WRONG_ORDER;
}
SUPR0IdcClose(&pGlobals->SupDrvIDC);
pGlobals->fIDCOpen = false;
}
return rc;
}
/**
* Establishes the IDC connection to SUPDRV and registers our component factory.
*
* @returns VBox status code.
* @param pGlobals Pointer to the globals.
* @sa vboxNetFltInitGlobalsAndIdc().
*/
DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals)
{
int rc;
Assert(!pGlobals->fIDCOpen);
/*
* Establish a connection to SUPDRV and register our component factory.
*/
rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
if (RT_SUCCESS(rc))
{
rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
if (RT_SUCCESS(rc))
{
pGlobals->fIDCOpen = true;
Log(("VBoxNetFlt: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
return rc;
}
/* bail out. */
LogRel(("VBoxNetFlt: Failed to register component factory, rc=%Rrc\n", rc));
SUPR0IdcClose(&pGlobals->SupDrvIDC);
}
return rc;
}
/**
* Deletes the globals.
*
* This must be called after the IDC connection has been closed,
* see vboxNetFltTryDeleteIdc().
*
* @param pGlobals Pointer to the globals.
* @sa vboxNetFltTryDeleteIdcAndGlobals()
*/
DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals)
{
Assert(!pGlobals->fIDCOpen);
/*
* Release resources.
*/
RTSemFastMutexDestroy(pGlobals->hFastMtx);
pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
}
/**
* Initializes the globals.
*
* @returns VBox status code.
* @param pGlobals Pointer to the globals.
* @sa vboxNetFltInitGlobalsAndIdc().
*/
DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals)
{
/*
* Initialize the common portions of the structure.
*/
int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
if (RT_SUCCESS(rc))
{
pGlobals->pInstanceHead = NULL;
pGlobals->TrunkFactory.pfnRelease = vboxNetFltFactoryRelease;
pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetFltFactoryCreateAndConnect;
#if defined(RT_OS_WINDOWS) && defined(VBOX_TAPMINIPORT)
strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
#else
strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetFlt");
#endif
pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetFltQueryFactoryInterface;
pGlobals->fIDCOpen = false;
return rc;
}
return rc;
}
/**
* Called by the native part when the OS wants the driver to unload.
*
* @returns VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
*
* @param pGlobals Pointer to the globals.
*/
DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals)
{
int rc = vboxNetFltTryDeleteIdc(pGlobals);
if (RT_SUCCESS(rc))
vboxNetFltDeleteGlobals(pGlobals);
return rc;
}
/**
* Called by the native driver/kext module initialization routine.
*
* It will initialize the common parts of the globals, assuming the caller
* has already taken care of the OS specific bits, and establish the IDC
* connection to SUPDRV.
*
* @returns VBox status code.
* @param pGlobals Pointer to the globals.
*/
DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals)
{
/*
* Initialize the common portions of the structure.
*/
int rc = vboxNetFltInitGlobals(pGlobals);
if (RT_SUCCESS(rc))
{
rc = vboxNetFltInitIdc(pGlobals);
if (RT_SUCCESS(rc))
return rc;
/* bail out. */
vboxNetFltDeleteGlobals(pGlobals);
}
return rc;
}