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