VirtualBox

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

Last change on this file since 31900 was 30112, checked in by vboxsync, 15 years ago

iprt/asm.h,*: Added ASMAtomicWriteNullPtr and ASMAtomicUoWriteNullPtr to better deal with NULL being 0 in C++.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 41.3 KB
Line 
1/* $Id: VBoxNetFlt-darwin.cpp 30112 2010-06-09 12:31:50Z 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 sufficent to identify our mbufs. For the icmp scenario the ethernet
98 * header naturarlly 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 untill 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 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
230 ifnet_t pIfNet = NULL;
231
232 /*
233 * Be careful here to avoid problems racing the detached callback.
234 */
235 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
236 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
237 {
238 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
239 if (pIfNet)
240 ifnet_reference(pIfNet);
241 }
242 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
243
244 return pIfNet;
245}
246
247
248/**
249 * Release the host interface handle previously retained
250 * by vboxNetFltDarwinRetainIfNet.
251 *
252 * @param pThis The instance.
253 * @param pIfNet The vboxNetFltDarwinRetainIfNet return value, NULL is fine.
254 */
255DECLINLINE(void) vboxNetFltDarwinReleaseIfNet(PVBOXNETFLTINS pThis, ifnet_t pIfNet)
256{
257 NOREF(pThis);
258 if (pIfNet)
259 ifnet_release(pIfNet);
260}
261
262
263/**
264 * Checks whether this is an mbuf created by vboxNetFltDarwinMBufFromSG,
265 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
266 *
267 * @returns true / false accordingly.
268 * @param pThis The instance.
269 * @param pMBuf The mbuf.
270 * @param pvFrame The frame pointer, optional.
271 */
272DECLINLINE(bool) vboxNetFltDarwinMBufIsOur(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
273{
274 NOREF(pThis);
275
276 /*
277 * Lookup the tag set by vboxNetFltDarwinMBufFromSG.
278 */
279 PCVBOXNETFLTTAG pTagData;
280 size_t cbTagData;
281 errno_t err = mbuf_tag_find(pMBuf, g_idTag, 0 /* type */, &cbTagData, (void **)&pTagData);
282 if (err)
283 return false;
284 AssertReturn(cbTagData == sizeof(*pTagData), false);
285
286 /*
287 * Dig out the ethernet header from the mbuf.
288 */
289 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
290 if (!pEthHdr)
291 pEthHdr = (PCRTNETETHERHDR)mbuf_pkthdr_header(pMBuf);
292 if (!pEthHdr)
293 pEthHdr = (PCRTNETETHERHDR)mbuf_data(pMBuf);
294 /* ASSUMING that there is enough data to work on! */
295 if ( pEthHdr->DstMac.au8[0] != pTagData->EthHdr.DstMac.au8[0]
296 || pEthHdr->DstMac.au8[1] != pTagData->EthHdr.DstMac.au8[1]
297 || pEthHdr->DstMac.au8[2] != pTagData->EthHdr.DstMac.au8[2]
298 || pEthHdr->DstMac.au8[3] != pTagData->EthHdr.DstMac.au8[3]
299 || pEthHdr->DstMac.au8[4] != pTagData->EthHdr.DstMac.au8[4]
300 || pEthHdr->DstMac.au8[5] != pTagData->EthHdr.DstMac.au8[5]
301 || pEthHdr->SrcMac.au8[0] != pTagData->EthHdr.SrcMac.au8[0]
302 || pEthHdr->SrcMac.au8[1] != pTagData->EthHdr.SrcMac.au8[1]
303 || pEthHdr->SrcMac.au8[2] != pTagData->EthHdr.SrcMac.au8[2]
304 || pEthHdr->SrcMac.au8[3] != pTagData->EthHdr.SrcMac.au8[3]
305 || pEthHdr->SrcMac.au8[4] != pTagData->EthHdr.SrcMac.au8[4]
306 || pEthHdr->SrcMac.au8[5] != pTagData->EthHdr.SrcMac.au8[5]
307 || pEthHdr->EtherType != pTagData->EthHdr.EtherType)
308 {
309 Log3(("tagged, but the ethernet header has changed\n"));
310 return false;
311 }
312
313 return true;
314}
315
316
317/**
318 * Internal worker that create a darwin mbuf for a (scatter/)gather list.
319 *
320 * @returns Pointer to the mbuf.
321 * @param pThis The instance.
322 * @param pSG The (scatter/)gather list.
323 */
324static mbuf_t vboxNetFltDarwinMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
325{
326 /// @todo future? mbuf_how_t How = preemtion enabled ? MBUF_DONTWAIT : MBUF_WAITOK;
327 mbuf_how_t How = MBUF_WAITOK;
328
329 /*
330 * We can't make use of the physical addresses on darwin because the way the
331 * mbuf / cluster stuffe works (see mbuf_data_to_physical and mcl_to_paddr).
332 * So, because we're lazy, we will ASSUME that all SGs coming from INTNET
333 * will only contain one single segment.
334 */
335 Assert(pSG->cSegsUsed == 1);
336 Assert(pSG->cbTotal == pSG->aSegs[0].cb);
337 Assert(pSG->cbTotal > 0);
338
339 /*
340 * We need some way of getting back to our instance data when
341 * the mbuf is freed, so use pvUserData for this.
342 * -- this is not relevant anylonger! --
343 */
344 Assert(!pSG->pvUserData || pSG->pvUserData == pThis);
345 Assert(!pSG->pvUserData2);
346 pSG->pvUserData = pThis;
347
348 /*
349 * Allocate a packet and copy over the data.
350 *
351 * Using mbuf_attachcluster() here would've been nice but there are two
352 * issues with it: (1) it's 10.5.x only, and (2) the documentation indicates
353 * that it's not supposed to be used for really external buffers. The 2nd
354 * point might be argued against considering that the only m_clattach user
355 * is mallocs memory for the ext mbuf and not doing what's stated in the docs.
356 * However, it's hard to tell if these m_clattach buffers actually makes it
357 * to the NICs or not, and even if they did, the NIC would need the physical
358 * addresses for the pages they contain and might end up copying the data
359 * to a new mbuf anyway.
360 *
361 * So, in the end it's better to just do it the simple way that will work
362 * 100%, even if it involes some extra work (alloc + copy) we really wished
363 * to avoid.
364 */
365 mbuf_t pPkt = NULL;
366 errno_t err = mbuf_allocpacket(How, pSG->cbTotal, NULL, &pPkt);
367 if (!err)
368 {
369 /* Skip zero sized memory buffers (paranoia). */
370 mbuf_t pCur = pPkt;
371 while (pCur && !mbuf_maxlen(pCur))
372 pCur = mbuf_next(pCur);
373 Assert(pCur);
374
375 /* Set the required packet header attributes. */
376 mbuf_pkthdr_setlen(pPkt, pSG->cbTotal);
377 mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
378
379 /* Special case the single buffer copy. */
380 if ( mbuf_next(pCur)
381 && mbuf_maxlen(pCur) >= pSG->cbTotal)
382 {
383 mbuf_setlen(pCur, pSG->cbTotal);
384 IntNetSgRead(pSG, mbuf_data(pCur));
385 }
386 else
387 {
388 /* Multi buffer copying. */
389 size_t cbLeft = pSG->cbTotal;
390 size_t offSrc = 0;
391 while (cbLeft > 0 && pCur)
392 {
393 size_t cb = mbuf_maxlen(pCur);
394 if (cb > cbLeft)
395 cb = cbLeft;
396 mbuf_setlen(pCur, cb);
397 IntNetSgReadEx(pSG, offSrc, cb, mbuf_data(pCur));
398
399 /* advance */
400 offSrc += cb;
401 cbLeft -= cb;
402 pCur = mbuf_next(pCur);
403 }
404 Assert(cbLeft == 0);
405 }
406 if (!err)
407 {
408 /*
409 * Tag the packet and return successfully.
410 */
411 PVBOXNETFLTTAG pTagData;
412 err = mbuf_tag_allocate(pPkt, g_idTag, 0 /* type */, sizeof(VBOXNETFLTTAG) /* tag len */, How, (void **)&pTagData);
413 if (!err)
414 {
415 Assert(pSG->aSegs[0].cb >= sizeof(pTagData->EthHdr));
416 memcpy(&pTagData->EthHdr, pSG->aSegs[0].pv, sizeof(pTagData->EthHdr));
417 return pPkt;
418 }
419
420 /* bailout: */
421 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
422 }
423
424 mbuf_freem(pPkt);
425 }
426 else
427 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
428 pSG->pvUserData = NULL;
429
430 return NULL;
431}
432
433
434/**
435 * Calculates the number of segments required to represent the mbuf.
436 *
437 * @returns Number of segments.
438 * @param pThis The instance.
439 * @param pMBuf The mbuf.
440 * @param pvFrame The frame pointer, optional.
441 */
442DECLINLINE(unsigned) vboxNetFltDarwinMBufCalcSGSegs(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
443{
444 NOREF(pThis);
445
446 /*
447 * Count the buffers in the chain.
448 */
449 unsigned cSegs = 0;
450 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
451 if (mbuf_len(pCur))
452 cSegs++;
453 else if ( !cSegs
454 && pvFrame
455 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
456 cSegs++;
457
458#ifdef PADD_RUNT_FRAMES_FROM_HOST
459 /*
460 * Add one buffer if the total is less than the ethernet minimum 60 bytes.
461 * This may allocate a segment too much if the ethernet header is separated,
462 * but that shouldn't harm us much.
463 */
464 if (mbuf_pkthdr_len(pMBuf) < 60)
465 cSegs++;
466#endif
467
468#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
469 /* maximize the number of segments. */
470 cSegs = RT_MAX(VBOXNETFLT_DARWIN_MAX_SEGS - 1, cSegs);
471#endif
472
473 return cSegs ? cSegs : 1;
474}
475
476
477/**
478 * Initializes a SG list from an mbuf.
479 *
480 * @returns Number of segments.
481 * @param pThis The instance.
482 * @param pMBuf The mbuf.
483 * @param pSG The SG.
484 * @param pvFrame The frame pointer, optional.
485 * @param cSegs The number of segments allocated for the SG.
486 * This should match the number in the mbuf exactly!
487 * @param fSrc The source of the frame.
488 */
489DECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
490{
491 NOREF(pThis);
492
493 /*
494 * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
495 */
496 unsigned iSeg = 0;
497 IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
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 * Helper for determining whether the host wants the interface to be
617 * promiscuous.
618 */
619static bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
620{
621 bool fRc = false;
622 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
623 if (pIfNet)
624 {
625 /* gather the data */
626 uint16_t fIf = ifnet_flags(pIfNet);
627 unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
628 bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
629 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
630
631 /* calc the return. */
632 fRc = (fIf & IFF_PROMISC)
633 && cPromisc > fSetPromiscuous;
634 }
635 return fRc;
636}
637
638
639
640/**
641 *
642 * @see iff_detached_func in the darwin kpi.
643 */
644static void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
645{
646 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
647 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
648 uint64_t NanoTS = RTTimeSystemNanoTS();
649 LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
650 pThis, NanoTS, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
651
652 Assert(!pThis->fDisconnectedFromHost);
653 Assert(!pThis->fRediscoveryPending);
654
655 /*
656 * If we've put it into promiscuous mode, undo that now. If we don't
657 * the if_pcount will go all wrong when it's replugged.
658 */
659 if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
660 ifnet_set_promiscuous(pIfNet, 0);
661
662 /*
663 * We carefully take the spinlock and increase the interface reference
664 * behind it in order to avoid problematic races with the detached callback.
665 */
666 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
667
668 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
669 int cPromisc = VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
670
671 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
672 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
673 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
674 pThis->u.s.fSetPromiscuous = false;
675 ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
676 ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
677 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
678
679 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
680
681 if (pIfNet)
682 ifnet_release(pIfNet);
683 LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
684}
685
686
687/**
688 *
689 * @see iff_ioctl_func in the darwin kpi.
690 */
691static errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
692{
693 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
694 LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
695
696 /*
697 * Update fOtherPromiscuous.
698 */
699 /** @todo we'll have to find the offset of if_pcount to get this right! */
700 //if (uCmd == SIOCSIFFLAGS)
701 //{
702 //
703 //}
704
705 /*
706 * We didn't handle it, continue processing.
707 */
708 NOREF(pThis);
709 NOREF(eProtocol);
710 NOREF(uCmd);
711 NOREF(pvArg);
712 return EOPNOTSUPP;
713}
714
715
716/**
717 *
718 * @see iff_event_func in the darwin kpi.
719 */
720static void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
721{
722 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
723 LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
724
725 NOREF(pThis);
726 NOREF(pIfNet);
727 NOREF(eProtocol);
728 NOREF(pEvMsg);
729
730 /*
731 * Watch out for the interface going online / offline.
732 */
733 if ( VALID_PTR(pThis)
734 && VALID_PTR(pEvMsg)
735 && pEvMsg->vendor_code == KEV_VENDOR_APPLE
736 && pEvMsg->kev_class == KEV_NETWORK_CLASS
737 && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
738 {
739 if (pThis->u.s.pIfNet == pIfNet)
740 {
741 if (pEvMsg->event_code == KEV_DL_LINK_ON)
742 {
743 if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
744 {
745 /* failed to bring it online. */
746 errno_t err = ifnet_set_promiscuous(pIfNet, 1);
747 if (!err)
748 {
749 ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
750 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
751 Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
752 }
753 else
754 Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
755 }
756 else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
757 && !(ifnet_flags(pIfNet) & IFF_PROMISC))
758 {
759 /* Try fix the inconsistency. */
760 errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
761 if (!err)
762 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
763 if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
764 Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
765 else
766 Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
767 pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
768 }
769 else
770 Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
771 }
772 else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
773 Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
774/** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
775/** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
776 }
777 else
778 Log(("vboxNetFltDarwinIffEvent: pThis->u.s.pIfNet=%p pIfNet=%p (%d)\n", pThis->u.s.pIfNet, pIfNet, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
779 }
780 else if (VALID_PTR(pEvMsg))
781 Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
782 pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
783}
784
785
786/**
787 * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
788 *
789 * @returns 0 or EJUSTRETURN.
790 * @param pThis The instance.
791 * @param pMBuf The mbuf.
792 * @param pvFrame The start of the frame, optional.
793 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
794 * @param eProtocol The protocol.
795 */
796static errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
797 uint32_t fSrc, protocol_family_t eProtocol)
798{
799 /*
800 * Drop it immediately?
801 */
802 Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
803 pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
804 if (!pMBuf)
805 return 0;
806#if 0 /* debugging lost icmp packets */
807 if (mbuf_pkthdr_len(pMBuf) > 0x300)
808 {
809 uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
810 Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
811 }
812#endif
813 if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
814 return 0;
815
816 /*
817 * Active? Retain the instance and increment the busy counter.
818 */
819 if (!vboxNetFltTryRetainBusyActive(pThis))
820 return 0;
821
822 /*
823 * Finalize out-bound packets since the stack puts off finalizing
824 * TCP/IP checksums as long as possible.
825 * ASSUMES this only applies to outbound IP packets.
826 */
827 if ( (fSrc & INTNETTRUNKDIR_HOST)
828 && eProtocol == PF_INET)
829 {
830 Assert(!pvFrame);
831 mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
832 }
833
834 /*
835 * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
836 */
837 bool fDropIt = false;
838 unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
839 if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
840 {
841 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
842 vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
843
844 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
845 if (fDropIt)
846 mbuf_freem(pMBuf);
847 }
848
849 vboxNetFltRelease(pThis, true /* fBusy */);
850
851 return fDropIt ? EJUSTRETURN : 0;
852}
853
854
855/**
856 * From the host.
857 *
858 * @see iff_output_func in the darwin kpi.
859 */
860static errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
861{
862 /** @todo there was some note about the ethernet header here or something like that... */
863
864 NOREF(eProtocol);
865 NOREF(pIfNet);
866 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
867}
868
869
870/**
871 * From the wire.
872 *
873 * @see iff_input_func in the darwin kpi.
874 */
875static errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
876{
877 NOREF(eProtocol);
878 NOREF(pIfNet);
879 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
880}
881
882
883/**
884 * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
885 *
886 * @returns VBox status code.
887 * @param pThis The instance.
888 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
889 * flood the release log.
890 */
891static int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
892{
893 LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
894
895 /*
896 * Locate the interface first.
897 *
898 * The pIfNet member is updated before iflt_attach is called and used
899 * to deal with the hypothetical case where someone rips out the
900 * interface immediately after our iflt_attach call.
901 */
902 ifnet_t pIfNet = NULL;
903 errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
904 if (err)
905 {
906 Assert(err == ENXIO);
907 if (!fRediscovery)
908 LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
909 else
910 Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
911 return VERR_INTNET_FLT_IF_NOT_FOUND;
912 }
913
914 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
915 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
916 ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
917 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
918
919 /*
920 * Get the mac address while we still have a valid ifnet reference.
921 */
922 err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
923 if (!err)
924 {
925 /*
926 * Try attach the filter.
927 */
928 struct iff_filter RegRec;
929 RegRec.iff_cookie = pThis;
930 RegRec.iff_name = "VBoxNetFlt";
931 RegRec.iff_protocol = 0;
932 RegRec.iff_input = vboxNetFltDarwinIffInput;
933 RegRec.iff_output = vboxNetFltDarwinIffOutput;
934 RegRec.iff_event = vboxNetFltDarwinIffEvent;
935 RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
936 RegRec.iff_detached = vboxNetFltDarwinIffDetached;
937 interface_filter_t pIfFilter = NULL;
938 err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
939 Assert(err || pIfFilter);
940
941 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
942 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
943 if (pIfNet && !err)
944 {
945 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
946 ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
947 pIfNet = NULL; /* don't dereference it */
948 }
949 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
950
951 /* Report capabilities. */
952 if ( !pIfNet
953 && vboxNetFltTryRetainBusyNotDisconnected(pThis))
954 {
955 Assert(pThis->pSwitchPort);
956 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
957 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
958 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
959 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
960 vboxNetFltRelease(pThis, true /*fBusy*/);
961 }
962 }
963
964 /* Release the interface on failure. */
965 if (pIfNet)
966 ifnet_release(pIfNet);
967
968 int rc = RTErrConvertFromErrno(err);
969 if (RT_SUCCESS(rc))
970 LogRel(("VBoxFltDrv: attached to '%s' / %.*Rhxs\n", pThis->szName, sizeof(pThis->u.s.MacAddr), &pThis->u.s.MacAddr));
971 else
972 LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
973 return rc;
974}
975
976
977bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
978{
979 vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
980 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
981}
982
983
984int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
985{
986 NOREF(pvIfData);
987
988 int rc = VINF_SUCCESS;
989 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
990 if (pIfNet)
991 {
992 /*
993 * Create a mbuf for the gather list and push it onto the wire.
994 */
995 if (fDst & INTNETTRUNKDIR_WIRE)
996 {
997 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
998 if (pMBuf)
999 {
1000 errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
1001 if (err)
1002 rc = RTErrConvertFromErrno(err);
1003 }
1004 else
1005 rc = VERR_NO_MEMORY;
1006 }
1007
1008 /*
1009 * Create a mbuf for the gather list and push it onto the host stack.
1010 */
1011 if (fDst & INTNETTRUNKDIR_HOST)
1012 {
1013 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1014 if (pMBuf)
1015 {
1016 /* This is what IONetworkInterface::inputPacket does. */
1017 unsigned const cbEthHdr = 14;
1018 mbuf_pkthdr_setheader(pMBuf, mbuf_data(pMBuf));
1019 mbuf_pkthdr_setlen(pMBuf, mbuf_pkthdr_len(pMBuf) - cbEthHdr);
1020 mbuf_setdata(pMBuf, (uint8_t *)mbuf_data(pMBuf) + cbEthHdr, mbuf_len(pMBuf) - cbEthHdr);
1021 mbuf_pkthdr_setrcvif(pMBuf, pIfNet); /* will crash without this. */
1022
1023 errno_t err = ifnet_input(pIfNet, pMBuf, NULL);
1024 if (err)
1025 rc = RTErrConvertFromErrno(err);
1026 }
1027 else
1028 rc = VERR_NO_MEMORY;
1029 }
1030
1031 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1032 }
1033
1034 return rc;
1035}
1036
1037
1038void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1039{
1040 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1041 if (pIfNet)
1042 {
1043 if (pThis->fDisablePromiscuous)
1044 {
1045 /*
1046 * Promiscuous mode should not be used (wireless), we just need to
1047 * make sure the interface is up.
1048 */
1049 if (fActive)
1050 {
1051 u_int16_t fIf = ifnet_flags(pIfNet);
1052 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1053 {
1054 ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1055 ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1056 }
1057 }
1058 }
1059 else
1060 {
1061 /*
1062 * This api is a bit weird, the best reference is the code.
1063 *
1064 * Also, we have a bit or race conditions wrt the maintance of
1065 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1066 */
1067 unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
1068 u_int16_t fIf;
1069 if (fActive)
1070 {
1071 Assert(!pThis->u.s.fSetPromiscuous);
1072 errno_t err = ENETDOWN;
1073 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
1074
1075 /*
1076 * Try bring the interface up and running if it's down.
1077 */
1078 fIf = ifnet_flags(pIfNet);
1079 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1080 {
1081 err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1082 errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1083 if (!err)
1084 err = err2;
1085 fIf = ifnet_flags(pIfNet);
1086 }
1087
1088 /*
1089 * Is it already up? If it isn't, leave it to the link event or
1090 * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
1091 */
1092 if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1093 {
1094 err = ifnet_set_promiscuous(pIfNet, 1);
1095 pThis->u.s.fSetPromiscuous = err == 0;
1096 if (!err)
1097 {
1098 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1099
1100 /* check if it actually worked, this stuff is not always behaving well. */
1101 if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
1102 {
1103 err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
1104 if (!err)
1105 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1106 if (!err)
1107 Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1108 else
1109 Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
1110 pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1111 }
1112 }
1113 else
1114 Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1115 }
1116 else if (!err)
1117 Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1118 if (err)
1119 LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1120 }
1121 else
1122 {
1123 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1124 if (pThis->u.s.fSetPromiscuous)
1125 {
1126 errno_t err = ifnet_set_promiscuous(pIfNet, 0);
1127 AssertMsg(!err, ("%d\n", err)); NOREF(err);
1128 }
1129 pThis->u.s.fSetPromiscuous = false;
1130
1131 fIf = ifnet_flags(pIfNet);
1132 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1133 }
1134 }
1135
1136 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1137 }
1138}
1139
1140
1141int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1142{
1143 /* Nothing to do here. */
1144 return VINF_SUCCESS;
1145}
1146
1147
1148int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1149{
1150 /* Nothing to do here. */
1151 return VINF_SUCCESS;
1152}
1153
1154
1155void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1156{
1157 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
1158 interface_filter_t pIfFilter;
1159
1160 /*
1161 * Carefully obtain the interface filter reference and detach it.
1162 */
1163 RTSpinlockAcquireNoInts(pThis->hSpinlock, &Tmp);
1164 pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
1165 if (pIfFilter)
1166 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
1167 RTSpinlockReleaseNoInts(pThis->hSpinlock, &Tmp);
1168
1169 if (pIfFilter)
1170 iflt_detach(pIfFilter);
1171}
1172
1173
1174int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1175{
1176 NOREF(pvContext);
1177 return vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
1178}
1179
1180
1181int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1182{
1183 /*
1184 * Init the darwin specific members.
1185 */
1186 pThis->u.s.pIfNet = NULL;
1187 pThis->u.s.pIfFilter = NULL;
1188 pThis->u.s.fSetPromiscuous = false;
1189 pThis->u.s.fNeedSetPromiscuous = false;
1190 //pThis->u.s.MacAddr = {0};
1191
1192 return VINF_SUCCESS;
1193}
1194
1195
1196void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
1197{
1198 NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
1199}
1200
1201
1202int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
1203{
1204 /* Nothing to do */
1205 NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
1206 return VINF_SUCCESS;
1207}
1208
1209
1210int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
1211{
1212 /* Nothing to do */
1213 NOREF(pThis); NOREF(pvIfData);
1214 return VINF_SUCCESS;
1215}
1216
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