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