VirtualBox

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

Last change on this file since 41774 was 41774, checked in by vboxsync, 12 years ago

bugref..

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