de4157257515400c2c25373591135f110227b68cvboxsync/* $Id$ */
de4157257515400c2c25373591135f110227b68cvboxsync/** @file
de4157257515400c2c25373591135f110227b68cvboxsync * VBoxNetFlt - Network Filter Driver (Host), Darwin Specific Code.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * Copyright (C) 2006-2014 Oracle Corporation
de4157257515400c2c25373591135f110227b68cvboxsync *
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * available from http://www.virtualbox.org. This file is free software;
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * General Public License (GPL) as published by the Free Software
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b263fac6f6e7fa933c7bfb2a45d598fe8e458c09vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Header Files *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
de4157257515400c2c25373591135f110227b68cvboxsync/*
de4157257515400c2c25373591135f110227b68cvboxsync * Deal with conflicts first.
de4157257515400c2c25373591135f110227b68cvboxsync * PVM - BSD mess, that FreeBSD has correct a long time ago.
de4157257515400c2c25373591135f110227b68cvboxsync * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/types.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/param.h>
de4157257515400c2c25373591135f110227b68cvboxsync#undef PVM
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#include <IOKit/IOLib.h> /* Assert as function */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
de4157257515400c2c25373591135f110227b68cvboxsync#include <VBox/log.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <VBox/err.h>
6dd8f5023a9ba7588212331db90059553136fe33vboxsync#include <VBox/intnetinline.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <VBox/version.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/initterm.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/assert.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/spinlock.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/semaphore.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/process.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/alloc.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/alloca.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/time.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <iprt/net.h>
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync#include <iprt/thread.h>
de4157257515400c2c25373591135f110227b68cvboxsync
296a2ed12c9cf6041bb8006c2223605d2f56c6c9vboxsync#include "../../darwin/VBoxNetSend.h"
296a2ed12c9cf6041bb8006c2223605d2f56c6c9vboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#include <mach/kmod.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/conf.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/errno.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/ioccom.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/malloc.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/proc.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/socket.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/sockio.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/kern_event.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <net/kpi_interface.h>
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
de4157257515400c2c25373591135f110227b68cvboxsync#include <sys/kpi_mbuf.h>
de4157257515400c2c25373591135f110227b68cvboxsync#include <net/kpi_interfacefilter.h>
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_END
de4157257515400c2c25373591135f110227b68cvboxsync#include <net/if.h>
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#define VBOXNETFLT_OS_SPECFIC 1
de4157257515400c2c25373591135f110227b68cvboxsync#include "../VBoxNetFltInternal.h"
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Defined Constants And Macros *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
de4157257515400c2c25373591135f110227b68cvboxsync/** The maximum number of SG segments.
de4157257515400c2c25373591135f110227b68cvboxsync * Used to prevent stack overflow and similar bad stuff. */
de4157257515400c2c25373591135f110227b68cvboxsync#define VBOXNETFLT_DARWIN_MAX_SEGS 32
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#if 0
de4157257515400c2c25373591135f110227b68cvboxsync/** For testing extremely segmented frames. */
de4157257515400c2c25373591135f110227b68cvboxsync#define VBOXNETFLT_DARWIN_TEST_SEG_SIZE 14
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Internal Functions *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_BEGIN
de4157257515400c2c25373591135f110227b68cvboxsyncstatic kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData);
de4157257515400c2c25373591135f110227b68cvboxsyncstatic kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData);
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_END
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Structures and Typedefs *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * The mbuf tag data.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * We have to associate the ethernet header with each packet we're sending
de4157257515400c2c25373591135f110227b68cvboxsync * because things like icmp will inherit the tag it self so the tag along
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * isn't sufficient to identify our mbufs. For the icmp scenario the ethernet
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * header naturally changes before the packet is send pack, so let check it.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsynctypedef struct VBOXNETFLTTAG
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /** The ethernet header of the outgoing frame. */
de4157257515400c2c25373591135f110227b68cvboxsync RTNETETHERHDR EthHdr;
de4157257515400c2c25373591135f110227b68cvboxsync} VBOXNETFLTTAG;
de4157257515400c2c25373591135f110227b68cvboxsync/** Pointer to a VBoxNetFlt mbuf tag. */
de4157257515400c2c25373591135f110227b68cvboxsynctypedef VBOXNETFLTTAG *PVBOXNETFLTTAG;
de4157257515400c2c25373591135f110227b68cvboxsync/** Pointer to a const VBoxNetFlt mbuf tag. */
de4157257515400c2c25373591135f110227b68cvboxsynctypedef VBOXNETFLTTAG const *PCVBOXNETFLTTAG;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/*******************************************************************************
de4157257515400c2c25373591135f110227b68cvboxsync* Global Variables *
de4157257515400c2c25373591135f110227b68cvboxsync*******************************************************************************/
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Declare the module stuff.
de4157257515400c2c25373591135f110227b68cvboxsync */
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_BEGIN
de4157257515400c2c25373591135f110227b68cvboxsyncextern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
de4157257515400c2c25373591135f110227b68cvboxsyncextern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncKMOD_EXPLICIT_DECL(VBoxNetFlt, VBOX_VERSION_STRING, _start, _stop)
de4157257515400c2c25373591135f110227b68cvboxsyncDECLHIDDEN(kmod_start_func_t *) _realmain = VBoxNetFltDarwinStart;
de4157257515400c2c25373591135f110227b68cvboxsyncDECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxNetFltDarwinStop;
de4157257515400c2c25373591135f110227b68cvboxsyncDECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_END
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * The (common) global data.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/** The unique tag id for this module.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * This is basically a unique string hash that lives on until reboot.
de4157257515400c2c25373591135f110227b68cvboxsync * It is used for tagging mbufs. */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic mbuf_tag_id_t g_idTag;
de4157257515400c2c25373591135f110227b68cvboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync/** The offset of the struct ifnet::if_pcount variable.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @remarks Initial value is valid for Lion and earlier. We adjust it on attach
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * for later releases. */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
de4157257515400c2c25373591135f110227b68cvboxsync + sizeof(u_long) /*if_refcnt*/;
de4157257515400c2c25373591135f110227b68cvboxsync/** Macro for accessing ifnet::if_pcount. */
de4157257515400c2c25373591135f110227b68cvboxsync#define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync/** The size of area of ifnet structure we try to locate if_pcount in. */
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync#define VBOXNETFLT_DARWIN_IFNET_SIZE 256
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync/** Indicates whether g_offIfNetPCount has been adjusted already (no point in
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * doing it more than once). */
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsyncstatic bool g_fNetPCountFound = false;
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync/**
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * Change the promiscuous setting and try spot the changed in @a pIfNet.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync *
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @returns Offset of potential p_count field.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @param pIfNet The interface we're attaching to.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @param iPromisc Whether to enable (1) or disable (0) promiscuous mode.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync *
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @note This implementation relies on if_pcount to be aligned on sizeof(int).
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync */
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsyncstatic unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync{
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)];
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync memcpy(aiSavedState, pIfNet, sizeof(aiSavedState));
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync ifnet_set_promiscuous(pIfNet, iPromisc);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync int const iDiff = iPromisc ? 1 : -1;
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync /*
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * We assume that ifnet structure will never have less members in front of if_pcount
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * than it used to have in Lion. If this turns out to be false assumption we will
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * have to start from zero offset.
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync */
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++)
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff)
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync return i * sizeof(int);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync return 0;
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync}
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync/**
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * Detect and adjust the offset of ifnet::if_pcount.
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync *
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync * @param pIfNet The interface we're attaching to.
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync */
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsyncstatic void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync{
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync if (g_fNetPCountFound)
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync return;
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync /*
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * It would be nice to use locking at this point, but it is not available via KPI.
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * This is why we try several times. At each attempt we modify if_pcount four times
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync * to rule out false detections.
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync */
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync unsigned offTry1, offTry2, offTry3, offTry4;
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync for (int iAttempt = 0; iAttempt < 3; iAttempt++)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync {
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync offTry2 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync if (offTry1 == offTry2 && offTry2 == offTry3 && offTry3 == offTry4)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync {
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync if (g_offIfNetPCount != offTry1)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync {
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount));
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync g_offIfNetPCount = offTry1;
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync g_fNetPCountFound = true;
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync }
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync break;
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync }
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync }
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync if (g_offIfNetPCount != offTry1)
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1));
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
3d3defbbc6d46d5bbb80fca96508b609ef983948vboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Start the kernel module.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync int rc;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Initialize IPRT and find our module tag id.
de4157257515400c2c25373591135f110227b68cvboxsync * (IPRT is shared with VBoxDrv, it creates the loggers.)
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync rc = RTR0Init(0);
de4157257515400c2c25373591135f110227b68cvboxsync if (RT_SUCCESS(rc))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Log(("VBoxNetFltDarwinStart\n"));
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = mbuf_tag_id_find("org.VirtualBox.kext.VBoxFltDrv", &g_idTag);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Initialize the globals and connect to the support driver.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
de4157257515400c2c25373591135f110227b68cvboxsync * for establishing the connect to the support driver.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
de4157257515400c2c25373591135f110227b68cvboxsync if (RT_SUCCESS(rc))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxFltDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
de4157257515400c2c25373591135f110227b68cvboxsync return KMOD_RETURN_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxFltDrv: failed to initialize device extension (rc=%d)\n", rc));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxFltDrv: mbuf_tag_id_find failed, err=%d\n", err));
de4157257515400c2c25373591135f110227b68cvboxsync RTR0Term();
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync printf("VBoxFltDrv: failed to initialize IPRT (rc=%d)\n", rc);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
de4157257515400c2c25373591135f110227b68cvboxsync return KMOD_RETURN_FAILURE;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Stop the kernel module.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync Log(("VBoxNetFltDarwinStop\n"));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Refuse to unload if anyone is currently using the filter driver.
de4157257515400c2c25373591135f110227b68cvboxsync * This is important as I/O kit / xnu will to be able to do usage
de4157257515400c2c25373591135f110227b68cvboxsync * tracking for us!
de4157257515400c2c25373591135f110227b68cvboxsync */
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync int rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
de4157257515400c2c25373591135f110227b68cvboxsync if (RT_FAILURE(rc))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Log(("VBoxNetFltDarwinStop - failed, busy.\n"));
de4157257515400c2c25373591135f110227b68cvboxsync return KMOD_RETURN_FAILURE;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Undo the work done during start (in reverse order).
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync RTR0Term();
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return KMOD_RETURN_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Reads and retains the host interface handle.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns The handle, NULL if detached.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncDECLINLINE(ifnet_t) vboxNetFltDarwinRetainIfNet(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_t pIfNet = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Be careful here to avoid problems racing the detached callback.
de4157257515400c2c25373591135f110227b68cvboxsync */
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync RTSpinlockAcquire(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
de4157257515400c2c25373591135f110227b68cvboxsync {
c7ff622115966b69b482bd2896662e40d823b22fvboxsync pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_reference(pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync }
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync RTSpinlockRelease(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return pIfNet;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Release the host interface handle previously retained
de4157257515400c2c25373591135f110227b68cvboxsync * by vboxNetFltDarwinRetainIfNet.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pIfNet The vboxNetFltDarwinRetainIfNet return value, NULL is fine.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncDECLINLINE(void) vboxNetFltDarwinReleaseIfNet(PVBOXNETFLTINS pThis, ifnet_t pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_release(pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Checks whether this is an mbuf created by vboxNetFltDarwinMBufFromSG,
de4157257515400c2c25373591135f110227b68cvboxsync * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns true / false accordingly.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pMBuf The mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pvFrame The frame pointer, optional.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncDECLINLINE(bool) vboxNetFltDarwinMBufIsOur(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Lookup the tag set by vboxNetFltDarwinMBufFromSG.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync PCVBOXNETFLTTAG pTagData;
de4157257515400c2c25373591135f110227b68cvboxsync size_t cbTagData;
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = mbuf_tag_find(pMBuf, g_idTag, 0 /* type */, &cbTagData, (void **)&pTagData);
de4157257515400c2c25373591135f110227b68cvboxsync if (err)
de4157257515400c2c25373591135f110227b68cvboxsync return false;
de4157257515400c2c25373591135f110227b68cvboxsync AssertReturn(cbTagData == sizeof(*pTagData), false);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Dig out the ethernet header from the mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
de4157257515400c2c25373591135f110227b68cvboxsync if (!pEthHdr)
de4157257515400c2c25373591135f110227b68cvboxsync pEthHdr = (PCRTNETETHERHDR)mbuf_pkthdr_header(pMBuf);
de4157257515400c2c25373591135f110227b68cvboxsync if (!pEthHdr)
de4157257515400c2c25373591135f110227b68cvboxsync pEthHdr = (PCRTNETETHERHDR)mbuf_data(pMBuf);
de4157257515400c2c25373591135f110227b68cvboxsync /* ASSUMING that there is enough data to work on! */
de4157257515400c2c25373591135f110227b68cvboxsync if ( pEthHdr->DstMac.au8[0] != pTagData->EthHdr.DstMac.au8[0]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->DstMac.au8[1] != pTagData->EthHdr.DstMac.au8[1]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->DstMac.au8[2] != pTagData->EthHdr.DstMac.au8[2]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->DstMac.au8[3] != pTagData->EthHdr.DstMac.au8[3]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->DstMac.au8[4] != pTagData->EthHdr.DstMac.au8[4]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->DstMac.au8[5] != pTagData->EthHdr.DstMac.au8[5]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[0] != pTagData->EthHdr.SrcMac.au8[0]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[1] != pTagData->EthHdr.SrcMac.au8[1]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[2] != pTagData->EthHdr.SrcMac.au8[2]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[3] != pTagData->EthHdr.SrcMac.au8[3]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[4] != pTagData->EthHdr.SrcMac.au8[4]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->SrcMac.au8[5] != pTagData->EthHdr.SrcMac.au8[5]
de4157257515400c2c25373591135f110227b68cvboxsync || pEthHdr->EtherType != pTagData->EthHdr.EtherType)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Log3(("tagged, but the ethernet header has changed\n"));
de4157257515400c2c25373591135f110227b68cvboxsync return false;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return true;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Internal worker that create a darwin mbuf for a (scatter/)gather list.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns Pointer to the mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pSG The (scatter/)gather list.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic mbuf_t vboxNetFltDarwinMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
de4157257515400c2c25373591135f110227b68cvboxsync{
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /// @todo future? mbuf_how_t How = preemption enabled ? MBUF_DONTWAIT : MBUF_WAITOK;
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_how_t How = MBUF_WAITOK;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * We need some way of getting back to our instance data when
de4157257515400c2c25373591135f110227b68cvboxsync * the mbuf is freed, so use pvUserData for this.
de4157257515400c2c25373591135f110227b68cvboxsync * -- this is not relevant anylonger! --
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync Assert(!pSG->pvUserData || pSG->pvUserData == pThis);
de4157257515400c2c25373591135f110227b68cvboxsync Assert(!pSG->pvUserData2);
de4157257515400c2c25373591135f110227b68cvboxsync pSG->pvUserData = pThis;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Allocate a packet and copy over the data.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * Using mbuf_attachcluster() here would've been nice but there are two
de4157257515400c2c25373591135f110227b68cvboxsync * issues with it: (1) it's 10.5.x only, and (2) the documentation indicates
de4157257515400c2c25373591135f110227b68cvboxsync * that it's not supposed to be used for really external buffers. The 2nd
de4157257515400c2c25373591135f110227b68cvboxsync * point might be argued against considering that the only m_clattach user
de4157257515400c2c25373591135f110227b68cvboxsync * is mallocs memory for the ext mbuf and not doing what's stated in the docs.
de4157257515400c2c25373591135f110227b68cvboxsync * However, it's hard to tell if these m_clattach buffers actually makes it
de4157257515400c2c25373591135f110227b68cvboxsync * to the NICs or not, and even if they did, the NIC would need the physical
de4157257515400c2c25373591135f110227b68cvboxsync * addresses for the pages they contain and might end up copying the data
de4157257515400c2c25373591135f110227b68cvboxsync * to a new mbuf anyway.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * So, in the end it's better to just do it the simple way that will work
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * 100%, even if it involves some extra work (alloc + copy) we really wished
de4157257515400c2c25373591135f110227b68cvboxsync * to avoid.
bc1484a141a5638d1c26739e77e8a47c77dc2da3vboxsync *
bc1484a141a5638d1c26739e77e8a47c77dc2da3vboxsync * Note. We can't make use of the physical addresses on darwin because the
bc1484a141a5638d1c26739e77e8a47c77dc2da3vboxsync * way the mbuf / cluster stuff works (see mbuf_data_to_physical and
9641e02b95aae845618ab3021d1e23a92e3be4abvboxsync * mcl_to_paddr).
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_t pPkt = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = mbuf_allocpacket(How, pSG->cbTotal, NULL, &pPkt);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* Skip zero sized memory buffers (paranoia). */
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_t pCur = pPkt;
de4157257515400c2c25373591135f110227b68cvboxsync while (pCur && !mbuf_maxlen(pCur))
de4157257515400c2c25373591135f110227b68cvboxsync pCur = mbuf_next(pCur);
de4157257515400c2c25373591135f110227b68cvboxsync Assert(pCur);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* Set the required packet header attributes. */
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_pkthdr_setlen(pPkt, pSG->cbTotal);
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* Special case the single buffer copy. */
de4157257515400c2c25373591135f110227b68cvboxsync if ( mbuf_next(pCur)
de4157257515400c2c25373591135f110227b68cvboxsync && mbuf_maxlen(pCur) >= pSG->cbTotal)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_setlen(pCur, pSG->cbTotal);
9127c416edfd6f9266e387f7abd7aa9904eecbc9vboxsync IntNetSgRead(pSG, mbuf_data(pCur));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* Multi buffer copying. */
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync size_t cbLeft = pSG->cbTotal;
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync size_t offSrc = 0;
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync while (cbLeft > 0 && pCur)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync size_t cb = mbuf_maxlen(pCur);
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync if (cb > cbLeft)
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync cb = cbLeft;
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_setlen(pCur, cb);
9127c416edfd6f9266e387f7abd7aa9904eecbc9vboxsync IntNetSgReadEx(pSG, offSrc, cb, mbuf_data(pCur));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* advance */
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync offSrc += cb;
108e1eed4a717344ddcec74bcae3ea793fda4724vboxsync cbLeft -= cb;
de4157257515400c2c25373591135f110227b68cvboxsync pCur = mbuf_next(pCur);
de4157257515400c2c25373591135f110227b68cvboxsync }
5564cd8c0930563b66d9ebcf709a31ad75bfe709vboxsync Assert(cbLeft == 0);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Tag the packet and return successfully.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync PVBOXNETFLTTAG pTagData;
de4157257515400c2c25373591135f110227b68cvboxsync err = mbuf_tag_allocate(pPkt, g_idTag, 0 /* type */, sizeof(VBOXNETFLTTAG) /* tag len */, How, (void **)&pTagData);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Assert(pSG->aSegs[0].cb >= sizeof(pTagData->EthHdr));
de4157257515400c2c25373591135f110227b68cvboxsync memcpy(&pTagData->EthHdr, pSG->aSegs[0].pv, sizeof(pTagData->EthHdr));
de4157257515400c2c25373591135f110227b68cvboxsync return pPkt;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* bailout: */
de4157257515400c2c25373591135f110227b68cvboxsync AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_freem(pPkt);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
de4157257515400c2c25373591135f110227b68cvboxsync pSG->pvUserData = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return NULL;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Calculates the number of segments required to represent the mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns Number of segments.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pMBuf The mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pvFrame The frame pointer, optional.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncDECLINLINE(unsigned) vboxNetFltDarwinMBufCalcSGSegs(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Count the buffers in the chain.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync unsigned cSegs = 0;
de4157257515400c2c25373591135f110227b68cvboxsync for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
de4157257515400c2c25373591135f110227b68cvboxsync if (mbuf_len(pCur))
de4157257515400c2c25373591135f110227b68cvboxsync cSegs++;
de4157257515400c2c25373591135f110227b68cvboxsync else if ( !cSegs
de4157257515400c2c25373591135f110227b68cvboxsync && pvFrame
de4157257515400c2c25373591135f110227b68cvboxsync && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
de4157257515400c2c25373591135f110227b68cvboxsync cSegs++;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#ifdef PADD_RUNT_FRAMES_FROM_HOST
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Add one buffer if the total is less than the ethernet minimum 60 bytes.
de4157257515400c2c25373591135f110227b68cvboxsync * This may allocate a segment too much if the ethernet header is separated,
de4157257515400c2c25373591135f110227b68cvboxsync * but that shouldn't harm us much.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if (mbuf_pkthdr_len(pMBuf) < 60)
de4157257515400c2c25373591135f110227b68cvboxsync cSegs++;
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
de4157257515400c2c25373591135f110227b68cvboxsync /* maximize the number of segments. */
de4157257515400c2c25373591135f110227b68cvboxsync cSegs = RT_MAX(VBOXNETFLT_DARWIN_MAX_SEGS - 1, cSegs);
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return cSegs ? cSegs : 1;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Initializes a SG list from an mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns Number of segments.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pMBuf The mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pSG The SG.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pvFrame The frame pointer, optional.
de4157257515400c2c25373591135f110227b68cvboxsync * @param cSegs The number of segments allocated for the SG.
de4157257515400c2c25373591135f110227b68cvboxsync * This should match the number in the mbuf exactly!
de4157257515400c2c25373591135f110227b68cvboxsync * @param fSrc The source of the frame.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncDECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
6dd8f5023a9ba7588212331db90059553136fe33vboxsync * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync unsigned iSeg = 0;
9127c416edfd6f9266e387f7abd7aa9904eecbc9vboxsync IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
de4157257515400c2c25373591135f110227b68cvboxsync for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync size_t cbSeg = mbuf_len(pCur);
de4157257515400c2c25373591135f110227b68cvboxsync if (cbSeg)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync void *pvSeg = mbuf_data(pCur);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* deal with pvFrame */
de4157257515400c2c25373591135f110227b68cvboxsync if (!iSeg && pvFrame && pvFrame != pvSeg)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync void *pvStart = mbuf_datastart(pMBuf);
de4157257515400c2c25373591135f110227b68cvboxsync uintptr_t offSeg = (uintptr_t)pvSeg - (uintptr_t)pvStart;
de4157257515400c2c25373591135f110227b68cvboxsync uintptr_t offSegEnd = offSeg + cbSeg;
de4157257515400c2c25373591135f110227b68cvboxsync Assert(pvStart && pvSeg && offSeg < mbuf_maxlen(pMBuf) && offSegEnd <= mbuf_maxlen(pMBuf)); NOREF(offSegEnd);
de4157257515400c2c25373591135f110227b68cvboxsync uintptr_t offFrame = (uintptr_t)pvFrame - (uintptr_t)pvStart;
de4157257515400c2c25373591135f110227b68cvboxsync if (RT_LIKELY(offFrame < offSeg))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync pvSeg = pvFrame;
de4157257515400c2c25373591135f110227b68cvboxsync cbSeg += offSeg - offFrame;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync AssertMsgFailed(("pvFrame=%p pvStart=%p pvSeg=%p offSeg=%p cbSeg=%#zx offSegEnd=%p offFrame=%p maxlen=%#zx\n",
de4157257515400c2c25373591135f110227b68cvboxsync pvFrame, pvStart, pvSeg, offSeg, cbSeg, offSegEnd, offFrame, mbuf_maxlen(pMBuf)));
de4157257515400c2c25373591135f110227b68cvboxsync pvFrame = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync AssertBreak(iSeg < cSegs);
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cbTotal += cbSeg;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].cb = cbSeg;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].pv = pvSeg;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
de4157257515400c2c25373591135f110227b68cvboxsync iSeg++;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync /* The pvFrame might be in a now empty buffer. */
de4157257515400c2c25373591135f110227b68cvboxsync else if ( !iSeg
de4157257515400c2c25373591135f110227b68cvboxsync && pvFrame
de4157257515400c2c25373591135f110227b68cvboxsync && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync cbSeg = (uintptr_t)mbuf_datastart(pMBuf) + mbuf_maxlen(pMBuf) - (uintptr_t)pvFrame;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cbTotal += cbSeg;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].cb = cbSeg;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].pv = pvFrame;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
de4157257515400c2c25373591135f110227b68cvboxsync iSeg++;
de4157257515400c2c25373591135f110227b68cvboxsync pvFrame = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync Assert(iSeg && iSeg <= cSegs);
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cSegsUsed = iSeg;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#ifdef PADD_RUNT_FRAMES_FROM_HOST
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Add a trailer if the frame is too small.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * Since we're getting to the packet before it is framed, it has not
de4157257515400c2c25373591135f110227b68cvboxsync * yet been padded. The current solution is to add a segment pointing
de4157257515400c2c25373591135f110227b68cvboxsync * to a buffer containing all zeros and pray that works for all frames...
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync AssertReturnVoid(iSeg < cSegs);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync static uint8_t const s_abZero[128] = {0};
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSeg].cb = 60 - pSG->cbTotal;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cbTotal = 60;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cSegsUsed++;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Redistribute the segments.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if (pSG->cSegsUsed < pSG->cSegsAlloc)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* copy the segments to the end. */
de4157257515400c2c25373591135f110227b68cvboxsync int iSrc = pSG->cSegsUsed;
de4157257515400c2c25373591135f110227b68cvboxsync int iDst = pSG->cSegsAlloc;
de4157257515400c2c25373591135f110227b68cvboxsync while (iSrc > 0)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync iDst--;
de4157257515400c2c25373591135f110227b68cvboxsync iSrc--;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iDst] = pSG->aSegs[iSrc];
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* create small segments from the start. */
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cSegsUsed = pSG->cSegsAlloc;
de4157257515400c2c25373591135f110227b68cvboxsync iSrc = iDst;
de4157257515400c2c25373591135f110227b68cvboxsync iDst = 0;
de4157257515400c2c25373591135f110227b68cvboxsync while ( iDst < iSrc
de4157257515400c2c25373591135f110227b68cvboxsync && iDst < pSG->cSegsAlloc)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iDst].Phys = NIL_RTHCPHYS;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iDst].pv = pSG->aSegs[iSrc].pv;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iDst].cb = RT_MIN(pSG->aSegs[iSrc].cb, VBOXNETFLT_DARWIN_TEST_SEG_SIZE);
de4157257515400c2c25373591135f110227b68cvboxsync if (pSG->aSegs[iDst].cb != pSG->aSegs[iSrc].cb)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSrc].cb -= pSG->aSegs[iDst].cb;
de4157257515400c2c25373591135f110227b68cvboxsync pSG->aSegs[iSrc].pv = (uint8_t *)pSG->aSegs[iSrc].pv + pSG->aSegs[iDst].cb;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else if (++iSrc >= pSG->cSegsAlloc)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync pSG->cSegsUsed = iDst + 1;
de4157257515400c2c25373591135f110227b68cvboxsync break;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync iDst++;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync AssertMsg(!pvFrame, ("pvFrame=%p pMBuf=%p iSeg=%d\n", pvFrame, pMBuf, iSeg));
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync/**
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync * Helper for determining whether the host wants the interface to be
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync * promiscuous.
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync */
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsyncstatic bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync{
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync bool fRc = false;
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync if (pIfNet)
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync {
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync /* gather the data */
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync uint16_t fIf = ifnet_flags(pIfNet);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync /* calc the return. */
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync fRc = (fIf & IFF_PROMISC)
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync && cPromisc > fSetPromiscuous;
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync }
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync return fRc;
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync}
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @see iff_detached_func in the darwin kpi.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
de4157257515400c2c25373591135f110227b68cvboxsync uint64_t NanoTS = RTTimeSystemNanoTS();
de4157257515400c2c25373591135f110227b68cvboxsync LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
de4157257515400c2c25373591135f110227b68cvboxsync pThis, NanoTS, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync Assert(!pThis->fDisconnectedFromHost);
de4157257515400c2c25373591135f110227b68cvboxsync Assert(!pThis->fRediscoveryPending);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * If we've put it into promiscuous mode, undo that now. If we don't
de4157257515400c2c25373591135f110227b68cvboxsync * the if_pcount will go all wrong when it's replugged.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_set_promiscuous(pIfNet, 0);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * We carefully take the spinlock and increase the interface reference
de4157257515400c2c25373591135f110227b68cvboxsync * behind it in order to avoid problematic races with the detached callback.
de4157257515400c2c25373591135f110227b68cvboxsync */
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync RTSpinlockAcquire(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync
c7ff622115966b69b482bd2896662e40d823b22fvboxsync pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
de4157257515400c2c25373591135f110227b68cvboxsync int cPromisc = VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
de4157257515400c2c25373591135f110227b68cvboxsync
060db742c6ecb43560beec8754fb5b9c13bd7856vboxsync ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
060db742c6ecb43560beec8754fb5b9c13bd7856vboxsync ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
de4157257515400c2c25373591135f110227b68cvboxsync pThis->u.s.fSetPromiscuous = false;
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
de4157257515400c2c25373591135f110227b68cvboxsync
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync RTSpinlockRelease(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_release(pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @see iff_ioctl_func in the darwin kpi.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
de4157257515400c2c25373591135f110227b68cvboxsync LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Update fOtherPromiscuous.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync /** @todo we'll have to find the offset of if_pcount to get this right! */
de4157257515400c2c25373591135f110227b68cvboxsync //if (uCmd == SIOCSIFFLAGS)
de4157257515400c2c25373591135f110227b68cvboxsync //{
de4157257515400c2c25373591135f110227b68cvboxsync //
de4157257515400c2c25373591135f110227b68cvboxsync //}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * We didn't handle it, continue processing.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(eProtocol);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(uCmd);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pvArg);
de4157257515400c2c25373591135f110227b68cvboxsync return EOPNOTSUPP;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @see iff_event_func in the darwin kpi.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
de4157257515400c2c25373591135f110227b68cvboxsync LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(eProtocol);
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(pEvMsg);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Watch out for the interface going online / offline.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if ( VALID_PTR(pThis)
de4157257515400c2c25373591135f110227b68cvboxsync && VALID_PTR(pEvMsg)
de4157257515400c2c25373591135f110227b68cvboxsync && pEvMsg->vendor_code == KEV_VENDOR_APPLE
de4157257515400c2c25373591135f110227b68cvboxsync && pEvMsg->kev_class == KEV_NETWORK_CLASS
de4157257515400c2c25373591135f110227b68cvboxsync && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync if (pThis->u.s.pIfNet == pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync if (pEvMsg->event_code == KEV_DL_LINK_ON)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* failed to bring it online. */
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = ifnet_set_promiscuous(pIfNet, 1);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
de4157257515400c2c25373591135f110227b68cvboxsync && !(ifnet_flags(pIfNet) & IFF_PROMISC))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* Try fix the inconsistency. */
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
de4157257515400c2c25373591135f110227b68cvboxsync if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
de4157257515400c2c25373591135f110227b68cvboxsync pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync/** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync/** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: pThis->u.s.pIfNet=%p pIfNet=%p (%d)\n", pThis->u.s.pIfNet, pIfNet, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else if (VALID_PTR(pEvMsg))
de4157257515400c2c25373591135f110227b68cvboxsync Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
de4157257515400c2c25373591135f110227b68cvboxsync pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns 0 or EJUSTRETURN.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pMBuf The mbuf.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pvFrame The start of the frame, optional.
de4157257515400c2c25373591135f110227b68cvboxsync * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
de4157257515400c2c25373591135f110227b68cvboxsync * @param eProtocol The protocol.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync uint32_t fSrc, protocol_family_t eProtocol)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Drop it immediately?
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
de4157257515400c2c25373591135f110227b68cvboxsync pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
de4157257515400c2c25373591135f110227b68cvboxsync if (!pMBuf)
de4157257515400c2c25373591135f110227b68cvboxsync return 0;
de4157257515400c2c25373591135f110227b68cvboxsync#if 0 /* debugging lost icmp packets */
de4157257515400c2c25373591135f110227b68cvboxsync if (mbuf_pkthdr_len(pMBuf) > 0x300)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
de4157257515400c2c25373591135f110227b68cvboxsync Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync#endif
de4157257515400c2c25373591135f110227b68cvboxsync if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
de4157257515400c2c25373591135f110227b68cvboxsync return 0;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Active? Retain the instance and increment the busy counter.
de4157257515400c2c25373591135f110227b68cvboxsync */
d61e6f5b11d9031623420bd7ed3013477d8f402dvboxsync if (!vboxNetFltTryRetainBusyActive(pThis))
de4157257515400c2c25373591135f110227b68cvboxsync return 0;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Finalize out-bound packets since the stack puts off finalizing
de4157257515400c2c25373591135f110227b68cvboxsync * TCP/IP checksums as long as possible.
de4157257515400c2c25373591135f110227b68cvboxsync * ASSUMES this only applies to outbound IP packets.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if ( (fSrc & INTNETTRUNKDIR_HOST)
de4157257515400c2c25373591135f110227b68cvboxsync && eProtocol == PF_INET)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Assert(!pvFrame);
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync bool fDropIt = false;
de4157257515400c2c25373591135f110227b68cvboxsync unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
de4157257515400c2c25373591135f110227b68cvboxsync if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
de4157257515400c2c25373591135f110227b68cvboxsync vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
de4157257515400c2c25373591135f110227b68cvboxsync
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
de4157257515400c2c25373591135f110227b68cvboxsync if (fDropIt)
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync {
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync /*
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync * Check if this interface is in promiscuous mode. We should not drop
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync * any packets before they get to the driver as it passes them to tap
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync * callbacks in order for BPF to work properly.
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync */
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync if (vboxNetFltDarwinIsPromiscuous(pThis))
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync fDropIt = false;
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync else
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync mbuf_freem(pMBuf);
22f8a6ac3bcf3e3c8aa1f275097d6f7ce392195bvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync vboxNetFltRelease(pThis, true /* fBusy */);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return fDropIt ? EJUSTRETURN : 0;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * From the host.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @see iff_output_func in the darwin kpi.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /** @todo there was some note about the ethernet header here or something like that... */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(eProtocol);
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync NOREF(pIfNet);
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * From the wire.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @see iff_input_func in the darwin kpi.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync NOREF(eProtocol);
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync NOREF(pIfNet);
1fa6e123716bad466cddb3c25227616b1c87ab83vboxsync return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync/** A worker thread for vboxNetFltSendDummy(). */
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsyncstatic DECLCALLBACK(int) vboxNetFltSendDummyWorker(RTTHREAD hThreadSelf, void *pvUser)
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync{
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync Assert(pvUser);
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync ifnet_t pIfNet = (ifnet_t)pvUser;
3b17f75f3a22443ca2d6f44be26de8335ff4bcd3vboxsync return VBoxNetSendDummy(pIfNet);
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync}
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync/**
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync * Prevent GUI icon freeze issue when VirtualBoxVM process terminates.
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync *
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync * This function is a workaround for stuck-in-dock issue. The idea here is to
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync * send a dummy packet to an interface from the context of a kernel thread.
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync * Therefore, an XNU's receive thread (which is created as a result if we are
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync * the first who is communicating with the interface) will be associated with
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync * the kernel thread instead of VirtualBoxVM process.
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync *
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync * @param pIfNet Interface to be used to send data.
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync */
01a4740f743b850f8337988627cd58dd2abbb81bvboxsyncstatic void vboxNetFltSendDummy(ifnet_t pIfNet)
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync{
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync RTTHREAD hThread;
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync int rc = RTThreadCreate(&hThread, vboxNetFltSendDummyWorker, (void *)pIfNet, 0,
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "DummyThread");
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync if (RT_SUCCESS(rc))
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync {
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL);
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync LogFlow(("vboxNetFltSendDummy: a dummy packet has been successfully sent in order to prevent stuck-in-dock issue\n"));
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync }
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync else
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync LogFlow(("vboxNetFltSendDummy: unable to send dummy packet in order to prevent stuck-in-dock issue\n"));
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync}
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync/**
de4157257515400c2c25373591135f110227b68cvboxsync * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * @returns VBox status code.
de4157257515400c2c25373591135f110227b68cvboxsync * @param pThis The instance.
de4157257515400c2c25373591135f110227b68cvboxsync * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
de4157257515400c2c25373591135f110227b68cvboxsync * flood the release log.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsyncstatic int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Locate the interface first.
de4157257515400c2c25373591135f110227b68cvboxsync *
de4157257515400c2c25373591135f110227b68cvboxsync * The pIfNet member is updated before iflt_attach is called and used
de4157257515400c2c25373591135f110227b68cvboxsync * to deal with the hypothetical case where someone rips out the
de4157257515400c2c25373591135f110227b68cvboxsync * interface immediately after our iflt_attach call.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_t pIfNet = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync if (err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync Assert(err == ENXIO);
de4157257515400c2c25373591135f110227b68cvboxsync if (!fRediscovery)
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
de4157257515400c2c25373591135f110227b68cvboxsync return VERR_INTNET_FLT_IF_NOT_FOUND;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync RTSpinlockAcquire(pThis->hSpinlock);
c7ff622115966b69b482bd2896662e40d823b22fvboxsync ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync RTSpinlockRelease(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync /* Adjust g_offIfNetPCount as it varies for different versions of xnu. */
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync vboxNetFltDarwinDetectPCountOffset(pIfNet);
b341bcbb341f01cceb1767c9e45fb6ecf161a46evboxsync
3d04769b3e1089409cc9461b6ba3d7af25183aadvboxsync /* Prevent stuck-in-dock issue by associating interface receive thread with kernel thread. */
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync vboxNetFltSendDummy(pIfNet);
01a4740f743b850f8337988627cd58dd2abbb81bvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Get the mac address while we still have a valid ifnet reference.
de4157257515400c2c25373591135f110227b68cvboxsync */
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
de4157257515400c2c25373591135f110227b68cvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Try attach the filter.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync struct iff_filter RegRec;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_cookie = pThis;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_name = "VBoxNetFlt";
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_protocol = 0;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_input = vboxNetFltDarwinIffInput;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_output = vboxNetFltDarwinIffOutput;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_event = vboxNetFltDarwinIffEvent;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
de4157257515400c2c25373591135f110227b68cvboxsync RegRec.iff_detached = vboxNetFltDarwinIffDetached;
de4157257515400c2c25373591135f110227b68cvboxsync interface_filter_t pIfFilter = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
de4157257515400c2c25373591135f110227b68cvboxsync Assert(err || pIfFilter);
de4157257515400c2c25373591135f110227b68cvboxsync
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync RTSpinlockAcquire(pThis->hSpinlock);
c7ff622115966b69b482bd2896662e40d823b22fvboxsync pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet && !err)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
c7ff622115966b69b482bd2896662e40d823b22fvboxsync ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
de4157257515400c2c25373591135f110227b68cvboxsync pIfNet = NULL; /* don't dereference it */
de4157257515400c2c25373591135f110227b68cvboxsync }
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync RTSpinlockRelease(pThis->hSpinlock);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync /* Report capabilities. */
d61e6f5b11d9031623420bd7ed3013477d8f402dvboxsync if ( !pIfNet
d61e6f5b11d9031623420bd7ed3013477d8f402dvboxsync && vboxNetFltTryRetainBusyNotDisconnected(pThis))
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync {
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync Assert(pThis->pSwitchPort);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
e594418ef5afa60279edfc12b2c3ac8763701585vboxsync pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
d61e6f5b11d9031623420bd7ed3013477d8f402dvboxsync vboxNetFltRelease(pThis, true /*fBusy*/);
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /* Release the interface on failure. */
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_release(pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync int rc = RTErrConvertFromErrno(err);
de4157257515400c2c25373591135f110227b68cvboxsync if (RT_SUCCESS(rc))
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync LogRel(("VBoxFltDrv: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.MacAddr), &pThis->u.s.MacAddr));
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
de4157257515400c2c25373591135f110227b68cvboxsync return rc;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncbool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
de4157257515400c2c25373591135f110227b68cvboxsync return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsyncint vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
de4157257515400c2c25373591135f110227b68cvboxsync{
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync NOREF(pvIfData);
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync
de4157257515400c2c25373591135f110227b68cvboxsync int rc = VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Create a mbuf for the gather list and push it onto the wire.
24b0ae67111ae6f584f6c5d7fa0bcd5f83479157vboxsync *
24b0ae67111ae6f584f6c5d7fa0bcd5f83479157vboxsync * Note! If the interface is in the promiscuous mode we need to send the
24b0ae67111ae6f584f6c5d7fa0bcd5f83479157vboxsync * packet down the stack so it reaches the driver and Berkeley
5981e6935987b08737b730b63a41acc1dd696377vboxsync * Packet Filter (see @bugref{5817}).
bbe8696c67e29320d46ccca627b580540fc2c541vboxsync */
bbe8696c67e29320d46ccca627b580540fc2c541vboxsync if ((fDst & INTNETTRUNKDIR_WIRE) || vboxNetFltDarwinIsPromiscuous(pThis))
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
de4157257515400c2c25373591135f110227b68cvboxsync if (pMBuf)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
de4157257515400c2c25373591135f110227b68cvboxsync if (err)
de4157257515400c2c25373591135f110227b68cvboxsync rc = RTErrConvertFromErrno(err);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync rc = VERR_NO_MEMORY;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Create a mbuf for the gather list and push it onto the host stack.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync if (fDst & INTNETTRUNKDIR_HOST)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
de4157257515400c2c25373591135f110227b68cvboxsync if (pMBuf)
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /* This is what IONetworkInterface::inputPacket does. */
de4157257515400c2c25373591135f110227b68cvboxsync unsigned const cbEthHdr = 14;
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_pkthdr_setheader(pMBuf, mbuf_data(pMBuf));
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_pkthdr_setlen(pMBuf, mbuf_pkthdr_len(pMBuf) - cbEthHdr);
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_setdata(pMBuf, (uint8_t *)mbuf_data(pMBuf) + cbEthHdr, mbuf_len(pMBuf) - cbEthHdr);
de4157257515400c2c25373591135f110227b68cvboxsync mbuf_pkthdr_setrcvif(pMBuf, pIfNet); /* will crash without this. */
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync errno_t err = ifnet_input(pIfNet, pMBuf, NULL);
de4157257515400c2c25373591135f110227b68cvboxsync if (err)
de4157257515400c2c25373591135f110227b68cvboxsync rc = RTErrConvertFromErrno(err);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync rc = VERR_NO_MEMORY;
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return rc;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncvoid vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfNet)
de4157257515400c2c25373591135f110227b68cvboxsync {
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync if (pThis->fDisablePromiscuous)
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync /*
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * Promiscuous mode should not be used (wireless), we just need to
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * make sure the interface is up.
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync */
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync if (fActive)
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync {
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync u_int16_t fIf = ifnet_flags(pIfNet);
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync {
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync }
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync }
fa0eb85ad87e090d4fa2890b5c51d01d26c1aaf3vboxsync }
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync {
de4157257515400c2c25373591135f110227b68cvboxsync /*
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * This api is a bit weird, the best reference is the code.
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync *
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Also, we have a bit or race conditions wrt the maintenance of
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
de4157257515400c2c25373591135f110227b68cvboxsync */
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync u_int16_t fIf;
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (fActive)
de4157257515400c2c25373591135f110227b68cvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Assert(!pThis->u.s.fSetPromiscuous);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync errno_t err = ENETDOWN;
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
de4157257515400c2c25373591135f110227b68cvboxsync
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync /*
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * Try bring the interface up and running if it's down.
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync */
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync fIf = ifnet_flags(pIfNet);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
de4157257515400c2c25373591135f110227b68cvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (!err)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync err = err2;
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync fIf = ifnet_flags(pIfNet);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync /*
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * Is it already up? If it isn't, leave it to the link event or
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync */
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync err = ifnet_set_promiscuous(pIfNet, 1);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync pThis->u.s.fSetPromiscuous = err == 0;
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (!err)
de4157257515400c2c25373591135f110227b68cvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync /* check if it actually worked, this stuff is not always behaving well. */
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (!err)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (!err)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync else
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync else
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync else if (!err)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (err)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
de4157257515400c2c25373591135f110227b68cvboxsync }
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync else
de4157257515400c2c25373591135f110227b68cvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync if (pThis->u.s.fSetPromiscuous)
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync {
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync errno_t err = ifnet_set_promiscuous(pIfNet, 0);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync AssertMsg(!err, ("%d\n", err)); NOREF(err);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync }
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync pThis->u.s.fSetPromiscuous = false;
de4157257515400c2c25373591135f110227b68cvboxsync
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync fIf = ifnet_flags(pIfNet);
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
b3e99f2211c081f3d92cc7649f1dc50317bccc0fvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
de4157257515400c2c25373591135f110227b68cvboxsync }
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncint vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /* Nothing to do here. */
de4157257515400c2c25373591135f110227b68cvboxsync return VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncint vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /* Nothing to do here. */
de4157257515400c2c25373591135f110227b68cvboxsync return VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncvoid vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync interface_filter_t pIfFilter;
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Carefully obtain the interface filter reference and detach it.
de4157257515400c2c25373591135f110227b68cvboxsync */
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync RTSpinlockAcquire(pThis->hSpinlock);
c7ff622115966b69b482bd2896662e40d823b22fvboxsync pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfFilter)
060db742c6ecb43560beec8754fb5b9c13bd7856vboxsync ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
c0b6af690ad705bddfa87c643b89770a7a0aaf5avboxsync RTSpinlockRelease(pThis->hSpinlock);
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync if (pIfFilter)
de4157257515400c2c25373591135f110227b68cvboxsync iflt_detach(pIfFilter);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
2e9654cb261eaac1deb1f590be4ba71594755a03vboxsyncint vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
de4157257515400c2c25373591135f110227b68cvboxsync{
2e9654cb261eaac1deb1f590be4ba71594755a03vboxsync NOREF(pvContext);
de4157257515400c2c25373591135f110227b68cvboxsync return vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsyncint vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
de4157257515400c2c25373591135f110227b68cvboxsync{
de4157257515400c2c25373591135f110227b68cvboxsync /*
de4157257515400c2c25373591135f110227b68cvboxsync * Init the darwin specific members.
de4157257515400c2c25373591135f110227b68cvboxsync */
de4157257515400c2c25373591135f110227b68cvboxsync pThis->u.s.pIfNet = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync pThis->u.s.pIfFilter = NULL;
de4157257515400c2c25373591135f110227b68cvboxsync pThis->u.s.fSetPromiscuous = false;
de4157257515400c2c25373591135f110227b68cvboxsync pThis->u.s.fNeedSetPromiscuous = false;
5f809eed9fe4d8f7f317e8102657eb877fd5fbdavboxsync //pThis->u.s.MacAddr = {0};
de4157257515400c2c25373591135f110227b68cvboxsync
de4157257515400c2c25373591135f110227b68cvboxsync return VINF_SUCCESS;
de4157257515400c2c25373591135f110227b68cvboxsync}
de4157257515400c2c25373591135f110227b68cvboxsync
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsyncvoid vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync{
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync}
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsyncint vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync{
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync /* Nothing to do */
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync return VINF_SUCCESS;
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync}
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsyncint vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync{
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync /* Nothing to do */
586cf6585d0e6aa3bef888eeff116bf82d18cd83vboxsync NOREF(pThis); NOREF(pvIfData);
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync return VINF_SUCCESS;
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync}
69c51855f3b700d7b26a81f1d7dfed523097b7e2vboxsync