VirtualBox

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

Last change on this file since 56265 was 56265, checked in by vboxsync, 9 years ago

VBoxNetFlt-darwin: be consistent when passing IPv4 address to the
pfnNotifyHostAddress callback, use the same variable.

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