VirtualBox

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

最後變更 在這個檔案從28723是 28723,由 vboxsync 提交於 15 年 前

IntNet/NetFlt: Added INTNETTRUNKSWPORT::pfnReportGsoCapabilities and enabled ring-0 transmission for linux hosts and wires.

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

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