VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • 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 98103 2023-01-17 14:15:46Z 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 * @returns Number of segments.
595 * @param pThis The instance.
596 * @param pMBuf The mbuf.
597 * @param pSG The SG.
598 * @param pvFrame The frame pointer, optional.
599 * @param cSegs The number of segments allocated for the SG.
600 * This should match the number in the mbuf exactly!
601 * @param fSrc The source of the frame.
602 */
603DECLINLINE(void) vboxNetFltDarwinMBufToSG(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame, PINTNETSG pSG, unsigned cSegs,
604 uint32_t fSrc)
605{
606 RT_NOREF(pThis, fSrc);
607
608 /*
609 * Walk the chain and convert the buffers to segments. Works INTNETSG::cbTotal.
610 */
611 unsigned iSeg = 0;
612 IntNetSgInitTempSegs(pSG, 0 /*cbTotal*/, cSegs, 0 /*cSegsUsed*/);
613 for (mbuf_t pCur = pMBuf; pCur; pCur = mbuf_next(pCur))
614 {
615 size_t cbSeg = mbuf_len(pCur);
616 if (cbSeg)
617 {
618 void *pvSeg = mbuf_data(pCur);
619
620 /* deal with pvFrame */
621 if (!iSeg && pvFrame && pvFrame != pvSeg)
622 {
623 void *pvStart = mbuf_datastart(pMBuf);
624 uintptr_t offSeg = (uintptr_t)pvSeg - (uintptr_t)pvStart;
625 uintptr_t offSegEnd = offSeg + cbSeg;
626 Assert(pvStart && pvSeg && offSeg < mbuf_maxlen(pMBuf) && offSegEnd <= mbuf_maxlen(pMBuf)); NOREF(offSegEnd);
627 uintptr_t offFrame = (uintptr_t)pvFrame - (uintptr_t)pvStart;
628 if (RT_LIKELY(offFrame < offSeg))
629 {
630 pvSeg = pvFrame;
631 cbSeg += offSeg - offFrame;
632 }
633 else
634 AssertMsgFailed(("pvFrame=%p pvStart=%p pvSeg=%p offSeg=%p cbSeg=%#zx offSegEnd=%p offFrame=%p maxlen=%#zx\n",
635 pvFrame, pvStart, pvSeg, offSeg, cbSeg, offSegEnd, offFrame, mbuf_maxlen(pMBuf)));
636 pvFrame = NULL;
637 }
638
639 AssertBreak(iSeg < cSegs);
640 pSG->cbTotal += cbSeg;
641 pSG->aSegs[iSeg].cb = cbSeg;
642 pSG->aSegs[iSeg].pv = pvSeg;
643 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
644 iSeg++;
645 }
646 /* The pvFrame might be in a now empty buffer. */
647 else if ( !iSeg
648 && pvFrame
649 && (uintptr_t)pvFrame - (uintptr_t)mbuf_datastart(pMBuf) < mbuf_maxlen(pMBuf))
650 {
651 cbSeg = (uintptr_t)mbuf_datastart(pMBuf) + mbuf_maxlen(pMBuf) - (uintptr_t)pvFrame;
652 pSG->cbTotal += cbSeg;
653 pSG->aSegs[iSeg].cb = cbSeg;
654 pSG->aSegs[iSeg].pv = pvFrame;
655 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
656 iSeg++;
657 pvFrame = NULL;
658 }
659 }
660
661 Assert(iSeg && iSeg <= cSegs);
662 pSG->cSegsUsed = iSeg;
663
664#ifdef PADD_RUNT_FRAMES_FROM_HOST
665 /*
666 * Add a trailer if the frame is too small.
667 *
668 * Since we're getting to the packet before it is framed, it has not
669 * yet been padded. The current solution is to add a segment pointing
670 * to a buffer containing all zeros and pray that works for all frames...
671 */
672 if (pSG->cbTotal < 60 && (fSrc == INTNETTRUNKDIR_HOST))
673 {
674 AssertReturnVoid(iSeg < cSegs);
675
676 static uint8_t const s_abZero[128] = {0};
677 pSG->aSegs[iSeg].Phys = NIL_RTHCPHYS;
678 pSG->aSegs[iSeg].pv = (void *)&s_abZero[0];
679 pSG->aSegs[iSeg].cb = 60 - pSG->cbTotal;
680 pSG->cbTotal = 60;
681 pSG->cSegsUsed++;
682 }
683#endif
684
685#ifdef VBOXNETFLT_DARWIN_TEST_SEG_SIZE
686 /*
687 * Redistribute the segments.
688 */
689 if (pSG->cSegsUsed < pSG->cSegsAlloc)
690 {
691 /* copy the segments to the end. */
692 int iSrc = pSG->cSegsUsed;
693 int iDst = pSG->cSegsAlloc;
694 while (iSrc > 0)
695 {
696 iDst--;
697 iSrc--;
698 pSG->aSegs[iDst] = pSG->aSegs[iSrc];
699 }
700
701 /* create small segments from the start. */
702 pSG->cSegsUsed = pSG->cSegsAlloc;
703 iSrc = iDst;
704 iDst = 0;
705 while ( iDst < iSrc
706 && iDst < pSG->cSegsAlloc)
707 {
708 pSG->aSegs[iDst].Phys = NIL_RTHCPHYS;
709 pSG->aSegs[iDst].pv = pSG->aSegs[iSrc].pv;
710 pSG->aSegs[iDst].cb = RT_MIN(pSG->aSegs[iSrc].cb, VBOXNETFLT_DARWIN_TEST_SEG_SIZE);
711 if (pSG->aSegs[iDst].cb != pSG->aSegs[iSrc].cb)
712 {
713 pSG->aSegs[iSrc].cb -= pSG->aSegs[iDst].cb;
714 pSG->aSegs[iSrc].pv = (uint8_t *)pSG->aSegs[iSrc].pv + pSG->aSegs[iDst].cb;
715 }
716 else if (++iSrc >= pSG->cSegsAlloc)
717 {
718 pSG->cSegsUsed = iDst + 1;
719 break;
720 }
721 iDst++;
722 }
723 }
724#endif
725
726 AssertMsg(!pvFrame, ("pvFrame=%p pMBuf=%p iSeg=%d\n", pvFrame, pMBuf, iSeg));
727}
728
729
730/**
731 * Helper for determining whether the host wants the interface to be
732 * promiscuous.
733 */
734static bool vboxNetFltDarwinIsPromiscuous(PVBOXNETFLTINS pThis)
735{
736 bool fRc = false;
737 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
738 if (pIfNet)
739 {
740 /* gather the data */
741 uint16_t fIf = ifnet_flags(pIfNet);
742 unsigned cPromisc = VBOX_GET_PCOUNT(pIfNet);
743 bool fSetPromiscuous = ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous);
744 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
745
746 /* calc the return. */
747 fRc = (fIf & IFF_PROMISC)
748 && cPromisc > fSetPromiscuous;
749 }
750 return fRc;
751}
752
753
754
755/**
756 *
757 * @see iff_detached_func in the darwin kpi.
758 */
759static void vboxNetFltDarwinIffDetached(void *pvThis, ifnet_t pIfNet)
760{
761 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
762 uint64_t NanoTS = RTTimeSystemNanoTS();
763 LogFlow(("vboxNetFltDarwinIffDetached: pThis=%p NanoTS=%RU64 (%d)\n",
764 pThis, NanoTS, RT_VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : -1));
765
766 Assert(!pThis->fDisconnectedFromHost);
767 Assert(!pThis->fRediscoveryPending);
768
769 /*
770 * If we've put it into promiscuous mode, undo that now. If we don't
771 * the if_pcount will go all wrong when it's replugged.
772 */
773 if (ASMAtomicXchgBool(&pThis->u.s.fSetPromiscuous, false))
774 ifnet_set_promiscuous(pIfNet, 0);
775
776 /*
777 * We carefully take the spinlock and increase the interface reference
778 * behind it in order to avoid problematic races with the detached callback.
779 */
780 RTSpinlockAcquire(pThis->hSpinlock);
781
782 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
783 int cPromisc = RT_VALID_PTR(pIfNet) ? VBOX_GET_PCOUNT(pIfNet) : - 1;
784
785 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfNet);
786 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
787 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
788 pThis->u.s.fSetPromiscuous = false;
789 ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, NanoTS);
790 ASMAtomicUoWriteBool(&pThis->fRediscoveryPending, false);
791 ASMAtomicWriteBool(&pThis->fDisconnectedFromHost, true);
792
793 RTSpinlockRelease(pThis->hSpinlock);
794
795 if (pIfNet)
796 ifnet_release(pIfNet);
797 LogRel(("VBoxNetFlt: was detached from '%s' (%d)\n", pThis->szName, cPromisc));
798}
799
800
801/**
802 *
803 * @see iff_ioctl_func in the darwin kpi.
804 */
805static errno_t vboxNetFltDarwinIffIoCtl(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, u_long uCmd, void *pvArg)
806{
807 RT_NOREF(pIfNet);
808 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
809 LogFlow(("vboxNetFltDarwinIffIoCtl: pThis=%p uCmd=%lx\n", pThis, uCmd));
810
811 /*
812 * Update fOtherPromiscuous.
813 */
814 /** @todo we'll have to find the offset of if_pcount to get this right! */
815 //if (uCmd == SIOCSIFFLAGS)
816 //{
817 //
818 //}
819
820 /*
821 * We didn't handle it, continue processing.
822 */
823 NOREF(pThis);
824 NOREF(eProtocol);
825 NOREF(uCmd);
826 NOREF(pvArg);
827 return EOPNOTSUPP;
828}
829
830
831/**
832 *
833 * @see iff_event_func in the darwin kpi.
834 */
835static void vboxNetFltDarwinIffEvent(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, const struct kev_msg *pEvMsg)
836{
837 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvThis;
838 LogFlow(("vboxNetFltDarwinIffEvent: pThis=%p\n", pThis));
839
840 NOREF(pThis);
841 NOREF(pIfNet);
842 NOREF(eProtocol);
843 NOREF(pEvMsg);
844
845 /*
846 * Watch out for the interface going online / offline.
847 */
848 if ( RT_VALID_PTR(pThis)
849 && RT_VALID_PTR(pEvMsg)
850 && pEvMsg->vendor_code == KEV_VENDOR_APPLE
851 && pEvMsg->kev_class == KEV_NETWORK_CLASS
852 && pEvMsg->kev_subclass == KEV_DL_SUBCLASS)
853 {
854 if (pThis->u.s.pIfNet == pIfNet)
855 {
856 if (pEvMsg->event_code == KEV_DL_LINK_ON)
857 {
858 if (ASMAtomicUoReadBool(&pThis->u.s.fNeedSetPromiscuous))
859 {
860 /* failed to bring it online. */
861 errno_t err = ifnet_set_promiscuous(pIfNet, 1);
862 if (!err)
863 {
864 ASMAtomicWriteBool(&pThis->u.s.fSetPromiscuous, true);
865 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
866 Log(("vboxNetFltDarwinIffEvent: enabled promiscuous mode on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
867 }
868 else
869 Log(("vboxNetFltDarwinIffEvent: ifnet_set_promiscuous failed on %s, err=%d (%d)\n", pThis->szName, err, VBOX_GET_PCOUNT(pIfNet)));
870 }
871 else if ( ASMAtomicUoReadBool(&pThis->u.s.fSetPromiscuous)
872 && !(ifnet_flags(pIfNet) & IFF_PROMISC))
873 {
874 /* Try fix the inconsistency. */
875 errno_t err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
876 if (!err)
877 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
878 if (!err && (ifnet_flags(pIfNet) & IFF_PROMISC))
879 Log(("vboxNetFltDarwinIffEvent: fixed IFF_PROMISC on %s (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
880 else
881 Log(("vboxNetFltDarwinIffEvent: failed to fix IFF_PROMISC on %s, err=%d flags=%#x (%d)\n",
882 pThis->szName, err, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
883 }
884 else
885 Log(("vboxNetFltDarwinIffEvent: online, '%s'. flags=%#x (%d)\n", pThis->szName, ifnet_flags(pIfNet), VBOX_GET_PCOUNT(pIfNet)));
886 }
887 else if (pEvMsg->event_code == KEV_DL_LINK_OFF)
888 Log(("vboxNetFltDarwinIffEvent: %s goes down (%d)\n", pThis->szName, VBOX_GET_PCOUNT(pIfNet)));
889/** @todo KEV_DL_LINK_ADDRESS_CHANGED -> pfnReportMacAddress */
890/** @todo KEV_DL_SIFFLAGS -> pfnReportPromiscuousMode */
891 }
892 else
893 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));
894 }
895 else if (RT_VALID_PTR(pEvMsg))
896 Log(("vboxNetFltDarwinIffEvent: vendor_code=%#x kev_class=%#x kev_subclass=%#x event_code=%#x\n",
897 pEvMsg->vendor_code, pEvMsg->kev_class, pEvMsg->kev_subclass, pEvMsg->event_code));
898}
899
900
901/**
902 * Internal worker for vboxNetFltDarwinIffInput and vboxNetFltDarwinIffOutput,
903 *
904 * @returns 0 or EJUSTRETURN.
905 * @param pThis The instance.
906 * @param pMBuf The mbuf.
907 * @param pvFrame The start of the frame, optional.
908 * @param fSrc Where the packet (allegedly) comes from, one INTNETTRUNKDIR_* value.
909 * @param eProtocol The protocol.
910 */
911static errno_t vboxNetFltDarwinIffInputOutputWorker(PVBOXNETFLTINS pThis, mbuf_t pMBuf, void *pvFrame,
912 uint32_t fSrc, protocol_family_t eProtocol)
913{
914 /*
915 * Drop it immediately?
916 */
917 Log2(("vboxNetFltDarwinIffInputOutputWorker: pThis=%p pMBuf=%p pvFrame=%p fSrc=%#x cbPkt=%x\n",
918 pThis, pMBuf, pvFrame, fSrc, pMBuf ? mbuf_pkthdr_len(pMBuf) : -1));
919 if (!pMBuf)
920 return 0;
921#if 0 /* debugging lost icmp packets */
922 if (mbuf_pkthdr_len(pMBuf) > 0x300)
923 {
924 uint8_t *pb = (uint8_t *)(pvFrame ? pvFrame : mbuf_data(pMBuf));
925 Log3(("D=%.6Rhxs S=%.6Rhxs T=%04x IFF\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
926 }
927#endif
928 if (vboxNetFltDarwinMBufIsOur(pThis, pMBuf, pvFrame))
929 return 0;
930
931 /*
932 * Active? Retain the instance and increment the busy counter.
933 */
934 if (!vboxNetFltTryRetainBusyActive(pThis))
935 return 0;
936
937 /*
938 * Finalize out-bound packets since the stack puts off finalizing
939 * TCP/IP checksums as long as possible.
940 * ASSUMES this only applies to outbound IP packets.
941 */
942 if (fSrc == INTNETTRUNKDIR_HOST)
943 {
944 Assert(!pvFrame);
945 mbuf_outbound_finalize(pMBuf, eProtocol, sizeof(RTNETETHERHDR));
946 }
947
948 /*
949 * Create a (scatter/)gather list for the mbuf and feed it to the internal network.
950 */
951 bool fDropIt = false;
952 unsigned cSegs = vboxNetFltDarwinMBufCalcSGSegs(pThis, pMBuf, pvFrame);
953 if (cSegs < VBOXNETFLT_DARWIN_MAX_SEGS)
954 {
955 PINTNETSG pSG = (PINTNETSG)alloca(RT_UOFFSETOF_DYN(INTNETSG, aSegs[cSegs]));
956 vboxNetFltDarwinMBufToSG(pThis, pMBuf, pvFrame, pSG, cSegs, fSrc);
957
958 fDropIt = pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, NULL /* pvIf */, pSG, fSrc);
959 if (fDropIt)
960 {
961 /*
962 * If the interface is in promiscuous mode we should let
963 * all inbound packets (this one was for a bridged guest)
964 * reach the driver as it passes them to tap callbacks in
965 * order for BPF to work properly.
966 */
967 if ( fSrc == INTNETTRUNKDIR_WIRE
968 && vboxNetFltDarwinIsPromiscuous(pThis))
969 {
970 fDropIt = false;
971 }
972
973 /*
974 * A packet from the host to a guest. As we won't pass it
975 * to the drvier/wire we need to feed it to bpf ourselves.
976 *
977 * XXX: TODO: bpf should be done before; use pfnPreRecv?
978 */
979 if (fSrc == INTNETTRUNKDIR_HOST)
980 {
981 bpf_tap_out(pThis->u.s.pIfNet, DLT_EN10MB, pMBuf, NULL, 0);
982 ifnet_stat_increment_out(pThis->u.s.pIfNet, 1, mbuf_len(pMBuf), 0);
983 }
984 }
985 }
986
987 vboxNetFltRelease(pThis, true /* fBusy */);
988
989 if (fDropIt)
990 {
991 mbuf_freem(pMBuf);
992 return EJUSTRETURN;
993 }
994 return 0;
995}
996
997
998/**
999 * From the host.
1000 *
1001 * @see iff_output_func in the darwin kpi.
1002 */
1003static errno_t vboxNetFltDarwinIffOutput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf)
1004{
1005 /** @todo there was some note about the ethernet header here or something like that... */
1006
1007 NOREF(eProtocol);
1008 NOREF(pIfNet);
1009 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, NULL, INTNETTRUNKDIR_HOST, eProtocol);
1010}
1011
1012
1013/**
1014 * From the wire.
1015 *
1016 * @see iff_input_func in the darwin kpi.
1017 */
1018static errno_t vboxNetFltDarwinIffInput(void *pvThis, ifnet_t pIfNet, protocol_family_t eProtocol, mbuf_t *ppMBuf, char **ppchFrame)
1019{
1020 RT_NOREF(eProtocol, pIfNet);
1021 return vboxNetFltDarwinIffInputOutputWorker((PVBOXNETFLTINS)pvThis, *ppMBuf, *ppchFrame, INTNETTRUNKDIR_WIRE, eProtocol);
1022}
1023
1024
1025/** A worker thread for vboxNetFltSendDummy(). */
1026static DECLCALLBACK(int) vboxNetFltSendDummyWorker(RTTHREAD hThreadSelf, void *pvUser)
1027{
1028 RT_NOREF(hThreadSelf);
1029 Assert(pvUser);
1030 ifnet_t pIfNet = (ifnet_t)pvUser;
1031 return VBoxNetSendDummy(pIfNet);
1032}
1033
1034
1035/**
1036 * Prevent GUI icon freeze issue when VirtualBoxVM process terminates.
1037 *
1038 * This function is a workaround for stuck-in-dock issue. The idea here is to
1039 * send a dummy packet to an interface from the context of a kernel thread.
1040 * Therefore, an XNU's receive thread (which is created as a result if we are
1041 * the first who is communicating with the interface) will be associated with
1042 * the kernel thread instead of VirtualBoxVM process.
1043 *
1044 * @param pIfNet Interface to be used to send data.
1045 */
1046static void vboxNetFltSendDummy(ifnet_t pIfNet)
1047{
1048 RTTHREAD hThread;
1049 int rc = RTThreadCreate(&hThread, vboxNetFltSendDummyWorker, (void *)pIfNet, 0,
1050 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "DummyThread");
1051 if (RT_SUCCESS(rc))
1052 {
1053 RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL);
1054 LogFlow(("vboxNetFltSendDummy: a dummy packet has been successfully sent in order to prevent stuck-in-dock issue\n"));
1055 }
1056 else
1057 LogFlow(("vboxNetFltSendDummy: unable to send dummy packet in order to prevent stuck-in-dock issue\n"));
1058}
1059
1060
1061/**
1062 * Internal worker for vboxNetFltOsInitInstance and vboxNetFltOsMaybeRediscovered.
1063 *
1064 * @returns VBox status code.
1065 * @param pThis The instance.
1066 * @param fRediscovery If set we're doing a rediscovery attempt, so, don't
1067 * flood the release log.
1068 */
1069static int vboxNetFltDarwinAttachToInterface(PVBOXNETFLTINS pThis, bool fRediscovery)
1070{
1071 LogFlow(("vboxNetFltDarwinAttachToInterface: pThis=%p (%s)\n", pThis, pThis->szName));
1072 IPRT_DARWIN_SAVE_EFL_AC();
1073
1074 /*
1075 * Locate the interface first.
1076 *
1077 * The pIfNet member is updated before iflt_attach is called and used
1078 * to deal with the hypothetical case where someone rips out the
1079 * interface immediately after our iflt_attach call.
1080 */
1081 ifnet_t pIfNet = NULL;
1082 errno_t err = ifnet_find_by_name(pThis->szName, &pIfNet);
1083 if (err)
1084 {
1085 Assert(err == ENXIO);
1086 if (!fRediscovery)
1087 LogRel(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
1088 else
1089 Log(("VBoxFltDrv: failed to find ifnet '%s' (err=%d)\n", pThis->szName, err));
1090 IPRT_DARWIN_RESTORE_EFL_AC();
1091 return VERR_INTNET_FLT_IF_NOT_FOUND;
1092 }
1093
1094 AssertCompileMemberAlignment(VBOXNETFLTINS, u.s.pIfNet, ARCH_BITS / 8);
1095 AssertMsg(!((uintptr_t)&pThis->u.s.pIfNet & (ARCH_BITS / 8 - 1)), ("pThis=%p\n", pThis));
1096 RTSpinlockAcquire(pThis->hSpinlock);
1097 ASMAtomicUoWritePtr(&pThis->u.s.pIfNet, pIfNet);
1098 RTSpinlockRelease(pThis->hSpinlock);
1099
1100 /* Adjust g_offIfNetPCount as it varies for different versions of xnu. */
1101 vboxNetFltDarwinDetectPCountOffset(pIfNet);
1102
1103 /* Prevent stuck-in-dock issue by associating interface receive thread with kernel thread. */
1104 vboxNetFltSendDummy(pIfNet);
1105
1106 /*
1107 * Get the mac address while we still have a valid ifnet reference.
1108 */
1109 err = ifnet_lladdr_copy_bytes(pIfNet, &pThis->u.s.MacAddr, sizeof(pThis->u.s.MacAddr));
1110 if (!err)
1111 {
1112 /*
1113 * Try attach the filter.
1114 */
1115 struct iff_filter RegRec;
1116 RegRec.iff_cookie = pThis;
1117 RegRec.iff_name = "VBoxNetFlt";
1118 RegRec.iff_protocol = 0;
1119 RegRec.iff_input = vboxNetFltDarwinIffInput;
1120 RegRec.iff_output = vboxNetFltDarwinIffOutput;
1121 RegRec.iff_event = vboxNetFltDarwinIffEvent;
1122 RegRec.iff_ioctl = vboxNetFltDarwinIffIoCtl;
1123 RegRec.iff_detached = vboxNetFltDarwinIffDetached;
1124 interface_filter_t pIfFilter = NULL;
1125 err = iflt_attach(pIfNet, &RegRec, &pIfFilter);
1126 Assert(err || pIfFilter);
1127
1128 RTSpinlockAcquire(pThis->hSpinlock);
1129 pIfNet = ASMAtomicUoReadPtrT(&pThis->u.s.pIfNet, ifnet_t);
1130 if (pIfNet && !err)
1131 {
1132 ASMAtomicUoWriteBool(&pThis->fDisconnectedFromHost, false);
1133 ASMAtomicUoWritePtr(&pThis->u.s.pIfFilter, pIfFilter);
1134 pIfNet = NULL; /* don't dereference it */
1135 }
1136 RTSpinlockRelease(pThis->hSpinlock);
1137
1138 /* Report capabilities. */
1139 if ( !pIfNet
1140 && vboxNetFltTryRetainBusyNotDisconnected(pThis))
1141 {
1142 Assert(pThis->pSwitchPort);
1143 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pThis->u.s.MacAddr);
1144#if 0
1145 /*
1146 * XXX: Don't tell SrvIntNetR0 if the interface is
1147 * promiscuous, because there's no code yet to update that
1148 * information and we don't want it stuck, spamming all
1149 * traffic to the host.
1150 */
1151 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort, vboxNetFltDarwinIsPromiscuous(pThis));
1152#endif
1153 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
1154 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
1155 vboxNetFltRelease(pThis, true /*fBusy*/);
1156 }
1157 }
1158
1159 /* Release the interface on failure. */
1160 if (pIfNet)
1161 ifnet_release(pIfNet);
1162
1163 int rc = RTErrConvertFromErrno(err);
1164 if (RT_SUCCESS(rc))
1165 LogRel(("VBoxFltDrv: attached to '%s' / %RTmac\n", pThis->szName, &pThis->u.s.MacAddr));
1166 else
1167 LogRel(("VBoxFltDrv: failed to attach to ifnet '%s' (err=%d)\n", pThis->szName, err));
1168 IPRT_DARWIN_RESTORE_EFL_AC();
1169 return rc;
1170}
1171
1172
1173bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
1174{
1175 vboxNetFltDarwinAttachToInterface(pThis, true /* fRediscovery */);
1176 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
1177}
1178
1179
1180int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
1181{
1182 IPRT_DARWIN_SAVE_EFL_AC();
1183 NOREF(pvIfData);
1184
1185 int rc = VINF_SUCCESS;
1186 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1187 if (pIfNet)
1188 {
1189 /*
1190 * Create a mbuf for the gather list and push it onto the wire.
1191 * BPF tap and stats will be taken care of by the driver.
1192 */
1193 if (fDst & INTNETTRUNKDIR_WIRE)
1194 {
1195 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1196 if (pMBuf)
1197 {
1198 errno_t err = ifnet_output_raw(pIfNet, PF_LINK, pMBuf);
1199 if (err)
1200 rc = RTErrConvertFromErrno(err);
1201 }
1202 else
1203 rc = VERR_NO_MEMORY;
1204 }
1205
1206 /*
1207 * Create a mbuf for the gather list and push it onto the host stack.
1208 * BPF tap and stats are on us.
1209 */
1210 if (fDst & INTNETTRUNKDIR_HOST)
1211 {
1212 mbuf_t pMBuf = vboxNetFltDarwinMBufFromSG(pThis, pSG);
1213 if (pMBuf)
1214 {
1215 void *pvEthHdr = mbuf_data(pMBuf);
1216 unsigned const cbEthHdr = 14;
1217 struct ifnet_stat_increment_param stats;
1218
1219 RT_ZERO(stats);
1220 stats.packets_in = 1;
1221 stats.bytes_in = mbuf_len(pMBuf); /* full ethernet frame */
1222
1223 mbuf_pkthdr_setrcvif(pMBuf, pIfNet);
1224 mbuf_pkthdr_setheader(pMBuf, pvEthHdr); /* link-layer header */
1225 mbuf_adj(pMBuf, cbEthHdr); /* move to payload */
1226
1227#if 0 /* XXX: disabled since we don't request promiscuous from intnet */
1228 /*
1229 * TODO: Since intnet knows whether it forwarded us
1230 * this packet because it's for us or because we are
1231 * promiscuous, it can perhaps set a flag for us in
1232 * INTNETSG::fFlags so that we don't have to re-check
1233 * it here.
1234 */
1235 PCRTNETETHERHDR pcEthHdr = (PCRTNETETHERHDR)pvEthHdr;
1236 if ( (pcEthHdr->DstMac.au8[0] & 1) == 0 /* unicast? */
1237 && memcmp(&pcEthHdr->DstMac, &pThis->u.s.MacAddr, sizeof(RTMAC)) != 0)
1238 {
1239 mbuf_setflags_mask(pMBuf, MBUF_PROMISC, MBUF_PROMISC);
1240 }
1241#endif
1242
1243 bpf_tap_in(pIfNet, DLT_EN10MB, pMBuf, pvEthHdr, cbEthHdr);
1244 errno_t err = ifnet_input(pIfNet, pMBuf, &stats);
1245 if (err)
1246 rc = RTErrConvertFromErrno(err);
1247 }
1248 else
1249 rc = VERR_NO_MEMORY;
1250 }
1251
1252 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1253 }
1254
1255 IPRT_DARWIN_RESTORE_EFL_AC();
1256 return rc;
1257}
1258
1259
1260void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
1261{
1262 IPRT_DARWIN_SAVE_EFL_AC();
1263 ifnet_t pIfNet = vboxNetFltDarwinRetainIfNet(pThis);
1264 if (pIfNet)
1265 {
1266 if (pThis->fDisablePromiscuous)
1267 {
1268 /*
1269 * Promiscuous mode should not be used (wireless), we just need to
1270 * make sure the interface is up.
1271 */
1272 if (fActive)
1273 {
1274 u_int16_t fIf = ifnet_flags(pIfNet);
1275 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1276 {
1277 ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1278 ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1279 }
1280 }
1281 }
1282 else
1283 {
1284 /*
1285 * This api is a bit weird, the best reference is the code.
1286 *
1287 * Also, we have a bit or race conditions wrt the maintenance of
1288 * host the interface promiscuity for vboxNetFltPortOsIsPromiscuous.
1289 */
1290 unsigned const cPromiscBefore = VBOX_GET_PCOUNT(pIfNet);
1291 u_int16_t fIf;
1292 if (fActive)
1293 {
1294 Assert(!pThis->u.s.fSetPromiscuous);
1295 errno_t err = ENETDOWN;
1296 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, true);
1297
1298 /*
1299 * Try bring the interface up and running if it's down.
1300 */
1301 fIf = ifnet_flags(pIfNet);
1302 if ((fIf & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1303 {
1304 err = ifnet_set_flags(pIfNet, IFF_UP, IFF_UP);
1305 errno_t err2 = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1306 if (!err)
1307 err = err2;
1308 fIf = ifnet_flags(pIfNet);
1309 }
1310
1311 /*
1312 * Is it already up? If it isn't, leave it to the link event or
1313 * we'll upset if_pcount (as stated above, ifnet_set_promiscuous is weird).
1314 */
1315 if ((fIf & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1316 {
1317 err = ifnet_set_promiscuous(pIfNet, 1);
1318 pThis->u.s.fSetPromiscuous = err == 0;
1319 if (!err)
1320 {
1321 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1322
1323 /* check if it actually worked, this stuff is not always behaving well. */
1324 if (!(ifnet_flags(pIfNet) & IFF_PROMISC))
1325 {
1326 err = ifnet_set_flags(pIfNet, IFF_PROMISC, IFF_PROMISC);
1327 if (!err)
1328 err = ifnet_ioctl(pIfNet, 0, SIOCSIFFLAGS, NULL);
1329 if (!err)
1330 Log(("vboxNetFlt: fixed IFF_PROMISC on %s (%d->%d)\n", pThis->szName, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1331 else
1332 Log(("VBoxNetFlt: failed to fix IFF_PROMISC on %s, err=%d (%d->%d)\n",
1333 pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1334 }
1335 }
1336 else
1337 Log(("VBoxNetFlt: ifnet_set_promiscuous -> err=%d grr! (%d->%d)\n", err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1338 }
1339 else if (!err)
1340 Log(("VBoxNetFlt: Waiting for the link to come up... (%d->%d)\n", cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1341 if (err)
1342 LogRel(("VBoxNetFlt: Failed to put '%s' into promiscuous mode, err=%d (%d->%d)\n", pThis->szName, err, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1343 }
1344 else
1345 {
1346 ASMAtomicWriteBool(&pThis->u.s.fNeedSetPromiscuous, false);
1347 if (pThis->u.s.fSetPromiscuous)
1348 {
1349 errno_t err = ifnet_set_promiscuous(pIfNet, 0);
1350 AssertMsg(!err, ("%d\n", err)); NOREF(err);
1351 }
1352 pThis->u.s.fSetPromiscuous = false;
1353
1354 fIf = ifnet_flags(pIfNet);
1355 Log(("VBoxNetFlt: fIf=%#x; %d->%d\n", fIf, cPromiscBefore, VBOX_GET_PCOUNT(pIfNet)));
1356 }
1357 }
1358
1359 vboxNetFltDarwinReleaseIfNet(pThis, pIfNet);
1360 }
1361 IPRT_DARWIN_RESTORE_EFL_AC();
1362}
1363
1364
1365int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
1366{
1367 /* Nothing to do here. */
1368 RT_NOREF(pThis);
1369 return VINF_SUCCESS;
1370}
1371
1372
1373int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
1374{
1375 /* Nothing to do here. */
1376 RT_NOREF(pThis);
1377 return VINF_SUCCESS;
1378}
1379
1380
1381void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
1382{
1383 IPRT_DARWIN_SAVE_EFL_AC();
1384
1385 /*
1386 * Carefully obtain the interface filter reference and detach it.
1387 */
1388 RTSpinlockAcquire(pThis->hSpinlock);
1389 interface_filter_t pIfFilter = ASMAtomicUoReadPtrT(&pThis->u.s.pIfFilter, interface_filter_t);
1390 if (pIfFilter)
1391 ASMAtomicUoWriteNullPtr(&pThis->u.s.pIfFilter);
1392 RTSpinlockRelease(pThis->hSpinlock);
1393
1394 if (pIfFilter)
1395 iflt_detach(pIfFilter);
1396
1397 if (pThis->u.s.pSysSock != NULL)
1398 {
1399 RT_GCC_NO_WARN_DEPRECATED_BEGIN
1400
1401 sock_close(pThis->u.s.pSysSock);
1402 pThis->u.s.pSysSock = NULL;
1403
1404 RT_GCC_NO_WARN_DEPRECATED_END
1405 }
1406
1407 IPRT_DARWIN_RESTORE_EFL_AC();
1408}
1409
1410
1411int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
1412{
1413 NOREF(pvContext);
1414
1415 int rc = vboxNetFltDarwinAttachToInterface(pThis, false /* fRediscovery */);
1416 if (RT_FAILURE(rc))
1417 return rc;
1418
1419 if (pThis->pSwitchPort->pfnNotifyHostAddress == NULL)
1420 return rc;
1421
1422 /*
1423 * XXX: uwe
1424 *
1425 * Learn host's IP addresses and set up notifications for changes.
1426 * To avoid racing, set up notifications first.
1427 *
1428 * XXX: This should probably be global, since the only thing
1429 * specific to ifnet here is its IPv6 link-local address.
1430 */
1431 IPRT_DARWIN_SAVE_EFL_AC();
1432 errno_t error;
1433
1434 /** @todo Figure out how to replace the socket stuff we use to detect
1435 * addresses here as 10.5 deprecates it. */
1436 RT_GCC_NO_WARN_DEPRECATED_BEGIN
1437
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 /** @todo reorg code to not have numerous returns with duplicate code... */
1447
1448 error = sock_socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT,
1449 vboxNetFltDarwinSysSockUpcall, pThis,
1450 &pThis->u.s.pSysSock);
1451 if (error != 0)
1452 {
1453 LogRel(("sock_socket(SYSPROTO_EVENT): error %d\n", error));
1454 IPRT_DARWIN_RESTORE_EFL_AC();
1455 return rc;
1456 }
1457
1458 int nbio = 1;
1459 error = sock_ioctl(pThis->u.s.pSysSock, FIONBIO, &nbio);
1460 if (error != 0)
1461 {
1462 LogRel(("FIONBIO: error %d\n", error));
1463 sock_close(pThis->u.s.pSysSock);
1464 IPRT_DARWIN_RESTORE_EFL_AC();
1465 return rc;
1466 }
1467
1468 if (!sock_isnonblocking(pThis->u.s.pSysSock))
1469 {
1470 LogRel(("FIONBIO ok, but socket is blocking?!\n"));
1471 sock_close(pThis->u.s.pSysSock);
1472 IPRT_DARWIN_RESTORE_EFL_AC();
1473 return rc;
1474 }
1475
1476 struct kev_request req;
1477 req.vendor_code = KEV_VENDOR_APPLE;
1478 req.kev_class = KEV_NETWORK_CLASS;
1479 req.kev_subclass = KEV_ANY_SUBCLASS; /* need both INET and INET6, so have to request all */
1480
1481 error = sock_ioctl(pThis->u.s.pSysSock, SIOCSKEVFILT, &req);
1482 if (error != 0)
1483 {
1484 LogRel(("SIOCSKEVFILT: error %d\n", error));
1485 sock_close(pThis->u.s.pSysSock);
1486 IPRT_DARWIN_RESTORE_EFL_AC();
1487 return rc;
1488 }
1489 RT_GCC_NO_WARN_DEPRECATED_END
1490
1491 ifnet_t pIfNet = pThis->u.s.pIfNet; /* already retained */
1492
1493 ifaddr_t *pIfAddrList;
1494 error = ifnet_get_address_list(/* all interfaces*/ NULL, &pIfAddrList);
1495 if (error != 0)
1496 {
1497 LogRel(("ifnet_get_address_list: error %d\n", error));
1498 IPRT_DARWIN_RESTORE_EFL_AC();
1499 return rc;
1500 }
1501
1502 for (ifaddr_t *pIfAddr = pIfAddrList; *pIfAddr != NULL; ++pIfAddr)
1503 {
1504 ifaddr_t ifa = *pIfAddr;
1505 sa_family_t family = ifaddr_address_family(ifa);
1506 struct sockaddr_storage ss;
1507
1508 error = ifaddr_address(ifa, (struct sockaddr *)&ss, sizeof(ss));
1509 if (error != 0)
1510 {
1511 LogRel(("getting address family %d: error %d\n", family, error));
1512 continue;
1513 }
1514
1515 if (family == AF_INET)
1516 {
1517 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
1518 u_int32_t u32Addr = ntohl(sin->sin_addr.s_addr);
1519
1520 if (VBOX_IN_LOOPBACK(u32Addr))
1521 continue;
1522
1523 if (ifaddr_ifnet(ifa) != pIfNet && VBOX_IN_LINKLOCAL(u32Addr))
1524 continue;
1525
1526 Log(("> inet %RTnaipv4\n", sin->sin_addr.s_addr));
1527 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1528 /* :fAdded */ true, kIntNetAddrType_IPv4, &sin->sin_addr);
1529 }
1530 else if (family == AF_INET6)
1531 {
1532 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
1533
1534 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
1535 continue;
1536
1537 /* link-local from other interfaces are out of scope */
1538 if (ifaddr_ifnet(ifa) != pIfNet && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1539 continue;
1540
1541 Log(("> inet6 %RTnaipv6\n", &sin6->sin6_addr));
1542 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort,
1543 /* :fAdded */ true, kIntNetAddrType_IPv6, &sin6->sin6_addr);
1544 }
1545 }
1546
1547 ifnet_free_address_list(pIfAddrList);
1548
1549 /*
1550 * Now that we've got current addresses, check for events that
1551 * might have happened while we were working.
1552 */
1553 vboxNetFltDarwinSysSockUpcall(pThis->u.s.pSysSock, pThis, MBUF_DONTWAIT);
1554
1555 IPRT_DARWIN_RESTORE_EFL_AC();
1556 return rc;
1557}
1558
1559
1560static void vboxNetFltDarwinSysSockUpcall(socket_t pSysSock, void *pvData, int fWait)
1561{
1562 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvData;
1563 errno_t error;
1564
1565 NOREF(fWait);
1566
1567 if (RT_UNLIKELY(pSysSock != pThis->u.s.pSysSock))
1568 {
1569 Log(("vboxNetFltDarwinSysSockUpcall: %p != %p?\n", pSysSock, pThis->u.s.pSysSock));
1570 return;
1571 }
1572
1573 ifnet_t pIfNet = pThis->u.s.pIfNet; /* XXX: retain? */
1574 ifnet_family_t if_family = ifnet_family(pIfNet);
1575 u_int32_t if_unit = ifnet_unit(pIfNet);
1576
1577 for (;;)
1578 {
1579 mbuf_t m;
1580 size_t len = sizeof(struct kern_event_msg) - sizeof(u_int32_t) + sizeof(struct kev_in6_data);
1581
1582 RT_GCC_NO_WARN_DEPRECATED_BEGIN
1583 error = sock_receivembuf(pSysSock, NULL, &m, 0, &len);
1584 RT_GCC_NO_WARN_DEPRECATED_END
1585 if (error != 0)
1586 {
1587 if (error == EWOULDBLOCK)
1588 {
1589 Log(("vboxNetFltDarwinSysSockUpcall: EWOULDBLOCK - we are done\n"));
1590 error = 0;
1591 }
1592 else
1593 Log(("sock_receivembuf: error %d\n", error));
1594 break;
1595 }
1596
1597 if (len < sizeof(struct kern_event_msg) - sizeof(u_int32_t))
1598 {
1599 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short\n", (unsigned int)len));
1600 mbuf_freem(m);
1601 return;
1602 }
1603
1604 struct kern_event_msg *msg = (struct kern_event_msg *)mbuf_data(m);
1605 if (msg->kev_subclass == KEV_INET_SUBCLASS)
1606 {
1607 if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in_data))
1608 {
1609 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET_SUBCLASS\n", (unsigned int)len));
1610 mbuf_freem(m);
1611 return;
1612 }
1613
1614 struct kev_in_data *iev = (struct kev_in_data *)msg->event_data;
1615 struct net_event_data *link = &iev->link_data;
1616 PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev->ia_addr;
1617 u_int32_t u32Addr = ntohl(pAddr->IPv4.u);
1618
1619 if (VBOX_IN_LOOPBACK(u32Addr))
1620 {
1621 mbuf_freem(m);
1622 continue;
1623 }
1624
1625 if ( (link->if_family != if_family || link->if_unit != if_unit)
1626 && VBOX_IN_LINKLOCAL(u32Addr))
1627 {
1628 mbuf_freem(m);
1629 continue;
1630 }
1631
1632 switch (msg->event_code)
1633 {
1634 case KEV_INET_NEW_ADDR:
1635 Log(("KEV_INET_NEW_ADDR %.*s%d: %RTnaipv4\n", IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1636 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, true /*fAdded*/, kIntNetAddrType_IPv4, pAddr);
1637 break;
1638
1639 case KEV_INET_ADDR_DELETED:
1640 Log(("KEV_INET_ADDR_DELETED %.*s%d: %RTnaipv4\n", IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1641 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, false /*fAdded*/, kIntNetAddrType_IPv4, pAddr);
1642 break;
1643
1644 default:
1645 Log(("KEV INET event %u %.*s%d: addr %RTnaipv4\n",
1646 msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr->IPv4.u));
1647 break;
1648 }
1649 }
1650 else if (msg->kev_subclass == KEV_INET6_SUBCLASS)
1651 {
1652 if (len - (sizeof(struct kern_event_msg) - sizeof(u_int32_t)) < sizeof(struct kev_in6_data))
1653 {
1654 Log(("vboxNetFltDarwinSysSockUpcall: %u bytes is too short for KEV_INET6_SUBCLASS\n",
1655 (unsigned int)len));
1656 mbuf_freem(m);
1657 return;
1658 }
1659
1660 struct kev_in6_data *iev6 = (struct kev_in6_data *)msg->event_data;
1661 struct net_event_data *link = &iev6->link_data;
1662 PCRTNETADDRU pAddr = (PCRTNETADDRU)&iev6->ia_addr.sin6_addr;
1663
1664 if (IN6_IS_ADDR_LOOPBACK(&iev6->ia_addr.sin6_addr))
1665 {
1666 mbuf_freem(m);
1667 continue;
1668 }
1669
1670 if ( (link->if_family != if_family || link->if_unit != if_unit)
1671 && IN6_IS_ADDR_LINKLOCAL(&iev6->ia_addr.sin6_addr))
1672 {
1673 mbuf_freem(m);
1674 continue;
1675 }
1676
1677 switch (msg->event_code)
1678 {
1679 case KEV_INET6_NEW_USER_ADDR:
1680 Log(("KEV_INET6_NEW_USER_ADDR %.*s%d: %RTnaipv6\n",
1681 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1682 goto kev_inet6_new;
1683
1684 case KEV_INET6_NEW_LL_ADDR:
1685 Log(("KEV_INET6_NEW_LL_ADDR %.*s%d: %RTnaipv6\n",
1686 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1687 goto kev_inet6_new;
1688
1689 case KEV_INET6_NEW_RTADV_ADDR:
1690 Log(("KEV_INET6_NEW_RTADV_ADDR %.*s%d: %RTnaipv6\n",
1691 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1692 goto kev_inet6_new;
1693
1694 kev_inet6_new:
1695 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, true /*fAdded*/, kIntNetAddrType_IPv6, pAddr);
1696 break;
1697
1698 case KEV_INET6_ADDR_DELETED:
1699 Log(("KEV_INET6_ADDR_DELETED %.*s%d: %RTnaipv6\n",
1700 IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1701
1702 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, false /*fAdded*/, kIntNetAddrType_IPv6, pAddr);
1703 break;
1704
1705 default:
1706 Log(("KEV INET6 event %u %.*s%d: addr %RTnaipv6\n",
1707 msg->event_code, IFNAMSIZ, link->if_name, link->if_unit, pAddr));
1708 break;
1709 }
1710 }
1711 else
1712 Log(("vboxNetFltDarwinSysSockUpcall: subclass %u ignored\n", (unsigned)msg->kev_subclass));
1713
1714 mbuf_freem(m);
1715 }
1716}
1717
1718
1719int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
1720{
1721 /*
1722 * Init the darwin specific members.
1723 */
1724 pThis->u.s.pIfNet = NULL;
1725 pThis->u.s.pIfFilter = NULL;
1726 pThis->u.s.fSetPromiscuous = false;
1727 pThis->u.s.fNeedSetPromiscuous = false;
1728 //pThis->u.s.MacAddr = {0};
1729 pThis->u.s.pSysSock = NULL;
1730
1731 return VINF_SUCCESS;
1732}
1733
1734
1735void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
1736{
1737 NOREF(pThis); NOREF(pvIfData); NOREF(pMac);
1738}
1739
1740
1741int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
1742{
1743 /* Nothing to do */
1744 NOREF(pThis); NOREF(pvIf); NOREF(ppvIfData);
1745 return VINF_SUCCESS;
1746}
1747
1748
1749int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
1750{
1751 /* Nothing to do */
1752 NOREF(pThis); NOREF(pvIfData);
1753 return VINF_SUCCESS;
1754}
1755
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use