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