1 | /* $Id: VBoxNetFlt.c 17214 2009-02-27 16:32:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetFlt - Network Filter Driver (Host), Common Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 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 | /** @page pg_netflt VBoxNetFlt - Network Interface Filter
|
---|
23 | *
|
---|
24 | * This is a kernel module that attaches to a real interface on the host
|
---|
25 | * and filters and injects packets.
|
---|
26 | *
|
---|
27 | * In the big picture we're one of the three trunk interface on the internal
|
---|
28 | * network, the one named "NIC Filter Driver": @image html Networking_Overview.gif
|
---|
29 | *
|
---|
30 | *
|
---|
31 | * @section sec_netflt_msc Locking / Sequence Diagrams
|
---|
32 | *
|
---|
33 | * This secion contains a few sequence diagrams describing the problematic
|
---|
34 | * transitions of a host interface filter instance.
|
---|
35 | *
|
---|
36 | * The thing that makes it all a bit problematic is that multiple events may
|
---|
37 | * happen at the same time, and that we have to be very careful to avoid
|
---|
38 | * deadlocks caused by mixing our locks with the ones in the host kernel.
|
---|
39 | * The main events are receive, send, async send completion, disappearance of
|
---|
40 | * the host networking interface and it's reappearance. The latter two events
|
---|
41 | * are can be caused by driver unloading/loading or the device being physical
|
---|
42 | * unplugged (e.g. a USB network device).
|
---|
43 | *
|
---|
44 | * The strategy for dealing with these issues are:
|
---|
45 | * - Use a simple state machine.
|
---|
46 | * - Require the user (IntNet) to serialize all its calls to us,
|
---|
47 | * while at the same time not owning any lock used by any of the
|
---|
48 | * the callbacks we might call on receive and async send completion.
|
---|
49 | * - Make sure we're 100% idle before disconnecting, and have a
|
---|
50 | * disconnected status on both sides to fend off async calls.
|
---|
51 | * - Protect the host specific interface handle and the state variables
|
---|
52 | * using a spinlock.
|
---|
53 | *
|
---|
54 | *
|
---|
55 | * @subsection subsec_netflt_msc_dis_rel Disconnect from the network and release
|
---|
56 | *
|
---|
57 | * @msc
|
---|
58 | * VM, IntNet, NetFlt, Kernel, Wire;
|
---|
59 | *
|
---|
60 | * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
|
---|
61 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
62 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
|
---|
63 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
64 | * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
|
---|
65 | * NetFlt=>Kernel [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
66 | * Kernel->Wire [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
67 | *
|
---|
68 | * --- [label="Suspending the trunk interface"];
|
---|
69 | * IntNet=>IntNet [label="Lock Network"];
|
---|
70 | *
|
---|
71 | * Wire->Kernel [label="pkt1 - racing us", linecolor="red", textcolor="red"];
|
---|
72 | * Kernel=>>NetFlt [label="pkt1 - racing us", linecolor="red", textcolor="red"];
|
---|
73 | * NetFlt=>>IntNet [label="pkt1 recv - blocks", linecolor="red", textcolor="red"];
|
---|
74 | *
|
---|
75 | * IntNet=>IntNet [label="Mark Trunk Suspended"];
|
---|
76 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
77 | *
|
---|
78 | * IntNet=>NetFlt [label="pfnSetActive(false)"];
|
---|
79 | * NetFlt=>NetFlt [label="Mark inactive (atomic)"];
|
---|
80 | * IntNet<<NetFlt;
|
---|
81 | * IntNet=>NetFlt [label="pfnWaitForIdle(forever)"];
|
---|
82 | *
|
---|
83 | * IntNet=>>NetFlt [label="pkt1 to host", linecolor="red", textcolor="red"];
|
---|
84 | * NetFlt=>>Kernel [label="pkt1 to host", linecolor="red", textcolor="red"];
|
---|
85 | *
|
---|
86 | * Kernel<-Wire [label="pkt0 on wire", linecolor="green", textcolor="green"];
|
---|
87 | * NetFlt<<Kernel [label="pkt0 on wire", linecolor="green", textcolor="green"];
|
---|
88 | * IntNet<<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
89 | * IntNet<<=IntNet [label="Lock Net, free SG, Unlock Net", linecolor="green", textcolor="green"];
|
---|
90 | * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
91 | * NetFlt<-NetFlt [label="idle", linecolor="green", textcolor="green"];
|
---|
92 | *
|
---|
93 | * IntNet<<NetFlt [label="idle (pfnWaitForIdle)"];
|
---|
94 | *
|
---|
95 | * Wire->Kernel [label="pkt2", linecolor="red", textcolor="red"];
|
---|
96 | * Kernel=>>NetFlt [label="pkt2", linecolor="red", textcolor="red"];
|
---|
97 | * NetFlt=>>Kernel [label="pkt2 to host", linecolor="red", textcolor="red"];
|
---|
98 | *
|
---|
99 | * VM->IntNet [label="pkt3", linecolor="green", textcolor="green"];
|
---|
100 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
101 | * IntNet=>IntNet [label="Route packet -> drop", linecolor="green", textcolor="green" ];
|
---|
102 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
103 | *
|
---|
104 | * --- [label="The trunk interface is idle now, disconnect it"];
|
---|
105 | * IntNet=>IntNet [label="Lock Network"];
|
---|
106 | * IntNet=>IntNet [label="Unlink Trunk"];
|
---|
107 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
108 | * IntNet=>NetFlt [label="pfnDisconnectAndRelease"];
|
---|
109 | * NetFlt=>Kernel [label="iflt_detach"];
|
---|
110 | * NetFlt<<=Kernel [label="iff_detached"];
|
---|
111 | * NetFlt>>Kernel [label="iff_detached"];
|
---|
112 | * NetFlt<<Kernel [label="iflt_detach"];
|
---|
113 | * NetFlt=>NetFlt [label="Release"];
|
---|
114 | * IntNet<<NetFlt [label="pfnDisconnectAndRelease"];
|
---|
115 | *
|
---|
116 | * @endmsc
|
---|
117 | *
|
---|
118 | *
|
---|
119 | *
|
---|
120 | * @subsection subsec_netflt_msc_hif_rm Host Interface Removal
|
---|
121 | *
|
---|
122 | * The ifnet_t (pIf) is a tricky customer as any reference to it can potentially
|
---|
123 | * race the filter detaching. The simple way of solving it on Darwin is to guard
|
---|
124 | * all access to the pIf member with a spinlock. The other host systems will
|
---|
125 | * probably have similar race conditions, so the spinlock is a generic thing.
|
---|
126 | *
|
---|
127 | * @msc
|
---|
128 | * VM, IntNet, NetFlt, Kernel;
|
---|
129 | *
|
---|
130 | * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
|
---|
131 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
132 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
|
---|
133 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
134 | * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
|
---|
135 | * NetFlt=>Kernel [label="ifnet_reference w/ spinlock", linecolor="green", textcolor="green" ];
|
---|
136 | * NetFlt<<Kernel [label="ifnet_reference", linecolor="green", textcolor="green" ];
|
---|
137 | * NetFlt=>Kernel [label="pkt0 to wire (blocks)", linecolor="green", textcolor="green" ];
|
---|
138 | *
|
---|
139 | * --- [label="The host interface is being disconnected"];
|
---|
140 | * Kernel->NetFlt [label="iff_detached"];
|
---|
141 | * NetFlt=>Kernel [label="ifnet_release w/ spinlock"];
|
---|
142 | * NetFlt<<Kernel [label="ifnet_release"];
|
---|
143 | * NetFlt=>NetFlt [label="fDisconnectedFromHost=true"];
|
---|
144 | * NetFlt>>Kernel [label="iff_detached"];
|
---|
145 | *
|
---|
146 | * NetFlt<<Kernel [label="dropped", linecolor="green", textcolor="green"];
|
---|
147 | * NetFlt=>NetFlt [label="Acquire spinlock", linecolor="green", textcolor="green"];
|
---|
148 | * NetFlt=>Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
|
---|
149 | * NetFlt<<Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
|
---|
150 | * NetFlt=>NetFlt [label="pIf=NULL", linecolor="green", textcolor="green"];
|
---|
151 | * NetFlt=>NetFlt [label="Release spinlock", linecolor="green", textcolor="green"];
|
---|
152 | * IntNet<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
153 | * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
154 | * IntNet<<NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
155 | *
|
---|
156 | * @endmsc
|
---|
157 | *
|
---|
158 | *
|
---|
159 | *
|
---|
160 | * @subsection subsec_netflt_msc_hif_rm Host Interface Rediscovery
|
---|
161 | *
|
---|
162 | * The rediscovery is performed when we receive a send request and a certain
|
---|
163 | * period have elapsed since the last attempt, i.e. we're polling it. We
|
---|
164 | * synchronize the rediscovery with disconnection from the internal network
|
---|
165 | * by means of the pfnWaitForIdle call, so no special handling is required.
|
---|
166 | *
|
---|
167 | * @msc
|
---|
168 | * VM2, VM1, IntNet, NetFlt, Kernel, Wire;
|
---|
169 | *
|
---|
170 | * --- [label="Rediscovery conditions are not met"];
|
---|
171 | * VM1->IntNet [label="pkt0"];
|
---|
172 | * IntNet=>IntNet [label="Lock Network"];
|
---|
173 | * IntNet=>IntNet [label="Route packet -> wire"];
|
---|
174 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
175 | * IntNet=>NetFlt [label="pkt0 to wire"];
|
---|
176 | * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
|
---|
177 | * IntNet<<NetFlt [label="pkt0 to wire (dropped)"];
|
---|
178 | *
|
---|
179 | * --- [label="Rediscovery conditions"];
|
---|
180 | * VM1->IntNet [label="pkt1"];
|
---|
181 | * IntNet=>IntNet [label="Lock Network"];
|
---|
182 | * IntNet=>IntNet [label="Route packet -> wire"];
|
---|
183 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
184 | * IntNet=>NetFlt [label="pkt1 to wire"];
|
---|
185 | * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
|
---|
186 | * NetFlt=>NetFlt [label="fRediscoveryPending=true w/ spinlock"];
|
---|
187 | * NetFlt=>Kernel [label="ifnet_find_by_name"];
|
---|
188 | * NetFlt<<Kernel [label="ifnet_find_by_name (success)"];
|
---|
189 | *
|
---|
190 | * VM2->IntNet [label="pkt2", linecolor="red", textcolor="red"];
|
---|
191 | * IntNet=>IntNet [label="Lock Network", linecolor="red", textcolor="red"];
|
---|
192 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="red", textcolor="red"];
|
---|
193 | * IntNet=>IntNet [label="Unlock Network", linecolor="red", textcolor="red"];
|
---|
194 | * IntNet=>NetFlt [label="pkt2 to wire", linecolor="red", textcolor="red"];
|
---|
195 | * NetFlt=>NetFlt [label="!pIf || fRediscoveryPending (w/ spinlock)", linecolor="red", textcolor="red"];
|
---|
196 | * IntNet<<NetFlt [label="pkt2 to wire (dropped)", linecolor="red", textcolor="red"];
|
---|
197 |
|
---|
198 | * NetFlt=>Kernel [label="iflt_attach"];
|
---|
199 | * NetFlt<<Kernel [label="iflt_attach (success)"];
|
---|
200 | * NetFlt=>NetFlt [label="Acquire spinlock"];
|
---|
201 | * NetFlt=>NetFlt [label="Set pIf and update flags"];
|
---|
202 | * NetFlt=>NetFlt [label="Release spinlock"];
|
---|
203 | *
|
---|
204 | * NetFlt=>Kernel [label="pkt1 to wire"];
|
---|
205 | * Kernel->Wire [label="pkt1 to wire"];
|
---|
206 | * NetFlt<<Kernel [label="pkt1 to wire"];
|
---|
207 | * IntNet<<NetFlt [label="pkt1 to wire"];
|
---|
208 | *
|
---|
209 | *
|
---|
210 | * @endmsc
|
---|
211 | *
|
---|
212 | */
|
---|
213 |
|
---|
214 | /*******************************************************************************
|
---|
215 | * Header Files *
|
---|
216 | *******************************************************************************/
|
---|
217 | #define LOG_GROUP LOG_GROUP_NET_FLT_DRV
|
---|
218 | #include "VBoxNetFltInternal.h"
|
---|
219 |
|
---|
220 | #include <VBox/sup.h>
|
---|
221 | #include <VBox/log.h>
|
---|
222 | #include <VBox/err.h>
|
---|
223 | #include <iprt/assert.h>
|
---|
224 | #include <iprt/string.h>
|
---|
225 | #include <iprt/spinlock.h>
|
---|
226 | #include <iprt/uuid.h>
|
---|
227 | #include <iprt/mem.h>
|
---|
228 | #include <iprt/time.h>
|
---|
229 | #include <iprt/semaphore.h>
|
---|
230 |
|
---|
231 |
|
---|
232 | /*******************************************************************************
|
---|
233 | * Defined Constants And Macros *
|
---|
234 | *******************************************************************************/
|
---|
235 | #define IFPORT_2_VBOXNETFLTINS(pIfPort) \
|
---|
236 | ( (PVBOXNETFLTINS)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETFLTINS, MyPort)) )
|
---|
237 |
|
---|
238 |
|
---|
239 | AssertCompileMemberSize(VBOXNETFLTINS, enmState, sizeof(uint32_t));
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Sets the enmState member atomically.
|
---|
243 | *
|
---|
244 | * Used for all updates.
|
---|
245 | *
|
---|
246 | * @param pThis The instance.
|
---|
247 | * @param enmNewState The new value.
|
---|
248 | */
|
---|
249 | DECLINLINE(void) vboxNetFltSetState(PVBOXNETFLTINS pThis, VBOXNETFTLINSSTATE enmNewState)
|
---|
250 | {
|
---|
251 | ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Gets the enmState member atomically.
|
---|
257 | *
|
---|
258 | * Used for all reads.
|
---|
259 | *
|
---|
260 | * @returns The enmState value.
|
---|
261 | * @param pThis The instance.
|
---|
262 | */
|
---|
263 | DECLINLINE(VBOXNETFTLINSSTATE) vboxNetFltGetState(PVBOXNETFLTINS pThis)
|
---|
264 | {
|
---|
265 | return (VBOXNETFTLINSSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Finds a instance by its name, the caller does the locking.
|
---|
271 | *
|
---|
272 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
273 | * @param pGlobals The globals.
|
---|
274 | * @param pszName The name of the instance.
|
---|
275 | */
|
---|
276 | static PVBOXNETFLTINS vboxNetFltFindInstanceLocked(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
277 | {
|
---|
278 | PVBOXNETFLTINS pCur;
|
---|
279 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
280 | if (!strcmp(pszName, pCur->szName))
|
---|
281 | return pCur;
|
---|
282 | return NULL;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Finds a instance by its name, will request the mutex.
|
---|
288 | *
|
---|
289 | * No reference to the instance is retained, we're assuming the caller to
|
---|
290 | * already have one but just for some reason doesn't have the pointer to it.
|
---|
291 | *
|
---|
292 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
293 | * @param pGlobals The globals.
|
---|
294 | * @param pszName The name of the instance.
|
---|
295 | */
|
---|
296 | DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
297 | {
|
---|
298 | PVBOXNETFLTINS pRet;
|
---|
299 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
300 | AssertRCReturn(rc, NULL);
|
---|
301 |
|
---|
302 | pRet = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
303 |
|
---|
304 | rc = RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
305 | AssertRC(rc);
|
---|
306 | return pRet;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Unlinks an instance from the chain.
|
---|
312 | *
|
---|
313 | * @param pGlobals The globals.
|
---|
314 | * @param pToUnlink The instance to unlink.
|
---|
315 | */
|
---|
316 | static void vboxNetFltUnlinkLocked(PVBOXNETFLTGLOBALS pGlobals, PVBOXNETFLTINS pToUnlink)
|
---|
317 | {
|
---|
318 | if (pGlobals->pInstanceHead == pToUnlink)
|
---|
319 | pGlobals->pInstanceHead = pToUnlink->pNext;
|
---|
320 | else
|
---|
321 | {
|
---|
322 | PVBOXNETFLTINS pCur;
|
---|
323 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
324 | if (pCur->pNext == pToUnlink)
|
---|
325 | {
|
---|
326 | pCur->pNext = pToUnlink->pNext;
|
---|
327 | break;
|
---|
328 | }
|
---|
329 | Assert(pCur);
|
---|
330 | }
|
---|
331 | pToUnlink->pNext = NULL;
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Performs interface rediscovery if it was disconnected from the host.
|
---|
337 | *
|
---|
338 | * @returns true if successfully rediscovered and connected, false if not.
|
---|
339 | * @param pThis The instance.
|
---|
340 | */
|
---|
341 | static bool vboxNetFltMaybeRediscovered(PVBOXNETFLTINS pThis)
|
---|
342 | {
|
---|
343 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
344 | uint64_t Now = RTTimeNanoTS();
|
---|
345 | bool fRediscovered;
|
---|
346 | bool fDoIt;
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Rediscovered already? Time to try again?
|
---|
350 | */
|
---|
351 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
352 |
|
---|
353 | fRediscovered = !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
|
---|
354 | fDoIt = !fRediscovered
|
---|
355 | && !ASMAtomicUoReadBool(&pThis->fRediscoveryPending)
|
---|
356 | && Now - ASMAtomicUoReadU64(&pThis->NanoTSLastRediscovery) > UINT64_C(5000000000); /* 5 sec */
|
---|
357 | if (fDoIt)
|
---|
358 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, true);
|
---|
359 |
|
---|
360 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Call the OS specific code to do the job.
|
---|
364 | * Update the state when the call returns, that is everything except for
|
---|
365 | * the fDisconnectedFromHost flag which the OS specific code shall set.
|
---|
366 | */
|
---|
367 | if (fDoIt)
|
---|
368 | {
|
---|
369 | fRediscovered = vboxNetFltOsMaybeRediscovered(pThis);
|
---|
370 |
|
---|
371 | Assert(!fRediscovered || !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost));
|
---|
372 |
|
---|
373 | ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, RTTimeNanoTS());
|
---|
374 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, false);
|
---|
375 |
|
---|
376 | if (fRediscovered)
|
---|
377 | vboxNetFltPortOsSetActive(pThis, pThis->fActive);
|
---|
378 | }
|
---|
379 |
|
---|
380 | return fRediscovered;
|
---|
381 | }
|
---|
382 |
|
---|
383 | #ifdef RT_WITH_W64_UNWIND_HACK
|
---|
384 | # if defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64)
|
---|
385 | # define NETFLT_DECL_CALLBACK(type) DECLASM(DECLHIDDEN(type))
|
---|
386 | # define NETFLT_CALLBACK(_n) netfltNtWrap##_n
|
---|
387 |
|
---|
388 | NETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortXmit)(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst);
|
---|
389 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous)(PINTNETTRUNKIFPORT pIfPort);
|
---|
390 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortGetMacAddress)(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac);
|
---|
391 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsHostMac)(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac);
|
---|
392 | NETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortWaitForIdle)(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies);
|
---|
393 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortSetActive)(PINTNETTRUNKIFPORT pIfPort, bool fActive);
|
---|
394 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease)(PINTNETTRUNKIFPORT pIfPort);
|
---|
395 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRetain)(PINTNETTRUNKIFPORT pIfPort);
|
---|
396 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRelease)(PINTNETTRUNKIFPORT pIfPort);
|
---|
397 |
|
---|
398 | # else
|
---|
399 | # error "UNSUPPORTED (RT_WITH_W64_UNWIND_HACK)"
|
---|
400 | # endif
|
---|
401 | #else
|
---|
402 | # define NETFLT_DECL_CALLBACK(type) static DECLCALLBACK(type)
|
---|
403 | # define NETFLT_CALLBACK(_n) _n
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * @copydoc INTNETTRUNKIFPORT::pfnXmit
|
---|
408 | */
|
---|
409 | NETFLT_DECL_CALLBACK(int) vboxNetFltPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
|
---|
410 | {
|
---|
411 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
412 | int rc = VINF_SUCCESS;
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Input validation.
|
---|
416 | */
|
---|
417 | AssertPtr(pThis);
|
---|
418 | AssertPtr(pSG);
|
---|
419 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
420 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
421 | Assert(pThis->fActive);
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * Do a busy retain and then make sure we're connected to the interface
|
---|
425 | * before invoking the OS specific code.
|
---|
426 | */
|
---|
427 | vboxNetFltRetain(pThis, true /* fBusy */);
|
---|
428 | if ( !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)
|
---|
429 | || vboxNetFltMaybeRediscovered(pThis))
|
---|
430 | rc = vboxNetFltPortOsXmit(pThis, pSG, fDst);
|
---|
431 | vboxNetFltRelease(pThis, true /* fBusy */);
|
---|
432 |
|
---|
433 | return rc;
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
|
---|
439 | */
|
---|
440 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
|
---|
441 | {
|
---|
442 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * Input validation.
|
---|
446 | */
|
---|
447 | AssertPtr(pThis);
|
---|
448 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
449 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
450 | Assert(pThis->fActive);
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Ask the OS specific code.
|
---|
454 | */
|
---|
455 | return vboxNetFltPortOsIsPromiscuous(pThis);
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
|
---|
461 | */
|
---|
462 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
|
---|
463 | {
|
---|
464 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
465 |
|
---|
466 | /*
|
---|
467 | * Input validation.
|
---|
468 | */
|
---|
469 | AssertPtr(pThis);
|
---|
470 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
471 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
472 | Assert(pThis->fActive);
|
---|
473 |
|
---|
474 | /*
|
---|
475 | * Forward the question to the OS specific code.
|
---|
476 | */
|
---|
477 | vboxNetFltPortOsGetMacAddress(pThis, pMac);
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * @copydoc INTNETTRUNKIFPORT::pfnIsHostMac
|
---|
483 | */
|
---|
484 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsHostMac(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac)
|
---|
485 | {
|
---|
486 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * Input validation.
|
---|
490 | */
|
---|
491 | AssertPtr(pThis);
|
---|
492 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
493 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
494 | Assert(pThis->fActive);
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * Ask the OS specific code.
|
---|
498 | */
|
---|
499 | return vboxNetFltPortOsIsHostMac(pThis, pMac);
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
|
---|
505 | */
|
---|
506 | NETFLT_DECL_CALLBACK(int) vboxNetFltPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
|
---|
507 | {
|
---|
508 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
509 | int rc;
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * Input validation.
|
---|
513 | */
|
---|
514 | AssertPtr(pThis);
|
---|
515 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
516 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
517 | AssertReturn(!pThis->fActive, VERR_INVALID_STATE);
|
---|
518 |
|
---|
519 | /*
|
---|
520 | * Go to sleep on the semaphore after checking the busy count.
|
---|
521 | */
|
---|
522 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
523 |
|
---|
524 | rc = VINF_SUCCESS;
|
---|
525 | while (pThis->cBusy && RT_SUCCESS(rc))
|
---|
526 | rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
|
---|
527 |
|
---|
528 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
529 |
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * @copydoc INTNETTRUNKIFPORT::pfnSetActive
|
---|
536 | */
|
---|
537 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
|
---|
538 | {
|
---|
539 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
540 |
|
---|
541 | /*
|
---|
542 | * Input validation.
|
---|
543 | */
|
---|
544 | AssertPtr(pThis);
|
---|
545 | AssertPtr(pThis->pGlobals);
|
---|
546 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
547 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, false);
|
---|
548 |
|
---|
549 | /*
|
---|
550 | * We're assuming that the caller is serializing the calls, so we don't
|
---|
551 | * have to be extremely careful here. Just update first and then call
|
---|
552 | * the OS specific code, the update must be serialized for various reasons.
|
---|
553 | */
|
---|
554 | if (ASMAtomicReadBool(&pThis->fActive) != fActive)
|
---|
555 | {
|
---|
556 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
557 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
558 | ASMAtomicWriteBool(&pThis->fActive, fActive);
|
---|
559 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
560 |
|
---|
561 | vboxNetFltPortOsSetActive(pThis, fActive);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | fActive = !fActive;
|
---|
565 | return !fActive;
|
---|
566 | }
|
---|
567 |
|
---|
568 |
|
---|
569 | /**
|
---|
570 | * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
|
---|
571 | */
|
---|
572 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
573 | {
|
---|
574 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
575 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Serious paranoia.
|
---|
579 | */
|
---|
580 | AssertPtr(pThis);
|
---|
581 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
582 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
583 | AssertPtr(pThis->pGlobals);
|
---|
584 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
585 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
586 | Assert(pThis->szName[0]);
|
---|
587 |
|
---|
588 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
589 | Assert(!pThis->fActive);
|
---|
590 | Assert(!pThis->fRediscoveryPending);
|
---|
591 | Assert(!pThis->cBusy);
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Disconnect and release it.
|
---|
595 | */
|
---|
596 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
597 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
598 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
599 |
|
---|
600 | vboxNetFltOsDisconnectIt(pThis);
|
---|
601 | pThis->pSwitchPort = NULL;
|
---|
602 |
|
---|
603 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
604 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
605 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Unconnected);
|
---|
606 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
607 | #endif
|
---|
608 |
|
---|
609 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Destroy a device that has been disconnected from the switch.
|
---|
615 | *
|
---|
616 | * @returns true if the instance is destroyed, false otherwise.
|
---|
617 | * @param pThis The instance to be destroyed. This is
|
---|
618 | * no longer valid when this function returns.
|
---|
619 | */
|
---|
620 | static bool vboxNetFltDestroyInstance(PVBOXNETFLTINS pThis)
|
---|
621 | {
|
---|
622 | PVBOXNETFLTGLOBALS pGlobals = pThis->pGlobals;
|
---|
623 | uint32_t cRefs = ASMAtomicUoReadU32((uint32_t volatile *)&pThis->cRefs);
|
---|
624 | int rc;
|
---|
625 | LogFlow(("vboxNetFltDestroyInstance: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
626 |
|
---|
627 | /*
|
---|
628 | * Validate the state.
|
---|
629 | */
|
---|
630 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
631 | Assert( vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting
|
---|
632 | || vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
633 | #else
|
---|
634 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting);
|
---|
635 | #endif
|
---|
636 | Assert(!pThis->fActive);
|
---|
637 | Assert(!pThis->fRediscoveryPending);
|
---|
638 | Assert(!pThis->cRefs);
|
---|
639 | Assert(!pThis->cBusy);
|
---|
640 | Assert(!pThis->pSwitchPort);
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * Make sure the state is 'disconnecting' / 'destroying' and let the OS
|
---|
644 | * specific code do its part of the cleanup outside the mutex.
|
---|
645 | */
|
---|
646 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
647 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
648 | /** @todo r=bird: This looks kind of insane! I ASSUME this is specific to the
|
---|
649 | * static config and to devices in the Unconnected state only. This *looks* like
|
---|
650 | * a very unhealthy race between driver unloading and vboxNetFltFactoryCreateAndConnect.
|
---|
651 | * If I'm right, then vboxNetFltFactoryCreateAndConnect should be made to back down one
|
---|
652 | * way or the other, it should not the other way around. (see suggestion further down)
|
---|
653 | *
|
---|
654 | * If I'm wrong, then please explain in full.
|
---|
655 | */
|
---|
656 | if (cRefs != 0)
|
---|
657 | {
|
---|
658 | Assert(cRefs < UINT32_MAX / 2);
|
---|
659 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
660 | return false;
|
---|
661 | }
|
---|
662 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroying);
|
---|
663 | #else
|
---|
664 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
665 | #endif
|
---|
666 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
667 |
|
---|
668 | vboxNetFltOsDeleteInstance(pThis);
|
---|
669 |
|
---|
670 | /*
|
---|
671 | * Unlink the instance and free up its resources.
|
---|
672 | */
|
---|
673 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
674 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroyed);
|
---|
675 | vboxNetFltUnlinkLocked(pGlobals, pThis);
|
---|
676 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
677 |
|
---|
678 | RTSemEventDestroy(pThis->hEventIdle);
|
---|
679 | pThis->hEventIdle = NIL_RTSEMEVENT;
|
---|
680 | RTSpinlockDestroy(pThis->hSpinlock);
|
---|
681 | pThis->hSpinlock = NIL_RTSPINLOCK;
|
---|
682 | RTMemFree(pThis);
|
---|
683 | return true;
|
---|
684 | }
|
---|
685 |
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Releases a reference to the specified instance.
|
---|
689 | *
|
---|
690 | * This method will destroy the instance when the count reaches 0.
|
---|
691 | * It will also take care of decrementing the counter and idle wakeup.
|
---|
692 | *
|
---|
693 | * @param pThis The instance.
|
---|
694 | * @param fBusy Whether the busy counter should be decremented too.
|
---|
695 | */
|
---|
696 | DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
697 | {
|
---|
698 | uint32_t cRefs;
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Paranoid Android.
|
---|
702 | */
|
---|
703 | AssertPtr(pThis);
|
---|
704 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
705 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
706 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
707 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
708 | AssertPtr(pThis->pGlobals);
|
---|
709 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
710 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
711 | Assert(pThis->szName[0]);
|
---|
712 |
|
---|
713 | /*
|
---|
714 | * Work the busy counter.
|
---|
715 | */
|
---|
716 | if (fBusy)
|
---|
717 | {
|
---|
718 | cRefs = ASMAtomicDecU32(&pThis->cBusy);
|
---|
719 | if (!cRefs)
|
---|
720 | {
|
---|
721 | int rc = RTSemEventSignal(pThis->hEventIdle);
|
---|
722 | AssertRC(rc);
|
---|
723 | }
|
---|
724 | else
|
---|
725 | Assert(cRefs < UINT32_MAX / 2);
|
---|
726 | }
|
---|
727 |
|
---|
728 | /*
|
---|
729 | * The object reference counting.
|
---|
730 | */
|
---|
731 | cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
732 | if (!cRefs)
|
---|
733 | vboxNetFltDestroyInstance(pThis);
|
---|
734 | else
|
---|
735 | Assert(cRefs < UINT32_MAX / 2);
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
741 | */
|
---|
742 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
743 | {
|
---|
744 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
745 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Retains a reference to the specified instance and a busy reference too.
|
---|
751 | *
|
---|
752 | * @param pThis The instance.
|
---|
753 | * @param fBusy Whether the busy counter should be incremented as well.
|
---|
754 | */
|
---|
755 | DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
756 | {
|
---|
757 | uint32_t cRefs;
|
---|
758 |
|
---|
759 | /*
|
---|
760 | * Paranoid Android.
|
---|
761 | */
|
---|
762 | AssertPtr(pThis);
|
---|
763 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
764 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
765 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
766 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
767 | AssertPtr(pThis->pGlobals);
|
---|
768 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
769 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
770 | Assert(pThis->szName[0]);
|
---|
771 |
|
---|
772 | /*
|
---|
773 | * Retain the object.
|
---|
774 | */
|
---|
775 | cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
776 | Assert(cRefs > 1 && cRefs < UINT32_MAX / 2);
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Work the busy counter.
|
---|
780 | */
|
---|
781 | if (fBusy)
|
---|
782 | {
|
---|
783 | cRefs = ASMAtomicIncU32(&pThis->cBusy);
|
---|
784 | Assert(cRefs > 0 && cRefs < UINT32_MAX / 2);
|
---|
785 | }
|
---|
786 |
|
---|
787 | NOREF(cRefs);
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 | /**
|
---|
792 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
793 | */
|
---|
794 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortRetain(PINTNETTRUNKIFPORT pIfPort)
|
---|
795 | {
|
---|
796 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
797 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
798 | }
|
---|
799 |
|
---|
800 |
|
---|
801 | /**
|
---|
802 | * Connects the instance to the specified switch port.
|
---|
803 | *
|
---|
804 | * Called while owning the lock. We're ASSUMING that the internal
|
---|
805 | * networking code is already owning an recursive mutex, so, there
|
---|
806 | * will be no deadlocks when vboxNetFltOsConnectIt calls back into
|
---|
807 | * it for setting preferences.
|
---|
808 | *
|
---|
809 | * @returns VBox status code.
|
---|
810 | * @param pThis The instance.
|
---|
811 | * @param pSwitchPort The port on the internal network 'switch'.
|
---|
812 | * @param ppIfPort Where to return our port interface.
|
---|
813 | */
|
---|
814 | static int vboxNetFltConnectIt(PVBOXNETFLTINS pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
815 | {
|
---|
816 | int rc;
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * Validate state.
|
---|
820 | */
|
---|
821 | Assert(!pThis->fActive);
|
---|
822 | Assert(!pThis->fRediscoveryPending);
|
---|
823 | Assert(!pThis->cBusy);
|
---|
824 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
825 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
826 | #else
|
---|
827 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Initializing);
|
---|
828 | #endif
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Do the job.
|
---|
832 | * Note that we're calling the os stuff while owning the semaphore here.
|
---|
833 | */
|
---|
834 | pThis->pSwitchPort = pSwitchPort;
|
---|
835 | rc = vboxNetFltOsConnectIt(pThis);
|
---|
836 | if (RT_SUCCESS(rc))
|
---|
837 | {
|
---|
838 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Connected);
|
---|
839 | *ppIfPort = &pThis->MyPort;
|
---|
840 | }
|
---|
841 | else
|
---|
842 | pThis->pSwitchPort = NULL;
|
---|
843 |
|
---|
844 | Assert(!pThis->fActive);
|
---|
845 | return rc;
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Creates a new instance.
|
---|
851 | *
|
---|
852 | * The new instance will be in the suspended state in a dynamic config and in
|
---|
853 | * the inactive in a static one.
|
---|
854 | *
|
---|
855 | * Called without owning the lock, but will request is several times.
|
---|
856 | *
|
---|
857 | * @returns VBox status code.
|
---|
858 | * @param pGlobals The globals.
|
---|
859 | * @param pszName The instance name.
|
---|
860 | * @param pSwitchPort The port on the switch that we're connected with (dynamic only).
|
---|
861 | * @param fNoPromisc Do not attempt going into promiscuous mode.
|
---|
862 | * @param pvContext Context argument for vboxNetFltOsInitInstance.
|
---|
863 | * @param ppIfPort Where to store the pointer to our port interface (dynamic only).
|
---|
864 | */
|
---|
865 | static int vboxNetFltNewInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PINTNETTRUNKSWPORT pSwitchPort,
|
---|
866 | bool fNoPromisc, void *pvContext, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
867 | {
|
---|
868 | /*
|
---|
869 | * Allocate and initialize a new instance before requesting the mutex.
|
---|
870 | */
|
---|
871 | int rc;
|
---|
872 | size_t const cchName = strlen(pszName);
|
---|
873 | PVBOXNETFLTINS pNew = (PVBOXNETFLTINS)RTMemAllocZ(RT_OFFSETOF(VBOXNETFLTINS, szName[cchName + 1]));
|
---|
874 | if (!pNew)
|
---|
875 | return VERR_INTNET_FLT_IF_FAILED;
|
---|
876 | pNew->pNext = NULL;
|
---|
877 | pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
|
---|
878 | pNew->MyPort.pfnRetain = NETFLT_CALLBACK(vboxNetFltPortRetain);
|
---|
879 | pNew->MyPort.pfnRelease = NETFLT_CALLBACK(vboxNetFltPortRelease);
|
---|
880 | pNew->MyPort.pfnDisconnectAndRelease= NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease);
|
---|
881 | pNew->MyPort.pfnSetActive = NETFLT_CALLBACK(vboxNetFltPortSetActive);
|
---|
882 | pNew->MyPort.pfnWaitForIdle = NETFLT_CALLBACK(vboxNetFltPortWaitForIdle);
|
---|
883 | pNew->MyPort.pfnGetMacAddress = NETFLT_CALLBACK(vboxNetFltPortGetMacAddress);
|
---|
884 | pNew->MyPort.pfnIsHostMac = NETFLT_CALLBACK(vboxNetFltPortIsHostMac);
|
---|
885 | pNew->MyPort.pfnIsPromiscuous = NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous);
|
---|
886 | pNew->MyPort.pfnXmit = NETFLT_CALLBACK(vboxNetFltPortXmit);
|
---|
887 | pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
|
---|
888 | pNew->pSwitchPort = NULL;
|
---|
889 | pNew->pGlobals = pGlobals;
|
---|
890 | pNew->hSpinlock = NIL_RTSPINLOCK;
|
---|
891 | pNew->enmState = kVBoxNetFltInsState_Initializing;
|
---|
892 | pNew->fActive = false;
|
---|
893 | pNew->fDisconnectedFromHost = false;
|
---|
894 | pNew->fRediscoveryPending = false;
|
---|
895 | pNew->fDisablePromiscuous = fNoPromisc;
|
---|
896 | pNew->NanoTSLastRediscovery = INT64_MAX;
|
---|
897 | pNew->cRefs = 1;
|
---|
898 | pNew->cBusy = 0;
|
---|
899 | pNew->hEventIdle = NIL_RTSEMEVENT;
|
---|
900 | memcpy(pNew->szName, pszName, cchName + 1);
|
---|
901 |
|
---|
902 | rc = RTSpinlockCreate(&pNew->hSpinlock);
|
---|
903 | if (RT_SUCCESS(rc))
|
---|
904 | {
|
---|
905 | rc = RTSemEventCreate(&pNew->hEventIdle);
|
---|
906 | if (RT_SUCCESS(rc))
|
---|
907 | {
|
---|
908 | rc = vboxNetFltOsPreInitInstance(pNew);
|
---|
909 | if (RT_SUCCESS(rc))
|
---|
910 | {
|
---|
911 | /*
|
---|
912 | * Insert the instance into the chain, checking for
|
---|
913 | * duplicates first of course (race).
|
---|
914 | */
|
---|
915 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
916 | if (RT_SUCCESS(rc))
|
---|
917 | {
|
---|
918 | if (!vboxNetFltFindInstanceLocked(pGlobals, pszName))
|
---|
919 | {
|
---|
920 | pNew->pNext = pGlobals->pInstanceHead;
|
---|
921 | pGlobals->pInstanceHead = pNew;
|
---|
922 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * Call the OS specific initialization code.
|
---|
926 | */
|
---|
927 | rc = vboxNetFltOsInitInstance(pNew, pvContext);
|
---|
928 | RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
929 | if (RT_SUCCESS(rc))
|
---|
930 | {
|
---|
931 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
932 | /*
|
---|
933 | * Static instances are unconnected at birth.
|
---|
934 | */
|
---|
935 | Assert(!pSwitchPort);
|
---|
936 | pNew->enmState = kVBoxNetFltInsState_Unconnected;
|
---|
937 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
938 | *ppIfPort = &pNew->MyPort;
|
---|
939 | return rc;
|
---|
940 |
|
---|
941 | #else /* !VBOXNETFLT_STATIC_CONFIG */
|
---|
942 | /*
|
---|
943 | * Connect it as well, the OS specific bits has to be done outside
|
---|
944 | * the lock as they may call back to into intnet.
|
---|
945 | */
|
---|
946 | rc = vboxNetFltConnectIt(pNew, pSwitchPort, ppIfPort);
|
---|
947 | if (RT_SUCCESS(rc))
|
---|
948 | {
|
---|
949 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
950 | Assert(*ppIfPort == &pNew->MyPort);
|
---|
951 | return rc;
|
---|
952 | }
|
---|
953 |
|
---|
954 | /* Bail out (failed). */
|
---|
955 | vboxNetFltOsDeleteInstance(pNew);
|
---|
956 | #endif /* !VBOXNETFLT_STATIC_CONFIG */
|
---|
957 | }
|
---|
958 | vboxNetFltUnlinkLocked(pGlobals, pNew);
|
---|
959 | }
|
---|
960 | else
|
---|
961 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
962 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
963 | }
|
---|
964 | }
|
---|
965 | RTSemEventDestroy(pNew->hEventIdle);
|
---|
966 | }
|
---|
967 | RTSpinlockDestroy(pNew->hSpinlock);
|
---|
968 | }
|
---|
969 |
|
---|
970 | RTMemFree(pNew);
|
---|
971 | return rc;
|
---|
972 | }
|
---|
973 |
|
---|
974 |
|
---|
975 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
976 | /**
|
---|
977 | * Searches for the NetFlt instance by its name and creates the new one if not found.
|
---|
978 | *
|
---|
979 | * @returns VBox status code.
|
---|
980 | * @retval VINF_SUCCESS and *ppInstance if a new instance was created.
|
---|
981 | * @retval VINF_ALREADY_INITIALIZED and *ppInstance if an instance already exists.
|
---|
982 | *
|
---|
983 | * @param pGlobal Pointer to the globals.
|
---|
984 | * @param pszName The instance name.
|
---|
985 | * @param ppInstance Where to return the instance pointer on success.
|
---|
986 | * @param pvContext Context which needs to be passed along to vboxNetFltOsInitInstance.
|
---|
987 | */
|
---|
988 | DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void *pvContext)
|
---|
989 | {
|
---|
990 | PINTNETTRUNKIFPORT pIfPort;
|
---|
991 | PVBOXNETFLTINS pCur;
|
---|
992 | int rc;
|
---|
993 |
|
---|
994 | *ppInstance = NULL;
|
---|
995 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
996 | AssertRCReturn(rc, rc);
|
---|
997 |
|
---|
998 | /*
|
---|
999 | * Look for an existing instance in the list.
|
---|
1000 | *
|
---|
1001 | * There might be an existing one in the list if the driver was unbound
|
---|
1002 | * while it was connected to an internal network. We're running into
|
---|
1003 | * a destruction race that is a bit similar to the one in
|
---|
1004 | * vboxNetFltFactoryCreateAndConnect, only the roles are reversed
|
---|
1005 | * and we're not in a position to back down. Instead of backing down
|
---|
1006 | * we'll delay a bit giving the other thread time to complete the
|
---|
1007 | * destructor.
|
---|
1008 | */
|
---|
1009 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
1010 | while (pCur)
|
---|
1011 | {
|
---|
1012 | #if 0
|
---|
1013 | uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
|
---|
1014 | if (cRefs > 1)
|
---|
1015 | {
|
---|
1016 | VBOXNETFTLINSSTATE enmState = vboxNetFltGetState(pCur);
|
---|
1017 | switch (enmState)
|
---|
1018 | {
|
---|
1019 | case kVBoxNetFltInsState_Unconnected:
|
---|
1020 | case kVBoxNetFltInsState_Connected:
|
---|
1021 | case kVBoxNetFltInsState_Disconnecting:
|
---|
1022 | if (pCur->fDisconnectedFromHost)
|
---|
1023 | {
|
---|
1024 | /* Wait for it to exit the transitional disconnecting
|
---|
1025 | state. It might otherwise be running the risk of
|
---|
1026 | upsetting the OS specific code... */
|
---|
1027 | /** @todo This reconnect stuff should be serialized correctly for static
|
---|
1028 | * devices. Shouldn't it? In the dynamic case we're using the INTNET
|
---|
1029 | * outbound thrunk lock, but that doesn't quite cut it here, or does
|
---|
1030 | * it? We could either transition to initializing or make a callback
|
---|
1031 | * while owning the mutext here... */
|
---|
1032 | if (enmState == kVBoxNetFltInsState_Disconnecting)
|
---|
1033 | {
|
---|
1034 | do
|
---|
1035 | {
|
---|
1036 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1037 | RTThreadSleep(2); /* (2ms) */
|
---|
1038 | RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1039 | enmState = vboxNetFltGetState(pCur);
|
---|
1040 | }
|
---|
1041 | while (enmState == kVBoxNetFltInsState_Disconnecting);
|
---|
1042 | AssertMsg(enmState == kVBoxNetFltInsState_Unconnected, ("%d\n", enmState));
|
---|
1043 | Assert(pCur->fDisconnectedFromHost);
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1047 | *ppInstance = pCur;
|
---|
1048 | return VINF_ALREADY_INITIALIZED;
|
---|
1049 | }
|
---|
1050 | /* fall thru */
|
---|
1051 |
|
---|
1052 | default:
|
---|
1053 | {
|
---|
1054 | bool fDfH = pCur->fDisconnectedFromHost;
|
---|
1055 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1056 | vboxNetFltRelease(pCur, false /* fBusy */);
|
---|
1057 | LogRel(("VBoxNetFlt: Huh? An instance of '%s' already exists! [pCur=%p cRefs=%d fDfH=%RTbool enmState=%d]\n",
|
---|
1058 | pszName, pCur, cRefs - 1, fDfH, enmState));
|
---|
1059 | *ppInstance = NULL;
|
---|
1060 | return VERR_INTNET_FLT_IF_BUSY;
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | /* Zero references, it's being destroyed. Delay a bit so the destructor
|
---|
1066 | can finish its work and try again. (vboxNetFltNewInstance will fail
|
---|
1067 | with duplicate name if we don't.) */
|
---|
1068 | # ifdef RT_STRICT
|
---|
1069 | Assert(cRefs == 1);
|
---|
1070 | enmState = vboxNetFltGetState(pCur);
|
---|
1071 | AssertMsg( enmState == kVBoxNetFltInsState_Unconnected
|
---|
1072 | || enmState == kVBoxNetFltInsState_Disconnecting
|
---|
1073 | || enmState == kVBoxNetFltInsState_Destroyed, ("%d\n", enmState));
|
---|
1074 | # endif
|
---|
1075 | ASMAtomicDecU32(&pCur->cRefs);
|
---|
1076 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1077 | RTThreadSleep(2); /* (2ms) */
|
---|
1078 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1079 | AssertRCReturn(rc, rc);
|
---|
1080 |
|
---|
1081 | #else /* old code: */
|
---|
1082 | VBOXNETFTLINSSTATE enmState = vboxNetFltGetState(pCur);
|
---|
1083 | if ( enmState != kVBoxNetFltInsState_Destroying
|
---|
1084 | && enmState != kVBoxNetFltInsState_Destroyed)
|
---|
1085 | {
|
---|
1086 | /** @todo r=bird: who is making sure this is a genuine reconnection case? */
|
---|
1087 | vboxNetFltRetain(pCur, false /* fBusy */);
|
---|
1088 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1089 | *ppInstance = pCur;
|
---|
1090 | return VINF_ALREADY_INITIALIZED;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | /* wait 2 ms */ /** @todo r=bird: Doesn't RTThreadSleep() work here? If it's bust, I'd like to know it so we can fix it... */
|
---|
1094 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1095 | RTSemEventWait(pGlobals->hBlockEvent, 2);
|
---|
1096 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1097 | AssertRCReturn(rc, rc);
|
---|
1098 | #endif
|
---|
1099 |
|
---|
1100 | /* try again */
|
---|
1101 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Try create a new instance.
|
---|
1108 | * (fNoPromisc is overridden in the vboxNetFltFactoryCreateAndConnect path, so pass true here.)
|
---|
1109 | */
|
---|
1110 | rc = vboxNetFltNewInstance(pGlobals, pszName, NULL, true /* fNoPromisc */, pvContext, &pIfPort);
|
---|
1111 | if (RT_SUCCESS(rc))
|
---|
1112 | *ppInstance = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
1113 | else
|
---|
1114 | *ppInstance = NULL;
|
---|
1115 |
|
---|
1116 | return rc;
|
---|
1117 | }
|
---|
1118 | #endif /* VBOXNETFLT_STATIC_CONFIG */
|
---|
1119 |
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
|
---|
1123 | */
|
---|
1124 | static DECLCALLBACK(int) vboxNetFltFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
|
---|
1125 | PINTNETTRUNKSWPORT pSwitchPort, uint32_t fFlags,
|
---|
1126 | PINTNETTRUNKIFPORT *ppIfPort)
|
---|
1127 | {
|
---|
1128 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
1129 | PVBOXNETFLTINS pCur;
|
---|
1130 | int rc;
|
---|
1131 |
|
---|
1132 | LogFlow(("vboxNetFltFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
|
---|
1133 | Assert(pGlobals->cFactoryRefs > 0);
|
---|
1134 | AssertMsgReturn(!(fFlags & ~(INTNETTRUNKFACTORY_FLAG_NO_PROMISC)),
|
---|
1135 | ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
1136 |
|
---|
1137 | /*
|
---|
1138 | * Static: Find instance, check if busy, connect if not.
|
---|
1139 | * Dynamic: Check for duplicate / busy interface instance.
|
---|
1140 | */
|
---|
1141 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1142 | AssertRCReturn(rc, rc);
|
---|
1143 |
|
---|
1144 | #if defined(VBOX_TAPMINIPORT) && defined(RT_OS_WINDOWS)
|
---|
1145 | /* temporary hack to pick up the first adapter */
|
---|
1146 | pCur = pGlobals->pInstanceHead; /** @todo Don't for get to remove this temporary hack... :-) */
|
---|
1147 | #else
|
---|
1148 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
1149 | #endif
|
---|
1150 | if (pCur)
|
---|
1151 | {
|
---|
1152 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1153 | # if 0 /** @todo r=bird: We need to fix the race here. The race is against release+destructor, the
|
---|
1154 | * tell tale is a cRefs of and since cRefs is manipulated in an atomic fashion we can simply attempt
|
---|
1155 | * to grab a reference atomically, the worst thing that can happen is that we have to decrement it again..
|
---|
1156 | * Here is my suggestion: */
|
---|
1157 | /* Try grab a reference. If the count had already reached zero we're racing the
|
---|
1158 | destructor code and must back down. */
|
---|
1159 | uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
|
---|
1160 | if (cRefs > 1)
|
---|
1161 | {
|
---|
1162 | if (vboxNetFltGetState(pCur) == kVBoxNetFltInsState_Unconnected)
|
---|
1163 | {
|
---|
1164 | pCur->fDisablePromiscuous = !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC);
|
---|
1165 | rc = vboxNetFltConnectIt(pCur, pSwitchPort, ppIfPort);
|
---|
1166 | if (RT_SUCCESS(rc))
|
---|
1167 | pCur = NULL; /* Don't release it, reference given to the caller. */
|
---|
1168 | }
|
---|
1169 | else
|
---|
1170 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
1171 | }
|
---|
1172 | else
|
---|
1173 | {
|
---|
1174 | Assert(cRefs == 1);
|
---|
1175 | ASMAtomicDecU32(&pCur->cRefs);
|
---|
1176 | pCur = NULL; /* nothing to release */
|
---|
1177 | rc = VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1181 | if (pCur)
|
---|
1182 | vboxNetFltRelease(pCur, false /* fBusy */);
|
---|
1183 |
|
---|
1184 | # else
|
---|
1185 | if (vboxNetFltGetState(pCur) == kVBoxNetFltInsState_Unconnected)
|
---|
1186 | {
|
---|
1187 | vboxNetFltRetain(pCur, false /* fBusy */); /** @todo r=bird: Who releases this on failure? */
|
---|
1188 | pCur->fDisablePromiscuous = !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC);
|
---|
1189 | rc = vboxNetFltConnectIt(pCur, pSwitchPort, ppIfPort);
|
---|
1190 | }
|
---|
1191 | else
|
---|
1192 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
1193 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1194 | # endif
|
---|
1195 | #else
|
---|
1196 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
1197 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1198 | #endif
|
---|
1199 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
1200 | return rc;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1204 |
|
---|
1205 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1206 | rc = VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
1207 | #else
|
---|
1208 | /*
|
---|
1209 | * Dynamically create a new instance.
|
---|
1210 | */
|
---|
1211 | rc = vboxNetFltNewInstance(pGlobals,
|
---|
1212 | pszName,
|
---|
1213 | pSwitchPort,
|
---|
1214 | !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC),
|
---|
1215 | NULL,
|
---|
1216 | ppIfPort);
|
---|
1217 | #endif
|
---|
1218 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
1219 | return rc;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 |
|
---|
1223 | /**
|
---|
1224 | * @copydoc INTNETTRUNKFACTORY::pfnRelease
|
---|
1225 | */
|
---|
1226 | static DECLCALLBACK(void) vboxNetFltFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
|
---|
1227 | {
|
---|
1228 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
1229 |
|
---|
1230 | int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
|
---|
1231 | Assert(cRefs >= 0); NOREF(cRefs);
|
---|
1232 | LogFlow(("vboxNetFltFactoryRelease: cRefs=%d (new)\n", cRefs));
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 |
|
---|
1236 | /**
|
---|
1237 | * Implements the SUPDRV component factor interface query method.
|
---|
1238 | *
|
---|
1239 | * @returns Pointer to an interface. NULL if not supported.
|
---|
1240 | *
|
---|
1241 | * @param pSupDrvFactory Pointer to the componet factory registration structure.
|
---|
1242 | * @param pSession The session - unused.
|
---|
1243 | * @param pszInterfaceUuid The factory interface id.
|
---|
1244 | */
|
---|
1245 | static DECLCALLBACK(void *) vboxNetFltQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
|
---|
1246 | {
|
---|
1247 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, SupDrvFactory));
|
---|
1248 |
|
---|
1249 | /*
|
---|
1250 | * Convert the UUID strings and compare them.
|
---|
1251 | */
|
---|
1252 | RTUUID UuidReq;
|
---|
1253 | int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
|
---|
1254 | if (RT_SUCCESS(rc))
|
---|
1255 | {
|
---|
1256 | if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
|
---|
1257 | {
|
---|
1258 | ASMAtomicIncS32(&pGlobals->cFactoryRefs);
|
---|
1259 | return &pGlobals->TrunkFactory;
|
---|
1260 | }
|
---|
1261 | #ifdef LOG_ENABLED
|
---|
1262 | /* log legacy queries */
|
---|
1263 | /* else if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_V1_UUID_STR))
|
---|
1264 | Log(("VBoxNetFlt: V1 factory query\n"));
|
---|
1265 | */
|
---|
1266 | else
|
---|
1267 | Log(("VBoxNetFlt: unknown factory interface query (%s)\n", pszInterfaceUuid));
|
---|
1268 | #endif
|
---|
1269 | }
|
---|
1270 | else
|
---|
1271 | Log(("VBoxNetFlt: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
|
---|
1272 |
|
---|
1273 | return NULL;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | /**
|
---|
1278 | * Checks whether the VBoxNetFlt wossname can be unloaded.
|
---|
1279 | *
|
---|
1280 | * This will return false if someone is currently using the module.
|
---|
1281 | *
|
---|
1282 | * @returns true if it's relatively safe to unload it, otherwise false.
|
---|
1283 | * @param pGlobals Pointer to the globals.
|
---|
1284 | */
|
---|
1285 | DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1286 | {
|
---|
1287 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1288 | bool fRc = !pGlobals->pInstanceHead
|
---|
1289 | && pGlobals->cFactoryRefs <= 0;
|
---|
1290 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1291 | AssertRC(rc);
|
---|
1292 | return fRc;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 |
|
---|
1296 | /**
|
---|
1297 | * Try to close the IDC connection to SUPDRV if established.
|
---|
1298 | *
|
---|
1299 | * @returns VBox status code.
|
---|
1300 | * @retval VINF_SUCCESS on success.
|
---|
1301 | * @retval VERR_WRONG_ORDER if we're busy.
|
---|
1302 | *
|
---|
1303 | * @param pGlobals Pointer to the globals.
|
---|
1304 | *
|
---|
1305 | * @sa vboxNetFltTryDeleteIdcAndGlobals()
|
---|
1306 | */
|
---|
1307 | DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1308 | {
|
---|
1309 | int rc;
|
---|
1310 |
|
---|
1311 | Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
|
---|
1312 |
|
---|
1313 | /*
|
---|
1314 | * Check before trying to deregister the factory.
|
---|
1315 | */
|
---|
1316 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1317 | return VERR_WRONG_ORDER;
|
---|
1318 |
|
---|
1319 | if (!pGlobals->fIDCOpen)
|
---|
1320 | rc = VINF_SUCCESS;
|
---|
1321 | else
|
---|
1322 | {
|
---|
1323 | /*
|
---|
1324 | * Disconnect from SUPDRV and check that nobody raced us,
|
---|
1325 | * reconnect if that should happen.
|
---|
1326 | */
|
---|
1327 | rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1328 | AssertRC(rc);
|
---|
1329 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1330 | {
|
---|
1331 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1332 | AssertRC(rc);
|
---|
1333 | return VERR_WRONG_ORDER;
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1337 | pGlobals->fIDCOpen = false;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | return rc;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 |
|
---|
1344 | /**
|
---|
1345 | * Establishes the IDC connection to SUPDRV and registers our component factory.
|
---|
1346 | *
|
---|
1347 | * @returns VBox status code.
|
---|
1348 | * @param pGlobals Pointer to the globals.
|
---|
1349 | * @sa vboxNetFltInitGlobalsAndIdc().
|
---|
1350 | */
|
---|
1351 | DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1352 | {
|
---|
1353 | int rc;
|
---|
1354 | Assert(!pGlobals->fIDCOpen);
|
---|
1355 |
|
---|
1356 | /*
|
---|
1357 | * Establish a connection to SUPDRV and register our component factory.
|
---|
1358 | */
|
---|
1359 | rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
|
---|
1360 | if (RT_SUCCESS(rc))
|
---|
1361 | {
|
---|
1362 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1363 | if (RT_SUCCESS(rc))
|
---|
1364 | {
|
---|
1365 | pGlobals->fIDCOpen = true;
|
---|
1366 | Log(("VBoxNetFlt: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
|
---|
1367 | return rc;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | /* bail out. */
|
---|
1371 | LogRel(("VBoxNetFlt: Failed to register component factory, rc=%Rrc\n", rc));
|
---|
1372 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | return rc;
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 |
|
---|
1379 | /**
|
---|
1380 | * Deletes the globals.
|
---|
1381 | *
|
---|
1382 | * This must be called after the IDC connection has been closed,
|
---|
1383 | * see vboxNetFltTryDeleteIdc().
|
---|
1384 | *
|
---|
1385 | * @param pGlobals Pointer to the globals.
|
---|
1386 | * @sa vboxNetFltTryDeleteIdcAndGlobals()
|
---|
1387 | */
|
---|
1388 | DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1389 | {
|
---|
1390 | Assert(!pGlobals->fIDCOpen);
|
---|
1391 |
|
---|
1392 | /*
|
---|
1393 | * Release resources.
|
---|
1394 | */
|
---|
1395 | RTSemFastMutexDestroy(pGlobals->hFastMtx);
|
---|
1396 | pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
|
---|
1397 |
|
---|
1398 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1399 | RTSemEventDestroy(pGlobals->hBlockEvent);
|
---|
1400 | pGlobals->hBlockEvent = NIL_RTSEMEVENT;
|
---|
1401 | #endif
|
---|
1402 |
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 |
|
---|
1406 | /**
|
---|
1407 | * Initializes the globals.
|
---|
1408 | *
|
---|
1409 | * @returns VBox status code.
|
---|
1410 | * @param pGlobals Pointer to the globals.
|
---|
1411 | * @sa vboxNetFltInitGlobalsAndIdc().
|
---|
1412 | */
|
---|
1413 | DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1414 | {
|
---|
1415 | /*
|
---|
1416 | * Initialize the common portions of the structure.
|
---|
1417 | */
|
---|
1418 | int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
|
---|
1419 | if (RT_SUCCESS(rc))
|
---|
1420 | {
|
---|
1421 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1422 | rc = RTSemEventCreate(&pGlobals->hBlockEvent);
|
---|
1423 | if (RT_SUCCESS(rc))
|
---|
1424 | {
|
---|
1425 | #endif
|
---|
1426 | pGlobals->pInstanceHead = NULL;
|
---|
1427 |
|
---|
1428 | pGlobals->TrunkFactory.pfnRelease = vboxNetFltFactoryRelease;
|
---|
1429 | pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetFltFactoryCreateAndConnect;
|
---|
1430 | #if defined(RT_OS_WINDOWS) && defined(VBOX_TAPMINIPORT)
|
---|
1431 | strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
|
---|
1432 | #else
|
---|
1433 | strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetFlt");
|
---|
1434 | #endif
|
---|
1435 | pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetFltQueryFactoryInterface;
|
---|
1436 | pGlobals->fIDCOpen = false;
|
---|
1437 |
|
---|
1438 | return rc;
|
---|
1439 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1440 | }
|
---|
1441 | RTSemFastMutexDestroy(pGlobals->hFastMtx);
|
---|
1442 | #endif
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | return rc;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 |
|
---|
1449 | /**
|
---|
1450 | * Called by the native part when the OS wants the driver to unload.
|
---|
1451 | *
|
---|
1452 | * @returns VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
|
---|
1453 | *
|
---|
1454 | * @param pGlobals Pointer to the globals.
|
---|
1455 | */
|
---|
1456 | DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1457 | {
|
---|
1458 | int rc = vboxNetFltTryDeleteIdc(pGlobals);
|
---|
1459 | if (RT_SUCCESS(rc))
|
---|
1460 | vboxNetFltDeleteGlobals(pGlobals);
|
---|
1461 | return rc;
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 |
|
---|
1465 | /**
|
---|
1466 | * Called by the native driver/kext module initialization routine.
|
---|
1467 | *
|
---|
1468 | * It will initialize the common parts of the globals, assuming the caller
|
---|
1469 | * has already taken care of the OS specific bits, and establish the IDC
|
---|
1470 | * connection to SUPDRV.
|
---|
1471 | *
|
---|
1472 | * @returns VBox status code.
|
---|
1473 | * @param pGlobals Pointer to the globals.
|
---|
1474 | */
|
---|
1475 | DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1476 | {
|
---|
1477 | /*
|
---|
1478 | * Initialize the common portions of the structure.
|
---|
1479 | */
|
---|
1480 | int rc = vboxNetFltInitGlobals(pGlobals);
|
---|
1481 | if (RT_SUCCESS(rc))
|
---|
1482 | {
|
---|
1483 | rc = vboxNetFltInitIdc(pGlobals);
|
---|
1484 | if (RT_SUCCESS(rc))
|
---|
1485 | return rc;
|
---|
1486 |
|
---|
1487 | /* bail out. */
|
---|
1488 | vboxNetFltDeleteGlobals(pGlobals);
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | return rc;
|
---|
1492 | }
|
---|
1493 |
|
---|