[11935] | 1 | /* $Id: VBoxNetFlt-darwin.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxNetFlt - Network Filter Driver (Host), Darwin Specific Code.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[76553] | 7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[11935] | 8 | *
|
---|
[15527] | 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[69250] | 16 | *
|
---|
| 17 | * The contents of this file may alternatively be used under the terms
|
---|
| 18 | * of the Common Development and Distribution License Version 1.0
|
---|
| 19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
| 20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
| 21 | * CDDL are applicable instead of those of the GPL.
|
---|
| 22 | *
|
---|
| 23 | * You may elect to license modified versions of this file under the
|
---|
| 24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[11935] | 25 | */
|
---|
| 26 |
|
---|
[57358] | 27 |
|
---|
| 28 | /*********************************************************************************************************************************
|
---|
| 29 | * Header Files *
|
---|
| 30 | *********************************************************************************************************************************/
|
---|
[57252] | 31 | #define LOG_GROUP LOG_GROUP_NET_FLT_DRV
|
---|
| 32 | #include "../../../Runtime/r0drv/darwin/the-darwin-kernel.h"
|
---|
[11935] | 33 |
|
---|
| 34 | #include <VBox/log.h>
|
---|
| 35 | #include <VBox/err.h>
|
---|
[28025] | 36 | #include <VBox/intnetinline.h>
|
---|
[11935] | 37 | #include <VBox/version.h>
|
---|
| 38 | #include <iprt/initterm.h>
|
---|
| 39 | #include <iprt/assert.h>
|
---|
| 40 | #include <iprt/spinlock.h>
|
---|
| 41 | #include <iprt/semaphore.h>
|
---|
| 42 | #include <iprt/process.h>
|
---|
| 43 | #include <iprt/alloc.h>
|
---|
| 44 | #include <iprt/alloca.h>
|
---|
| 45 | #include <iprt/time.h>
|
---|
| 46 | #include <iprt/net.h>
|
---|
[50321] | 47 | #include <iprt/thread.h>
|
---|
[11935] | 48 |
|
---|
[50410] | 49 | #include "../../darwin/VBoxNetSend.h"
|
---|
| 50 |
|
---|
[11935] | 51 | #include <mach/kmod.h>
|
---|
| 52 | #include <sys/conf.h>
|
---|
| 53 | #include <sys/errno.h>
|
---|
| 54 | #include <sys/ioccom.h>
|
---|
[55652] | 55 | #include <sys/filio.h>
|
---|
[11935] | 56 | #include <sys/malloc.h>
|
---|
| 57 | #include <sys/proc.h>
|
---|
| 58 | #include <sys/socket.h>
|
---|
| 59 | #include <sys/sockio.h>
|
---|
| 60 | #include <sys/kern_event.h>
|
---|
| 61 | #include <net/kpi_interface.h>
|
---|
[20374] | 62 | RT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
|
---|
[11935] | 63 | #include <sys/kpi_mbuf.h>
|
---|
| 64 | #include <net/kpi_interfacefilter.h>
|
---|
[20374] | 65 | RT_C_DECLS_END
|
---|
[55652] | 66 |
|
---|
| 67 | #include <sys/kpi_socket.h>
|
---|
[11935] | 68 | #include <net/if.h>
|
---|
[55652] | 69 | #include <net/if_var.h>
|
---|
[57904] | 70 | RT_C_DECLS_BEGIN
|
---|
| 71 | #include <net/bpf.h>
|
---|
| 72 | RT_C_DECLS_END
|
---|
[55652] | 73 | #include <netinet/in.h>
|
---|
| 74 | #include <netinet/in_var.h>
|
---|
| 75 | #include <netinet6/in6_var.h>
|
---|
[11935] | 76 |
|
---|
| 77 | #define VBOXNETFLT_OS_SPECFIC 1
|
---|
| 78 | #include "../VBoxNetFltInternal.h"
|
---|
| 79 |
|
---|
| 80 |
|
---|
[57358] | 81 | /*********************************************************************************************************************************
|
---|
| 82 | * Defined Constants And Macros *
|
---|
| 83 | *********************************************************************************************************************************/
|
---|
[11935] | 84 | /** The maximum number of SG segments.
|
---|
| 85 | * Used to prevent stack overflow and similar bad stuff. */
|
---|
| 86 | #define VBOXNETFLT_DARWIN_MAX_SEGS 32
|
---|
| 87 |
|
---|
| 88 | #if 0
|
---|
| 89 | /** For testing extremely segmented frames. */
|
---|
| 90 | #define VBOXNETFLT_DARWIN_TEST_SEG_SIZE 14
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
[56281] | 93 | /* XXX: hidden undef #ifdef __APPLE__ */
|
---|
| 94 | #define VBOX_IN_LOOPBACK(addr) (((addr) & IN_CLASSA_NET) == 0x7f000000)
|
---|
| 95 | #define VBOX_IN_LINKLOCAL(addr) (((addr) & IN_CLASSB_NET) == 0xa9fe0000)
|
---|
[11935] | 96 |
|
---|
[56281] | 97 |
|
---|
| 98 |
|
---|
[57358] | 99 | /*********************************************************************************************************************************
|
---|
| 100 | * Internal Functions *
|
---|
| 101 | *********************************************************************************************************************************/
|
---|
[20374] | 102 | RT_C_DECLS_BEGIN
|
---|
[11935] | 103 | static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData);
|
---|
| 104 | static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData);
|
---|
[55652] | 105 |
|
---|
| 106 | static void vboxNetFltDarwinSysSockUpcall(socket_t pSysSock, void *pvData, int fWait);
|
---|
[20374] | 107 | RT_C_DECLS_END
|
---|
[11935] | 108 |
|
---|
| 109 |
|
---|
[57358] | 110 | /*********************************************************************************************************************************
|
---|
| 111 | * Structures and Typedefs *
|
---|
| 112 | *********************************************************************************************************************************/
|
---|
[11935] | 113 | /**
|
---|
| 114 | * The mbuf tag data.
|
---|
| 115 | *
|
---|
| 116 | * We have to associate the ethernet header with each packet we're sending
|
---|
| 117 | * because things like icmp will inherit the tag it self so the tag along
|
---|
[33540] | 118 | * isn't sufficient to identify our mbufs. For the icmp scenario the ethernet
|
---|
| 119 | * header naturally changes before the packet is send pack, so let check it.
|
---|
[11935] | 120 | */
|
---|
| 121 | typedef struct VBOXNETFLTTAG
|
---|
| 122 | {
|
---|
| 123 | /** The ethernet header of the outgoing frame. */
|
---|
| 124 | RTNETETHERHDR EthHdr;
|
---|
| 125 | } VBOXNETFLTTAG;
|
---|
| 126 | /** Pointer to a VBoxNetFlt mbuf tag. */
|
---|
| 127 | typedef VBOXNETFLTTAG *PVBOXNETFLTTAG;
|
---|
| 128 | /** Pointer to a const VBoxNetFlt mbuf tag. */
|
---|
| 129 | typedef VBOXNETFLTTAG const *PCVBOXNETFLTTAG;
|
---|
| 130 |
|
---|
| 131 |
|
---|
[57358] | 132 | /*********************************************************************************************************************************
|
---|
| 133 | * Global Variables *
|
---|
| 134 | *********************************************************************************************************************************/
|
---|
[11935] | 135 | /**
|
---|
| 136 | * Declare the module stuff.
|
---|
| 137 | */
|
---|
[20374] | 138 | RT_C_DECLS_BEGIN
|
---|
[11935] | 139 | extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
|
---|
| 140 | extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
|
---|
| 141 |
|
---|
| 142 | KMOD_EXPLICIT_DECL(VBoxNetFlt, VBOX_VERSION_STRING, _start, _stop)
|
---|
| 143 | DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxNetFltDarwinStart;
|
---|
| 144 | DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxNetFltDarwinStop;
|
---|
| 145 | DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
|
---|
[20374] | 146 | RT_C_DECLS_END
|
---|
[11935] | 147 |
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * The (common) global data.
|
---|
| 151 | */
|
---|
| 152 | static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
|
---|
| 153 |
|
---|
| 154 | /** The unique tag id for this module.
|
---|
[33540] | 155 | * This is basically a unique string hash that lives on until reboot.
|
---|
[11935] | 156 | * It is used for tagging mbufs. */
|
---|
| 157 | static mbuf_tag_id_t g_idTag;
|
---|
| 158 |
|
---|
[50761] | 159 | /** The offset of the struct ifnet::if_pcount variable.
|
---|
| 160 | * @remarks Initial value is valid for Lion and earlier. We adjust it on attach
|
---|
| 161 | * for later releases. */
|
---|
[11935] | 162 | static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
|
---|
| 163 | + sizeof(u_long) /*if_refcnt*/;
|
---|
| 164 | /** Macro for accessing ifnet::if_pcount. */
|
---|
| 165 | #define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
|
---|
[50747] | 166 | /** The size of area of ifnet structure we try to locate if_pcount in. */
|
---|
| 167 | #define VBOXNETFLT_DARWIN_IFNET_SIZE 256
|
---|
[50761] | 168 | /** Indicates whether g_offIfNetPCount has been adjusted already (no point in
|
---|
| 169 | * doing it more than once). */
|
---|
| 170 | static bool g_fNetPCountFound = false;
|
---|
[11935] | 171 |
|
---|
[50761] | 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Change the promiscuous setting and try spot the changed in @a pIfNet.
|
---|
| 175 | *
|
---|
| 176 | * @returns Offset of potential p_count field.
|
---|
| 177 | * @param pIfNet The interface we're attaching to.
|
---|
| 178 | * @param iPromisc Whether to enable (1) or disable (0) promiscuous mode.
|
---|
| 179 | *
|
---|
| 180 | * @note This implementation relies on if_pcount to be aligned on sizeof(int).
|
---|
[50747] | 181 | */
|
---|
| 182 | static unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc)
|
---|
| 183 | {
|
---|
[50761] | 184 | int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)];
|
---|
| 185 | memcpy(aiSavedState, pIfNet, sizeof(aiSavedState));
|
---|
[50747] | 186 |
|
---|
| 187 | ifnet_set_promiscuous(pIfNet, iPromisc);
|
---|
| 188 |
|
---|
[50761] | 189 | int const iDiff = iPromisc ? 1 : -1;
|
---|
| 190 |
|
---|
[50747] | 191 | /*
|
---|
| 192 | * We assume that ifnet structure will never have less members in front of if_pcount
|
---|
| 193 | * than it used to have in Lion. If this turns out to be false assumption we will
|
---|
| 194 | * have to start from zero offset.
|
---|
| 195 | */
|
---|
[50761] | 196 | for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++)
|
---|
| 197 | if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff)
|
---|
| 198 | return i * sizeof(int);
|
---|
[50747] | 199 |
|
---|
[50761] | 200 | return 0;
|
---|
[50747] | 201 | }
|
---|
| 202 |
|
---|
[50761] | 203 |
|
---|
[11935] | 204 | /**
|
---|
[50747] | 205 | * Detect and adjust the offset of ifnet::if_pcount.
|
---|
[50761] | 206 | *
|
---|
| 207 | * @param pIfNet The interface we're attaching to.
|
---|
[50747] | 208 | */
|
---|
| 209 | static void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet)
|
---|
| 210 | {
|
---|
[50761] | 211 | if (g_fNetPCountFound)
|
---|
| 212 | return;
|
---|
| 213 |
|
---|
[50747] | 214 | /*
|
---|
| 215 | * It would be nice to use locking at this point, but it is not available via KPI.
|
---|
| 216 | * This is why we try several times. At each attempt we modify if_pcount four times
|
---|
| 217 | * to rule out false detections.
|
---|
| 218 | */
|
---|
[50761] | 219 | unsigned offTry1, offTry2, offTry3, offTry4;
|
---|
| 220 | for (int iAttempt = 0; iAttempt < 3; iAttempt++)
|
---|
[50747] | 221 | {
|
---|
| 222 | offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
|
---|
| 223 | offTry2 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
|
---|
| 224 | offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
|
---|
| 225 | offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
|
---|
| 226 | if (offTry1 == offTry2 && offTry2 == offTry3 && offTry3 == offTry4)
|
---|
| 227 | {
|
---|
| 228 | if (g_offIfNetPCount != offTry1)
|
---|
| 229 | {
|
---|
| 230 | Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount));
|
---|
| 231 | g_offIfNetPCount = offTry1;
|
---|
[50761] | 232 | g_fNetPCountFound = true;
|
---|
[50747] | 233 | }
|
---|
| 234 | break;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | if (g_offIfNetPCount != offTry1)
|
---|
| 239 | LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1));
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[50761] | 242 |
|
---|
[50747] | 243 | /**
|
---|
[11935] | 244 | * Start the kernel module.
|
---|
| 245 | */
|
---|
| 246 | static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData)
|
---|
| 247 | {
|
---|
[63512] | 248 | RT_NOREF(pKModInfo, pvData);
|
---|
[11935] | 249 |
|
---|
| 250 | /*
|
---|
| 251 | * Initialize IPRT and find our module tag id.
|
---|
| 252 | * (IPRT is shared with VBoxDrv, it creates the loggers.)
|
---|
| 253 | */
|
---|
[63512] | 254 | int rc = RTR0Init(0);
|
---|
[11935] | 255 | if (RT_SUCCESS(rc))
|
---|
| 256 | {
|
---|
| 257 | Log(("VBoxNetFltDarwinStart\n"));
|
---|
| 258 | errno_t err = mbuf_tag_id_find("org.VirtualBox.kext.VBoxFltDrv", &g_idTag);
|
---|
| 259 | if (!err)
|
---|
| 260 | {
|
---|
| 261 | /*
|
---|
| 262 | * Initialize the globals and connect to the support driver.
|
---|
| 263 | *
|
---|
| 264 | * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
|
---|
| 265 | * for establishing the connect to the support driver.
|
---|
| 266 | */
|
---|
| 267 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
[17184] | 268 | rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
|
---|
[11935] | 269 | if (RT_SUCCESS(rc))
|
---|
| 270 | {
|
---|
| 271 | LogRel(("VBoxFltDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
|
---|
| 272 | return KMOD_RETURN_SUCCESS;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | LogRel(("VBoxFltDrv: failed to initialize device extension (rc=%d)\n", rc));
|
---|
| 276 | }
|
---|
| 277 | else
|
---|
| 278 | LogRel(("VBoxFltDrv: mbuf_tag_id_find failed, err=%d\n", err));
|
---|
| 279 | RTR0Term();
|
---|
| 280 | }
|
---|
| 281 | else
|
---|
| 282 | printf("VBoxFltDrv: failed to initialize IPRT (rc=%d)\n", rc);
|
---|
| 283 |
|
---|
| 284 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
| 285 | return KMOD_RETURN_FAILURE;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * Stop the kernel module.
|
---|
| 291 | */
|
---|
| 292 | static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData)
|
---|
| 293 | {
|
---|
[63512] | 294 | RT_NOREF(pKModInfo, pvData);
|
---|
[11935] | 295 | Log(("VBoxNetFltDarwinStop\n"));
|
---|
| 296 |
|
---|
| 297 | /*
|
---|
| 298 | * Refuse to unload if anyone is currently using the filter driver.
|
---|
| 299 | * This is important as I/O kit / xnu will to be able to do usage
|
---|
| 300 | * tracking for us!
|
---|
| 301 | */
|
---|
[17184] | 302 | int rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
|
---|
[11935] | 303 | if (RT_FAILURE(rc))
|
---|
| 304 | {
|
---|
| 305 | Log(("VBoxNetFltDarwinStop - failed, busy.\n"));
|
---|
| 306 | return KMOD_RETURN_FAILURE;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | * Undo the work done during start (in reverse order).
|
---|
| 311 | */
|
---|
| 312 | memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
|
---|
| 313 |
|
---|
| 314 | RTR0Term();
|
---|
| 315 |
|
---|
| 316 | return KMOD_RETURN_SUCCESS;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * Reads and retains the host interface handle.
|
---|
| 322 | *
|
---|
| 323 | * @returns The handle, NULL if detached.
|
---|
| 324 | * @param pThis
|
---|
| 325 | */
|
---|
| 326 | DECLINLINE(ifnet_t) vboxNetFltDarwinRetainIfNet(PVBOXNETFLTINS pThis)
|
---|
| 327 | {
|
---|
| 328 | ifnet_t pIfNet = NULL;
|
---|
| 329 |
|
---|
| 330 | /*
|
---|
| 331 | * Be careful here to avoid problems racing the detached callback.
|
---|
| 332 | */
|
---|
[40806] | 333 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
[11935] | 334 | if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
|
---|
| 335 | {
|
---|
[30111] | 336 | pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
|
---|
[11935] | 337 | if (pIfNet)
|
---|
| 338 | ifnet_reference(pIfNet);
|
---|
| 339 | }
|
---|
[52618] | 340 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
[11935] | 341 |
|
---|
| 342 | return pIfNet;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 | /**
|
---|
| 347 | * Release the host interface handle previously retained
|
---|
| 348 | * by vboxNetFltDarwinRetainIfNet.
|
---|
| 349 | *
|
---|
| 350 | * @param pThis The instance.
|
---|
| 351 | * @param pIfNet The vboxNetFltDarwinRetainIfNet return value, NULL is fine.
|
---|
| 352 | */
|
---|
| 353 | DECLINLINE(void) vboxNetFltDarwinReleaseIfNet(PVBOXNETFLTINS pThis, ifnet_t pIfNet)
|
---|
| 354 | {
|
---|
| 355 | NOREF(pThis);
|
---|
| 356 | if (pIfNet)
|
---|
| 357 | ifnet_release(pIfNet);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 | /**
|
---|
| 362 | * Checks whether this is an mbuf created by vboxNetFltDarwinMBufFromSG,
|
---|
| 363 | * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
|
---|
| 364 | *
|
---|
| 365 | * @returns true / false accordingly.
|
---|
| 366 | * @param pThis The instance.
|
---|
| 367 | * @param pMBuf The mbuf.
|
---|
| 368 | * @param pvFrame The frame pointer, optional.
|
---|
| 369 | */
|
---|
| 370 | DECLINLINE(bool) vboxNetFltDarwinMBufIsOur(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
|
---|
| 371 | {
|
---|
| 372 | NOREF(pThis);
|
---|
| 373 |
|
---|
| 374 | /*
|
---|
| 375 | * Lookup the tag set by vboxNetFltDarwinMBufFromSG.
|
---|
| 376 | */
|
---|
| 377 | PCVBOXNETFLTTAG pTagData;
|
---|
| 378 | size_t cbTagData;
|
---|
| 379 | errno_t err = mbuf_tag_find(pMBuf, g_idTag, 0 /* type */, &cbTagData, (void **)&pTagData);
|
---|
| 380 | if (err)
|
---|
| 381 | return false;
|
---|
| 382 | AssertReturn(cbTagData == sizeof(*pTagData), false);
|
---|
| 383 |
|
---|
| 384 | /*
|
---|
| 385 | * Dig out the ethernet header from the mbuf.
|
---|
| 386 | */
|
---|
| 387 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
| 388 | if (!pEthHdr)
|
---|
| 389 | pEthHdr = (PCRTNETETHERHDR)mbuf_pkthdr_header(pMBuf);
|
---|
| 390 | if (!pEthHdr)
|
---|
| 391 | pEthHdr = (PCRTNETETHERHDR)mbuf_data(pMBuf);
|
---|
| 392 | /* ASSUMING that there is enough data to work on! */
|
---|
| 393 | if ( pEthHdr->DstMac.au8[0] != pTagData->EthHdr.DstMac.au8[0]
|
---|
| 394 | || pEthHdr->DstMac.au8[1] != pTagData->EthHdr.DstMac.au8[1]
|
---|
| 395 | || pEthHdr->DstMac.au8[2] != pTagData->EthHdr.DstMac.au8[2]
|
---|
| 396 | || pEthHdr->DstMac.au8[3] != pTagData->EthHdr.DstMac.au8[3]
|
---|
| 397 | || pEthHdr->DstMac.au8[4] != pTagData->EthHdr.DstMac.au8[4]
|
---|
| 398 | || pEthHdr->DstMac.au8[5] != pTagData->EthHdr.DstMac.au8[5]
|
---|
| 399 | || pEthHdr->SrcMac.au8[0] != pTagData->EthHdr.SrcMac.au8[0]
|
---|
| 400 | || pEthHdr->SrcMac.au8[1] != pTagData->EthHdr.SrcMac.au8[1]
|
---|
| 401 | || pEthHdr->SrcMac.au8[2] != pTagData->EthHdr.SrcMac.au8[2]
|
---|
| 402 | || pEthHdr->SrcMac.au8[3] != pTagData->EthHdr.SrcMac.au8[3]
|
---|
| 403 | || pEthHdr->SrcMac.au8[4] != pTagData->EthHdr.SrcMac.au8[4]
|
---|
| 404 | || pEthHdr->SrcMac.au8[5] != pTagData->EthHdr.SrcMac.au8[5]
|
---|
| 405 | || pEthHdr->EtherType != pTagData->EthHdr.EtherType)
|
---|
| 406 | {
|
---|
| 407 | Log3(("tagged, but the ethernet header has changed\n"));
|
---|
| 408 | return false;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | return true;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | /**
|
---|
| 416 | * Internal worker that create a darwin mbuf for a (scatter/)gather list.
|
---|
| 417 | *
|
---|
| 418 | * @returns Pointer to the mbuf.
|
---|
| 419 | * @param pThis The instance.
|
---|
| 420 | * @param pSG The (scatter/)gather list.
|
---|
| 421 | */
|
---|
| 422 | static mbuf_t vboxNetFltDarwinMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
|
---|
| 423 | {
|
---|
[33540] | 424 | /// @todo future? mbuf_how_t How = preemption enabled ? MBUF_DONTWAIT : MBUF_WAITOK;
|
---|
[11935] | 425 | mbuf_how_t How = MBUF_WAITOK;
|
---|
| 426 |
|
---|
| 427 | /*
|
---|
| 428 | * We need some way of getting back to our instance data when
|
---|
| 429 | * the mbuf is freed, so use pvUserData for this.
|
---|
| 430 | * -- this is not relevant anylonger! --
|
---|
| 431 | */
|
---|
| 432 | Assert(!pSG->pvUserData || pSG->pvUserData == pThis);
|
---|
| 433 | Assert(!pSG->pvUserData2);
|
---|
| 434 | pSG->pvUserData = pThis;
|
---|
| 435 |
|
---|
| 436 | /*
|
---|
| 437 | * Allocate a packet and copy over the data.
|
---|
| 438 | *
|
---|
| 439 | * Using mbuf_attachcluster() here would've been nice but there are two
|
---|
| 440 | * issues with it: (1) it's 10.5.x only, and (2) the documentation indicates
|
---|
| 441 | * that it's not supposed to be used for really external buffers. The 2nd
|
---|
| 442 | * point might be argued against considering that the only m_clattach user
|
---|
| 443 | * is mallocs memory for the ext mbuf and not doing what's stated in the docs.
|
---|
| 444 | * However, it's hard to tell if these m_clattach buffers actually makes it
|
---|
| 445 | * to the NICs or not, and even if they did, the NIC would need the physical
|
---|
| 446 | * addresses for the pages they contain and might end up copying the data
|
---|
| 447 | * to a new mbuf anyway.
|
---|
| 448 | *
|
---|
| 449 | * So, in the end it's better to just do it the simple way that will work
|
---|
[33540] | 450 | * 100%, even if it involves some extra work (alloc + copy) we really wished
|
---|
[11935] | 451 | * to avoid.
|
---|
[33676] | 452 | *
|
---|
| 453 | * Note. We can't make use of the physical addresses on darwin because the
|
---|
| 454 | * way the mbuf / cluster stuff works (see mbuf_data_to_physical and
|
---|
[32740] | 455 | * mcl_to_paddr).
|
---|
[11935] | 456 | */
|
---|
| 457 | mbuf_t pPkt = NULL;
|
---|
| 458 | errno_t err = mbuf_allocpacket(How, pSG->cbTotal, NULL, &pPkt);
|
---|
| 459 | if (!err)
|
---|
| 460 | {
|
---|
| 461 | /* Skip zero sized memory buffers (paranoia). */
|
---|
| 462 | mbuf_t pCur = pPkt;
|
---|
| 463 | while (pCur && !mbuf_maxlen(pCur))
|
---|
| 464 | pCur = mbuf_next(pCur);
|
---|
| 465 | Assert(pCur);
|
---|
| 466 |
|
---|
| 467 | /* Set the required packet header attributes. */
|
---|
| 468 | mbuf_pkthdr_setlen(pPkt, pSG->cbTotal);
|
---|
| 469 | mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
|
---|
| 470 |
|
---|
| 471 | /* Special case the single buffer copy. */
|
---|
| 472 | if ( mbuf_next(pCur)
|
---|
| 473 | && mbuf_maxlen(pCur) >= pSG->cbTotal)
|
---|
| 474 | {
|
---|
| 475 | mbuf_setlen(pCur, pSG->cbTotal);
|
---|
[28714] | 476 | IntNetSgRead(pSG, mbuf_data(pCur));
|
---|
[11935] | 477 | }
|
---|
| 478 | else
|
---|
| 479 | {
|
---|
| 480 | /* Multi buffer copying. */
|
---|
[28070] | 481 | size_t cbLeft = pSG->cbTotal;
|
---|
| 482 | size_t offSrc = 0;
|
---|
| 483 | while (cbLeft > 0 && pCur)
|
---|
[11935] | 484 | {
|
---|
| 485 | size_t cb = mbuf_maxlen(pCur);
|
---|
[28070] | 486 | if (cb > cbLeft)
|
---|
| 487 | cb = cbLeft;
|
---|
[11935] | 488 | mbuf_setlen(pCur, cb);
|
---|
[28714] | 489 | IntNetSgReadEx(pSG, offSrc, cb, mbuf_data(pCur));
|
---|
[11935] | 490 |
|
---|
| 491 | /* advance */
|
---|
[28070] | 492 | offSrc += cb;
|
---|
| 493 | cbLeft -= cb;
|
---|
[11935] | 494 | pCur = mbuf_next(pCur);
|
---|
| 495 | }
|
---|
[28077] | 496 | Assert(cbLeft == 0);
|
---|
[11935] | 497 | }
|
---|
| 498 | if (!err)
|
---|
| 499 | {
|
---|
| 500 | /*
|
---|
| 501 | * Tag the packet and return successfully.
|
---|
| 502 | */
|
---|
| 503 | PVBOXNETFLTTAG pTagData;
|
---|
| 504 | err = mbuf_tag_allocate(pPkt, g_idTag, 0 /* type */, sizeof(VBOXNETFLTTAG) /* tag len */, How, (void **)&pTagData);
|
---|
| 505 | if (!err)
|
---|
| 506 | {
|
---|
| 507 | Assert(pSG->aSegs[0].cb >= sizeof(pTagData->EthHdr));
|
---|
| 508 | memcpy(&pTagData->EthHdr, pSG->aSegs[0].pv, sizeof(pTagData->EthHdr));
|
---|
| 509 | return pPkt;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /* bailout: */
|
---|
| 513 | AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | mbuf_freem(pPkt);
|
---|
| 517 | }
|
---|
| 518 | else
|
---|
| 519 | AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
|
---|
| 520 | pSG->pvUserData = NULL;
|
---|
| 521 |
|
---|
| 522 | return NULL;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 |
|
---|
| 526 | /**
|
---|
| 527 | * Calculates the number of segments required to represent the mbuf.
|
---|
| 528 | *
|
---|
| 529 | * @returns Number of segments.
|
---|
| 530 | * @param pThis The instance.
|
---|
| 531 | * @param pMBuf The mbuf.
|
---|
| 532 | * @param pvFrame The frame pointer, optional.
|
---|
| 533 | */
|
---|
| 534 | DECLINLINE(unsigned) vboxNetFltDarwinMBufCalcSGSegs(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
|
---|
| 535 | {
|
---|
| 536 | NOREF(pThis);
|
---|
| 537 |
|
---|
| 538 | /*
|
---|
| 539 | * Count the buffers in the chain.
|
---|
| 540 | */
|
---|
| 541 | unsigned cSegs = 0;
|
---|
| 542 | for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
|
---|
| 543 | if (mbuf_len(pCur))
|
---|
| 544 | cSegs++;
|
---|
| 545 | else if ( !cSegs
|
---|
| 546 | && pvFrame
|
---|
| 547 | && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
|
---|
| 548 | cSegs++;
|
---|
| 549 |
|
---|
| 550 | #ifdef PADD_RUNT_FRAMES_FROM_HOST
|
---|
| 551 | /*
|
---|
| 552 | * Add one buffer if the total is less than the ethernet minimum 60 bytes.
|
---|
| 553 | * This may allocate a segment too much if the ethernet header is separated,
|
---|
| 554 | * but that shouldn't harm us much.
|
---|
| 555 | */
|
---|
| 556 | if (mbuf_pkthdr_len(pMBuf) < 60)
|
---|
| 557 | cSegs++;
|
---|
| 558 | #endif
|
---|
| 559 |
|
---|
| 560 | #ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
|
---|
| 561 | /* maximize the number of segments. */
|
---|
| 562 | cSegs = RT_MAX(VBOXNETFLT_DARWIN_MAX_SEGS - 1, cSegs);
|
---|
| 563 | #endif
|
---|
| 564 |
|
---|
| 565 | return cSegs ? cSegs : 1;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 |
|
---|
| 569 | /**
|
---|
| 570 | * Initializes a SG list from an mbuf.
|
---|
| 571 | *
|
---|
| 572 | * @returns Number of segments.
|
---|
| 573 | * @param pThis The instance.
|
---|
| 574 | * @param pMBuf The mbuf.
|
---|
| 575 | * @param pSG The SG.
|
---|
| 576 | * @param pvFrame The frame pointer, optional.
|
---|
| 577 | * @param cSegs The number of segments allocated for the SG.
|
---|
| 578 | * This should match the number in the mbuf exactly!
|
---|
| 579 | * @param fSrc The source of the frame.
|
---|
| 580 | */
|
---|
[63512] | 581 | DECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs,
|
---|
| 582 | uint32_t fSrc)
|
---|
[11935] | 583 | {
|
---|
[63512] | 584 | RT_NOREF(pThis, fSrc);
|
---|
[11935] | 585 |
|
---|
| 586 | /*
|
---|
[28025] | 587 | * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
|
---|
[11935] | 588 | */
|
---|
| 589 | unsigned iSeg = 0;
|
---|
[28714] | 590 | IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
|
---|
[11935] | 591 | for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
|
---|
| 592 | {
|
---|
| 593 | size_t cbSeg = mbuf_len(pCur);
|
---|
| 594 | if (cbSeg)
|
---|
| 595 | {
|
---|
| 596 | void *pvSeg = mbuf_data(pCur);
|
---|
| 597 |
|
---|
| 598 | /* deal with pvFrame */
|
---|
| 599 | if (!iSeg && pvFrame && pvFrame != pvSeg)
|
---|
| 600 | {
|
---|
| 601 | void *pvStart = mbuf_datastart(pMBuf);
|
---|
| 602 | uintptr_t offSeg = (uintptr_t)pvSeg - (uintptr_t)pvStart;
|
---|
| 603 | uintptr_t offSegEnd = offSeg + cbSeg;
|
---|
| 604 | Assert(pvStart && pvSeg && offSeg < mbuf_maxlen(pMBuf) && offSegEnd <= mbuf_maxlen(pMBuf)); NOREF(offSegEnd);
|
---|
| 605 | uintptr_t offFrame = (uintptr_t)pvFrame - (uintptr_t)pvStart;
|
---|
| 606 | if (RT_LIKELY(offFrame < offSeg))
|
---|
| 607 | {
|
---|
| 608 | pvSeg = pvFrame;
|
---|
| 609 | cbSeg += offSeg - offFrame;
|
---|
| 610 | }
|
---|
| 611 | else
|
---|
| 612 | AssertMsgFailed(("pvFrame=%p pvStart=%p pvSeg=%p offSeg=%p cbSeg=%#zx offSegEnd=%p offFrame=%p maxlen=%#zx\n",
|
---|
| 613 | pvFrame, pvStart, pvSeg, offSeg, cbSeg, offSegEnd, offFrame, mbuf_maxlen(pMBuf)));
|
---|
| 614 | pvFrame = NULL;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | AssertBreak(iSeg < cSegs);
|
---|
| 618 | pSG->cbTotal += cbSeg;
|
---|
| 619 | pSG->aSegs[iSeg].cb = cbSeg;
|
---|
| 620 | pSG->aSegs[iSeg].pv = pvSeg;
|
---|
| 621 | pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
|
---|
| 622 | iSeg++;
|
---|
| 623 | }
|
---|
| 624 | /* The pvFrame might be in a now empty buffer. */
|
---|
| 625 | else if ( !iSeg
|
---|
| 626 | && pvFrame
|
---|
| 627 | && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
|
---|
| 628 | {
|
---|
| 629 | cbSeg = (uintptr_t)mbuf_datastart(pMBuf) + mbuf_maxlen(pMBuf) - (uintptr_t)pvFrame;
|
---|
| 630 | pSG->cbTotal += cbSeg;
|
---|
| 631 | pSG->aSegs[iSeg].cb = cbSeg;
|
---|
| 632 | pSG->aSegs[iSeg].pv = pvFrame;
|
---|
| 633 | pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
|
---|
| 634 | iSeg++;
|
---|
| 635 | pvFrame = NULL;
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | Assert(iSeg && iSeg <= cSegs);
|
---|
| 640 | pSG->cSegsUsed = iSeg;
|
---|
| 641 |
|
---|
| 642 | #ifdef PADD_RUNT_FRAMES_FROM_HOST
|
---|
| 643 | /*
|
---|
| 644 | * Add a trailer if the frame is too small.
|
---|
| 645 | *
|
---|
| 646 | * Since we're getting to the packet before it is framed, it has not
|
---|
| 647 | * yet been padded. The current solution is to add a segment pointing
|
---|
| 648 | * to a buffer containing all zeros and pray that works for all frames...
|
---|
| 649 | */
|
---|
[57816] | 650 | if (pSG->cbTotal < 60 && (fSrc == INTNETTRUNKDIR_HOST))
|
---|
[11935] | 651 | {
|
---|
| 652 | AssertReturnVoid(iSeg < cSegs);
|
---|
| 653 |
|
---|
| 654 | static uint8_t const s_abZero[128] = {0};
|
---|
| 655 | pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
|
---|
| 656 | pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
|
---|
| 657 | pSG->aSegs[iSeg].cb = 60 - pSG->cbTotal;
|
---|
| 658 | pSG->cbTotal = 60;
|
---|
| 659 | pSG->cSegsUsed++;
|
---|
| 660 | }
|
---|
| 661 | #endif
|
---|
| 662 |
|
---|
| 663 | #ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
|
---|
| 664 | /*
|
---|
| 665 | * Redistribute the segments.
|
---|
| 666 | */
|
---|
| 667 | if (pSG->cSegsUsed < pSG->cSegsAlloc)
|
---|
| 668 | {
|
---|
| 669 | /* copy the segments to the end. */
|
---|
| 670 | int iSrc = pSG->cSegsUsed;
|
---|
| 671 | int iDst = pSG->cSegsAlloc;
|
---|
| 672 | while (iSrc > 0)
|
---|
| 673 | {
|
---|
| 674 | iDst--;
|
---|
| 675 | iSrc--;
|
---|
| 676 | pSG->aSegs[iDst] = pSG->aSegs[iSrc];
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | /* create small segments from the start. */
|
---|
| 680 | pSG->cSegsUsed = pSG->cSegsAlloc;
|
---|
| 681 | iSrc = iDst;
|
---|
| 682 | iDst = 0;
|
---|
| 683 | while ( iDst < iSrc
|
---|
| 684 | && iDst < pSG->cSegsAlloc)
|
---|
| 685 | {
|
---|
| 686 | pSG->aSegs[iDst].Phys = NIL_RTHCPHYS;
|
---|
| 687 | pSG->aSegs[iDst].pv = pSG->aSegs[iSrc].pv;
|
---|
| 688 | pSG->aSegs[iDst].cb = RT_MIN(pSG->aSegs[iSrc].cb, VBOXNETFLT_DARWIN_TEST_SEG_SIZE);
|
---|
| 689 | if (pSG->aSegs[iDst].cb != pSG->aSegs[iSrc].cb)
|
---|
| 690 | {
|
---|
| 691 | pSG->aSegs[iSrc].cb -= pSG->aSegs[iDst].cb;
|
---|
| 692 | pSG->aSegs[iSrc].pv = (uint8_t *)pSG->aSegs[iSrc].pv + pSG->aSegs[iDst].cb;
|
---|
| 693 | }
|
---|
| 694 | else if (++iSrc >= pSG->cSegsAlloc)
|
---|
| 695 | {
|
---|
| 696 | pSG->cSegsUsed = iDst + 1;
|
---|
| 697 | break;
|
---|
| 698 | }
|
---|
| 699 | iDst++;
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
| 702 | #endif
|
---|
| 703 |
|
---|
| 704 | AssertMsg(!pvFrame, ("pvFrame=%p pMBuf=%p iSeg=%d\n", pvFrame, pMBuf, iSeg));
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 |
|
---|
| 708 | /**
|
---|
[28666] | 709 | * Helper for determining whether the host wants the interface to be
|
---|
| 710 | * promiscuous.
|
---|
| 711 | */
|
---|
| 712 | static bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
|
---|
| 713 | {
|
---|
| 714 | bool fRc = false;
|
---|
| 715 | ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
|
---|
| 716 | if (pIfNet)
|
---|
| 717 | {
|
---|
| 718 | /* gather the data */
|
---|
| 719 | uint16_t fIf = ifnet_flags(pIfNet);
|
---|
| 720 | unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
|
---|
| 721 | bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
|
---|
| 722 | vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
|
---|
| 723 |
|
---|
| 724 | /* calc the return. */
|
---|
| 725 | fRc = (fIf & IFF_PROMISC)
|
---|
| 726 | && cPromisc > fSetPromiscuous;
|
---|
| 727 | }
|
---|
| 728 | return fRc;
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 |
|
---|
| 732 |
|
---|
| 733 | /**
|
---|
[11935] | 734 | *
|
---|
| 735 | * @see iff_detached_func in the darwin kpi.
|
---|
| 736 | */
|
---|
| 737 | static void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
|
---|
| 738 | {
|
---|
| 739 | PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
|
---|
| 740 | uint64_t NanoTS = RTTimeSystemNanoTS();
|
---|
| 741 | LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
|
---|
| 742 | pThis, NanoTS, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
|
---|
| 743 |
|
---|
| 744 | Assert(!pThis->fDisconnectedFromHost);
|
---|
| 745 | Assert(!pThis->fRediscoveryPending);
|
---|
| 746 |
|
---|
| 747 | /*
|
---|
| 748 | * If we've put it into promiscuous mode, undo that now. If we don't
|
---|
| 749 | * the if_pcount will go all wrong when it's replugged.
|
---|
| 750 | */
|
---|
| 751 | if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
|
---|
| 752 | ifnet_set_promiscuous(pIfNet, 0);
|
---|
| 753 |
|
---|
| 754 | /*
|
---|
| 755 | * We carefully take the spinlock and increase the interface reference
|
---|
| 756 | * behind it in order to avoid problematic races with the detached callback.
|
---|
| 757 | */
|
---|
[40806] | 758 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
[11935] | 759 |
|
---|
[30111] | 760 | pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
|
---|
[11935] | 761 | int cPromisc = VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
|
---|
| 762 |
|
---|
[30112] | 763 | ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
|
---|
| 764 | ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
|
---|
[11935] | 765 | ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
|
---|
| 766 | pThis->u.s.fSetPromiscuous = false;
|
---|
| 767 | ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
|
---|
| 768 | ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
|
---|
| 769 | ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
|
---|
| 770 |
|
---|
[52618] | 771 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
[11935] | 772 |
|
---|
| 773 | if (pIfNet)
|
---|
| 774 | ifnet_release(pIfNet);
|
---|
| 775 | LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
| 779 | /**
|
---|
| 780 | *
|
---|
| 781 | * @see iff_ioctl_func in the darwin kpi.
|
---|
| 782 | */
|
---|
| 783 | static errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
|
---|
| 784 | {
|
---|
[63512] | 785 | RT_NOREF(pIfNet);
|
---|
[11935] | 786 | PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
|
---|
| 787 | LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
|
---|
| 788 |
|
---|
| 789 | /*
|
---|
| 790 | * Update fOtherPromiscuous.
|
---|
| 791 | */
|
---|
| 792 | /** @todo we'll have to find the offset of if_pcount to get this right! */
|
---|
| 793 | //if (uCmd == SIOCSIFFLAGS)
|
---|
| 794 | //{
|
---|
| 795 | //
|
---|
| 796 | //}
|
---|
| 797 |
|
---|
| 798 | /*
|
---|
| 799 | * We didn't handle it, continue processing.
|
---|
| 800 | */
|
---|
| 801 | NOREF(pThis);
|
---|
| 802 | NOREF(eProtocol);
|
---|
| 803 | NOREF(uCmd);
|
---|
| 804 | NOREF(pvArg);
|
---|
| 805 | return EOPNOTSUPP;
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 |
|
---|
| 809 | /**
|
---|
| 810 | *
|
---|
| 811 | * @see iff_event_func in the darwin kpi.
|
---|
| 812 | */
|
---|
| 813 | static void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
|
---|
| 814 | {
|
---|
| 815 | PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
|
---|
| 816 | LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
|
---|
| 817 |
|
---|
| 818 | NOREF(pThis);
|
---|
| 819 | NOREF(pIfNet);
|
---|
| 820 | NOREF(eProtocol);
|
---|
| 821 | NOREF(pEvMsg);
|
---|
| 822 |
|
---|
| 823 | /*
|
---|
| 824 | * Watch out for the interface going online / offline.
|
---|
| 825 | */
|
---|
| 826 | if ( VALID_PTR(pThis)
|
---|
| 827 | && VALID_PTR(pEvMsg)
|
---|
| 828 | && pEvMsg->vendor_code == KEV_VENDOR_APPLE
|
---|
| 829 | && pEvMsg->kev_class == KEV_NETWORK_CLASS
|
---|
| 830 | && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
|
---|
| 831 | {
|
---|
| 832 | if (pThis->u.s.pIfNet == pIfNet)
|
---|
| 833 | {
|
---|
| 834 | if (pEvMsg->event_code == KEV_DL_LINK_ON)
|
---|
| 835 | {
|
---|
| 836 | if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
|
---|
| 837 | {
|
---|
| 838 | /* failed to bring it online. */
|
---|
| 839 | errno_t err = ifnet_set_promiscuous(pIfNet, 1);
|
---|
| 840 | if (!err)
|
---|
| 841 | {
|
---|
| 842 | ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
|
---|
| 843 | ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
|
---|
| 844 | Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 845 | }
|
---|
| 846 | else
|
---|
| 847 | Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 848 | }
|
---|
| 849 | else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
|
---|
| 850 | && !(ifnet_flags(pIfNet) & IFF_PROMISC))
|
---|
| 851 | {
|
---|
| 852 | /* Try fix the inconsistency. */
|
---|
| 853 | errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
|
---|
| 854 | if (!err)
|
---|
| 855 | err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
|
---|
| 856 | if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
|
---|
| 857 | Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 858 | else
|
---|
| 859 | Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
|
---|
| 860 | pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 861 | }
|
---|
| 862 | else
|
---|
| 863 | Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 864 | }
|
---|
| 865 | else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
|
---|
| 866 | Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
|
---|
[28666] | 867 | /** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
|
---|
| 868 | /** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
|
---|
[11935] | 869 | }
|
---|
| 870 | else
|
---|
| 871 | Log(("vboxNetFltDarwinIffEvent: pThis->u.s.pIfNet=%p pIfNet=%p (%d)\n", pThis->u.s.pIfNet, pIfNet, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
|
---|
| 872 | }
|
---|
| 873 | else if (VALID_PTR(pEvMsg))
|
---|
| 874 | Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
|
---|
| 875 | pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 |
|
---|
| 879 | /**
|
---|
| 880 | * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
|
---|
| 881 | *
|
---|
| 882 | * @returns 0 or EJUSTRETURN.
|
---|
| 883 | * @param pThis The instance.
|
---|
| 884 | * @param pMBuf The mbuf.
|
---|
| 885 | * @param pvFrame The start of the frame, optional.
|
---|
| 886 | * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
|
---|
| 887 | * @param eProtocol The protocol.
|
---|
| 888 | */
|
---|
| 889 | static errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
|
---|
[38408] | 890 | uint32_t fSrc, protocol_family_t eProtocol)
|
---|
[11935] | 891 | {
|
---|
| 892 | /*
|
---|
| 893 | * Drop it immediately?
|
---|
| 894 | */
|
---|
| 895 | Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
|
---|
| 896 | pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
|
---|
| 897 | if (!pMBuf)
|
---|
| 898 | return 0;
|
---|
| 899 | #if 0 /* debugging lost icmp packets */
|
---|
| 900 | if (mbuf_pkthdr_len(pMBuf) > 0x300)
|
---|
| 901 | {
|
---|
| 902 | uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
|
---|
| 903 | Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
|
---|
| 904 | }
|
---|
| 905 | #endif
|
---|
| 906 | if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
|
---|
| 907 | return 0;
|
---|
| 908 |
|
---|
| 909 | /*
|
---|
| 910 | * Active? Retain the instance and increment the busy counter.
|
---|
| 911 | */
|
---|
[28830] | 912 | if (!vboxNetFltTryRetainBusyActive(pThis))
|
---|
[11935] | 913 | return 0;
|
---|
| 914 |
|
---|
| 915 | /*
|
---|
| 916 | * Finalize out-bound packets since the stack puts off finalizing
|
---|
| 917 | * TCP/IP checksums as long as possible.
|
---|
| 918 | * ASSUMES this only applies to outbound IP packets.
|
---|
| 919 | */
|
---|
[57816] | 920 | if ( (fSrc == INTNETTRUNKDIR_HOST)
|
---|
[11935] | 921 | && eProtocol == PF_INET)
|
---|
| 922 | {
|
---|
| 923 | Assert(!pvFrame);
|
---|
| 924 | mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | /*
|
---|
| 928 | * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
|
---|
| 929 | */
|
---|
| 930 | bool fDropIt = false;
|
---|
| 931 | unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
|
---|
| 932 | if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
|
---|
| 933 | {
|
---|
[73097] | 934 | PINTNETSG pSG = (PINTNETSG)alloca(RT_UOFFSETOF_DYN(INTNETSG, aSegs[cSegs]));
|
---|
[11935] | 935 | vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
|
---|
| 936 |
|
---|
[29662] | 937 | fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
|
---|
[11935] | 938 | if (fDropIt)
|
---|
[38370] | 939 | {
|
---|
| 940 | /*
|
---|
[57904] | 941 | * If the interface is in promiscuous mode we should let
|
---|
| 942 | * all inbound packets (this one was for a bridged guest)
|
---|
| 943 | * reach the driver as it passes them to tap callbacks in
|
---|
| 944 | * order for BPF to work properly.
|
---|
[38370] | 945 | */
|
---|
[57904] | 946 | if ( fSrc == INTNETTRUNKDIR_WIRE
|
---|
| 947 | && vboxNetFltDarwinIsPromiscuous(pThis))
|
---|
| 948 | {
|
---|
[38370] | 949 | fDropIt = false;
|
---|
[57904] | 950 | }
|
---|
| 951 |
|
---|
| 952 | /*
|
---|
| 953 | * A packet from the host to a guest. As we won't pass it
|
---|
| 954 | * to the drvier/wire we need to feed it to bpf ourselves.
|
---|
[57912] | 955 | *
|
---|
| 956 | * XXX: TODO: bpf should be done before; use pfnPreRecv?
|
---|
[57904] | 957 | */
|
---|
| 958 | if (fSrc == INTNETTRUNKDIR_HOST)
|
---|
| 959 | {
|
---|
| 960 | bpf_tap_out(pThis->u.s.pIfNet, DLT_EN10MB, pMBuf, NULL, 0);
|
---|
[57912] | 961 | ifnet_stat_increment_out(pThis->u.s.pIfNet, 1, mbuf_len(pMBuf), 0);
|
---|
[57904] | 962 | }
|
---|
[38370] | 963 | }
|
---|
[11935] | 964 | }
|
---|
| 965 |
|
---|
| 966 | vboxNetFltRelease(pThis, true /* fBusy */);
|
---|
| 967 |
|
---|
[57817] | 968 | if (fDropIt)
|
---|
| 969 | {
|
---|
| 970 | mbuf_freem(pMBuf);
|
---|
| 971 | return EJUSTRETURN;
|
---|
| 972 | }
|
---|
| 973 | return 0;
|
---|
[11935] | 974 | }
|
---|
| 975 |
|
---|
| 976 |
|
---|
| 977 | /**
|
---|
| 978 | * From the host.
|
---|
| 979 | *
|
---|
| 980 | * @see iff_output_func in the darwin kpi.
|
---|
| 981 | */
|
---|
| 982 | static errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
|
---|
| 983 | {
|
---|
| 984 | /** @todo there was some note about the ethernet header here or something like that... */
|
---|
| 985 |
|
---|
| 986 | NOREF(eProtocol);
|
---|
[38408] | 987 | NOREF(pIfNet);
|
---|
| 988 | return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
|
---|
[11935] | 989 | }
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 | /**
|
---|
| 993 | * From the wire.
|
---|
| 994 | *
|
---|
| 995 | * @see iff_input_func in the darwin kpi.
|
---|
| 996 | */
|
---|
| 997 | static errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
|
---|
| 998 | {
|
---|
[63512] | 999 | RT_NOREF(eProtocol, pIfNet);
|
---|
[38408] | 1000 | return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
|
---|
[11935] | 1001 | }
|
---|
| 1002 |
|
---|
| 1003 |
|
---|
[50379] | 1004 | /** A worker thread for vboxNetFltSendDummy(). */
|
---|
| 1005 | static DECLCALLBACK(int) vboxNetFltSendDummyWorker(RTTHREAD hThreadSelf, void *pvUser)
|
---|
[50321] | 1006 | {
|
---|
[63512] | 1007 | RT_NOREF(hThreadSelf);
|
---|
[50321] | 1008 | Assert(pvUser);
|
---|
| 1009 | ifnet_t pIfNet = (ifnet_t)pvUser;
|
---|
[50322] | 1010 | return VBoxNetSendDummy(pIfNet);
|
---|
[50321] | 1011 | }
|
---|
| 1012 |
|
---|
[50379] | 1013 |
|
---|
| 1014 | /**
|
---|
[50321] | 1015 | * Prevent GUI icon freeze issue when VirtualBoxVM process terminates.
|
---|
| 1016 | *
|
---|
[50379] | 1017 | * This function is a workaround for stuck-in-dock issue. The idea here is to
|
---|
| 1018 | * send a dummy packet to an interface from the context of a kernel thread.
|
---|
| 1019 | * Therefore, an XNU's receive thread (which is created as a result if we are
|
---|
| 1020 | * the first who is communicating with the interface) will be associated with
|
---|
| 1021 | * the kernel thread instead of VirtualBoxVM process.
|
---|
[50321] | 1022 | *
|
---|
| 1023 | * @param pIfNet Interface to be used to send data.
|
---|
| 1024 | */
|
---|
| 1025 | static void vboxNetFltSendDummy(ifnet_t pIfNet)
|
---|
| 1026 | {
|
---|
[50379] | 1027 | RTTHREAD hThread;
|
---|
| 1028 | int rc = RTThreadCreate(&hThread, vboxNetFltSendDummyWorker, (void *)pIfNet, 0,
|
---|
| 1029 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "DummyThread");
|
---|
[50321] | 1030 | if (RT_SUCCESS(rc))
|
---|
| 1031 | {
|
---|
[50379] | 1032 | RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL);
|
---|
[50321] | 1033 | LogFlow(("vboxNetFltSendDummy: a dummy packet has been successfully sent in order to prevent stuck-in-dock issue\n"));
|
---|
| 1034 | }
|
---|
| 1035 | else
|
---|
| 1036 | LogFlow(("vboxNetFltSendDummy: unable to send dummy packet in order to prevent stuck-in-dock issue\n"));
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
[50379] | 1039 |
|
---|
[11935] | 1040 | /**
|
---|
| 1041 | * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
|
---|
| 1042 | *
|
---|
| 1043 | * @returns VBox status code.
|
---|
| 1044 | * @param pThis The instance.
|
---|
| 1045 | * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
|
---|
| 1046 | * flood the release log.
|
---|
| 1047 | */
|
---|
| 1048 | static int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
|
---|
| 1049 | {
|
---|
| 1050 | LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
[57253] | 1051 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[11935] | 1052 |
|
---|
| 1053 | /*
|
---|
| 1054 | * Locate the interface first.
|
---|
| 1055 | *
|
---|
| 1056 | * The pIfNet member is updated before iflt_attach is called and used
|
---|
| 1057 | * to deal with the hypothetical case where someone rips out the
|
---|
| 1058 | * interface immediately after our iflt_attach call.
|
---|
| 1059 | */
|
---|
| 1060 | ifnet_t pIfNet = NULL;
|
---|
| 1061 | errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
|
---|
| 1062 | if (err)
|
---|
| 1063 | {
|
---|
| 1064 | Assert(err == ENXIO);
|
---|
| 1065 | if (!fRediscovery)
|
---|
| 1066 | LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
|
---|
| 1067 | else
|
---|
| 1068 | Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
|
---|
[57253] | 1069 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[11935] | 1070 | return VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
| 1071 | }
|
---|
| 1072 |
|
---|
[75687] | 1073 | AssertCompileMemberAlignment(VBOXNETFLTINS, u.s.pIfNet, ARCH_BITS / 8);
|
---|
| 1074 | AssertMsg(!((uintptr_t)&pThis->u.s.pIfNet & (ARCH_BITS / 8 - 1)), ("pThis=%p\n", pThis));
|
---|
[40806] | 1075 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
[30111] | 1076 | ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
|
---|
[52618] | 1077 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
[11935] | 1078 |
|
---|
[50747] | 1079 | /* Adjust g_offIfNetPCount as it varies for different versions of xnu. */
|
---|
| 1080 | vboxNetFltDarwinDetectPCountOffset(pIfNet);
|
---|
| 1081 |
|
---|
[50379] | 1082 | /* Prevent stuck-in-dock issue by associating interface receive thread with kernel thread. */
|
---|
[50321] | 1083 | vboxNetFltSendDummy(pIfNet);
|
---|
| 1084 |
|
---|
[11935] | 1085 | /*
|
---|
| 1086 | * Get the mac address while we still have a valid ifnet reference.
|
---|
| 1087 | */
|
---|
[28666] | 1088 | err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
|
---|
[11935] | 1089 | if (!err)
|
---|
| 1090 | {
|
---|
| 1091 | /*
|
---|
| 1092 | * Try attach the filter.
|
---|
| 1093 | */
|
---|
| 1094 | struct iff_filter RegRec;
|
---|
| 1095 | RegRec.iff_cookie = pThis;
|
---|
| 1096 | RegRec.iff_name = "VBoxNetFlt";
|
---|
| 1097 | RegRec.iff_protocol = 0;
|
---|
| 1098 | RegRec.iff_input = vboxNetFltDarwinIffInput;
|
---|
| 1099 | RegRec.iff_output = vboxNetFltDarwinIffOutput;
|
---|
| 1100 | RegRec.iff_event = vboxNetFltDarwinIffEvent;
|
---|
| 1101 | RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
|
---|
| 1102 | RegRec.iff_detached = vboxNetFltDarwinIffDetached;
|
---|
| 1103 | interface_filter_t pIfFilter = NULL;
|
---|
| 1104 | err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
|
---|
| 1105 | Assert(err || pIfFilter);
|
---|
| 1106 |
|
---|
[40806] | 1107 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
[30111] | 1108 | pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
|
---|
[11935] | 1109 | if (pIfNet && !err)
|
---|
| 1110 | {
|
---|
| 1111 | ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
|
---|
[30111] | 1112 | ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
|
---|
[11935] | 1113 | pIfNet = NULL; /* don't dereference it */
|
---|
| 1114 | }
|
---|
[52618] | 1115 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
[28666] | 1116 |
|
---|
| 1117 | /* Report capabilities. */
|
---|
[28830] | 1118 | if ( !pIfNet
|
---|
| 1119 | && vboxNetFltTryRetainBusyNotDisconnected(pThis))
|
---|
[28666] | 1120 | {
|
---|
| 1121 | Assert(pThis->pSwitchPort);
|
---|
| 1122 | pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
|
---|
[57904] | 1123 | #if 0
|
---|
[62466] | 1124 | /*
|
---|
[57904] | 1125 | * XXX: Don't tell SrvIntNetR0 if the interface is
|
---|
| 1126 | * promiscuous, because there's no code yet to update that
|
---|
| 1127 | * information and we don't want it stuck, spamming all
|
---|
| 1128 | * traffic to the host.
|
---|
| 1129 | */
|
---|
[28666] | 1130 | pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
|
---|
[57904] | 1131 | #endif
|
---|
[28666] | 1132 | pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
|
---|
[28723] | 1133 | pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
|
---|
[28830] | 1134 | vboxNetFltRelease(pThis, true /*fBusy*/);
|
---|
[28666] | 1135 | }
|
---|
[11935] | 1136 | }
|
---|
| 1137 |
|
---|
| 1138 | /* Release the interface on failure. */
|
---|
| 1139 | if (pIfNet)
|
---|
| 1140 | ifnet_release(pIfNet);
|
---|
| 1141 |
|
---|
| 1142 | int rc = RTErrConvertFromErrno(err);
|
---|
| 1143 | if (RT_SUCCESS(rc))
|
---|
[56723] | 1144 | LogRel(("VBoxFltDrv: attached to '%s' / %RTmac\n", pThis->szName, &pThis->u.s.MacAddr));
|
---|
[11935] | 1145 | else
|
---|
| 1146 | LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
|
---|
[57253] | 1147 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[11935] | 1148 | return rc;
|
---|
| 1149 | }
|
---|
| 1150 |
|
---|
| 1151 |
|
---|
| 1152 | bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
|
---|
| 1153 | {
|
---|
| 1154 | vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
|
---|
| 1155 | return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 |
|
---|
[29662] | 1159 | int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
|
---|
[11935] | 1160 | {
|
---|
[57253] | 1161 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[29662] | 1162 | NOREF(pvIfData);
|
---|
| 1163 |
|
---|
[11935] | 1164 | int rc = VINF_SUCCESS;
|
---|
| 1165 | ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
|
---|
| 1166 | if (pIfNet)
|
---|
| 1167 | {
|
---|
| 1168 | /*
|
---|
| 1169 | * Create a mbuf for the gather list and push it onto the wire.
|
---|
[57912] | 1170 | * BPF tap and stats will be taken care of by the driver.
|
---|
[11935] | 1171 | */
|
---|
[57904] | 1172 | if (fDst & INTNETTRUNKDIR_WIRE)
|
---|
[11935] | 1173 | {
|
---|
| 1174 | mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
|
---|
| 1175 | if (pMBuf)
|
---|
| 1176 | {
|
---|
| 1177 | errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
|
---|
| 1178 | if (err)
|
---|
| 1179 | rc = RTErrConvertFromErrno(err);
|
---|
| 1180 | }
|
---|
| 1181 | else
|
---|
| 1182 | rc = VERR_NO_MEMORY;
|
---|
| 1183 | }
|
---|
| 1184 |
|
---|
| 1185 | /*
|
---|
| 1186 | * Create a mbuf for the gather list and push it onto the host stack.
|
---|
[57912] | 1187 | * BPF tap and stats are on us.
|
---|
[11935] | 1188 | */
|
---|
| 1189 | if (fDst & INTNETTRUNKDIR_HOST)
|
---|
| 1190 | {
|
---|
| 1191 | mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
|
---|
| 1192 | if (pMBuf)
|
---|
| 1193 | {
|
---|
[57904] | 1194 | void *pvEthHdr = mbuf_data(pMBuf);
|
---|
[11935] | 1195 | unsigned const cbEthHdr = 14;
|
---|
[57912] | 1196 | struct ifnet_stat_increment_param stats;
|
---|
[11935] | 1197 |
|
---|
[57912] | 1198 | RT_ZERO(stats);
|
---|
| 1199 | stats.packets_in = 1;
|
---|
| 1200 | stats.bytes_in = mbuf_len(pMBuf); /* full ethernet frame */
|
---|
| 1201 |
|
---|
[57904] | 1202 | mbuf_pkthdr_setrcvif(pMBuf, pIfNet);
|
---|
| 1203 | mbuf_pkthdr_setheader(pMBuf, pvEthHdr); /* link-layer header */
|
---|
| 1204 | mbuf_adj(pMBuf, cbEthHdr); /* move to payload */
|
---|
| 1205 |
|
---|
[58107] | 1206 | #if 0 /* XXX: disabled since we don't request promiscuous from intnet */
|
---|
| 1207 | /*
|
---|
| 1208 | * TODO: Since intnet knows whether it forwarded us
|
---|
| 1209 | * this packet because it's for us or because we are
|
---|
| 1210 | * promiscuous, it can perhaps set a flag for us in
|
---|
| 1211 | * INTNETSG::fFlags so that we don't have to re-check
|
---|
| 1212 | * it here.
|
---|
| 1213 | */
|
---|
| 1214 | PCRTNETETHERHDR pcEthHdr = (PCRTNETETHERHDR)pvEthHdr;
|
---|
| 1215 | if ( (pcEthHdr->DstMac.au8[0] & 1) == 0 /* unicast? */
|
---|
| 1216 | && memcmp(&pcEthHdr->DstMac, &pThis->u.s.MacAddr, sizeof(RTMAC)) != 0)
|
---|
| 1217 | {
|
---|
| 1218 | mbuf_setflags_mask(pMBuf, MBUF_PROMISC, MBUF_PROMISC);
|
---|
| 1219 | }
|
---|
| 1220 | #endif
|
---|
| 1221 |
|
---|
[57904] | 1222 | bpf_tap_in(pIfNet, DLT_EN10MB, pMBuf, pvEthHdr, cbEthHdr);
|
---|
[57912] | 1223 | errno_t err = ifnet_input(pIfNet, pMBuf, &stats);
|
---|
[11935] | 1224 | if (err)
|
---|
| 1225 | rc = RTErrConvertFromErrno(err);
|
---|
| 1226 | }
|
---|
| 1227 | else
|
---|
| 1228 | rc = VERR_NO_MEMORY;
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
[57253] | 1234 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[11935] | 1235 | return rc;
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
| 1239 | void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
|
---|
| 1240 | {
|
---|
[57253] | 1241 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[11935] | 1242 | ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
|
---|
| 1243 | if (pIfNet)
|
---|
| 1244 | {
|
---|
[16195] | 1245 | if (pThis->fDisablePromiscuous)
|
---|
| 1246 | {
|
---|
[18098] | 1247 | /*
|
---|
| 1248 | * Promiscuous mode should not be used (wireless), we just need to
|
---|
| 1249 | * make sure the interface is up.
|
---|
| 1250 | */
|
---|
[16195] | 1251 | if (fActive)
|
---|
| 1252 | {
|
---|
| 1253 | u_int16_t fIf = ifnet_flags(pIfNet);
|
---|
| 1254 | if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
|
---|
| 1255 | {
|
---|
| 1256 | ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
|
---|
| 1257 | ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
|
---|
| 1258 | }
|
---|
| 1259 | }
|
---|
| 1260 | }
|
---|
[18098] | 1261 | else
|
---|
[11935] | 1262 | {
|
---|
| 1263 | /*
|
---|
[18098] | 1264 | * This api is a bit weird, the best reference is the code.
|
---|
| 1265 | *
|
---|
[33540] | 1266 | * Also, we have a bit or race conditions wrt the maintenance of
|
---|
[18098] | 1267 | * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
|
---|
[11935] | 1268 | */
|
---|
[18098] | 1269 | unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
|
---|
| 1270 | u_int16_t fIf;
|
---|
| 1271 | if (fActive)
|
---|
[11935] | 1272 | {
|
---|
[18098] | 1273 | Assert(!pThis->u.s.fSetPromiscuous);
|
---|
| 1274 | errno_t err = ENETDOWN;
|
---|
| 1275 | ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
|
---|
| 1276 |
|
---|
| 1277 | /*
|
---|
| 1278 | * Try bring the interface up and running if it's down.
|
---|
| 1279 | */
|
---|
[11935] | 1280 | fIf = ifnet_flags(pIfNet);
|
---|
[18098] | 1281 | if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
|
---|
| 1282 | {
|
---|
| 1283 | err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
|
---|
| 1284 | errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
|
---|
| 1285 | if (!err)
|
---|
| 1286 | err = err2;
|
---|
| 1287 | fIf = ifnet_flags(pIfNet);
|
---|
| 1288 | }
|
---|
[11935] | 1289 |
|
---|
[18098] | 1290 | /*
|
---|
| 1291 | * Is it already up? If it isn't, leave it to the link event or
|
---|
| 1292 | * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
|
---|
| 1293 | */
|
---|
| 1294 | if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
|
---|
[11935] | 1295 | {
|
---|
[18098] | 1296 | err = ifnet_set_promiscuous(pIfNet, 1);
|
---|
| 1297 | pThis->u.s.fSetPromiscuous = err == 0;
|
---|
| 1298 | if (!err)
|
---|
| 1299 | {
|
---|
| 1300 | ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
|
---|
[11935] | 1301 |
|
---|
[18098] | 1302 | /* check if it actually worked, this stuff is not always behaving well. */
|
---|
| 1303 | if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
|
---|
| 1304 | {
|
---|
| 1305 | err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
|
---|
| 1306 | if (!err)
|
---|
| 1307 | err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
|
---|
| 1308 | if (!err)
|
---|
| 1309 | Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 1310 | else
|
---|
| 1311 | Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
|
---|
| 1312 | pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 1313 | }
|
---|
[11935] | 1314 | }
|
---|
[18098] | 1315 | else
|
---|
| 1316 | Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
[11935] | 1317 | }
|
---|
[18098] | 1318 | else if (!err)
|
---|
| 1319 | Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
| 1320 | if (err)
|
---|
| 1321 | LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
[11935] | 1322 | }
|
---|
[18098] | 1323 | else
|
---|
[11935] | 1324 | {
|
---|
[18098] | 1325 | ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
|
---|
| 1326 | if (pThis->u.s.fSetPromiscuous)
|
---|
| 1327 | {
|
---|
| 1328 | errno_t err = ifnet_set_promiscuous(pIfNet, 0);
|
---|
| 1329 | AssertMsg(!err, ("%d\n", err)); NOREF(err);
|
---|
| 1330 | }
|
---|
| 1331 | pThis->u.s.fSetPromiscuous = false;
|
---|
| 1332 |
|
---|
| 1333 | fIf = ifnet_flags(pIfNet);
|
---|
| 1334 | Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
|
---|
[11935] | 1335 | }
|
---|
| 1336 | }
|
---|
| 1337 |
|
---|
| 1338 | vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
|
---|
| 1339 | }
|
---|
[57253] | 1340 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[11935] | 1341 | }
|
---|
| 1342 |
|
---|
| 1343 |
|
---|
| 1344 | int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
|
---|
| 1345 | {
|
---|
| 1346 | /* Nothing to do here. */
|
---|
[63512] | 1347 | RT_NOREF(pThis);
|
---|
[11935] | 1348 | return VINF_SUCCESS;
|
---|
| 1349 | }
|
---|
| 1350 |
|
---|
| 1351 |
|
---|
| 1352 | int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
|
---|
| 1353 | {
|
---|
| 1354 | /* Nothing to do here. */
|
---|
[63512] | 1355 | RT_NOREF(pThis);
|
---|
[11935] | 1356 | return VINF_SUCCESS;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 |
|
---|
| 1360 | void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
|
---|
| 1361 | {
|
---|
[57253] | 1362 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[11935] | 1363 |
|
---|
| 1364 | /*
|
---|
| 1365 | * Carefully obtain the interface filter reference and detach it.
|
---|
| 1366 | */
|
---|
[40806] | 1367 | RTSpinlockAcquire(pThis->hSpinlock);
|
---|
[57253] | 1368 | interface_filter_t pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
|
---|
[11935] | 1369 | if (pIfFilter)
|
---|
[30112] | 1370 | ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
|
---|
[52618] | 1371 | RTSpinlockRelease(pThis->hSpinlock);
|
---|
[11935] | 1372 |
|
---|
| 1373 | if (pIfFilter)
|
---|
| 1374 | iflt_detach(pIfFilter);
|
---|
[55652] | 1375 |
|
---|
| 1376 | if (pThis->u.s.pSysSock != NULL)
|
---|
| 1377 | {
|
---|
| 1378 | sock_close(pThis->u.s.pSysSock);
|
---|
| 1379 | pThis->u.s.pSysSock = NULL;
|
---|
| 1380 | }
|
---|
[57253] | 1381 |
|
---|
| 1382 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[11935] | 1383 | }
|
---|
| 1384 |
|
---|
| 1385 |
|
---|
[17186] | 1386 | int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
|
---|
[11935] | 1387 | {
|
---|
[17186] | 1388 | NOREF(pvContext);
|
---|
[55652] | 1389 |
|
---|
| 1390 | int rc = vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
|
---|
| 1391 | if (RT_FAILURE(rc))
|
---|
| 1392 | return rc;
|
---|
| 1393 |
|
---|
| 1394 | if (pThis->pSwitchPort->pfnNotifyHostAddress == NULL)
|
---|
| 1395 | return rc;
|
---|
| 1396 |
|
---|
| 1397 | /*
|
---|
| 1398 | * XXX: uwe
|
---|
| 1399 | *
|
---|
| 1400 | * Learn host's IP addresses and set up notifications for changes.
|
---|
| 1401 | * To avoid racing, set up notifications first.
|
---|
| 1402 | *
|
---|
| 1403 | * XXX: This should probably be global, since the only thing
|
---|
| 1404 | * specific to ifnet here is its IPv6 link-local address.
|
---|
| 1405 | */
|
---|
[57253] | 1406 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[55652] | 1407 | errno_t error;
|
---|
| 1408 |
|
---|
[57253] | 1409 | /** @todo reorg code to not have numerous returns with duplicate code... */
|
---|
[55652] | 1410 | error = sock_socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT,
|
---|
| 1411 | vboxNetFltDarwinSysSockUpcall, pThis,
|
---|
| 1412 | &pThis->u.s.pSysSock);
|
---|
| 1413 | if (error != 0)
|
---|
| 1414 | {
|
---|
| 1415 | LogRel(("sock_socket(SYSPROTO_EVENT): error %d\n", error));
|
---|
[57253] | 1416 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[55652] | 1417 | return rc;
|
---|
| 1418 | }
|
---|
| 1419 |
|
---|
| 1420 | int nbio = 1;
|
---|
| 1421 | error = sock_ioctl(pThis->u.s.pSysSock, FIONBIO, &nbio);
|
---|
| 1422 | if (error != 0)
|
---|
| 1423 | {
|
---|
| 1424 | LogRel(("FIONBIO: error %d\n", error));
|
---|
| 1425 | sock_close(pThis->u.s.pSysSock);
|
---|
[57253] | 1426 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[55652] | 1427 | return rc;
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | if (!sock_isnonblocking(pThis->u.s.pSysSock))
|
---|
| 1431 | {
|
---|
| 1432 | LogRel(("FIONBIO ok, but socket is blocking?!\n"));
|
---|
| 1433 | sock_close(pThis->u.s.pSysSock);
|
---|
[57253] | 1434 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[56315] | 1435 | return rc;
|
---|
[55652] | 1436 | }
|
---|
| 1437 |
|
---|
| 1438 | struct kev_request req;
|
---|
| 1439 | req.vendor_code = KEV_VENDOR_APPLE;
|
---|
| 1440 | req.kev_class = KEV_NETWORK_CLASS;
|
---|
| 1441 | req.kev_subclass = KEV_ANY_SUBCLASS; /* need both INET and INET6, so have to request all */
|
---|
| 1442 |
|
---|
| 1443 | error = sock_ioctl(pThis->u.s.pSysSock, SIOCSKEVFILT, &req);
|
---|
| 1444 | if (error != 0)
|
---|
| 1445 | {
|
---|
| 1446 | LogRel(("SIOCSKEVFILT: error %d\n", error));
|
---|
| 1447 | sock_close(pThis->u.s.pSysSock);
|
---|
[57253] | 1448 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[55652] | 1449 | return rc;
|
---|
| 1450 | }
|
---|
| 1451 |
|
---|
| 1452 | ifnet_t pIfNet = pThis->u.s.pIfNet; /* already retained */
|
---|
| 1453 |
|
---|
| 1454 | ifaddr_t *pIfAddrList;
|
---|
| 1455 | error = ifnet_get_address_list(/* all interfaces*/ NULL, &pIfAddrList);
|
---|
| 1456 | if (error != 0)
|
---|
| 1457 | {
|
---|
| 1458 | LogRel(("ifnet_get_address_list: error %d\n", error));
|
---|
[57253] | 1459 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[55652] | 1460 | return rc;
|
---|
| 1461 | }
|
---|
| 1462 |
|
---|
| 1463 | for (ifaddr_t *pIfAddr = pIfAddrList; *pIfAddr != NULL; ++pIfAddr)
|
---|
| 1464 | {
|
---|
| 1465 | ifaddr_t ifa = *pIfAddr;
|
---|
| 1466 | sa_family_t family = ifaddr_address_family(ifa);
|
---|
| 1467 | struct sockaddr_storage ss;
|
---|
| 1468 |
|
---|
| 1469 | error = ifaddr_address(ifa, (struct sockaddr *)&ss, sizeof(ss));
|
---|
| 1470 | if (error != 0)
|
---|
| 1471 | {
|
---|
| 1472 | LogRel(("getting address family %d: error %d\n", family, error));
|
---|
| 1473 | continue;
|
---|
| 1474 | }
|
---|
| 1475 |
|
---|
| 1476 | if (family == AF_INET)
|
---|
| 1477 | {
|
---|
| 1478 | struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
---|
| 1479 | u_int32_t u32Addr = ntohl(sin->sin_addr.s_addr);
|
---|
| 1480 |
|
---|
[56281] | 1481 | if (VBOX_IN_LOOPBACK(u32Addr))
|
---|
[55652] | 1482 | continue;
|
---|
| 1483 |
|
---|
[56281] | 1484 | if (ifaddr_ifnet(ifa) != pIfNet && VBOX_IN_LINKLOCAL(u32Addr))
|
---|
| 1485 | continue;
|
---|
| 1486 |
|
---|
[55652] | 1487 | Log(("> inet %RTnaipv4\n", sin->sin_addr.s_addr));
|
---|
| 1488 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
| 1489 | /* :fAdded */ true, kIntNetAddrType_IPv4, &sin->sin_addr);
|
---|
| 1490 | }
|
---|
| 1491 | else if (family == AF_INET6)
|
---|
| 1492 | {
|
---|
| 1493 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
---|
| 1494 |
|
---|
| 1495 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
|
---|
| 1496 | continue;
|
---|
| 1497 |
|
---|
| 1498 | /* link-local from other interfaces are out of scope */
|
---|
[56281] | 1499 | if (ifaddr_ifnet(ifa) != pIfNet && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
|
---|
[55652] | 1500 | continue;
|
---|
| 1501 |
|
---|
| 1502 | Log(("> inet6 %RTnaipv6\n", &sin6->sin6_addr));
|
---|
| 1503 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
| 1504 | /* :fAdded */ true, kIntNetAddrType_IPv6, &sin6->sin6_addr);
|
---|
| 1505 | }
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | ifnet_free_address_list(pIfAddrList);
|
---|
| 1509 |
|
---|
| 1510 | /*
|
---|
| 1511 | * Now that we've got current addresses, check for events that
|
---|
| 1512 | * might have happened while we were working.
|
---|
| 1513 | */
|
---|
| 1514 | vboxNetFltDarwinSysSockUpcall(pThis->u.s.pSysSock, pThis, MBUF_DONTWAIT);
|
---|
| 1515 |
|
---|
[57253] | 1516 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[55652] | 1517 | return rc;
|
---|
[11935] | 1518 | }
|
---|
| 1519 |
|
---|
| 1520 |
|
---|
[55652] | 1521 | static void vboxNetFltDarwinSysSockUpcall(socket_t pSysSock, void *pvData, int fWait)
|
---|
| 1522 | {
|
---|
| 1523 | PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvData;
|
---|
| 1524 | errno_t error;
|
---|
| 1525 |
|
---|
| 1526 | NOREF(fWait);
|
---|
| 1527 |
|
---|
| 1528 | if (RT_UNLIKELY(pSysSock != pThis->u.s.pSysSock))
|
---|
| 1529 | {
|
---|
| 1530 | Log(("vboxNetFltDarwinSysSockUpcall: %p != %p?\n",
|
---|
| 1531 | pSysSock, pThis->u.s.pSysSock));
|
---|
| 1532 | return;
|
---|
| 1533 | }
|
---|
| 1534 |
|
---|
[56281] | 1535 | ifnet_t pIfNet = pThis->u.s.pIfNet; /* XXX: retain? */
|
---|
| 1536 | ifnet_family_t if_family = ifnet_family(pIfNet);
|
---|
| 1537 | u_int32_t if_unit = ifnet_unit(pIfNet);
|
---|
| 1538 |
|
---|
[57251] | 1539 | for (;;)
|
---|
| 1540 | {
|
---|
[55652] | 1541 | mbuf_t m;
|
---|
[57251] | 1542 | size_t len = sizeof(struct kern_event_msg) - sizeof(u_int32_t) + sizeof(struct kev_in6_data);
|
---|
[55652] | 1543 |
|
---|
| 1544 | error = sock_receivembuf(pSysSock, NULL, &m, 0, &len);
|
---|
[57251] | 1545 | if (error != 0)
|
---|
[55652] | 1546 | {
|
---|
[57251] | 1547 | if (error == EWOULDBLOCK)
|
---|
| 1548 | {
|
---|
| 1549 | Log(("vboxNetFltDarwinSysSockUpcall: EWOULDBLOCK - we are done\n"));
|
---|
| 1550 | error = 0;
|
---|
| 1551 | }
|
---|
| 1552 | else
|
---|
| 1553 | Log(("sock_receivembuf: error %d\n", error));
|
---|
[55652] | 1554 | break;
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 | if (len < sizeof(struct kern_event_msg) - sizeof(u_int32_t))
|
---|
| 1558 | {
|
---|
[57251] | 1559 | Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short\n", (unsigned int)len));
|
---|
[55652] | 1560 | mbuf_freem(m);
|
---|
| 1561 | return;
|
---|
| 1562 | }
|
---|
| 1563 |
|
---|
| 1564 | struct kern_event_msg *msg = (struct kern_event_msg *)mbuf_data(m);
|
---|
| 1565 | if (msg->kev_subclass == KEV_INET_SUBCLASS)
|
---|
| 1566 | {
|
---|
| 1567 | if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in_data))
|
---|
| 1568 | {
|
---|
| 1569 | Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET_SUBCLASS\n",
|
---|
| 1570 | (unsigned int)len));
|
---|
| 1571 | mbuf_freem(m);
|
---|
| 1572 | return;
|
---|
| 1573 | }
|
---|
| 1574 |
|
---|
| 1575 | struct kev_in_data *iev = (struct kev_in_data *)msg->event_data;
|
---|
[56266] | 1576 | struct net_event_data *link = &iev->link_data;
|
---|
[55652] | 1577 | PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev->ia_addr;
|
---|
[56281] | 1578 | u_int32_t u32Addr = ntohl(pAddr->IPv4.u);
|
---|
| 1579 |
|
---|
| 1580 | if (VBOX_IN_LOOPBACK(u32Addr))
|
---|
| 1581 | {
|
---|
| 1582 | mbuf_freem(m);
|
---|
| 1583 | continue;
|
---|
| 1584 | }
|
---|
| 1585 |
|
---|
| 1586 | if ( (link->if_family != if_family || link->if_unit != if_unit)
|
---|
| 1587 | && VBOX_IN_LINKLOCAL(u32Addr))
|
---|
| 1588 | {
|
---|
| 1589 | mbuf_freem(m);
|
---|
| 1590 | continue;
|
---|
| 1591 | }
|
---|
| 1592 |
|
---|
[55652] | 1593 | switch (msg->event_code)
|
---|
| 1594 | {
|
---|
| 1595 | case KEV_INET_NEW_ADDR:
|
---|
[57251] | 1596 | Log(("KEV_INET_NEW_ADDR %.*s%d: %RTnaipv4\n", IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
|
---|
| 1597 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, true /*fAdded*/, kIntNetAddrType_IPv4, pAddr);
|
---|
[55652] | 1598 | break;
|
---|
| 1599 |
|
---|
| 1600 | case KEV_INET_ADDR_DELETED:
|
---|
[57251] | 1601 | Log(("KEV_INET_ADDR_DELETED %.*s%d: %RTnaipv4\n", IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
|
---|
| 1602 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, false /*fAdded*/, kIntNetAddrType_IPv4, pAddr);
|
---|
[55652] | 1603 | break;
|
---|
| 1604 |
|
---|
| 1605 | default:
|
---|
[56266] | 1606 | Log(("KEV INET event %u %.*s%d: addr %RTnaipv4\n",
|
---|
[56281] | 1607 | msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
|
---|
[55652] | 1608 | break;
|
---|
| 1609 | }
|
---|
| 1610 | }
|
---|
| 1611 | else if (msg->kev_subclass == KEV_INET6_SUBCLASS)
|
---|
| 1612 | {
|
---|
| 1613 | if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in6_data))
|
---|
| 1614 | {
|
---|
| 1615 | Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET6_SUBCLASS\n",
|
---|
| 1616 | (unsigned int)len));
|
---|
| 1617 | mbuf_freem(m);
|
---|
| 1618 | return;
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | struct kev_in6_data *iev6 = (struct kev_in6_data *)msg->event_data;
|
---|
[56266] | 1622 | struct net_event_data *link = &iev6->link_data;
|
---|
[55652] | 1623 | PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev6->ia_addr.sin6_addr;
|
---|
[56281] | 1624 |
|
---|
| 1625 | if (IN6_IS_ADDR_LOOPBACK(&iev6->ia_addr.sin6_addr))
|
---|
| 1626 | {
|
---|
| 1627 | mbuf_freem(m);
|
---|
| 1628 | continue;
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
| 1631 | if ( (link->if_family != if_family || link->if_unit != if_unit)
|
---|
| 1632 | && IN6_IS_ADDR_LINKLOCAL(&iev6->ia_addr.sin6_addr))
|
---|
| 1633 | {
|
---|
| 1634 | mbuf_freem(m);
|
---|
| 1635 | continue;
|
---|
| 1636 | }
|
---|
| 1637 |
|
---|
[55652] | 1638 | switch (msg->event_code)
|
---|
| 1639 | {
|
---|
| 1640 | case KEV_INET6_NEW_USER_ADDR:
|
---|
[56266] | 1641 | Log(("KEV_INET6_NEW_USER_ADDR %.*s%d: %RTnaipv6\n",
|
---|
| 1642 | IFNAMSIZ, link->if_name, link->if_unit, pAddr));
|
---|
[55652] | 1643 | goto kev_inet6_new;
|
---|
| 1644 |
|
---|
| 1645 | case KEV_INET6_NEW_LL_ADDR:
|
---|
[56266] | 1646 | Log(("KEV_INET6_NEW_LL_ADDR %.*s%d: %RTnaipv6\n",
|
---|
| 1647 | IFNAMSIZ, link->if_name, link->if_unit, pAddr));
|
---|
[55652] | 1648 | goto kev_inet6_new;
|
---|
| 1649 |
|
---|
| 1650 | case KEV_INET6_NEW_RTADV_ADDR:
|
---|
[56266] | 1651 | Log(("KEV_INET6_NEW_RTADV_ADDR %.*s%d: %RTnaipv6\n",
|
---|
| 1652 | IFNAMSIZ, link->if_name, link->if_unit, pAddr));
|
---|
[55652] | 1653 | goto kev_inet6_new;
|
---|
| 1654 |
|
---|
| 1655 | kev_inet6_new:
|
---|
| 1656 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
| 1657 | /* :fAdded */ true, kIntNetAddrType_IPv6, pAddr);
|
---|
| 1658 | break;
|
---|
| 1659 |
|
---|
| 1660 | case KEV_INET6_ADDR_DELETED:
|
---|
[56266] | 1661 | Log(("KEV_INET6_ADDR_DELETED %.*s%d: %RTnaipv6\n",
|
---|
| 1662 | IFNAMSIZ, link->if_name, link->if_unit, pAddr));
|
---|
| 1663 |
|
---|
[55652] | 1664 | pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
|
---|
| 1665 | /* :fAdded */ false, kIntNetAddrType_IPv6, pAddr);
|
---|
| 1666 | break;
|
---|
| 1667 |
|
---|
| 1668 | default:
|
---|
[56266] | 1669 | Log(("KEV INET6 event %u %.*s%d: addr %RTnaipv6\n",
|
---|
| 1670 | msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr));
|
---|
[55652] | 1671 | break;
|
---|
| 1672 | }
|
---|
| 1673 | }
|
---|
| 1674 | else
|
---|
[57251] | 1675 | Log(("vboxNetFltDarwinSysSockUpcall: subclass %u ignored\n", (unsigned)msg->kev_subclass));
|
---|
[55652] | 1676 |
|
---|
| 1677 | mbuf_freem(m);
|
---|
| 1678 | }
|
---|
| 1679 | }
|
---|
| 1680 |
|
---|
| 1681 |
|
---|
[11935] | 1682 | int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
|
---|
| 1683 | {
|
---|
| 1684 | /*
|
---|
| 1685 | * Init the darwin specific members.
|
---|
| 1686 | */
|
---|
| 1687 | pThis->u.s.pIfNet = NULL;
|
---|
| 1688 | pThis->u.s.pIfFilter = NULL;
|
---|
| 1689 | pThis->u.s.fSetPromiscuous = false;
|
---|
| 1690 | pThis->u.s.fNeedSetPromiscuous = false;
|
---|
[28666] | 1691 | //pThis->u.s.MacAddr = {0};
|
---|
[55652] | 1692 | pThis->u.s.pSysSock = NULL;
|
---|
[11935] | 1693 |
|
---|
| 1694 | return VINF_SUCCESS;
|
---|
| 1695 | }
|
---|
| 1696 |
|
---|
[29491] | 1697 |
|
---|
[29662] | 1698 | void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
|
---|
[29491] | 1699 | {
|
---|
[29662] | 1700 | NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
|
---|
[29491] | 1701 | }
|
---|
| 1702 |
|
---|
| 1703 |
|
---|
[29662] | 1704 | int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
|
---|
[29491] | 1705 | {
|
---|
| 1706 | /* Nothing to do */
|
---|
[29662] | 1707 | NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
|
---|
[29491] | 1708 | return VINF_SUCCESS;
|
---|
| 1709 | }
|
---|
| 1710 |
|
---|
| 1711 |
|
---|
[29662] | 1712 | int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
|
---|
[29491] | 1713 | {
|
---|
| 1714 | /* Nothing to do */
|
---|
[29662] | 1715 | NOREF(pThis); NOREF(pvIfData);
|
---|
[29491] | 1716 | return VINF_SUCCESS;
|
---|
| 1717 | }
|
---|
| 1718 |
|
---|