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