1 | /* $Id: VBoxNetFlt.c 18064 2009-03-18 12:52:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetFlt - Network Filter Driver (Host), Common Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2009 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 | #include <iprt/thread.h>
|
---|
231 |
|
---|
232 |
|
---|
233 | /*******************************************************************************
|
---|
234 | * Defined Constants And Macros *
|
---|
235 | *******************************************************************************/
|
---|
236 | #define IFPORT_2_VBOXNETFLTINS(pIfPort) \
|
---|
237 | ( (PVBOXNETFLTINS)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETFLTINS, MyPort)) )
|
---|
238 |
|
---|
239 |
|
---|
240 | AssertCompileMemberSize(VBOXNETFLTINS, enmState, sizeof(uint32_t));
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Sets the enmState member atomically.
|
---|
244 | *
|
---|
245 | * Used for all updates.
|
---|
246 | *
|
---|
247 | * @param pThis The instance.
|
---|
248 | * @param enmNewState The new value.
|
---|
249 | */
|
---|
250 | DECLINLINE(void) vboxNetFltSetState(PVBOXNETFLTINS pThis, VBOXNETFTLINSSTATE enmNewState)
|
---|
251 | {
|
---|
252 | ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Gets the enmState member atomically.
|
---|
258 | *
|
---|
259 | * Used for all reads.
|
---|
260 | *
|
---|
261 | * @returns The enmState value.
|
---|
262 | * @param pThis The instance.
|
---|
263 | */
|
---|
264 | DECLINLINE(VBOXNETFTLINSSTATE) vboxNetFltGetState(PVBOXNETFLTINS pThis)
|
---|
265 | {
|
---|
266 | return (VBOXNETFTLINSSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Finds a instance by its name, the caller does the locking.
|
---|
272 | *
|
---|
273 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
274 | * @param pGlobals The globals.
|
---|
275 | * @param pszName The name of the instance.
|
---|
276 | */
|
---|
277 | static PVBOXNETFLTINS vboxNetFltFindInstanceLocked(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
278 | {
|
---|
279 | PVBOXNETFLTINS pCur;
|
---|
280 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
281 | if (!strcmp(pszName, pCur->szName))
|
---|
282 | return pCur;
|
---|
283 | return NULL;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Finds a instance by its name, will request the mutex.
|
---|
289 | *
|
---|
290 | * No reference to the instance is retained, we're assuming the caller to
|
---|
291 | * already have one but just for some reason doesn't have the pointer to it.
|
---|
292 | *
|
---|
293 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
294 | * @param pGlobals The globals.
|
---|
295 | * @param pszName The name of the instance.
|
---|
296 | */
|
---|
297 | DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
298 | {
|
---|
299 | PVBOXNETFLTINS pRet;
|
---|
300 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
301 | AssertRCReturn(rc, NULL);
|
---|
302 |
|
---|
303 | pRet = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
304 |
|
---|
305 | rc = RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
306 | AssertRC(rc);
|
---|
307 | return pRet;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Unlinks an instance from the chain.
|
---|
313 | *
|
---|
314 | * @param pGlobals The globals.
|
---|
315 | * @param pToUnlink The instance to unlink.
|
---|
316 | */
|
---|
317 | static void vboxNetFltUnlinkLocked(PVBOXNETFLTGLOBALS pGlobals, PVBOXNETFLTINS pToUnlink)
|
---|
318 | {
|
---|
319 | if (pGlobals->pInstanceHead == pToUnlink)
|
---|
320 | pGlobals->pInstanceHead = pToUnlink->pNext;
|
---|
321 | else
|
---|
322 | {
|
---|
323 | PVBOXNETFLTINS pCur;
|
---|
324 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
325 | if (pCur->pNext == pToUnlink)
|
---|
326 | {
|
---|
327 | pCur->pNext = pToUnlink->pNext;
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | Assert(pCur);
|
---|
331 | }
|
---|
332 | pToUnlink->pNext = NULL;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Performs interface rediscovery if it was disconnected from the host.
|
---|
338 | *
|
---|
339 | * @returns true if successfully rediscovered and connected, false if not.
|
---|
340 | * @param pThis The instance.
|
---|
341 | */
|
---|
342 | static bool vboxNetFltMaybeRediscovered(PVBOXNETFLTINS pThis)
|
---|
343 | {
|
---|
344 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
345 | uint64_t Now = RTTimeNanoTS();
|
---|
346 | bool fRediscovered;
|
---|
347 | bool fDoIt;
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Rediscovered already? Time to try again?
|
---|
351 | */
|
---|
352 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
353 |
|
---|
354 | fRediscovered = !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
|
---|
355 | fDoIt = !fRediscovered
|
---|
356 | && !ASMAtomicUoReadBool(&pThis->fRediscoveryPending)
|
---|
357 | && Now - ASMAtomicUoReadU64(&pThis->NanoTSLastRediscovery) > UINT64_C(5000000000); /* 5 sec */
|
---|
358 | if (fDoIt)
|
---|
359 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, true);
|
---|
360 |
|
---|
361 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Call the OS specific code to do the job.
|
---|
365 | * Update the state when the call returns, that is everything except for
|
---|
366 | * the fDisconnectedFromHost flag which the OS specific code shall set.
|
---|
367 | */
|
---|
368 | if (fDoIt)
|
---|
369 | {
|
---|
370 | fRediscovered = vboxNetFltOsMaybeRediscovered(pThis);
|
---|
371 |
|
---|
372 | Assert(!fRediscovered || !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost));
|
---|
373 |
|
---|
374 | ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, RTTimeNanoTS());
|
---|
375 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, false);
|
---|
376 |
|
---|
377 | if (fRediscovered)
|
---|
378 | vboxNetFltPortOsSetActive(pThis, pThis->fActive);
|
---|
379 | }
|
---|
380 |
|
---|
381 | return fRediscovered;
|
---|
382 | }
|
---|
383 |
|
---|
384 | #ifdef RT_WITH_W64_UNWIND_HACK
|
---|
385 | # if defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64)
|
---|
386 | # define NETFLT_DECL_CALLBACK(type) DECLASM(DECLHIDDEN(type))
|
---|
387 | # define NETFLT_CALLBACK(_n) netfltNtWrap##_n
|
---|
388 |
|
---|
389 | NETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortXmit)(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst);
|
---|
390 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous)(PINTNETTRUNKIFPORT pIfPort);
|
---|
391 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortGetMacAddress)(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac);
|
---|
392 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortIsHostMac)(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac);
|
---|
393 | NETFLT_DECL_CALLBACK(int) NETFLT_CALLBACK(vboxNetFltPortWaitForIdle)(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies);
|
---|
394 | NETFLT_DECL_CALLBACK(bool) NETFLT_CALLBACK(vboxNetFltPortSetActive)(PINTNETTRUNKIFPORT pIfPort, bool fActive);
|
---|
395 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease)(PINTNETTRUNKIFPORT pIfPort);
|
---|
396 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRetain)(PINTNETTRUNKIFPORT pIfPort);
|
---|
397 | NETFLT_DECL_CALLBACK(void) NETFLT_CALLBACK(vboxNetFltPortRelease)(PINTNETTRUNKIFPORT pIfPort);
|
---|
398 |
|
---|
399 | # else
|
---|
400 | # error "UNSUPPORTED (RT_WITH_W64_UNWIND_HACK)"
|
---|
401 | # endif
|
---|
402 | #else
|
---|
403 | # define NETFLT_DECL_CALLBACK(type) static DECLCALLBACK(type)
|
---|
404 | # define NETFLT_CALLBACK(_n) _n
|
---|
405 | #endif
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * @copydoc INTNETTRUNKIFPORT::pfnXmit
|
---|
409 | */
|
---|
410 | NETFLT_DECL_CALLBACK(int) vboxNetFltPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
|
---|
411 | {
|
---|
412 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
413 | int rc = VINF_SUCCESS;
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * Input validation.
|
---|
417 | */
|
---|
418 | AssertPtr(pThis);
|
---|
419 | AssertPtr(pSG);
|
---|
420 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
421 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
422 | Assert(pThis->fActive);
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Do a busy retain and then make sure we're connected to the interface
|
---|
426 | * before invoking the OS specific code.
|
---|
427 | */
|
---|
428 | vboxNetFltRetain(pThis, true /* fBusy */);
|
---|
429 | if ( !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)
|
---|
430 | || vboxNetFltMaybeRediscovered(pThis))
|
---|
431 | rc = vboxNetFltPortOsXmit(pThis, pSG, fDst);
|
---|
432 | vboxNetFltRelease(pThis, true /* fBusy */);
|
---|
433 |
|
---|
434 | return rc;
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
|
---|
440 | */
|
---|
441 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
|
---|
442 | {
|
---|
443 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Input validation.
|
---|
447 | */
|
---|
448 | AssertPtr(pThis);
|
---|
449 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
450 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
451 | Assert(pThis->fActive);
|
---|
452 |
|
---|
453 | /*
|
---|
454 | * Ask the OS specific code.
|
---|
455 | */
|
---|
456 | return vboxNetFltPortOsIsPromiscuous(pThis);
|
---|
457 | }
|
---|
458 |
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
|
---|
462 | */
|
---|
463 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
|
---|
464 | {
|
---|
465 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Input validation.
|
---|
469 | */
|
---|
470 | AssertPtr(pThis);
|
---|
471 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
472 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
473 | Assert(pThis->fActive);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Forward the question to the OS specific code.
|
---|
477 | */
|
---|
478 | vboxNetFltPortOsGetMacAddress(pThis, pMac);
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * @copydoc INTNETTRUNKIFPORT::pfnIsHostMac
|
---|
484 | */
|
---|
485 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortIsHostMac(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac)
|
---|
486 | {
|
---|
487 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * Input validation.
|
---|
491 | */
|
---|
492 | AssertPtr(pThis);
|
---|
493 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
494 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
495 | Assert(pThis->fActive);
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Ask the OS specific code.
|
---|
499 | */
|
---|
500 | return vboxNetFltPortOsIsHostMac(pThis, pMac);
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
|
---|
506 | */
|
---|
507 | NETFLT_DECL_CALLBACK(int) vboxNetFltPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
|
---|
508 | {
|
---|
509 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
510 | int rc;
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Input validation.
|
---|
514 | */
|
---|
515 | AssertPtr(pThis);
|
---|
516 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
517 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
518 | AssertReturn(!pThis->fActive, VERR_INVALID_STATE);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Go to sleep on the semaphore after checking the busy count.
|
---|
522 | */
|
---|
523 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
524 |
|
---|
525 | rc = VINF_SUCCESS;
|
---|
526 | while (pThis->cBusy && RT_SUCCESS(rc))
|
---|
527 | rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
|
---|
528 |
|
---|
529 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
530 |
|
---|
531 | return rc;
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * @copydoc INTNETTRUNKIFPORT::pfnSetActive
|
---|
537 | */
|
---|
538 | NETFLT_DECL_CALLBACK(bool) vboxNetFltPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
|
---|
539 | {
|
---|
540 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
541 |
|
---|
542 | /*
|
---|
543 | * Input validation.
|
---|
544 | */
|
---|
545 | AssertPtr(pThis);
|
---|
546 | AssertPtr(pThis->pGlobals);
|
---|
547 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
548 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, false);
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * We're assuming that the caller is serializing the calls, so we don't
|
---|
552 | * have to be extremely careful here. Just update first and then call
|
---|
553 | * the OS specific code, the update must be serialized for various reasons.
|
---|
554 | */
|
---|
555 | if (ASMAtomicReadBool(&pThis->fActive) != fActive)
|
---|
556 | {
|
---|
557 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
558 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
559 | ASMAtomicWriteBool(&pThis->fActive, fActive);
|
---|
560 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
561 |
|
---|
562 | vboxNetFltPortOsSetActive(pThis, fActive);
|
---|
563 | }
|
---|
564 | else
|
---|
565 | fActive = !fActive;
|
---|
566 | return !fActive;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
|
---|
572 | */
|
---|
573 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
574 | {
|
---|
575 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
576 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Serious paranoia.
|
---|
580 | */
|
---|
581 | AssertPtr(pThis);
|
---|
582 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
583 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
584 | AssertPtr(pThis->pGlobals);
|
---|
585 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
586 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
587 | Assert(pThis->szName[0]);
|
---|
588 |
|
---|
589 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
590 | Assert(!pThis->fActive);
|
---|
591 | Assert(!pThis->fRediscoveryPending);
|
---|
592 | Assert(!pThis->cBusy);
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * Disconnect and release it.
|
---|
596 | */
|
---|
597 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
598 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
599 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
600 |
|
---|
601 | vboxNetFltOsDisconnectIt(pThis);
|
---|
602 | pThis->pSwitchPort = NULL;
|
---|
603 |
|
---|
604 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
605 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
606 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Unconnected);
|
---|
607 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
608 | #endif
|
---|
609 |
|
---|
610 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Destroy a device that has been disconnected from the switch.
|
---|
616 | *
|
---|
617 | * @returns true if the instance is destroyed, false otherwise.
|
---|
618 | * @param pThis The instance to be destroyed. This is
|
---|
619 | * no longer valid when this function returns.
|
---|
620 | */
|
---|
621 | static bool vboxNetFltDestroyInstance(PVBOXNETFLTINS pThis)
|
---|
622 | {
|
---|
623 | PVBOXNETFLTGLOBALS pGlobals = pThis->pGlobals;
|
---|
624 | uint32_t cRefs = ASMAtomicUoReadU32((uint32_t volatile *)&pThis->cRefs);
|
---|
625 | int rc;
|
---|
626 | LogFlow(("vboxNetFltDestroyInstance: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
627 |
|
---|
628 | /*
|
---|
629 | * Validate the state.
|
---|
630 | */
|
---|
631 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
632 | Assert( vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting
|
---|
633 | || vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
634 | #else
|
---|
635 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting);
|
---|
636 | #endif
|
---|
637 | Assert(!pThis->fActive);
|
---|
638 | Assert(!pThis->fRediscoveryPending);
|
---|
639 | Assert(!pThis->cRefs);
|
---|
640 | Assert(!pThis->cBusy);
|
---|
641 | Assert(!pThis->pSwitchPort);
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * Make sure the state is 'disconnecting' / 'destroying' and let the OS
|
---|
645 | * specific code do its part of the cleanup outside the mutex.
|
---|
646 | */
|
---|
647 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
648 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
649 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
650 |
|
---|
651 | vboxNetFltOsDeleteInstance(pThis);
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Unlink the instance and free up its resources.
|
---|
655 | */
|
---|
656 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
657 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroyed);
|
---|
658 | vboxNetFltUnlinkLocked(pGlobals, pThis);
|
---|
659 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
660 |
|
---|
661 | RTSemEventDestroy(pThis->hEventIdle);
|
---|
662 | pThis->hEventIdle = NIL_RTSEMEVENT;
|
---|
663 | RTSpinlockDestroy(pThis->hSpinlock);
|
---|
664 | pThis->hSpinlock = NIL_RTSPINLOCK;
|
---|
665 | RTMemFree(pThis);
|
---|
666 |
|
---|
667 | NOREF(cRefs);
|
---|
668 |
|
---|
669 | return true;
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Releases a reference to the specified instance.
|
---|
675 | *
|
---|
676 | * This method will destroy the instance when the count reaches 0.
|
---|
677 | * It will also take care of decrementing the counter and idle wakeup.
|
---|
678 | *
|
---|
679 | * @param pThis The instance.
|
---|
680 | * @param fBusy Whether the busy counter should be decremented too.
|
---|
681 | */
|
---|
682 | DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
683 | {
|
---|
684 | uint32_t cRefs;
|
---|
685 |
|
---|
686 | /*
|
---|
687 | * Paranoid Android.
|
---|
688 | */
|
---|
689 | AssertPtr(pThis);
|
---|
690 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
691 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
692 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
693 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
694 | AssertPtr(pThis->pGlobals);
|
---|
695 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
696 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
697 | Assert(pThis->szName[0]);
|
---|
698 |
|
---|
699 | /*
|
---|
700 | * Work the busy counter.
|
---|
701 | */
|
---|
702 | if (fBusy)
|
---|
703 | {
|
---|
704 | cRefs = ASMAtomicDecU32(&pThis->cBusy);
|
---|
705 | if (!cRefs)
|
---|
706 | {
|
---|
707 | int rc = RTSemEventSignal(pThis->hEventIdle);
|
---|
708 | AssertRC(rc);
|
---|
709 | }
|
---|
710 | else
|
---|
711 | Assert(cRefs < UINT32_MAX / 2);
|
---|
712 | }
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * The object reference counting.
|
---|
716 | */
|
---|
717 | cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
718 | if (!cRefs)
|
---|
719 | vboxNetFltDestroyInstance(pThis);
|
---|
720 | else
|
---|
721 | Assert(cRefs < UINT32_MAX / 2);
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
727 | */
|
---|
728 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
729 | {
|
---|
730 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
731 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Retains a reference to the specified instance and a busy reference too.
|
---|
737 | *
|
---|
738 | * @param pThis The instance.
|
---|
739 | * @param fBusy Whether the busy counter should be incremented as well.
|
---|
740 | */
|
---|
741 | DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
742 | {
|
---|
743 | uint32_t cRefs;
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * Paranoid Android.
|
---|
747 | */
|
---|
748 | AssertPtr(pThis);
|
---|
749 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
750 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
751 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
752 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
753 | AssertPtr(pThis->pGlobals);
|
---|
754 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
755 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
756 | Assert(pThis->szName[0]);
|
---|
757 |
|
---|
758 | /*
|
---|
759 | * Retain the object.
|
---|
760 | */
|
---|
761 | cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
762 | Assert(cRefs > 1 && cRefs < UINT32_MAX / 2);
|
---|
763 |
|
---|
764 | /*
|
---|
765 | * Work the busy counter.
|
---|
766 | */
|
---|
767 | if (fBusy)
|
---|
768 | {
|
---|
769 | cRefs = ASMAtomicIncU32(&pThis->cBusy);
|
---|
770 | Assert(cRefs > 0 && cRefs < UINT32_MAX / 2);
|
---|
771 | }
|
---|
772 |
|
---|
773 | NOREF(cRefs);
|
---|
774 | }
|
---|
775 |
|
---|
776 |
|
---|
777 | /**
|
---|
778 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
779 | */
|
---|
780 | NETFLT_DECL_CALLBACK(void) vboxNetFltPortRetain(PINTNETTRUNKIFPORT pIfPort)
|
---|
781 | {
|
---|
782 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
783 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * Connects the instance to the specified switch port.
|
---|
789 | *
|
---|
790 | * Called while owning the lock. We're ASSUMING that the internal
|
---|
791 | * networking code is already owning an recursive mutex, so, there
|
---|
792 | * will be no deadlocks when vboxNetFltOsConnectIt calls back into
|
---|
793 | * it for setting preferences.
|
---|
794 | *
|
---|
795 | * @returns VBox status code.
|
---|
796 | * @param pThis The instance.
|
---|
797 | * @param pSwitchPort The port on the internal network 'switch'.
|
---|
798 | * @param ppIfPort Where to return our port interface.
|
---|
799 | */
|
---|
800 | static int vboxNetFltConnectIt(PVBOXNETFLTINS pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
801 | {
|
---|
802 | int rc;
|
---|
803 |
|
---|
804 | /*
|
---|
805 | * Validate state.
|
---|
806 | */
|
---|
807 | Assert(!pThis->fActive);
|
---|
808 | Assert(!pThis->fRediscoveryPending);
|
---|
809 | Assert(!pThis->cBusy);
|
---|
810 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
811 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
812 | #else
|
---|
813 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Initializing);
|
---|
814 | #endif
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Do the job.
|
---|
818 | * Note that we're calling the os stuff while owning the semaphore here.
|
---|
819 | */
|
---|
820 | pThis->pSwitchPort = pSwitchPort;
|
---|
821 | rc = vboxNetFltOsConnectIt(pThis);
|
---|
822 | if (RT_SUCCESS(rc))
|
---|
823 | {
|
---|
824 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Connected);
|
---|
825 | *ppIfPort = &pThis->MyPort;
|
---|
826 | }
|
---|
827 | else
|
---|
828 | pThis->pSwitchPort = NULL;
|
---|
829 |
|
---|
830 | Assert(!pThis->fActive);
|
---|
831 | return rc;
|
---|
832 | }
|
---|
833 |
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Creates a new instance.
|
---|
837 | *
|
---|
838 | * The new instance will be in the suspended state in a dynamic config and in
|
---|
839 | * the inactive in a static one.
|
---|
840 | *
|
---|
841 | * Called without owning the lock, but will request is several times.
|
---|
842 | *
|
---|
843 | * @returns VBox status code.
|
---|
844 | * @param pGlobals The globals.
|
---|
845 | * @param pszName The instance name.
|
---|
846 | * @param pSwitchPort The port on the switch that we're connected with (dynamic only).
|
---|
847 | * @param fNoPromisc Do not attempt going into promiscuous mode.
|
---|
848 | * @param pvContext Context argument for vboxNetFltOsInitInstance.
|
---|
849 | * @param ppIfPort Where to store the pointer to our port interface (dynamic only).
|
---|
850 | */
|
---|
851 | static int vboxNetFltNewInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PINTNETTRUNKSWPORT pSwitchPort,
|
---|
852 | bool fNoPromisc, void *pvContext, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
853 | {
|
---|
854 | /*
|
---|
855 | * Allocate and initialize a new instance before requesting the mutex.
|
---|
856 | */
|
---|
857 | int rc;
|
---|
858 | size_t const cchName = strlen(pszName);
|
---|
859 | PVBOXNETFLTINS pNew = (PVBOXNETFLTINS)RTMemAllocZ(RT_OFFSETOF(VBOXNETFLTINS, szName[cchName + 1]));
|
---|
860 | if (!pNew)
|
---|
861 | return VERR_INTNET_FLT_IF_FAILED;
|
---|
862 | pNew->pNext = NULL;
|
---|
863 | pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
|
---|
864 | pNew->MyPort.pfnRetain = NETFLT_CALLBACK(vboxNetFltPortRetain);
|
---|
865 | pNew->MyPort.pfnRelease = NETFLT_CALLBACK(vboxNetFltPortRelease);
|
---|
866 | pNew->MyPort.pfnDisconnectAndRelease= NETFLT_CALLBACK(vboxNetFltPortDisconnectAndRelease);
|
---|
867 | pNew->MyPort.pfnSetActive = NETFLT_CALLBACK(vboxNetFltPortSetActive);
|
---|
868 | pNew->MyPort.pfnWaitForIdle = NETFLT_CALLBACK(vboxNetFltPortWaitForIdle);
|
---|
869 | pNew->MyPort.pfnGetMacAddress = NETFLT_CALLBACK(vboxNetFltPortGetMacAddress);
|
---|
870 | pNew->MyPort.pfnIsHostMac = NETFLT_CALLBACK(vboxNetFltPortIsHostMac);
|
---|
871 | pNew->MyPort.pfnIsPromiscuous = NETFLT_CALLBACK(vboxNetFltPortIsPromiscuous);
|
---|
872 | pNew->MyPort.pfnXmit = NETFLT_CALLBACK(vboxNetFltPortXmit);
|
---|
873 | pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
|
---|
874 | pNew->pSwitchPort = NULL;
|
---|
875 | pNew->pGlobals = pGlobals;
|
---|
876 | pNew->hSpinlock = NIL_RTSPINLOCK;
|
---|
877 | pNew->enmState = kVBoxNetFltInsState_Initializing;
|
---|
878 | pNew->fActive = false;
|
---|
879 | pNew->fDisconnectedFromHost = false;
|
---|
880 | pNew->fRediscoveryPending = false;
|
---|
881 | pNew->fDisablePromiscuous = fNoPromisc;
|
---|
882 | pNew->NanoTSLastRediscovery = INT64_MAX;
|
---|
883 | pNew->cRefs = 1;
|
---|
884 | pNew->cBusy = 0;
|
---|
885 | pNew->hEventIdle = NIL_RTSEMEVENT;
|
---|
886 | memcpy(pNew->szName, pszName, cchName + 1);
|
---|
887 |
|
---|
888 | rc = RTSpinlockCreate(&pNew->hSpinlock);
|
---|
889 | if (RT_SUCCESS(rc))
|
---|
890 | {
|
---|
891 | rc = RTSemEventCreate(&pNew->hEventIdle);
|
---|
892 | if (RT_SUCCESS(rc))
|
---|
893 | {
|
---|
894 | rc = vboxNetFltOsPreInitInstance(pNew);
|
---|
895 | if (RT_SUCCESS(rc))
|
---|
896 | {
|
---|
897 | /*
|
---|
898 | * Insert the instance into the chain, checking for
|
---|
899 | * duplicates first of course (race).
|
---|
900 | */
|
---|
901 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
902 | if (RT_SUCCESS(rc))
|
---|
903 | {
|
---|
904 | if (!vboxNetFltFindInstanceLocked(pGlobals, pszName))
|
---|
905 | {
|
---|
906 | pNew->pNext = pGlobals->pInstanceHead;
|
---|
907 | pGlobals->pInstanceHead = pNew;
|
---|
908 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
909 |
|
---|
910 | /*
|
---|
911 | * Call the OS specific initialization code.
|
---|
912 | */
|
---|
913 | rc = vboxNetFltOsInitInstance(pNew, pvContext);
|
---|
914 | RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
915 | if (RT_SUCCESS(rc))
|
---|
916 | {
|
---|
917 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
918 | /*
|
---|
919 | * Static instances are unconnected at birth.
|
---|
920 | */
|
---|
921 | Assert(!pSwitchPort);
|
---|
922 | pNew->enmState = kVBoxNetFltInsState_Unconnected;
|
---|
923 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
924 | *ppIfPort = &pNew->MyPort;
|
---|
925 | return rc;
|
---|
926 |
|
---|
927 | #else /* !VBOXNETFLT_STATIC_CONFIG */
|
---|
928 | /*
|
---|
929 | * Connect it as well, the OS specific bits has to be done outside
|
---|
930 | * the lock as they may call back to into intnet.
|
---|
931 | */
|
---|
932 | rc = vboxNetFltConnectIt(pNew, pSwitchPort, ppIfPort);
|
---|
933 | if (RT_SUCCESS(rc))
|
---|
934 | {
|
---|
935 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
936 | Assert(*ppIfPort == &pNew->MyPort);
|
---|
937 | return rc;
|
---|
938 | }
|
---|
939 |
|
---|
940 | /* Bail out (failed). */
|
---|
941 | vboxNetFltOsDeleteInstance(pNew);
|
---|
942 | #endif /* !VBOXNETFLT_STATIC_CONFIG */
|
---|
943 | }
|
---|
944 | vboxNetFltUnlinkLocked(pGlobals, pNew);
|
---|
945 | }
|
---|
946 | else
|
---|
947 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
948 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
949 | }
|
---|
950 | }
|
---|
951 | RTSemEventDestroy(pNew->hEventIdle);
|
---|
952 | }
|
---|
953 | RTSpinlockDestroy(pNew->hSpinlock);
|
---|
954 | }
|
---|
955 |
|
---|
956 | RTMemFree(pNew);
|
---|
957 | return rc;
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
962 | /**
|
---|
963 | * Searches for the NetFlt instance by its name and creates the new one if not found.
|
---|
964 | *
|
---|
965 | * @returns VBox status code.
|
---|
966 | * @retval VINF_SUCCESS and *ppInstance if a new instance was created.
|
---|
967 | * @retval VINF_ALREADY_INITIALIZED and *ppInstance if an instance already exists.
|
---|
968 | *
|
---|
969 | * @param pGlobal Pointer to the globals.
|
---|
970 | * @param pszName The instance name.
|
---|
971 | * @param ppInstance Where to return the instance pointer on success.
|
---|
972 | * @param pvContext Context which needs to be passed along to vboxNetFltOsInitInstance.
|
---|
973 | */
|
---|
974 | DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void *pvContext)
|
---|
975 | {
|
---|
976 | PINTNETTRUNKIFPORT pIfPort;
|
---|
977 | PVBOXNETFLTINS pCur;
|
---|
978 | VBOXNETFTLINSSTATE enmState;
|
---|
979 | int rc;
|
---|
980 |
|
---|
981 | *ppInstance = NULL;
|
---|
982 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
983 | AssertRCReturn(rc, rc);
|
---|
984 |
|
---|
985 | /*
|
---|
986 | * Look for an existing instance in the list.
|
---|
987 | *
|
---|
988 | * There might be an existing one in the list if the driver was unbound
|
---|
989 | * while it was connected to an internal network. We're running into
|
---|
990 | * a destruction race that is a bit similar to the one in
|
---|
991 | * vboxNetFltFactoryCreateAndConnect, only the roles are reversed
|
---|
992 | * and we're not in a position to back down. Instead of backing down
|
---|
993 | * we'll delay a bit giving the other thread time to complete the
|
---|
994 | * destructor.
|
---|
995 | */
|
---|
996 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
997 | while (pCur)
|
---|
998 | {
|
---|
999 | uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
|
---|
1000 | if (cRefs > 1)
|
---|
1001 | {
|
---|
1002 | enmState = vboxNetFltGetState(pCur);
|
---|
1003 | switch (enmState)
|
---|
1004 | {
|
---|
1005 | case kVBoxNetFltInsState_Unconnected:
|
---|
1006 | case kVBoxNetFltInsState_Connected:
|
---|
1007 | case kVBoxNetFltInsState_Disconnecting:
|
---|
1008 | if (pCur->fDisconnectedFromHost)
|
---|
1009 | {
|
---|
1010 | /* Wait for it to exit the transitional disconnecting
|
---|
1011 | state. It might otherwise be running the risk of
|
---|
1012 | upsetting the OS specific code... */
|
---|
1013 | /** @todo This reconnect stuff should be serialized correctly for static
|
---|
1014 | * devices. Shouldn't it? In the dynamic case we're using the INTNET
|
---|
1015 | * outbound thrunk lock, but that doesn't quite cut it here, or does
|
---|
1016 | * it? We could either transition to initializing or make a callback
|
---|
1017 | * while owning the mutext here... */
|
---|
1018 | if (enmState == kVBoxNetFltInsState_Disconnecting)
|
---|
1019 | {
|
---|
1020 | do
|
---|
1021 | {
|
---|
1022 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1023 | RTThreadSleep(2); /* (2ms) */
|
---|
1024 | RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1025 | enmState = vboxNetFltGetState(pCur);
|
---|
1026 | }
|
---|
1027 | while (enmState == kVBoxNetFltInsState_Disconnecting);
|
---|
1028 | AssertMsg(enmState == kVBoxNetFltInsState_Unconnected, ("%d\n", enmState));
|
---|
1029 | Assert(pCur->fDisconnectedFromHost);
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1033 | *ppInstance = pCur;
|
---|
1034 | return VINF_ALREADY_INITIALIZED;
|
---|
1035 | }
|
---|
1036 | /* fall thru */
|
---|
1037 |
|
---|
1038 | default:
|
---|
1039 | {
|
---|
1040 | bool fDfH = pCur->fDisconnectedFromHost;
|
---|
1041 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1042 | vboxNetFltRelease(pCur, false /* fBusy */);
|
---|
1043 | LogRel(("VBoxNetFlt: Huh? An instance of '%s' already exists! [pCur=%p cRefs=%d fDfH=%RTbool enmState=%d]\n",
|
---|
1044 | pszName, pCur, cRefs - 1, fDfH, enmState));
|
---|
1045 | *ppInstance = NULL;
|
---|
1046 | return VERR_INTNET_FLT_IF_BUSY;
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | /* Zero references, it's being destroyed. Delay a bit so the destructor
|
---|
1052 | can finish its work and try again. (vboxNetFltNewInstance will fail
|
---|
1053 | with duplicate name if we don't.) */
|
---|
1054 | # ifdef RT_STRICT
|
---|
1055 | Assert(cRefs == 1);
|
---|
1056 | enmState = vboxNetFltGetState(pCur);
|
---|
1057 | AssertMsg( enmState == kVBoxNetFltInsState_Unconnected
|
---|
1058 | || enmState == kVBoxNetFltInsState_Disconnecting
|
---|
1059 | || enmState == kVBoxNetFltInsState_Destroyed, ("%d\n", enmState));
|
---|
1060 | # endif
|
---|
1061 | ASMAtomicDecU32(&pCur->cRefs);
|
---|
1062 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1063 | RTThreadSleep(2); /* (2ms) */
|
---|
1064 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1065 | AssertRCReturn(rc, rc);
|
---|
1066 |
|
---|
1067 | /* try again */
|
---|
1068 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1072 |
|
---|
1073 | /*
|
---|
1074 | * Try create a new instance.
|
---|
1075 | * (fNoPromisc is overridden in the vboxNetFltFactoryCreateAndConnect path, so pass true here.)
|
---|
1076 | */
|
---|
1077 | rc = vboxNetFltNewInstance(pGlobals, pszName, NULL, true /* fNoPromisc */, pvContext, &pIfPort);
|
---|
1078 | if (RT_SUCCESS(rc))
|
---|
1079 | *ppInstance = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
1080 | else
|
---|
1081 | *ppInstance = NULL;
|
---|
1082 |
|
---|
1083 | return rc;
|
---|
1084 | }
|
---|
1085 | #endif /* VBOXNETFLT_STATIC_CONFIG */
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | /**
|
---|
1089 | * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
|
---|
1090 | */
|
---|
1091 | static DECLCALLBACK(int) vboxNetFltFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
|
---|
1092 | PINTNETTRUNKSWPORT pSwitchPort, uint32_t fFlags,
|
---|
1093 | PINTNETTRUNKIFPORT *ppIfPort)
|
---|
1094 | {
|
---|
1095 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
1096 | PVBOXNETFLTINS pCur;
|
---|
1097 | int rc;
|
---|
1098 |
|
---|
1099 | LogFlow(("vboxNetFltFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
|
---|
1100 | Assert(pGlobals->cFactoryRefs > 0);
|
---|
1101 | AssertMsgReturn(!(fFlags & ~(INTNETTRUNKFACTORY_FLAG_NO_PROMISC)),
|
---|
1102 | ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
1103 |
|
---|
1104 | /*
|
---|
1105 | * Static: Find instance, check if busy, connect if not.
|
---|
1106 | * Dynamic: Check for duplicate / busy interface instance.
|
---|
1107 | */
|
---|
1108 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1109 | AssertRCReturn(rc, rc);
|
---|
1110 |
|
---|
1111 | //#if defined(VBOXNETADP) && defined(RT_OS_WINDOWS)
|
---|
1112 | // /* temporary hack to pick up the first adapter */
|
---|
1113 | // pCur = pGlobals->pInstanceHead; /** @todo Don't for get to remove this temporary hack... :-) */
|
---|
1114 | //#else
|
---|
1115 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
1116 | //#endif
|
---|
1117 | if (pCur)
|
---|
1118 | {
|
---|
1119 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1120 | /* Try grab a reference. If the count had already reached zero we're racing the
|
---|
1121 | destructor code and must back down. */
|
---|
1122 | uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
|
---|
1123 | if (cRefs > 1)
|
---|
1124 | {
|
---|
1125 | if (vboxNetFltGetState(pCur) == kVBoxNetFltInsState_Unconnected)
|
---|
1126 | {
|
---|
1127 | pCur->fDisablePromiscuous = !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC);
|
---|
1128 | rc = vboxNetFltConnectIt(pCur, pSwitchPort, ppIfPort);
|
---|
1129 | if (RT_SUCCESS(rc))
|
---|
1130 | pCur = NULL; /* Don't release it, reference given to the caller. */
|
---|
1131 | }
|
---|
1132 | else
|
---|
1133 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
1134 | }
|
---|
1135 | else
|
---|
1136 | {
|
---|
1137 | Assert(cRefs == 1);
|
---|
1138 | ASMAtomicDecU32(&pCur->cRefs);
|
---|
1139 | pCur = NULL; /* nothing to release */
|
---|
1140 | rc = VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1144 | if (pCur)
|
---|
1145 | vboxNetFltRelease(pCur, false /* fBusy */);
|
---|
1146 | #else
|
---|
1147 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
1148 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1149 | #endif
|
---|
1150 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
1151 | return rc;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1155 |
|
---|
1156 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
1157 | rc = VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
1158 | #else
|
---|
1159 | /*
|
---|
1160 | * Dynamically create a new instance.
|
---|
1161 | */
|
---|
1162 | rc = vboxNetFltNewInstance(pGlobals,
|
---|
1163 | pszName,
|
---|
1164 | pSwitchPort,
|
---|
1165 | !!(fFlags & INTNETTRUNKFACTORY_FLAG_NO_PROMISC),
|
---|
1166 | NULL,
|
---|
1167 | ppIfPort);
|
---|
1168 | #endif
|
---|
1169 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
1170 | return rc;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 |
|
---|
1174 | /**
|
---|
1175 | * @copydoc INTNETTRUNKFACTORY::pfnRelease
|
---|
1176 | */
|
---|
1177 | static DECLCALLBACK(void) vboxNetFltFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
|
---|
1178 | {
|
---|
1179 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
1180 |
|
---|
1181 | int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
|
---|
1182 | Assert(cRefs >= 0); NOREF(cRefs);
|
---|
1183 | LogFlow(("vboxNetFltFactoryRelease: cRefs=%d (new)\n", cRefs));
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 |
|
---|
1187 | /**
|
---|
1188 | * Implements the SUPDRV component factor interface query method.
|
---|
1189 | *
|
---|
1190 | * @returns Pointer to an interface. NULL if not supported.
|
---|
1191 | *
|
---|
1192 | * @param pSupDrvFactory Pointer to the componet factory registration structure.
|
---|
1193 | * @param pSession The session - unused.
|
---|
1194 | * @param pszInterfaceUuid The factory interface id.
|
---|
1195 | */
|
---|
1196 | static DECLCALLBACK(void *) vboxNetFltQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
|
---|
1197 | {
|
---|
1198 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, SupDrvFactory));
|
---|
1199 |
|
---|
1200 | /*
|
---|
1201 | * Convert the UUID strings and compare them.
|
---|
1202 | */
|
---|
1203 | RTUUID UuidReq;
|
---|
1204 | int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
|
---|
1205 | if (RT_SUCCESS(rc))
|
---|
1206 | {
|
---|
1207 | if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
|
---|
1208 | {
|
---|
1209 | ASMAtomicIncS32(&pGlobals->cFactoryRefs);
|
---|
1210 | return &pGlobals->TrunkFactory;
|
---|
1211 | }
|
---|
1212 | #ifdef LOG_ENABLED
|
---|
1213 | /* log legacy queries */
|
---|
1214 | /* else if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_V1_UUID_STR))
|
---|
1215 | Log(("VBoxNetFlt: V1 factory query\n"));
|
---|
1216 | */
|
---|
1217 | else
|
---|
1218 | Log(("VBoxNetFlt: unknown factory interface query (%s)\n", pszInterfaceUuid));
|
---|
1219 | #endif
|
---|
1220 | }
|
---|
1221 | else
|
---|
1222 | Log(("VBoxNetFlt: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
|
---|
1223 |
|
---|
1224 | return NULL;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 |
|
---|
1228 | /**
|
---|
1229 | * Checks whether the VBoxNetFlt wossname can be unloaded.
|
---|
1230 | *
|
---|
1231 | * This will return false if someone is currently using the module.
|
---|
1232 | *
|
---|
1233 | * @returns true if it's relatively safe to unload it, otherwise false.
|
---|
1234 | * @param pGlobals Pointer to the globals.
|
---|
1235 | */
|
---|
1236 | DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1237 | {
|
---|
1238 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1239 | bool fRc = !pGlobals->pInstanceHead
|
---|
1240 | && pGlobals->cFactoryRefs <= 0;
|
---|
1241 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1242 | AssertRC(rc);
|
---|
1243 | return fRc;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 |
|
---|
1247 | /**
|
---|
1248 | * Try to close the IDC connection to SUPDRV if established.
|
---|
1249 | *
|
---|
1250 | * @returns VBox status code.
|
---|
1251 | * @retval VINF_SUCCESS on success.
|
---|
1252 | * @retval VERR_WRONG_ORDER if we're busy.
|
---|
1253 | *
|
---|
1254 | * @param pGlobals Pointer to the globals.
|
---|
1255 | *
|
---|
1256 | * @sa vboxNetFltTryDeleteIdcAndGlobals()
|
---|
1257 | */
|
---|
1258 | DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1259 | {
|
---|
1260 | int rc;
|
---|
1261 |
|
---|
1262 | Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
|
---|
1263 |
|
---|
1264 | /*
|
---|
1265 | * Check before trying to deregister the factory.
|
---|
1266 | */
|
---|
1267 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1268 | return VERR_WRONG_ORDER;
|
---|
1269 |
|
---|
1270 | if (!pGlobals->fIDCOpen)
|
---|
1271 | rc = VINF_SUCCESS;
|
---|
1272 | else
|
---|
1273 | {
|
---|
1274 | /*
|
---|
1275 | * Disconnect from SUPDRV and check that nobody raced us,
|
---|
1276 | * reconnect if that should happen.
|
---|
1277 | */
|
---|
1278 | rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1279 | AssertRC(rc);
|
---|
1280 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1281 | {
|
---|
1282 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1283 | AssertRC(rc);
|
---|
1284 | return VERR_WRONG_ORDER;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1288 | pGlobals->fIDCOpen = false;
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | return rc;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 |
|
---|
1295 | /**
|
---|
1296 | * Establishes the IDC connection to SUPDRV and registers our component factory.
|
---|
1297 | *
|
---|
1298 | * @returns VBox status code.
|
---|
1299 | * @param pGlobals Pointer to the globals.
|
---|
1300 | * @sa vboxNetFltInitGlobalsAndIdc().
|
---|
1301 | */
|
---|
1302 | DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1303 | {
|
---|
1304 | int rc;
|
---|
1305 | Assert(!pGlobals->fIDCOpen);
|
---|
1306 |
|
---|
1307 | /*
|
---|
1308 | * Establish a connection to SUPDRV and register our component factory.
|
---|
1309 | */
|
---|
1310 | rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
|
---|
1311 | if (RT_SUCCESS(rc))
|
---|
1312 | {
|
---|
1313 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1314 | if (RT_SUCCESS(rc))
|
---|
1315 | {
|
---|
1316 | pGlobals->fIDCOpen = true;
|
---|
1317 | Log(("VBoxNetFlt: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
|
---|
1318 | return rc;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /* bail out. */
|
---|
1322 | LogRel(("VBoxNetFlt: Failed to register component factory, rc=%Rrc\n", rc));
|
---|
1323 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | return rc;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 |
|
---|
1330 | /**
|
---|
1331 | * Deletes the globals.
|
---|
1332 | *
|
---|
1333 | * This must be called after the IDC connection has been closed,
|
---|
1334 | * see vboxNetFltTryDeleteIdc().
|
---|
1335 | *
|
---|
1336 | * @param pGlobals Pointer to the globals.
|
---|
1337 | * @sa vboxNetFltTryDeleteIdcAndGlobals()
|
---|
1338 | */
|
---|
1339 | DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1340 | {
|
---|
1341 | Assert(!pGlobals->fIDCOpen);
|
---|
1342 |
|
---|
1343 | /*
|
---|
1344 | * Release resources.
|
---|
1345 | */
|
---|
1346 | RTSemFastMutexDestroy(pGlobals->hFastMtx);
|
---|
1347 | pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Initializes the globals.
|
---|
1353 | *
|
---|
1354 | * @returns VBox status code.
|
---|
1355 | * @param pGlobals Pointer to the globals.
|
---|
1356 | * @sa vboxNetFltInitGlobalsAndIdc().
|
---|
1357 | */
|
---|
1358 | DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1359 | {
|
---|
1360 | /*
|
---|
1361 | * Initialize the common portions of the structure.
|
---|
1362 | */
|
---|
1363 | int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
|
---|
1364 | if (RT_SUCCESS(rc))
|
---|
1365 | {
|
---|
1366 | pGlobals->pInstanceHead = NULL;
|
---|
1367 |
|
---|
1368 | pGlobals->TrunkFactory.pfnRelease = vboxNetFltFactoryRelease;
|
---|
1369 | pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetFltFactoryCreateAndConnect;
|
---|
1370 | #if defined(RT_OS_WINDOWS) && defined(VBOXNETADP)
|
---|
1371 | strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
|
---|
1372 | #else
|
---|
1373 | strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetFlt");
|
---|
1374 | #endif
|
---|
1375 | pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetFltQueryFactoryInterface;
|
---|
1376 | pGlobals->fIDCOpen = false;
|
---|
1377 |
|
---|
1378 | return rc;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | return rc;
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 |
|
---|
1385 | /**
|
---|
1386 | * Called by the native part when the OS wants the driver to unload.
|
---|
1387 | *
|
---|
1388 | * @returns VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
|
---|
1389 | *
|
---|
1390 | * @param pGlobals Pointer to the globals.
|
---|
1391 | */
|
---|
1392 | DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1393 | {
|
---|
1394 | int rc = vboxNetFltTryDeleteIdc(pGlobals);
|
---|
1395 | if (RT_SUCCESS(rc))
|
---|
1396 | vboxNetFltDeleteGlobals(pGlobals);
|
---|
1397 | return rc;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 |
|
---|
1401 | /**
|
---|
1402 | * Called by the native driver/kext module initialization routine.
|
---|
1403 | *
|
---|
1404 | * It will initialize the common parts of the globals, assuming the caller
|
---|
1405 | * has already taken care of the OS specific bits, and establish the IDC
|
---|
1406 | * connection to SUPDRV.
|
---|
1407 | *
|
---|
1408 | * @returns VBox status code.
|
---|
1409 | * @param pGlobals Pointer to the globals.
|
---|
1410 | */
|
---|
1411 | DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1412 | {
|
---|
1413 | /*
|
---|
1414 | * Initialize the common portions of the structure.
|
---|
1415 | */
|
---|
1416 | int rc = vboxNetFltInitGlobals(pGlobals);
|
---|
1417 | if (RT_SUCCESS(rc))
|
---|
1418 | {
|
---|
1419 | rc = vboxNetFltInitIdc(pGlobals);
|
---|
1420 | if (RT_SUCCESS(rc))
|
---|
1421 | return rc;
|
---|
1422 |
|
---|
1423 | /* bail out. */
|
---|
1424 | vboxNetFltDeleteGlobals(pGlobals);
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | return rc;
|
---|
1428 | }
|
---|
1429 |
|
---|