VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp@ 50410

Last change on this file since 50410 was 50410, checked in by vboxsync, 11 years ago

OS X host: stuck-in-dock: move include/VBox/VBoxNetSend.h to src/VBox/HostDrivers/darwin/VBoxNetSend.h.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 42.9 KB
Line 
1/* $Id: VBoxNetFlt-darwin.cpp 50410 2014-02-11 09:37:33Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Darwin Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
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.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21/*
22 * Deal with conflicts first.
23 * PVM - BSD mess, that FreeBSD has correct a long time ago.
24 * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
25 */
26#include <iprt/types.h>
27#include <sys/param.h>
28#undef PVM
29
30#include <IOKit/IOLib.h> /* Assert as function */
31
32#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
33#include <VBox/log.h>
34#include <VBox/err.h>
35#include <VBox/intnetinline.h>
36#include <VBox/version.h>
37#include <iprt/initterm.h>
38#include <iprt/assert.h>
39#include <iprt/spinlock.h>
40#include <iprt/semaphore.h>
41#include <iprt/process.h>
42#include <iprt/alloc.h>
43#include <iprt/alloca.h>
44#include <iprt/time.h>
45#include <iprt/net.h>
46#include <iprt/thread.h>
47
48#include "../../darwin/VBoxNetSend.h"
49
50#include <mach/kmod.h>
51#include <sys/conf.h>
52#include <sys/errno.h>
53#include <sys/ioccom.h>
54#include <sys/malloc.h>
55#include <sys/proc.h>
56#include <sys/socket.h>
57#include <sys/sockio.h>
58#include <sys/kern_event.h>
59#include <net/kpi_interface.h>
60RT_C_DECLS_BEGIN /* Buggy 10.4 headers, fixed in 10.5. */
61#include <sys/kpi_mbuf.h>
62#include <net/kpi_interfacefilter.h>
63RT_C_DECLS_END
64#include <net/if.h>
65
66#define VBOXNETFLT_OS_SPECFIC 1
67#include "../VBoxNetFltInternal.h"
68
69
70/*******************************************************************************
71* Defined Constants And Macros *
72*******************************************************************************/
73/** The maximum number of SG segments.
74 * Used to prevent stack overflow and similar bad stuff. */
75#define VBOXNETFLT_DARWIN_MAX_SEGS 32
76
77#if 0
78/** For testing extremely segmented frames. */
79#define VBOXNETFLT_DARWIN_TEST_SEG_SIZE 14
80#endif
81
82
83/*******************************************************************************
84* Internal Functions *
85*******************************************************************************/
86RT_C_DECLS_BEGIN
87static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData);
88static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData);
89RT_C_DECLS_END
90
91
92/*******************************************************************************
93* Structures and Typedefs *
94*******************************************************************************/
95/**
96 * The mbuf tag data.
97 *
98 * We have to associate the ethernet header with each packet we're sending
99 * because things like icmp will inherit the tag it self so the tag along
100 * isn't sufficient to identify our mbufs. For the icmp scenario the ethernet
101 * header naturally changes before the packet is send pack, so let check it.
102 */
103typedef struct VBOXNETFLTTAG
104{
105 /** The ethernet header of the outgoing frame. */
106 RTNETETHERHDR EthHdr;
107} VBOXNETFLTTAG;
108/** Pointer to a VBoxNetFlt mbuf tag. */
109typedef VBOXNETFLTTAG *PVBOXNETFLTTAG;
110/** Pointer to a const VBoxNetFlt mbuf tag. */
111typedef VBOXNETFLTTAG const *PCVBOXNETFLTTAG;
112
113
114/*******************************************************************************
115* Global Variables *
116*******************************************************************************/
117/**
118 * Declare the module stuff.
119 */
120RT_C_DECLS_BEGIN
121extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
122extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
123
124KMOD_EXPLICIT_DECL(VBoxNetFlt, VBOX_VERSION_STRING, _start, _stop)
125DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxNetFltDarwinStart;
126DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxNetFltDarwinStop;
127DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
128RT_C_DECLS_END
129
130
131/**
132 * The (common) global data.
133 */
134static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
135
136/** The unique tag id for this module.
137 * This is basically a unique string hash that lives on until reboot.
138 * It is used for tagging mbufs. */
139static mbuf_tag_id_t g_idTag;
140
141/** the offset of the struct ifnet::if_pcount variable. */
142static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
143 + sizeof(u_long) /*if_refcnt*/;
144/** Macro for accessing ifnet::if_pcount. */
145#define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
146
147
148/**
149 * Start the kernel module.
150 */
151static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData)
152{
153 int rc;
154
155 /*
156 * Initialize IPRT and find our module tag id.
157 * (IPRT is shared with VBoxDrv, it creates the loggers.)
158 */
159 rc = RTR0Init(0);
160 if (RT_SUCCESS(rc))
161 {
162 Log(("VBoxNetFltDarwinStart\n"));
163 errno_t err = mbuf_tag_id_find("org.VirtualBox.kext.VBoxFltDrv", &g_idTag);
164 if (!err)
165 {
166 /*
167 * Initialize the globals and connect to the support driver.
168 *
169 * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
170 * for establishing the connect to the support driver.
171 */
172 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
173 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
174 if (RT_SUCCESS(rc))
175 {
176 LogRel(("VBoxFltDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
177 return KMOD_RETURN_SUCCESS;
178 }
179
180 LogRel(("VBoxFltDrv: failed to initialize device extension (rc=%d)\n", rc));
181 }
182 else
183 LogRel(("VBoxFltDrv: mbuf_tag_id_find failed, err=%d\n", err));
184 RTR0Term();
185 }
186 else
187 printf("VBoxFltDrv: failed to initialize IPRT (rc=%d)\n", rc);
188
189 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
190 return KMOD_RETURN_FAILURE;
191}
192
193
194/**
195 * Stop the kernel module.
196 */
197static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData)
198{
199 Log(("VBoxNetFltDarwinStop\n"));
200
201 /*
202 * Refuse to unload if anyone is currently using the filter driver.
203 * This is important as I/O kit / xnu will to be able to do usage
204 * tracking for us!
205 */
206 int rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
207 if (RT_FAILURE(rc))
208 {
209 Log(("VBoxNetFltDarwinStop - failed, busy.\n"));
210 return KMOD_RETURN_FAILURE;
211 }
212
213 /*
214 * Undo the work done during start (in reverse order).
215 */
216 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
217
218 RTR0Term();
219
220 return KMOD_RETURN_SUCCESS;
221}
222
223
224/**
225 * Reads and retains the host interface handle.
226 *
227 * @returns The handle, NULL if detached.
228 * @param pThis
229 */
230DECLINLINE(ifnet_t) vboxNetFltDarwinRetainIfNet(PVBOXNETFLTINS pThis)
231{
232 ifnet_t pIfNet = NULL;
233
234 /*
235 * Be careful here to avoid problems racing the detached callback.
236 */
237 RTSpinlockAcquire(pThis->hSpinlock);
238 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
239 {
240 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
241 if (pIfNet)
242 ifnet_reference(pIfNet);
243 }
244 RTSpinlockReleaseNoInts(pThis->hSpinlock);
245
246 return pIfNet;
247}
248
249
250/**
251 * Release the host interface handle previously retained
252 * by vboxNetFltDarwinRetainIfNet.
253 *
254 * @param pThis The instance.
255 * @param pIfNet The vboxNetFltDarwinRetainIfNet return value, NULL is fine.
256 */
257DECLINLINE(void) vboxNetFltDarwinReleaseIfNet(PVBOXNETFLTINS pThis, ifnet_t pIfNet)
258{
259 NOREF(pThis);
260 if (pIfNet)
261 ifnet_release(pIfNet);
262}
263
264
265/**
266 * Checks whether this is an mbuf created by vboxNetFltDarwinMBufFromSG,
267 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
268 *
269 * @returns true / false accordingly.
270 * @param pThis The instance.
271 * @param pMBuf The mbuf.
272 * @param pvFrame The frame pointer, optional.
273 */
274DECLINLINE(bool) vboxNetFltDarwinMBufIsOur(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
275{
276 NOREF(pThis);
277
278 /*
279 * Lookup the tag set by vboxNetFltDarwinMBufFromSG.
280 */
281 PCVBOXNETFLTTAG pTagData;
282 size_t cbTagData;
283 errno_t err = mbuf_tag_find(pMBuf, g_idTag, 0 /* type */, &cbTagData, (void **)&pTagData);
284 if (err)
285 return false;
286 AssertReturn(cbTagData == sizeof(*pTagData), false);
287
288 /*
289 * Dig out the ethernet header from the mbuf.
290 */
291 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
292 if (!pEthHdr)
293 pEthHdr = (PCRTNETETHERHDR)mbuf_pkthdr_header(pMBuf);
294 if (!pEthHdr)
295 pEthHdr = (PCRTNETETHERHDR)mbuf_data(pMBuf);
296 /* ASSUMING that there is enough data to work on! */
297 if ( pEthHdr->DstMac.au8[0] != pTagData->EthHdr.DstMac.au8[0]
298 || pEthHdr->DstMac.au8[1] != pTagData->EthHdr.DstMac.au8[1]
299 || pEthHdr->DstMac.au8[2] != pTagData->EthHdr.DstMac.au8[2]
300 || pEthHdr->DstMac.au8[3] != pTagData->EthHdr.DstMac.au8[3]
301 || pEthHdr->DstMac.au8[4] != pTagData->EthHdr.DstMac.au8[4]
302 || pEthHdr->DstMac.au8[5] != pTagData->EthHdr.DstMac.au8[5]
303 || pEthHdr->SrcMac.au8[0] != pTagData->EthHdr.SrcMac.au8[0]
304 || pEthHdr->SrcMac.au8[1] != pTagData->EthHdr.SrcMac.au8[1]
305 || pEthHdr->SrcMac.au8[2] != pTagData->EthHdr.SrcMac.au8[2]
306 || pEthHdr->SrcMac.au8[3] != pTagData->EthHdr.SrcMac.au8[3]
307 || pEthHdr->SrcMac.au8[4] != pTagData->EthHdr.SrcMac.au8[4]
308 || pEthHdr->SrcMac.au8[5] != pTagData->EthHdr.SrcMac.au8[5]
309 || pEthHdr->EtherType != pTagData->EthHdr.EtherType)
310 {
311 Log3(("tagged, but the ethernet header has changed\n"));
312 return false;
313 }
314
315 return true;
316}
317
318
319/**
320 * Internal worker that create a darwin mbuf for a (scatter/)gather list.
321 *
322 * @returns Pointer to the mbuf.
323 * @param pThis The instance.
324 * @param pSG The (scatter/)gather list.
325 */
326static mbuf_t vboxNetFltDarwinMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
327{
328 /// @todo future? mbuf_how_t How = preemption enabled ? MBUF_DONTWAIT : MBUF_WAITOK;
329 mbuf_how_t How = MBUF_WAITOK;
330
331 /*
332 * We need some way of getting back to our instance data when
333 * the mbuf is freed, so use pvUserData for this.
334 * -- this is not relevant anylonger! --
335 */
336 Assert(!pSG->pvUserData || pSG->pvUserData == pThis);
337 Assert(!pSG->pvUserData2);
338 pSG->pvUserData = pThis;
339
340 /*
341 * Allocate a packet and copy over the data.
342 *
343 * Using mbuf_attachcluster() here would've been nice but there are two
344 * issues with it: (1) it's 10.5.x only, and (2) the documentation indicates
345 * that it's not supposed to be used for really external buffers. The 2nd
346 * point might be argued against considering that the only m_clattach user
347 * is mallocs memory for the ext mbuf and not doing what's stated in the docs.
348 * However, it's hard to tell if these m_clattach buffers actually makes it
349 * to the NICs or not, and even if they did, the NIC would need the physical
350 * addresses for the pages they contain and might end up copying the data
351 * to a new mbuf anyway.
352 *
353 * So, in the end it's better to just do it the simple way that will work
354 * 100%, even if it involves some extra work (alloc + copy) we really wished
355 * to avoid.
356 *
357 * Note. We can't make use of the physical addresses on darwin because the
358 * way the mbuf / cluster stuff works (see mbuf_data_to_physical and
359 * mcl_to_paddr).
360 */
361 mbuf_t pPkt = NULL;
362 errno_t err = mbuf_allocpacket(How, pSG->cbTotal, NULL, &pPkt);
363 if (!err)
364 {
365 /* Skip zero sized memory buffers (paranoia). */
366 mbuf_t pCur = pPkt;
367 while (pCur && !mbuf_maxlen(pCur))
368 pCur = mbuf_next(pCur);
369 Assert(pCur);
370
371 /* Set the required packet header attributes. */
372 mbuf_pkthdr_setlen(pPkt, pSG->cbTotal);
373 mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
374
375 /* Special case the single buffer copy. */
376 if ( mbuf_next(pCur)
377 && mbuf_maxlen(pCur) >= pSG->cbTotal)
378 {
379 mbuf_setlen(pCur, pSG->cbTotal);
380 IntNetSgRead(pSG, mbuf_data(pCur));
381 }
382 else
383 {
384 /* Multi buffer copying. */
385 size_t cbLeft = pSG->cbTotal;
386 size_t offSrc = 0;
387 while (cbLeft > 0 && pCur)
388 {
389 size_t cb = mbuf_maxlen(pCur);
390 if (cb > cbLeft)
391 cb = cbLeft;
392 mbuf_setlen(pCur, cb);
393 IntNetSgReadEx(pSG, offSrc, cb, mbuf_data(pCur));
394
395 /* advance */
396 offSrc += cb;
397 cbLeft -= cb;
398 pCur = mbuf_next(pCur);
399 }
400 Assert(cbLeft == 0);
401 }
402 if (!err)
403 {
404 /*
405 * Tag the packet and return successfully.
406 */
407 PVBOXNETFLTTAG pTagData;
408 err = mbuf_tag_allocate(pPkt, g_idTag, 0 /* type */, sizeof(VBOXNETFLTTAG) /* tag len */, How, (void **)&pTagData);
409 if (!err)
410 {
411 Assert(pSG->aSegs[0].cb >= sizeof(pTagData->EthHdr));
412 memcpy(&pTagData->EthHdr, pSG->aSegs[0].pv, sizeof(pTagData->EthHdr));
413 return pPkt;
414 }
415
416 /* bailout: */
417 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
418 }
419
420 mbuf_freem(pPkt);
421 }
422 else
423 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
424 pSG->pvUserData = NULL;
425
426 return NULL;
427}
428
429
430/**
431 * Calculates the number of segments required to represent the mbuf.
432 *
433 * @returns Number of segments.
434 * @param pThis The instance.
435 * @param pMBuf The mbuf.
436 * @param pvFrame The frame pointer, optional.
437 */
438DECLINLINE(unsigned) vboxNetFltDarwinMBufCalcSGSegs(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
439{
440 NOREF(pThis);
441
442 /*
443 * Count the buffers in the chain.
444 */
445 unsigned cSegs = 0;
446 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
447 if (mbuf_len(pCur))
448 cSegs++;
449 else if ( !cSegs
450 && pvFrame
451 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
452 cSegs++;
453
454#ifdef PADD_RUNT_FRAMES_FROM_HOST
455 /*
456 * Add one buffer if the total is less than the ethernet minimum 60 bytes.
457 * This may allocate a segment too much if the ethernet header is separated,
458 * but that shouldn't harm us much.
459 */
460 if (mbuf_pkthdr_len(pMBuf) < 60)
461 cSegs++;
462#endif
463
464#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
465 /* maximize the number of segments. */
466 cSegs = RT_MAX(VBOXNETFLT_DARWIN_MAX_SEGS - 1, cSegs);
467#endif
468
469 return cSegs ? cSegs : 1;
470}
471
472
473/**
474 * Initializes a SG list from an mbuf.
475 *
476 * @returns Number of segments.
477 * @param pThis The instance.
478 * @param pMBuf The mbuf.
479 * @param pSG The SG.
480 * @param pvFrame The frame pointer, optional.
481 * @param cSegs The number of segments allocated for the SG.
482 * This should match the number in the mbuf exactly!
483 * @param fSrc The source of the frame.
484 */
485DECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
486{
487 NOREF(pThis);
488
489 /*
490 * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
491 */
492 unsigned iSeg = 0;
493 IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
494 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
495 {
496 size_t cbSeg = mbuf_len(pCur);
497 if (cbSeg)
498 {
499 void *pvSeg = mbuf_data(pCur);
500
501 /* deal with pvFrame */
502 if (!iSeg && pvFrame && pvFrame != pvSeg)
503 {
504 void *pvStart = mbuf_datastart(pMBuf);
505 uintptr_t offSeg = (uintptr_t)pvSeg - (uintptr_t)pvStart;
506 uintptr_t offSegEnd = offSeg + cbSeg;
507 Assert(pvStart && pvSeg && offSeg < mbuf_maxlen(pMBuf) && offSegEnd <= mbuf_maxlen(pMBuf)); NOREF(offSegEnd);
508 uintptr_t offFrame = (uintptr_t)pvFrame - (uintptr_t)pvStart;
509 if (RT_LIKELY(offFrame < offSeg))
510 {
511 pvSeg = pvFrame;
512 cbSeg += offSeg - offFrame;
513 }
514 else
515 AssertMsgFailed(("pvFrame=%p pvStart=%p pvSeg=%p offSeg=%p cbSeg=%#zx offSegEnd=%p offFrame=%p maxlen=%#zx\n",
516 pvFrame, pvStart, pvSeg, offSeg, cbSeg, offSegEnd, offFrame, mbuf_maxlen(pMBuf)));
517 pvFrame = NULL;
518 }
519
520 AssertBreak(iSeg < cSegs);
521 pSG->cbTotal += cbSeg;
522 pSG->aSegs[iSeg].cb = cbSeg;
523 pSG->aSegs[iSeg].pv = pvSeg;
524 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
525 iSeg++;
526 }
527 /* The pvFrame might be in a now empty buffer. */
528 else if ( !iSeg
529 && pvFrame
530 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
531 {
532 cbSeg = (uintptr_t)mbuf_datastart(pMBuf) + mbuf_maxlen(pMBuf) - (uintptr_t)pvFrame;
533 pSG->cbTotal += cbSeg;
534 pSG->aSegs[iSeg].cb = cbSeg;
535 pSG->aSegs[iSeg].pv = pvFrame;
536 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
537 iSeg++;
538 pvFrame = NULL;
539 }
540 }
541
542 Assert(iSeg && iSeg <= cSegs);
543 pSG->cSegsUsed = iSeg;
544
545#ifdef PADD_RUNT_FRAMES_FROM_HOST
546 /*
547 * Add a trailer if the frame is too small.
548 *
549 * Since we're getting to the packet before it is framed, it has not
550 * yet been padded. The current solution is to add a segment pointing
551 * to a buffer containing all zeros and pray that works for all frames...
552 */
553 if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
554 {
555 AssertReturnVoid(iSeg < cSegs);
556
557 static uint8_t const s_abZero[128] = {0};
558 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
559 pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
560 pSG->aSegs[iSeg].cb = 60 - pSG->cbTotal;
561 pSG->cbTotal = 60;
562 pSG->cSegsUsed++;
563 }
564#endif
565
566#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
567 /*
568 * Redistribute the segments.
569 */
570 if (pSG->cSegsUsed < pSG->cSegsAlloc)
571 {
572 /* copy the segments to the end. */
573 int iSrc = pSG->cSegsUsed;
574 int iDst = pSG->cSegsAlloc;
575 while (iSrc > 0)
576 {
577 iDst--;
578 iSrc--;
579 pSG->aSegs[iDst] = pSG->aSegs[iSrc];
580 }
581
582 /* create small segments from the start. */
583 pSG->cSegsUsed = pSG->cSegsAlloc;
584 iSrc = iDst;
585 iDst = 0;
586 while ( iDst < iSrc
587 && iDst < pSG->cSegsAlloc)
588 {
589 pSG->aSegs[iDst].Phys = NIL_RTHCPHYS;
590 pSG->aSegs[iDst].pv = pSG->aSegs[iSrc].pv;
591 pSG->aSegs[iDst].cb = RT_MIN(pSG->aSegs[iSrc].cb, VBOXNETFLT_DARWIN_TEST_SEG_SIZE);
592 if (pSG->aSegs[iDst].cb != pSG->aSegs[iSrc].cb)
593 {
594 pSG->aSegs[iSrc].cb -= pSG->aSegs[iDst].cb;
595 pSG->aSegs[iSrc].pv = (uint8_t *)pSG->aSegs[iSrc].pv + pSG->aSegs[iDst].cb;
596 }
597 else if (++iSrc >= pSG->cSegsAlloc)
598 {
599 pSG->cSegsUsed = iDst + 1;
600 break;
601 }
602 iDst++;
603 }
604 }
605#endif
606
607 AssertMsg(!pvFrame, ("pvFrame=%p pMBuf=%p iSeg=%d\n", pvFrame, pMBuf, iSeg));
608}
609
610
611/**
612 * Helper for determining whether the host wants the interface to be
613 * promiscuous.
614 */
615static bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
616{
617 bool fRc = false;
618 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
619 if (pIfNet)
620 {
621 /* gather the data */
622 uint16_t fIf = ifnet_flags(pIfNet);
623 unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
624 bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
625 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
626
627 /* calc the return. */
628 fRc = (fIf & IFF_PROMISC)
629 && cPromisc > fSetPromiscuous;
630 }
631 return fRc;
632}
633
634
635
636/**
637 *
638 * @see iff_detached_func in the darwin kpi.
639 */
640static void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
641{
642 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
643 uint64_t NanoTS = RTTimeSystemNanoTS();
644 LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
645 pThis, NanoTS, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
646
647 Assert(!pThis->fDisconnectedFromHost);
648 Assert(!pThis->fRediscoveryPending);
649
650 /*
651 * If we've put it into promiscuous mode, undo that now. If we don't
652 * the if_pcount will go all wrong when it's replugged.
653 */
654 if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
655 ifnet_set_promiscuous(pIfNet, 0);
656
657 /*
658 * We carefully take the spinlock and increase the interface reference
659 * behind it in order to avoid problematic races with the detached callback.
660 */
661 RTSpinlockAcquire(pThis->hSpinlock);
662
663 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
664 int cPromisc = VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
665
666 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
667 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
668 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
669 pThis->u.s.fSetPromiscuous = false;
670 ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
671 ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
672 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
673
674 RTSpinlockReleaseNoInts(pThis->hSpinlock);
675
676 if (pIfNet)
677 ifnet_release(pIfNet);
678 LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
679}
680
681
682/**
683 *
684 * @see iff_ioctl_func in the darwin kpi.
685 */
686static errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
687{
688 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
689 LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
690
691 /*
692 * Update fOtherPromiscuous.
693 */
694 /** @todo we'll have to find the offset of if_pcount to get this right! */
695 //if (uCmd == SIOCSIFFLAGS)
696 //{
697 //
698 //}
699
700 /*
701 * We didn't handle it, continue processing.
702 */
703 NOREF(pThis);
704 NOREF(eProtocol);
705 NOREF(uCmd);
706 NOREF(pvArg);
707 return EOPNOTSUPP;
708}
709
710
711/**
712 *
713 * @see iff_event_func in the darwin kpi.
714 */
715static void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
716{
717 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
718 LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
719
720 NOREF(pThis);
721 NOREF(pIfNet);
722 NOREF(eProtocol);
723 NOREF(pEvMsg);
724
725 /*
726 * Watch out for the interface going online / offline.
727 */
728 if ( VALID_PTR(pThis)
729 && VALID_PTR(pEvMsg)
730 && pEvMsg->vendor_code == KEV_VENDOR_APPLE
731 && pEvMsg->kev_class == KEV_NETWORK_CLASS
732 && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
733 {
734 if (pThis->u.s.pIfNet == pIfNet)
735 {
736 if (pEvMsg->event_code == KEV_DL_LINK_ON)
737 {
738 if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
739 {
740 /* failed to bring it online. */
741 errno_t err = ifnet_set_promiscuous(pIfNet, 1);
742 if (!err)
743 {
744 ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
745 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
746 Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
747 }
748 else
749 Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
750 }
751 else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
752 && !(ifnet_flags(pIfNet) & IFF_PROMISC))
753 {
754 /* Try fix the inconsistency. */
755 errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
756 if (!err)
757 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
758 if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
759 Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
760 else
761 Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
762 pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
763 }
764 else
765 Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
766 }
767 else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
768 Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
769/** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
770/** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
771 }
772 else
773 Log(("vboxNetFltDarwinIffEvent: pThis->u.s.pIfNet=%p pIfNet=%p (%d)\n", pThis->u.s.pIfNet, pIfNet, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
774 }
775 else if (VALID_PTR(pEvMsg))
776 Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
777 pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
778}
779
780
781/**
782 * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
783 *
784 * @returns 0 or EJUSTRETURN.
785 * @param pThis The instance.
786 * @param pMBuf The mbuf.
787 * @param pvFrame The start of the frame, optional.
788 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
789 * @param eProtocol The protocol.
790 */
791static errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
792 uint32_t fSrc, protocol_family_t eProtocol)
793{
794 /*
795 * Drop it immediately?
796 */
797 Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
798 pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
799 if (!pMBuf)
800 return 0;
801#if 0 /* debugging lost icmp packets */
802 if (mbuf_pkthdr_len(pMBuf) > 0x300)
803 {
804 uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
805 Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
806 }
807#endif
808 if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
809 return 0;
810
811 /*
812 * Active? Retain the instance and increment the busy counter.
813 */
814 if (!vboxNetFltTryRetainBusyActive(pThis))
815 return 0;
816
817 /*
818 * Finalize out-bound packets since the stack puts off finalizing
819 * TCP/IP checksums as long as possible.
820 * ASSUMES this only applies to outbound IP packets.
821 */
822 if ( (fSrc & INTNETTRUNKDIR_HOST)
823 && eProtocol == PF_INET)
824 {
825 Assert(!pvFrame);
826 mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
827 }
828
829 /*
830 * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
831 */
832 bool fDropIt = false;
833 unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
834 if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
835 {
836 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
837 vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
838
839 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
840 if (fDropIt)
841 {
842 /*
843 * Check if this interface is in promiscuous mode. We should not drop
844 * any packets before they get to the driver as it passes them to tap
845 * callbacks in order for BPF to work properly.
846 */
847 if (vboxNetFltDarwinIsPromiscuous(pThis))
848 fDropIt = false;
849 else
850 mbuf_freem(pMBuf);
851 }
852 }
853
854 vboxNetFltRelease(pThis, true /* fBusy */);
855
856 return fDropIt ? EJUSTRETURN : 0;
857}
858
859
860/**
861 * From the host.
862 *
863 * @see iff_output_func in the darwin kpi.
864 */
865static errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
866{
867 /** @todo there was some note about the ethernet header here or something like that... */
868
869 NOREF(eProtocol);
870 NOREF(pIfNet);
871 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
872}
873
874
875/**
876 * From the wire.
877 *
878 * @see iff_input_func in the darwin kpi.
879 */
880static errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
881{
882 NOREF(eProtocol);
883 NOREF(pIfNet);
884 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
885}
886
887
888/** A worker thread for vboxNetFltSendDummy(). */
889static DECLCALLBACK(int) vboxNetFltSendDummyWorker(RTTHREAD hThreadSelf, void *pvUser)
890{
891 Assert(pvUser);
892 ifnet_t pIfNet = (ifnet_t)pvUser;
893 return VBoxNetSendDummy(pIfNet);
894}
895
896
897/**
898 * Prevent GUI icon freeze issue when VirtualBoxVM process terminates.
899 *
900 * This function is a workaround for stuck-in-dock issue. The idea here is to
901 * send a dummy packet to an interface from the context of a kernel thread.
902 * Therefore, an XNU's receive thread (which is created as a result if we are
903 * the first who is communicating with the interface) will be associated with
904 * the kernel thread instead of VirtualBoxVM process.
905 *
906 * @param pIfNet Interface to be used to send data.
907 */
908static void vboxNetFltSendDummy(ifnet_t pIfNet)
909{
910 RTTHREAD hThread;
911 int rc = RTThreadCreate(&hThread, vboxNetFltSendDummyWorker, (void *)pIfNet, 0,
912 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "DummyThread");
913 if (RT_SUCCESS(rc))
914 {
915 RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL);
916 LogFlow(("vboxNetFltSendDummy: a dummy packet has been successfully sent in order to prevent stuck-in-dock issue\n"));
917 }
918 else
919 LogFlow(("vboxNetFltSendDummy: unable to send dummy packet in order to prevent stuck-in-dock issue\n"));
920}
921
922
923/**
924 * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
925 *
926 * @returns VBox status code.
927 * @param pThis The instance.
928 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
929 * flood the release log.
930 */
931static int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
932{
933 LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
934
935 /*
936 * Locate the interface first.
937 *
938 * The pIfNet member is updated before iflt_attach is called and used
939 * to deal with the hypothetical case where someone rips out the
940 * interface immediately after our iflt_attach call.
941 */
942 ifnet_t pIfNet = NULL;
943 errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
944 if (err)
945 {
946 Assert(err == ENXIO);
947 if (!fRediscovery)
948 LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
949 else
950 Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
951 return VERR_INTNET_FLT_IF_NOT_FOUND;
952 }
953
954 RTSpinlockAcquire(pThis->hSpinlock);
955 ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
956 RTSpinlockReleaseNoInts(pThis->hSpinlock);
957
958 /* Prevent stuck-in-dock issue by associating interface receive thread with kernel thread. */
959 vboxNetFltSendDummy(pIfNet);
960
961 /*
962 * Get the mac address while we still have a valid ifnet reference.
963 */
964 err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
965 if (!err)
966 {
967 /*
968 * Try attach the filter.
969 */
970 struct iff_filter RegRec;
971 RegRec.iff_cookie = pThis;
972 RegRec.iff_name = "VBoxNetFlt";
973 RegRec.iff_protocol = 0;
974 RegRec.iff_input = vboxNetFltDarwinIffInput;
975 RegRec.iff_output = vboxNetFltDarwinIffOutput;
976 RegRec.iff_event = vboxNetFltDarwinIffEvent;
977 RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
978 RegRec.iff_detached = vboxNetFltDarwinIffDetached;
979 interface_filter_t pIfFilter = NULL;
980 err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
981 Assert(err || pIfFilter);
982
983 RTSpinlockAcquire(pThis->hSpinlock);
984 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
985 if (pIfNet && !err)
986 {
987 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
988 ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
989 pIfNet = NULL; /* don't dereference it */
990 }
991 RTSpinlockReleaseNoInts(pThis->hSpinlock);
992
993 /* Report capabilities. */
994 if ( !pIfNet
995 && vboxNetFltTryRetainBusyNotDisconnected(pThis))
996 {
997 Assert(pThis->pSwitchPort);
998 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
999 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
1000 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
1001 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
1002 vboxNetFltRelease(pThis, true /*fBusy*/);
1003 }
1004 }
1005
1006 /* Release the interface on failure. */
1007 if (pIfNet)
1008 ifnet_release(pIfNet);
1009
1010 int rc = RTErrConvertFromErrno(err);
1011 if (RT_SUCCESS(rc))
1012 LogRel(("VBoxFltDrv: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.MacAddr), &pThis->u.s.MacAddr));
1013 else
1014 LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
1015 return rc;
1016}
1017
1018
1019bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
1020{
1021 vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
1022 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
1023}
1024
1025
1026int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
1027{
1028 NOREF(pvIfData);
1029
1030 int rc = VINF_SUCCESS;
1031 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1032 if (pIfNet)
1033 {
1034 /*
1035 * Create a mbuf for the gather list and push it onto the wire.
1036 *
1037 * Note! If the interface is in the promiscuous mode we need to send the
1038 * packet down the stack so it reaches the driver and Berkeley
1039 * Packet Filter (see @bugref{5817}).
1040 */
1041 if ((fDst & INTNETTRUNKDIR_WIRE) || vboxNetFltDarwinIsPromiscuous(pThis))
1042 {
1043 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1044 if (pMBuf)
1045 {
1046 errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
1047 if (err)
1048 rc = RTErrConvertFromErrno(err);
1049 }
1050 else
1051 rc = VERR_NO_MEMORY;
1052 }
1053
1054 /*
1055 * Create a mbuf for the gather list and push it onto the host stack.
1056 */
1057 if (fDst & INTNETTRUNKDIR_HOST)
1058 {
1059 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1060 if (pMBuf)
1061 {
1062 /* This is what IONetworkInterface::inputPacket does. */
1063 unsigned const cbEthHdr = 14;
1064 mbuf_pkthdr_setheader(pMBuf, mbuf_data(pMBuf));
1065 mbuf_pkthdr_setlen(pMBuf, mbuf_pkthdr_len(pMBuf) - cbEthHdr);
1066 mbuf_setdata(pMBuf, (uint8_t *)mbuf_data(pMBuf) + cbEthHdr, mbuf_len(pMBuf) - cbEthHdr);
1067 mbuf_pkthdr_setrcvif(pMBuf, pIfNet); /* will crash without this. */
1068
1069 errno_t err = ifnet_input(pIfNet, pMBuf, NULL);
1070 if (err)
1071 rc = RTErrConvertFromErrno(err);
1072 }
1073 else
1074 rc = VERR_NO_MEMORY;
1075 }
1076
1077 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1078 }
1079
1080 return rc;
1081}
1082
1083
1084void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1085{
1086 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1087 if (pIfNet)
1088 {
1089 if (pThis->fDisablePromiscuous)
1090 {
1091 /*
1092 * Promiscuous mode should not be used (wireless), we just need to
1093 * make sure the interface is up.
1094 */
1095 if (fActive)
1096 {
1097 u_int16_t fIf = ifnet_flags(pIfNet);
1098 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1099 {
1100 ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1101 ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1102 }
1103 }
1104 }
1105 else
1106 {
1107 /*
1108 * This api is a bit weird, the best reference is the code.
1109 *
1110 * Also, we have a bit or race conditions wrt the maintenance of
1111 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1112 */
1113 unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
1114 u_int16_t fIf;
1115 if (fActive)
1116 {
1117 Assert(!pThis->u.s.fSetPromiscuous);
1118 errno_t err = ENETDOWN;
1119 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
1120
1121 /*
1122 * Try bring the interface up and running if it's down.
1123 */
1124 fIf = ifnet_flags(pIfNet);
1125 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1126 {
1127 err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1128 errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1129 if (!err)
1130 err = err2;
1131 fIf = ifnet_flags(pIfNet);
1132 }
1133
1134 /*
1135 * Is it already up? If it isn't, leave it to the link event or
1136 * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
1137 */
1138 if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1139 {
1140 err = ifnet_set_promiscuous(pIfNet, 1);
1141 pThis->u.s.fSetPromiscuous = err == 0;
1142 if (!err)
1143 {
1144 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1145
1146 /* check if it actually worked, this stuff is not always behaving well. */
1147 if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
1148 {
1149 err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
1150 if (!err)
1151 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1152 if (!err)
1153 Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1154 else
1155 Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
1156 pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1157 }
1158 }
1159 else
1160 Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1161 }
1162 else if (!err)
1163 Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1164 if (err)
1165 LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1166 }
1167 else
1168 {
1169 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1170 if (pThis->u.s.fSetPromiscuous)
1171 {
1172 errno_t err = ifnet_set_promiscuous(pIfNet, 0);
1173 AssertMsg(!err, ("%d\n", err)); NOREF(err);
1174 }
1175 pThis->u.s.fSetPromiscuous = false;
1176
1177 fIf = ifnet_flags(pIfNet);
1178 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1179 }
1180 }
1181
1182 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1183 }
1184}
1185
1186
1187int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1188{
1189 /* Nothing to do here. */
1190 return VINF_SUCCESS;
1191}
1192
1193
1194int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1195{
1196 /* Nothing to do here. */
1197 return VINF_SUCCESS;
1198}
1199
1200
1201void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1202{
1203 interface_filter_t pIfFilter;
1204
1205 /*
1206 * Carefully obtain the interface filter reference and detach it.
1207 */
1208 RTSpinlockAcquire(pThis->hSpinlock);
1209 pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
1210 if (pIfFilter)
1211 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
1212 RTSpinlockReleaseNoInts(pThis->hSpinlock);
1213
1214 if (pIfFilter)
1215 iflt_detach(pIfFilter);
1216}
1217
1218
1219int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1220{
1221 NOREF(pvContext);
1222 return vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
1223}
1224
1225
1226int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1227{
1228 /*
1229 * Init the darwin specific members.
1230 */
1231 pThis->u.s.pIfNet = NULL;
1232 pThis->u.s.pIfFilter = NULL;
1233 pThis->u.s.fSetPromiscuous = false;
1234 pThis->u.s.fNeedSetPromiscuous = false;
1235 //pThis->u.s.MacAddr = {0};
1236
1237 return VINF_SUCCESS;
1238}
1239
1240
1241void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
1242{
1243 NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
1244}
1245
1246
1247int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
1248{
1249 /* Nothing to do */
1250 NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
1251 return VINF_SUCCESS;
1252}
1253
1254
1255int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
1256{
1257 /* Nothing to do */
1258 NOREF(pThis); NOREF(pvIfData);
1259 return VINF_SUCCESS;
1260}
1261
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette