VirtualBox

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

Last change on this file since 56743 was 56723, checked in by vboxsync, 10 years ago

VBoxNetFlt: use RTmac instead of Rhxs format specifier when logging the MAC address to the release log

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 56.0 KB
Line 
1/* $Id: VBoxNetFlt-darwin.cpp 56723 2015-07-01 07:49:13Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Darwin Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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#include <iprt/thread.h>
47
48#include "../../darwin/VBoxNetSend.h"
49
50#include <mach/kmod.h>
51#include <sys/conf.h>
52#include <sys/errno.h>
53#include <sys/ioccom.h>
54#include <sys/filio.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
66#include <sys/kpi_socket.h>
67#include <net/if.h>
68#include <net/if_var.h>
69#include <netinet/in.h>
70#include <netinet/in_var.h>
71#include <netinet6/in6_var.h>
72
73#define VBOXNETFLT_OS_SPECFIC 1
74#include "../VBoxNetFltInternal.h"
75
76
77/*******************************************************************************
78* Defined Constants And Macros *
79*******************************************************************************/
80/** The maximum number of SG segments.
81 * Used to prevent stack overflow and similar bad stuff. */
82#define VBOXNETFLT_DARWIN_MAX_SEGS 32
83
84#if 0
85/** For testing extremely segmented frames. */
86#define VBOXNETFLT_DARWIN_TEST_SEG_SIZE 14
87#endif
88
89/* XXX: hidden undef #ifdef __APPLE__ */
90#define VBOX_IN_LOOPBACK(addr) (((addr) & IN_CLASSA_NET) == 0x7f000000)
91#define VBOX_IN_LINKLOCAL(addr) (((addr) & IN_CLASSB_NET) == 0xa9fe0000)
92
93
94
95/*******************************************************************************
96* Internal Functions *
97*******************************************************************************/
98RT_C_DECLS_BEGIN
99static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData);
100static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData);
101
102static void vboxNetFltDarwinSysSockUpcall(socket_t pSysSock, void *pvData, int fWait);
103RT_C_DECLS_END
104
105
106/*******************************************************************************
107* Structures and Typedefs *
108*******************************************************************************/
109/**
110 * The mbuf tag data.
111 *
112 * We have to associate the ethernet header with each packet we're sending
113 * because things like icmp will inherit the tag it self so the tag along
114 * isn't sufficient to identify our mbufs. For the icmp scenario the ethernet
115 * header naturally changes before the packet is send pack, so let check it.
116 */
117typedef struct VBOXNETFLTTAG
118{
119 /** The ethernet header of the outgoing frame. */
120 RTNETETHERHDR EthHdr;
121} VBOXNETFLTTAG;
122/** Pointer to a VBoxNetFlt mbuf tag. */
123typedef VBOXNETFLTTAG *PVBOXNETFLTTAG;
124/** Pointer to a const VBoxNetFlt mbuf tag. */
125typedef VBOXNETFLTTAG const *PCVBOXNETFLTTAG;
126
127
128/*******************************************************************************
129* Global Variables *
130*******************************************************************************/
131/**
132 * Declare the module stuff.
133 */
134RT_C_DECLS_BEGIN
135extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
136extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
137
138KMOD_EXPLICIT_DECL(VBoxNetFlt, VBOX_VERSION_STRING, _start, _stop)
139DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxNetFltDarwinStart;
140DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxNetFltDarwinStop;
141DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
142RT_C_DECLS_END
143
144
145/**
146 * The (common) global data.
147 */
148static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
149
150/** The unique tag id for this module.
151 * This is basically a unique string hash that lives on until reboot.
152 * It is used for tagging mbufs. */
153static mbuf_tag_id_t g_idTag;
154
155/** The offset of the struct ifnet::if_pcount variable.
156 * @remarks Initial value is valid for Lion and earlier. We adjust it on attach
157 * for later releases. */
158static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
159 + sizeof(u_long) /*if_refcnt*/;
160/** Macro for accessing ifnet::if_pcount. */
161#define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
162/** The size of area of ifnet structure we try to locate if_pcount in. */
163#define VBOXNETFLT_DARWIN_IFNET_SIZE 256
164/** Indicates whether g_offIfNetPCount has been adjusted already (no point in
165 * doing it more than once). */
166static bool g_fNetPCountFound = false;
167
168
169/**
170 * Change the promiscuous setting and try spot the changed in @a pIfNet.
171 *
172 * @returns Offset of potential p_count field.
173 * @param pIfNet The interface we're attaching to.
174 * @param iPromisc Whether to enable (1) or disable (0) promiscuous mode.
175 *
176 * @note This implementation relies on if_pcount to be aligned on sizeof(int).
177 */
178static unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc)
179{
180 int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)];
181 memcpy(aiSavedState, pIfNet, sizeof(aiSavedState));
182
183 ifnet_set_promiscuous(pIfNet, iPromisc);
184
185 int const iDiff = iPromisc ? 1 : -1;
186
187 /*
188 * We assume that ifnet structure will never have less members in front of if_pcount
189 * than it used to have in Lion. If this turns out to be false assumption we will
190 * have to start from zero offset.
191 */
192 for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++)
193 if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff)
194 return i * sizeof(int);
195
196 return 0;
197}
198
199
200/**
201 * Detect and adjust the offset of ifnet::if_pcount.
202 *
203 * @param pIfNet The interface we're attaching to.
204 */
205static void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet)
206{
207 if (g_fNetPCountFound)
208 return;
209
210 /*
211 * It would be nice to use locking at this point, but it is not available via KPI.
212 * This is why we try several times. At each attempt we modify if_pcount four times
213 * to rule out false detections.
214 */
215 unsigned offTry1, offTry2, offTry3, offTry4;
216 for (int iAttempt = 0; iAttempt < 3; iAttempt++)
217 {
218 offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
219 offTry2 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
220 offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
221 offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
222 if (offTry1 == offTry2 && offTry2 == offTry3 && offTry3 == offTry4)
223 {
224 if (g_offIfNetPCount != offTry1)
225 {
226 Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount));
227 g_offIfNetPCount = offTry1;
228 g_fNetPCountFound = true;
229 }
230 break;
231 }
232 }
233
234 if (g_offIfNetPCount != offTry1)
235 LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1));
236}
237
238
239/**
240 * Start the kernel module.
241 */
242static kern_return_t VBoxNetFltDarwinStart(struct kmod_info *pKModInfo, void *pvData)
243{
244 int rc;
245
246 /*
247 * Initialize IPRT and find our module tag id.
248 * (IPRT is shared with VBoxDrv, it creates the loggers.)
249 */
250 rc = RTR0Init(0);
251 if (RT_SUCCESS(rc))
252 {
253 Log(("VBoxNetFltDarwinStart\n"));
254 errno_t err = mbuf_tag_id_find("org.VirtualBox.kext.VBoxFltDrv", &g_idTag);
255 if (!err)
256 {
257 /*
258 * Initialize the globals and connect to the support driver.
259 *
260 * This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
261 * for establishing the connect to the support driver.
262 */
263 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
264 rc = vboxNetFltInitGlobalsAndIdc(&g_VBoxNetFltGlobals);
265 if (RT_SUCCESS(rc))
266 {
267 LogRel(("VBoxFltDrv: version " VBOX_VERSION_STRING " r%d\n", VBOX_SVN_REV));
268 return KMOD_RETURN_SUCCESS;
269 }
270
271 LogRel(("VBoxFltDrv: failed to initialize device extension (rc=%d)\n", rc));
272 }
273 else
274 LogRel(("VBoxFltDrv: mbuf_tag_id_find failed, err=%d\n", err));
275 RTR0Term();
276 }
277 else
278 printf("VBoxFltDrv: failed to initialize IPRT (rc=%d)\n", rc);
279
280 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
281 return KMOD_RETURN_FAILURE;
282}
283
284
285/**
286 * Stop the kernel module.
287 */
288static kern_return_t VBoxNetFltDarwinStop(struct kmod_info *pKModInfo, void *pvData)
289{
290 Log(("VBoxNetFltDarwinStop\n"));
291
292 /*
293 * Refuse to unload if anyone is currently using the filter driver.
294 * This is important as I/O kit / xnu will to be able to do usage
295 * tracking for us!
296 */
297 int rc = vboxNetFltTryDeleteIdcAndGlobals(&g_VBoxNetFltGlobals);
298 if (RT_FAILURE(rc))
299 {
300 Log(("VBoxNetFltDarwinStop - failed, busy.\n"));
301 return KMOD_RETURN_FAILURE;
302 }
303
304 /*
305 * Undo the work done during start (in reverse order).
306 */
307 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
308
309 RTR0Term();
310
311 return KMOD_RETURN_SUCCESS;
312}
313
314
315/**
316 * Reads and retains the host interface handle.
317 *
318 * @returns The handle, NULL if detached.
319 * @param pThis
320 */
321DECLINLINE(ifnet_t) vboxNetFltDarwinRetainIfNet(PVBOXNETFLTINS pThis)
322{
323 ifnet_t pIfNet = NULL;
324
325 /*
326 * Be careful here to avoid problems racing the detached callback.
327 */
328 RTSpinlockAcquire(pThis->hSpinlock);
329 if (!ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost))
330 {
331 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
332 if (pIfNet)
333 ifnet_reference(pIfNet);
334 }
335 RTSpinlockRelease(pThis->hSpinlock);
336
337 return pIfNet;
338}
339
340
341/**
342 * Release the host interface handle previously retained
343 * by vboxNetFltDarwinRetainIfNet.
344 *
345 * @param pThis The instance.
346 * @param pIfNet The vboxNetFltDarwinRetainIfNet return value, NULL is fine.
347 */
348DECLINLINE(void) vboxNetFltDarwinReleaseIfNet(PVBOXNETFLTINS pThis, ifnet_t pIfNet)
349{
350 NOREF(pThis);
351 if (pIfNet)
352 ifnet_release(pIfNet);
353}
354
355
356/**
357 * Checks whether this is an mbuf created by vboxNetFltDarwinMBufFromSG,
358 * i.e. a buffer which we're pushing and should be ignored by the filter callbacks.
359 *
360 * @returns true / false accordingly.
361 * @param pThis The instance.
362 * @param pMBuf The mbuf.
363 * @param pvFrame The frame pointer, optional.
364 */
365DECLINLINE(bool) vboxNetFltDarwinMBufIsOur(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
366{
367 NOREF(pThis);
368
369 /*
370 * Lookup the tag set by vboxNetFltDarwinMBufFromSG.
371 */
372 PCVBOXNETFLTTAG pTagData;
373 size_t cbTagData;
374 errno_t err = mbuf_tag_find(pMBuf, g_idTag, 0 /* type */, &cbTagData, (void **)&pTagData);
375 if (err)
376 return false;
377 AssertReturn(cbTagData == sizeof(*pTagData), false);
378
379 /*
380 * Dig out the ethernet header from the mbuf.
381 */
382 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
383 if (!pEthHdr)
384 pEthHdr = (PCRTNETETHERHDR)mbuf_pkthdr_header(pMBuf);
385 if (!pEthHdr)
386 pEthHdr = (PCRTNETETHERHDR)mbuf_data(pMBuf);
387 /* ASSUMING that there is enough data to work on! */
388 if ( pEthHdr->DstMac.au8[0] != pTagData->EthHdr.DstMac.au8[0]
389 || pEthHdr->DstMac.au8[1] != pTagData->EthHdr.DstMac.au8[1]
390 || pEthHdr->DstMac.au8[2] != pTagData->EthHdr.DstMac.au8[2]
391 || pEthHdr->DstMac.au8[3] != pTagData->EthHdr.DstMac.au8[3]
392 || pEthHdr->DstMac.au8[4] != pTagData->EthHdr.DstMac.au8[4]
393 || pEthHdr->DstMac.au8[5] != pTagData->EthHdr.DstMac.au8[5]
394 || pEthHdr->SrcMac.au8[0] != pTagData->EthHdr.SrcMac.au8[0]
395 || pEthHdr->SrcMac.au8[1] != pTagData->EthHdr.SrcMac.au8[1]
396 || pEthHdr->SrcMac.au8[2] != pTagData->EthHdr.SrcMac.au8[2]
397 || pEthHdr->SrcMac.au8[3] != pTagData->EthHdr.SrcMac.au8[3]
398 || pEthHdr->SrcMac.au8[4] != pTagData->EthHdr.SrcMac.au8[4]
399 || pEthHdr->SrcMac.au8[5] != pTagData->EthHdr.SrcMac.au8[5]
400 || pEthHdr->EtherType != pTagData->EthHdr.EtherType)
401 {
402 Log3(("tagged, but the ethernet header has changed\n"));
403 return false;
404 }
405
406 return true;
407}
408
409
410/**
411 * Internal worker that create a darwin mbuf for a (scatter/)gather list.
412 *
413 * @returns Pointer to the mbuf.
414 * @param pThis The instance.
415 * @param pSG The (scatter/)gather list.
416 */
417static mbuf_t vboxNetFltDarwinMBufFromSG(PVBOXNETFLTINS pThis, PINTNETSG pSG)
418{
419 /// @todo future? mbuf_how_t How = preemption enabled ? MBUF_DONTWAIT : MBUF_WAITOK;
420 mbuf_how_t How = MBUF_WAITOK;
421
422 /*
423 * We need some way of getting back to our instance data when
424 * the mbuf is freed, so use pvUserData for this.
425 * -- this is not relevant anylonger! --
426 */
427 Assert(!pSG->pvUserData || pSG->pvUserData == pThis);
428 Assert(!pSG->pvUserData2);
429 pSG->pvUserData = pThis;
430
431 /*
432 * Allocate a packet and copy over the data.
433 *
434 * Using mbuf_attachcluster() here would've been nice but there are two
435 * issues with it: (1) it's 10.5.x only, and (2) the documentation indicates
436 * that it's not supposed to be used for really external buffers. The 2nd
437 * point might be argued against considering that the only m_clattach user
438 * is mallocs memory for the ext mbuf and not doing what's stated in the docs.
439 * However, it's hard to tell if these m_clattach buffers actually makes it
440 * to the NICs or not, and even if they did, the NIC would need the physical
441 * addresses for the pages they contain and might end up copying the data
442 * to a new mbuf anyway.
443 *
444 * So, in the end it's better to just do it the simple way that will work
445 * 100%, even if it involves some extra work (alloc + copy) we really wished
446 * to avoid.
447 *
448 * Note. We can't make use of the physical addresses on darwin because the
449 * way the mbuf / cluster stuff works (see mbuf_data_to_physical and
450 * mcl_to_paddr).
451 */
452 mbuf_t pPkt = NULL;
453 errno_t err = mbuf_allocpacket(How, pSG->cbTotal, NULL, &pPkt);
454 if (!err)
455 {
456 /* Skip zero sized memory buffers (paranoia). */
457 mbuf_t pCur = pPkt;
458 while (pCur && !mbuf_maxlen(pCur))
459 pCur = mbuf_next(pCur);
460 Assert(pCur);
461
462 /* Set the required packet header attributes. */
463 mbuf_pkthdr_setlen(pPkt, pSG->cbTotal);
464 mbuf_pkthdr_setheader(pPkt, mbuf_data(pCur));
465
466 /* Special case the single buffer copy. */
467 if ( mbuf_next(pCur)
468 && mbuf_maxlen(pCur) >= pSG->cbTotal)
469 {
470 mbuf_setlen(pCur, pSG->cbTotal);
471 IntNetSgRead(pSG, mbuf_data(pCur));
472 }
473 else
474 {
475 /* Multi buffer copying. */
476 size_t cbLeft = pSG->cbTotal;
477 size_t offSrc = 0;
478 while (cbLeft > 0 && pCur)
479 {
480 size_t cb = mbuf_maxlen(pCur);
481 if (cb > cbLeft)
482 cb = cbLeft;
483 mbuf_setlen(pCur, cb);
484 IntNetSgReadEx(pSG, offSrc, cb, mbuf_data(pCur));
485
486 /* advance */
487 offSrc += cb;
488 cbLeft -= cb;
489 pCur = mbuf_next(pCur);
490 }
491 Assert(cbLeft == 0);
492 }
493 if (!err)
494 {
495 /*
496 * Tag the packet and return successfully.
497 */
498 PVBOXNETFLTTAG pTagData;
499 err = mbuf_tag_allocate(pPkt, g_idTag, 0 /* type */, sizeof(VBOXNETFLTTAG) /* tag len */, How, (void **)&pTagData);
500 if (!err)
501 {
502 Assert(pSG->aSegs[0].cb >= sizeof(pTagData->EthHdr));
503 memcpy(&pTagData->EthHdr, pSG->aSegs[0].pv, sizeof(pTagData->EthHdr));
504 return pPkt;
505 }
506
507 /* bailout: */
508 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
509 }
510
511 mbuf_freem(pPkt);
512 }
513 else
514 AssertMsg(err == ENOMEM || err == EWOULDBLOCK, ("err=%d\n", err));
515 pSG->pvUserData = NULL;
516
517 return NULL;
518}
519
520
521/**
522 * Calculates the number of segments required to represent the mbuf.
523 *
524 * @returns Number of segments.
525 * @param pThis The instance.
526 * @param pMBuf The mbuf.
527 * @param pvFrame The frame pointer, optional.
528 */
529DECLINLINE(unsigned) vboxNetFltDarwinMBufCalcSGSegs(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame)
530{
531 NOREF(pThis);
532
533 /*
534 * Count the buffers in the chain.
535 */
536 unsigned cSegs = 0;
537 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
538 if (mbuf_len(pCur))
539 cSegs++;
540 else if ( !cSegs
541 && pvFrame
542 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
543 cSegs++;
544
545#ifdef PADD_RUNT_FRAMES_FROM_HOST
546 /*
547 * Add one buffer if the total is less than the ethernet minimum 60 bytes.
548 * This may allocate a segment too much if the ethernet header is separated,
549 * but that shouldn't harm us much.
550 */
551 if (mbuf_pkthdr_len(pMBuf) < 60)
552 cSegs++;
553#endif
554
555#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
556 /* maximize the number of segments. */
557 cSegs = RT_MAX(VBOXNETFLT_DARWIN_MAX_SEGS - 1, cSegs);
558#endif
559
560 return cSegs ? cSegs : 1;
561}
562
563
564/**
565 * Initializes a SG list from an mbuf.
566 *
567 * @returns Number of segments.
568 * @param pThis The instance.
569 * @param pMBuf The mbuf.
570 * @param pSG The SG.
571 * @param pvFrame The frame pointer, optional.
572 * @param cSegs The number of segments allocated for the SG.
573 * This should match the number in the mbuf exactly!
574 * @param fSrc The source of the frame.
575 */
576DECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
577{
578 NOREF(pThis);
579
580 /*
581 * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
582 */
583 unsigned iSeg = 0;
584 IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
585 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
586 {
587 size_t cbSeg = mbuf_len(pCur);
588 if (cbSeg)
589 {
590 void *pvSeg = mbuf_data(pCur);
591
592 /* deal with pvFrame */
593 if (!iSeg && pvFrame && pvFrame != pvSeg)
594 {
595 void *pvStart = mbuf_datastart(pMBuf);
596 uintptr_t offSeg = (uintptr_t)pvSeg - (uintptr_t)pvStart;
597 uintptr_t offSegEnd = offSeg + cbSeg;
598 Assert(pvStart && pvSeg && offSeg < mbuf_maxlen(pMBuf) && offSegEnd <= mbuf_maxlen(pMBuf)); NOREF(offSegEnd);
599 uintptr_t offFrame = (uintptr_t)pvFrame - (uintptr_t)pvStart;
600 if (RT_LIKELY(offFrame < offSeg))
601 {
602 pvSeg = pvFrame;
603 cbSeg += offSeg - offFrame;
604 }
605 else
606 AssertMsgFailed(("pvFrame=%p pvStart=%p pvSeg=%p offSeg=%p cbSeg=%#zx offSegEnd=%p offFrame=%p maxlen=%#zx\n",
607 pvFrame, pvStart, pvSeg, offSeg, cbSeg, offSegEnd, offFrame, mbuf_maxlen(pMBuf)));
608 pvFrame = NULL;
609 }
610
611 AssertBreak(iSeg < cSegs);
612 pSG->cbTotal += cbSeg;
613 pSG->aSegs[iSeg].cb = cbSeg;
614 pSG->aSegs[iSeg].pv = pvSeg;
615 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
616 iSeg++;
617 }
618 /* The pvFrame might be in a now empty buffer. */
619 else if ( !iSeg
620 && pvFrame
621 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
622 {
623 cbSeg = (uintptr_t)mbuf_datastart(pMBuf) + mbuf_maxlen(pMBuf) - (uintptr_t)pvFrame;
624 pSG->cbTotal += cbSeg;
625 pSG->aSegs[iSeg].cb = cbSeg;
626 pSG->aSegs[iSeg].pv = pvFrame;
627 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
628 iSeg++;
629 pvFrame = NULL;
630 }
631 }
632
633 Assert(iSeg && iSeg <= cSegs);
634 pSG->cSegsUsed = iSeg;
635
636#ifdef PADD_RUNT_FRAMES_FROM_HOST
637 /*
638 * Add a trailer if the frame is too small.
639 *
640 * Since we're getting to the packet before it is framed, it has not
641 * yet been padded. The current solution is to add a segment pointing
642 * to a buffer containing all zeros and pray that works for all frames...
643 */
644 if (pSG->cbTotal < 60 && (fSrc & INTNETTRUNKDIR_HOST))
645 {
646 AssertReturnVoid(iSeg < cSegs);
647
648 static uint8_t const s_abZero[128] = {0};
649 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
650 pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
651 pSG->aSegs[iSeg].cb = 60 - pSG->cbTotal;
652 pSG->cbTotal = 60;
653 pSG->cSegsUsed++;
654 }
655#endif
656
657#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
658 /*
659 * Redistribute the segments.
660 */
661 if (pSG->cSegsUsed < pSG->cSegsAlloc)
662 {
663 /* copy the segments to the end. */
664 int iSrc = pSG->cSegsUsed;
665 int iDst = pSG->cSegsAlloc;
666 while (iSrc > 0)
667 {
668 iDst--;
669 iSrc--;
670 pSG->aSegs[iDst] = pSG->aSegs[iSrc];
671 }
672
673 /* create small segments from the start. */
674 pSG->cSegsUsed = pSG->cSegsAlloc;
675 iSrc = iDst;
676 iDst = 0;
677 while ( iDst < iSrc
678 && iDst < pSG->cSegsAlloc)
679 {
680 pSG->aSegs[iDst].Phys = NIL_RTHCPHYS;
681 pSG->aSegs[iDst].pv = pSG->aSegs[iSrc].pv;
682 pSG->aSegs[iDst].cb = RT_MIN(pSG->aSegs[iSrc].cb, VBOXNETFLT_DARWIN_TEST_SEG_SIZE);
683 if (pSG->aSegs[iDst].cb != pSG->aSegs[iSrc].cb)
684 {
685 pSG->aSegs[iSrc].cb -= pSG->aSegs[iDst].cb;
686 pSG->aSegs[iSrc].pv = (uint8_t *)pSG->aSegs[iSrc].pv + pSG->aSegs[iDst].cb;
687 }
688 else if (++iSrc >= pSG->cSegsAlloc)
689 {
690 pSG->cSegsUsed = iDst + 1;
691 break;
692 }
693 iDst++;
694 }
695 }
696#endif
697
698 AssertMsg(!pvFrame, ("pvFrame=%p pMBuf=%p iSeg=%d\n", pvFrame, pMBuf, iSeg));
699}
700
701
702/**
703 * Helper for determining whether the host wants the interface to be
704 * promiscuous.
705 */
706static bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
707{
708 bool fRc = false;
709 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
710 if (pIfNet)
711 {
712 /* gather the data */
713 uint16_t fIf = ifnet_flags(pIfNet);
714 unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
715 bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
716 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
717
718 /* calc the return. */
719 fRc = (fIf & IFF_PROMISC)
720 && cPromisc > fSetPromiscuous;
721 }
722 return fRc;
723}
724
725
726
727/**
728 *
729 * @see iff_detached_func in the darwin kpi.
730 */
731static void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
732{
733 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
734 uint64_t NanoTS = RTTimeSystemNanoTS();
735 LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
736 pThis, NanoTS, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
737
738 Assert(!pThis->fDisconnectedFromHost);
739 Assert(!pThis->fRediscoveryPending);
740
741 /*
742 * If we've put it into promiscuous mode, undo that now. If we don't
743 * the if_pcount will go all wrong when it's replugged.
744 */
745 if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
746 ifnet_set_promiscuous(pIfNet, 0);
747
748 /*
749 * We carefully take the spinlock and increase the interface reference
750 * behind it in order to avoid problematic races with the detached callback.
751 */
752 RTSpinlockAcquire(pThis->hSpinlock);
753
754 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
755 int cPromisc = VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
756
757 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
758 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
759 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
760 pThis->u.s.fSetPromiscuous = false;
761 ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
762 ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
763 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
764
765 RTSpinlockRelease(pThis->hSpinlock);
766
767 if (pIfNet)
768 ifnet_release(pIfNet);
769 LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
770}
771
772
773/**
774 *
775 * @see iff_ioctl_func in the darwin kpi.
776 */
777static errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
778{
779 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
780 LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
781
782 /*
783 * Update fOtherPromiscuous.
784 */
785 /** @todo we'll have to find the offset of if_pcount to get this right! */
786 //if (uCmd == SIOCSIFFLAGS)
787 //{
788 //
789 //}
790
791 /*
792 * We didn't handle it, continue processing.
793 */
794 NOREF(pThis);
795 NOREF(eProtocol);
796 NOREF(uCmd);
797 NOREF(pvArg);
798 return EOPNOTSUPP;
799}
800
801
802/**
803 *
804 * @see iff_event_func in the darwin kpi.
805 */
806static void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
807{
808 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
809 LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
810
811 NOREF(pThis);
812 NOREF(pIfNet);
813 NOREF(eProtocol);
814 NOREF(pEvMsg);
815
816 /*
817 * Watch out for the interface going online / offline.
818 */
819 if ( VALID_PTR(pThis)
820 && VALID_PTR(pEvMsg)
821 && pEvMsg->vendor_code == KEV_VENDOR_APPLE
822 && pEvMsg->kev_class == KEV_NETWORK_CLASS
823 && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
824 {
825 if (pThis->u.s.pIfNet == pIfNet)
826 {
827 if (pEvMsg->event_code == KEV_DL_LINK_ON)
828 {
829 if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
830 {
831 /* failed to bring it online. */
832 errno_t err = ifnet_set_promiscuous(pIfNet, 1);
833 if (!err)
834 {
835 ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
836 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
837 Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
838 }
839 else
840 Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
841 }
842 else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
843 && !(ifnet_flags(pIfNet) & IFF_PROMISC))
844 {
845 /* Try fix the inconsistency. */
846 errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
847 if (!err)
848 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
849 if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
850 Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
851 else
852 Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
853 pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
854 }
855 else
856 Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
857 }
858 else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
859 Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
860/** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
861/** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
862 }
863 else
864 Log(("vboxNetFltDarwinIffEvent: pThis->u.s.pIfNet=%p pIfNet=%p (%d)\n", pThis->u.s.pIfNet, pIfNet, VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
865 }
866 else if (VALID_PTR(pEvMsg))
867 Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
868 pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
869}
870
871
872/**
873 * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
874 *
875 * @returns 0 or EJUSTRETURN.
876 * @param pThis The instance.
877 * @param pMBuf The mbuf.
878 * @param pvFrame The start of the frame, optional.
879 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
880 * @param eProtocol The protocol.
881 */
882static errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
883 uint32_t fSrc, protocol_family_t eProtocol)
884{
885 /*
886 * Drop it immediately?
887 */
888 Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
889 pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
890 if (!pMBuf)
891 return 0;
892#if 0 /* debugging lost icmp packets */
893 if (mbuf_pkthdr_len(pMBuf) > 0x300)
894 {
895 uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
896 Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
897 }
898#endif
899 if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
900 return 0;
901
902 /*
903 * Active? Retain the instance and increment the busy counter.
904 */
905 if (!vboxNetFltTryRetainBusyActive(pThis))
906 return 0;
907
908 /*
909 * Finalize out-bound packets since the stack puts off finalizing
910 * TCP/IP checksums as long as possible.
911 * ASSUMES this only applies to outbound IP packets.
912 */
913 if ( (fSrc & INTNETTRUNKDIR_HOST)
914 && eProtocol == PF_INET)
915 {
916 Assert(!pvFrame);
917 mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
918 }
919
920 /*
921 * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
922 */
923 bool fDropIt = false;
924 unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
925 if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
926 {
927 PINTNETSG pSG = (PINTNETSG)alloca(RT_OFFSETOF(INTNETSG, aSegs[cSegs]));
928 vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
929
930 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
931 if (fDropIt)
932 {
933 /*
934 * Check if this interface is in promiscuous mode. We should not drop
935 * any packets before they get to the driver as it passes them to tap
936 * callbacks in order for BPF to work properly.
937 */
938 if (vboxNetFltDarwinIsPromiscuous(pThis))
939 fDropIt = false;
940 else
941 mbuf_freem(pMBuf);
942 }
943 }
944
945 vboxNetFltRelease(pThis, true /* fBusy */);
946
947 return fDropIt ? EJUSTRETURN : 0;
948}
949
950
951/**
952 * From the host.
953 *
954 * @see iff_output_func in the darwin kpi.
955 */
956static errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
957{
958 /** @todo there was some note about the ethernet header here or something like that... */
959
960 NOREF(eProtocol);
961 NOREF(pIfNet);
962 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
963}
964
965
966/**
967 * From the wire.
968 *
969 * @see iff_input_func in the darwin kpi.
970 */
971static errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
972{
973 NOREF(eProtocol);
974 NOREF(pIfNet);
975 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
976}
977
978
979/** A worker thread for vboxNetFltSendDummy(). */
980static DECLCALLBACK(int) vboxNetFltSendDummyWorker(RTTHREAD hThreadSelf, void *pvUser)
981{
982 Assert(pvUser);
983 ifnet_t pIfNet = (ifnet_t)pvUser;
984 return VBoxNetSendDummy(pIfNet);
985}
986
987
988/**
989 * Prevent GUI icon freeze issue when VirtualBoxVM process terminates.
990 *
991 * This function is a workaround for stuck-in-dock issue. The idea here is to
992 * send a dummy packet to an interface from the context of a kernel thread.
993 * Therefore, an XNU's receive thread (which is created as a result if we are
994 * the first who is communicating with the interface) will be associated with
995 * the kernel thread instead of VirtualBoxVM process.
996 *
997 * @param pIfNet Interface to be used to send data.
998 */
999static void vboxNetFltSendDummy(ifnet_t pIfNet)
1000{
1001 RTTHREAD hThread;
1002 int rc = RTThreadCreate(&hThread, vboxNetFltSendDummyWorker, (void *)pIfNet, 0,
1003 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "DummyThread");
1004 if (RT_SUCCESS(rc))
1005 {
1006 RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL);
1007 LogFlow(("vboxNetFltSendDummy: a dummy packet has been successfully sent in order to prevent stuck-in-dock issue\n"));
1008 }
1009 else
1010 LogFlow(("vboxNetFltSendDummy: unable to send dummy packet in order to prevent stuck-in-dock issue\n"));
1011}
1012
1013
1014/**
1015 * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
1016 *
1017 * @returns VBox status code.
1018 * @param pThis The instance.
1019 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
1020 * flood the release log.
1021 */
1022static int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
1023{
1024 LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
1025
1026 /*
1027 * Locate the interface first.
1028 *
1029 * The pIfNet member is updated before iflt_attach is called and used
1030 * to deal with the hypothetical case where someone rips out the
1031 * interface immediately after our iflt_attach call.
1032 */
1033 ifnet_t pIfNet = NULL;
1034 errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
1035 if (err)
1036 {
1037 Assert(err == ENXIO);
1038 if (!fRediscovery)
1039 LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
1040 else
1041 Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
1042 return VERR_INTNET_FLT_IF_NOT_FOUND;
1043 }
1044
1045 RTSpinlockAcquire(pThis->hSpinlock);
1046 ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
1047 RTSpinlockRelease(pThis->hSpinlock);
1048
1049 /* Adjust g_offIfNetPCount as it varies for different versions of xnu. */
1050 vboxNetFltDarwinDetectPCountOffset(pIfNet);
1051
1052 /* Prevent stuck-in-dock issue by associating interface receive thread with kernel thread. */
1053 vboxNetFltSendDummy(pIfNet);
1054
1055 /*
1056 * Get the mac address while we still have a valid ifnet reference.
1057 */
1058 err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
1059 if (!err)
1060 {
1061 /*
1062 * Try attach the filter.
1063 */
1064 struct iff_filter RegRec;
1065 RegRec.iff_cookie = pThis;
1066 RegRec.iff_name = "VBoxNetFlt";
1067 RegRec.iff_protocol = 0;
1068 RegRec.iff_input = vboxNetFltDarwinIffInput;
1069 RegRec.iff_output = vboxNetFltDarwinIffOutput;
1070 RegRec.iff_event = vboxNetFltDarwinIffEvent;
1071 RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
1072 RegRec.iff_detached = vboxNetFltDarwinIffDetached;
1073 interface_filter_t pIfFilter = NULL;
1074 err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
1075 Assert(err || pIfFilter);
1076
1077 RTSpinlockAcquire(pThis->hSpinlock);
1078 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
1079 if (pIfNet && !err)
1080 {
1081 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
1082 ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
1083 pIfNet = NULL; /* don't dereference it */
1084 }
1085 RTSpinlockRelease(pThis->hSpinlock);
1086
1087 /* Report capabilities. */
1088 if ( !pIfNet
1089 && vboxNetFltTryRetainBusyNotDisconnected(pThis))
1090 {
1091 Assert(pThis->pSwitchPort);
1092 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
1093 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
1094 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
1095 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
1096 vboxNetFltRelease(pThis, true /*fBusy*/);
1097 }
1098 }
1099
1100 /* Release the interface on failure. */
1101 if (pIfNet)
1102 ifnet_release(pIfNet);
1103
1104 int rc = RTErrConvertFromErrno(err);
1105 if (RT_SUCCESS(rc))
1106 LogRel(("VBoxFltDrv: attached to '%s' / %RTmac\n", pThis->szName, &pThis->u.s.MacAddr));
1107 else
1108 LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
1109 return rc;
1110}
1111
1112
1113bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
1114{
1115 vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
1116 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
1117}
1118
1119
1120int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
1121{
1122 NOREF(pvIfData);
1123
1124 int rc = VINF_SUCCESS;
1125 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1126 if (pIfNet)
1127 {
1128 /*
1129 * Create a mbuf for the gather list and push it onto the wire.
1130 *
1131 * Note! If the interface is in the promiscuous mode we need to send the
1132 * packet down the stack so it reaches the driver and Berkeley
1133 * Packet Filter (see @bugref{5817}).
1134 */
1135 if ((fDst & INTNETTRUNKDIR_WIRE) || vboxNetFltDarwinIsPromiscuous(pThis))
1136 {
1137 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1138 if (pMBuf)
1139 {
1140 errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
1141 if (err)
1142 rc = RTErrConvertFromErrno(err);
1143 }
1144 else
1145 rc = VERR_NO_MEMORY;
1146 }
1147
1148 /*
1149 * Create a mbuf for the gather list and push it onto the host stack.
1150 */
1151 if (fDst & INTNETTRUNKDIR_HOST)
1152 {
1153 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1154 if (pMBuf)
1155 {
1156 /* This is what IONetworkInterface::inputPacket does. */
1157 unsigned const cbEthHdr = 14;
1158 mbuf_pkthdr_setheader(pMBuf, mbuf_data(pMBuf));
1159 mbuf_pkthdr_setlen(pMBuf, mbuf_pkthdr_len(pMBuf) - cbEthHdr);
1160 mbuf_setdata(pMBuf, (uint8_t *)mbuf_data(pMBuf) + cbEthHdr, mbuf_len(pMBuf) - cbEthHdr);
1161 mbuf_pkthdr_setrcvif(pMBuf, pIfNet); /* will crash without this. */
1162
1163 errno_t err = ifnet_input(pIfNet, pMBuf, NULL);
1164 if (err)
1165 rc = RTErrConvertFromErrno(err);
1166 }
1167 else
1168 rc = VERR_NO_MEMORY;
1169 }
1170
1171 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1172 }
1173
1174 return rc;
1175}
1176
1177
1178void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1179{
1180 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1181 if (pIfNet)
1182 {
1183 if (pThis->fDisablePromiscuous)
1184 {
1185 /*
1186 * Promiscuous mode should not be used (wireless), we just need to
1187 * make sure the interface is up.
1188 */
1189 if (fActive)
1190 {
1191 u_int16_t fIf = ifnet_flags(pIfNet);
1192 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1193 {
1194 ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1195 ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1196 }
1197 }
1198 }
1199 else
1200 {
1201 /*
1202 * This api is a bit weird, the best reference is the code.
1203 *
1204 * Also, we have a bit or race conditions wrt the maintenance of
1205 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1206 */
1207 unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
1208 u_int16_t fIf;
1209 if (fActive)
1210 {
1211 Assert(!pThis->u.s.fSetPromiscuous);
1212 errno_t err = ENETDOWN;
1213 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
1214
1215 /*
1216 * Try bring the interface up and running if it's down.
1217 */
1218 fIf = ifnet_flags(pIfNet);
1219 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1220 {
1221 err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1222 errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1223 if (!err)
1224 err = err2;
1225 fIf = ifnet_flags(pIfNet);
1226 }
1227
1228 /*
1229 * Is it already up? If it isn't, leave it to the link event or
1230 * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
1231 */
1232 if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1233 {
1234 err = ifnet_set_promiscuous(pIfNet, 1);
1235 pThis->u.s.fSetPromiscuous = err == 0;
1236 if (!err)
1237 {
1238 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1239
1240 /* check if it actually worked, this stuff is not always behaving well. */
1241 if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
1242 {
1243 err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
1244 if (!err)
1245 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1246 if (!err)
1247 Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1248 else
1249 Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
1250 pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1251 }
1252 }
1253 else
1254 Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1255 }
1256 else if (!err)
1257 Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1258 if (err)
1259 LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1260 }
1261 else
1262 {
1263 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1264 if (pThis->u.s.fSetPromiscuous)
1265 {
1266 errno_t err = ifnet_set_promiscuous(pIfNet, 0);
1267 AssertMsg(!err, ("%d\n", err)); NOREF(err);
1268 }
1269 pThis->u.s.fSetPromiscuous = false;
1270
1271 fIf = ifnet_flags(pIfNet);
1272 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1273 }
1274 }
1275
1276 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1277 }
1278}
1279
1280
1281int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1282{
1283 /* Nothing to do here. */
1284 return VINF_SUCCESS;
1285}
1286
1287
1288int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1289{
1290 /* Nothing to do here. */
1291 return VINF_SUCCESS;
1292}
1293
1294
1295void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1296{
1297 interface_filter_t pIfFilter;
1298
1299 /*
1300 * Carefully obtain the interface filter reference and detach it.
1301 */
1302 RTSpinlockAcquire(pThis->hSpinlock);
1303 pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
1304 if (pIfFilter)
1305 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
1306 RTSpinlockRelease(pThis->hSpinlock);
1307
1308 if (pIfFilter)
1309 iflt_detach(pIfFilter);
1310
1311 if (pThis->u.s.pSysSock != NULL)
1312 {
1313 sock_close(pThis->u.s.pSysSock);
1314 pThis->u.s.pSysSock = NULL;
1315 }
1316}
1317
1318
1319int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1320{
1321 NOREF(pvContext);
1322
1323 int rc = vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
1324 if (RT_FAILURE(rc))
1325 return rc;
1326
1327 if (pThis->pSwitchPort->pfnNotifyHostAddress == NULL)
1328 return rc;
1329
1330 /*
1331 * XXX: uwe
1332 *
1333 * Learn host's IP addresses and set up notifications for changes.
1334 * To avoid racing, set up notifications first.
1335 *
1336 * XXX: This should probably be global, since the only thing
1337 * specific to ifnet here is its IPv6 link-local address.
1338 */
1339 errno_t error;
1340
1341 error = sock_socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT,
1342 vboxNetFltDarwinSysSockUpcall, pThis,
1343 &pThis->u.s.pSysSock);
1344 if (error != 0)
1345 {
1346 LogRel(("sock_socket(SYSPROTO_EVENT): error %d\n", error));
1347 return rc;
1348 }
1349
1350 int nbio = 1;
1351 error = sock_ioctl(pThis->u.s.pSysSock, FIONBIO, &nbio);
1352 if (error != 0)
1353 {
1354 LogRel(("FIONBIO: error %d\n", error));
1355 sock_close(pThis->u.s.pSysSock);
1356 return rc;
1357 }
1358
1359 if (!sock_isnonblocking(pThis->u.s.pSysSock))
1360 {
1361 LogRel(("FIONBIO ok, but socket is blocking?!\n"));
1362 sock_close(pThis->u.s.pSysSock);
1363 return rc;
1364 }
1365
1366 struct kev_request req;
1367 req.vendor_code = KEV_VENDOR_APPLE;
1368 req.kev_class = KEV_NETWORK_CLASS;
1369 req.kev_subclass = KEV_ANY_SUBCLASS; /* need both INET and INET6, so have to request all */
1370
1371 error = sock_ioctl(pThis->u.s.pSysSock, SIOCSKEVFILT, &req);
1372 if (error != 0)
1373 {
1374 LogRel(("SIOCSKEVFILT: error %d\n", error));
1375 sock_close(pThis->u.s.pSysSock);
1376 return rc;
1377 }
1378
1379 ifnet_t pIfNet = pThis->u.s.pIfNet; /* already retained */
1380
1381 ifaddr_t *pIfAddrList;
1382 error = ifnet_get_address_list(/* all interfaces*/ NULL, &pIfAddrList);
1383 if (error != 0)
1384 {
1385 LogRel(("ifnet_get_address_list: error %d\n", error));
1386 return rc;
1387 }
1388
1389 for (ifaddr_t *pIfAddr = pIfAddrList; *pIfAddr != NULL; ++pIfAddr)
1390 {
1391 ifaddr_t ifa = *pIfAddr;
1392 sa_family_t family = ifaddr_address_family(ifa);
1393 struct sockaddr_storage ss;
1394
1395 error = ifaddr_address(ifa, (struct sockaddr *)&ss, sizeof(ss));
1396 if (error != 0)
1397 {
1398 LogRel(("getting address family %d: error %d\n", family, error));
1399 continue;
1400 }
1401
1402 if (family == AF_INET)
1403 {
1404 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
1405 u_int32_t u32Addr = ntohl(sin->sin_addr.s_addr);
1406
1407 if (VBOX_IN_LOOPBACK(u32Addr))
1408 continue;
1409
1410 if (ifaddr_ifnet(ifa) != pIfNet && VBOX_IN_LINKLOCAL(u32Addr))
1411 continue;
1412
1413 Log(("> inet %RTnaipv4\n", sin->sin_addr.s_addr));
1414 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1415 /* :fAdded */ true, kIntNetAddrType_IPv4, &sin->sin_addr);
1416 }
1417 else if (family == AF_INET6)
1418 {
1419 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
1420
1421 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
1422 continue;
1423
1424 /* link-local from other interfaces are out of scope */
1425 if (ifaddr_ifnet(ifa) != pIfNet && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1426 continue;
1427
1428 Log(("> inet6 %RTnaipv6\n", &sin6->sin6_addr));
1429 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1430 /* :fAdded */ true, kIntNetAddrType_IPv6, &sin6->sin6_addr);
1431 }
1432 }
1433
1434 ifnet_free_address_list(pIfAddrList);
1435
1436 /*
1437 * Now that we've got current addresses, check for events that
1438 * might have happened while we were working.
1439 */
1440 vboxNetFltDarwinSysSockUpcall(pThis->u.s.pSysSock, pThis, MBUF_DONTWAIT);
1441
1442 return rc;
1443}
1444
1445
1446static void vboxNetFltDarwinSysSockUpcall(socket_t pSysSock, void *pvData, int fWait)
1447{
1448 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvData;
1449 errno_t error;
1450
1451 NOREF(fWait);
1452
1453 if (RT_UNLIKELY(pSysSock != pThis->u.s.pSysSock))
1454 {
1455 Log(("vboxNetFltDarwinSysSockUpcall: %p != %p?\n",
1456 pSysSock, pThis->u.s.pSysSock));
1457 return;
1458 }
1459
1460 struct net_event_data my_link;
1461 ifnet_t pIfNet = pThis->u.s.pIfNet; /* XXX: retain? */
1462 ifnet_family_t if_family = ifnet_family(pIfNet);
1463 u_int32_t if_unit = ifnet_unit(pIfNet);
1464
1465 for (;;) {
1466 mbuf_t m;
1467 size_t len = sizeof(struct kern_event_msg) - sizeof(u_int32_t)
1468 + sizeof(struct kev_in6_data);
1469
1470 error = sock_receivembuf(pSysSock, NULL, &m, 0, &len);
1471 if (error == EWOULDBLOCK)
1472 {
1473 Log(("vboxNetFltDarwinSysSockUpcall: EWOULDBLOCK - we are done\n"));
1474 error = 0;
1475 break;
1476 }
1477 else if (error != 0)
1478 {
1479 Log(("sock_receivembuf: error %d\n", error));
1480 break;
1481 }
1482
1483 if (len < sizeof(struct kern_event_msg) - sizeof(u_int32_t))
1484 {
1485 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short\n",
1486 (unsigned int)len));
1487 mbuf_freem(m);
1488 return;
1489 }
1490
1491 struct kern_event_msg *msg = (struct kern_event_msg *)mbuf_data(m);
1492 if (msg->kev_subclass == KEV_INET_SUBCLASS)
1493 {
1494 if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in_data))
1495 {
1496 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET_SUBCLASS\n",
1497 (unsigned int)len));
1498 mbuf_freem(m);
1499 return;
1500 }
1501
1502 struct kev_in_data *iev = (struct kev_in_data *)msg->event_data;
1503 struct net_event_data *link = &iev->link_data;
1504 PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev->ia_addr;
1505 u_int32_t u32Addr = ntohl(pAddr->IPv4.u);
1506
1507 if (VBOX_IN_LOOPBACK(u32Addr))
1508 {
1509 mbuf_freem(m);
1510 continue;
1511 }
1512
1513 if ( (link->if_family != if_family || link->if_unit != if_unit)
1514 && VBOX_IN_LINKLOCAL(u32Addr))
1515 {
1516 mbuf_freem(m);
1517 continue;
1518 }
1519
1520 switch (msg->event_code)
1521 {
1522 case KEV_INET_NEW_ADDR:
1523 Log(("KEV_INET_NEW_ADDR %.*s%d: %RTnaipv4\n",
1524 IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1525
1526 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1527 /* :fAdded */ true, kIntNetAddrType_IPv4, pAddr);
1528 break;
1529
1530 case KEV_INET_ADDR_DELETED:
1531 Log(("KEV_INET_ADDR_DELETED %.*s%d: %RTnaipv4\n",
1532 IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1533
1534 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1535 /* :fAdded */ false, kIntNetAddrType_IPv4, pAddr);
1536 break;
1537
1538 default:
1539 Log(("KEV INET event %u %.*s%d: addr %RTnaipv4\n",
1540 msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1541 break;
1542 }
1543 }
1544 else if (msg->kev_subclass == KEV_INET6_SUBCLASS)
1545 {
1546 if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in6_data))
1547 {
1548 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET6_SUBCLASS\n",
1549 (unsigned int)len));
1550 mbuf_freem(m);
1551 return;
1552 }
1553
1554 struct kev_in6_data *iev6 = (struct kev_in6_data *)msg->event_data;
1555 struct net_event_data *link = &iev6->link_data;
1556 PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev6->ia_addr.sin6_addr;
1557
1558 if (IN6_IS_ADDR_LOOPBACK(&iev6->ia_addr.sin6_addr))
1559 {
1560 mbuf_freem(m);
1561 continue;
1562 }
1563
1564 if ( (link->if_family != if_family || link->if_unit != if_unit)
1565 && IN6_IS_ADDR_LINKLOCAL(&iev6->ia_addr.sin6_addr))
1566 {
1567 mbuf_freem(m);
1568 continue;
1569 }
1570
1571
1572
1573 switch (msg->event_code)
1574 {
1575 case KEV_INET6_NEW_USER_ADDR:
1576 Log(("KEV_INET6_NEW_USER_ADDR %.*s%d: %RTnaipv6\n",
1577 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1578 goto kev_inet6_new;
1579
1580 case KEV_INET6_NEW_LL_ADDR:
1581 Log(("KEV_INET6_NEW_LL_ADDR %.*s%d: %RTnaipv6\n",
1582 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1583 goto kev_inet6_new;
1584
1585 case KEV_INET6_NEW_RTADV_ADDR:
1586 Log(("KEV_INET6_NEW_RTADV_ADDR %.*s%d: %RTnaipv6\n",
1587 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1588 goto kev_inet6_new;
1589
1590 kev_inet6_new:
1591 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1592 /* :fAdded */ true, kIntNetAddrType_IPv6, pAddr);
1593 break;
1594
1595 case KEV_INET6_ADDR_DELETED:
1596 Log(("KEV_INET6_ADDR_DELETED %.*s%d: %RTnaipv6\n",
1597 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1598
1599 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1600 /* :fAdded */ false, kIntNetAddrType_IPv6, pAddr);
1601 break;
1602
1603 default:
1604 Log(("KEV INET6 event %u %.*s%d: addr %RTnaipv6\n",
1605 msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1606 break;
1607 }
1608 }
1609 else
1610 {
1611 Log(("vboxNetFltDarwinSysSockUpcall: subclass %u ignored\n",
1612 (unsigned)msg->kev_subclass));
1613 }
1614
1615 mbuf_freem(m);
1616 }
1617}
1618
1619
1620int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1621{
1622 /*
1623 * Init the darwin specific members.
1624 */
1625 pThis->u.s.pIfNet = NULL;
1626 pThis->u.s.pIfFilter = NULL;
1627 pThis->u.s.fSetPromiscuous = false;
1628 pThis->u.s.fNeedSetPromiscuous = false;
1629 //pThis->u.s.MacAddr = {0};
1630 pThis->u.s.pSysSock = NULL;
1631
1632 return VINF_SUCCESS;
1633}
1634
1635
1636void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
1637{
1638 NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
1639}
1640
1641
1642int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
1643{
1644 /* Nothing to do */
1645 NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
1646 return VINF_SUCCESS;
1647}
1648
1649
1650int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
1651{
1652 /* Nothing to do */
1653 NOREF(pThis); NOREF(pvIfData);
1654 return VINF_SUCCESS;
1655}
1656
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