VirtualBox

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

最後變更 在這個檔案從12691是 11982,由 vboxsync 提交於 16 年 前

All: license header changes for 2.0 (OSE headers, add Sun GPL/LGPL disclaimer)

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

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