VirtualBox

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

Last change on this file since 103251 was 99739, checked in by vboxsync, 19 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

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