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