VirtualBox

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

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

VBoxNet*darwin: Corrected two function delcarations (use DECL macros!), header guard and a few other nits. VBoxNetSend.h should move into src/VBox/HostDrivers/darwin/, btw.

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