1 | /* $Id: VBoxNetFlt.c 12653 2008-09-22 16:03:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetFlt - Network Filter Driver (Host), Common Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * Sun Microsystems, Inc. confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 | /** @page pg_netflt VBoxNetFlt - Network Interface Filter
|
---|
14 | *
|
---|
15 | * This is a kernel module that attaches to a real interface on the host
|
---|
16 | * and filters and injects packets.
|
---|
17 | *
|
---|
18 | * In the big picture we're one of the three trunk interface on the internal
|
---|
19 | * network, the one named "NIC Filter Driver": @image html Networking_Overview.gif
|
---|
20 | *
|
---|
21 | *
|
---|
22 | * @section sec_netflt_msc Locking / Sequence Diagrams
|
---|
23 | *
|
---|
24 | * This secion contains a few sequence diagrams describing the problematic
|
---|
25 | * transitions of a host interface filter instance.
|
---|
26 | *
|
---|
27 | * The thing that makes it all a bit problematic is that multiple events may
|
---|
28 | * happen at the same time, and that we have to be very careful to avoid
|
---|
29 | * deadlocks caused by mixing our locks with the ones in the host kernel.
|
---|
30 | * The main events are receive, send, async send completion, disappearance of
|
---|
31 | * the host networking interface and it's reappearance. The latter two events
|
---|
32 | * are can be caused by driver unloading/loading or the device being physical
|
---|
33 | * unplugged (e.g. a USB network device).
|
---|
34 | *
|
---|
35 | * The strategy for dealing with these issues are:
|
---|
36 | * - Use a simple state machine.
|
---|
37 | * - Require the user (IntNet) to serialize all its calls to us,
|
---|
38 | * while at the same time not owning any lock used by any of the
|
---|
39 | * the callbacks we might call on receive and async send completion.
|
---|
40 | * - Make sure we're 100% idle before disconnecting, and have a
|
---|
41 | * disconnected status on both sides to fend off async calls.
|
---|
42 | * - Protect the host specific interface handle and the state variables
|
---|
43 | * using a spinlock.
|
---|
44 | *
|
---|
45 | *
|
---|
46 | * @subsection subsec_netflt_msc_dis_rel Disconnect from the network and release
|
---|
47 | *
|
---|
48 | * @msc
|
---|
49 | * VM, IntNet, NetFlt, Kernel, Wire;
|
---|
50 | *
|
---|
51 | * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
|
---|
52 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
53 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
|
---|
54 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
55 | * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
|
---|
56 | * NetFlt=>Kernel [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
57 | * Kernel->Wire [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
58 | *
|
---|
59 | * --- [label="Suspending the trunk interface"];
|
---|
60 | * IntNet=>IntNet [label="Lock Network"];
|
---|
61 | *
|
---|
62 | * Wire->Kernel [label="pkt1 - racing us", linecolor="red", textcolor="red"];
|
---|
63 | * Kernel=>>NetFlt [label="pkt1 - racing us", linecolor="red", textcolor="red"];
|
---|
64 | * NetFlt=>>IntNet [label="pkt1 recv - blocks", linecolor="red", textcolor="red"];
|
---|
65 | *
|
---|
66 | * IntNet=>IntNet [label="Mark Trunk Suspended"];
|
---|
67 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
68 | *
|
---|
69 | * IntNet=>NetFlt [label="pfnSetActive(false)"];
|
---|
70 | * NetFlt=>NetFlt [label="Mark inactive (atomic)"];
|
---|
71 | * IntNet<<NetFlt;
|
---|
72 | * IntNet=>NetFlt [label="pfnWaitForIdle(forever)"];
|
---|
73 | *
|
---|
74 | * IntNet=>>NetFlt [label="pkt1 to host", linecolor="red", textcolor="red"];
|
---|
75 | * NetFlt=>>Kernel [label="pkt1 to host", linecolor="red", textcolor="red"];
|
---|
76 | *
|
---|
77 | * Kernel<-Wire [label="pkt0 on wire", linecolor="green", textcolor="green"];
|
---|
78 | * NetFlt<<Kernel [label="pkt0 on wire", linecolor="green", textcolor="green"];
|
---|
79 | * IntNet<<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
80 | * IntNet<<=IntNet [label="Lock Net, free SG, Unlock Net", linecolor="green", textcolor="green"];
|
---|
81 | * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
82 | * NetFlt<-NetFlt [label="idle", linecolor="green", textcolor="green"];
|
---|
83 | *
|
---|
84 | * IntNet<<NetFlt [label="idle (pfnWaitForIdle)"];
|
---|
85 | *
|
---|
86 | * Wire->Kernel [label="pkt2", linecolor="red", textcolor="red"];
|
---|
87 | * Kernel=>>NetFlt [label="pkt2", linecolor="red", textcolor="red"];
|
---|
88 | * NetFlt=>>Kernel [label="pkt2 to host", linecolor="red", textcolor="red"];
|
---|
89 | *
|
---|
90 | * VM->IntNet [label="pkt3", linecolor="green", textcolor="green"];
|
---|
91 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
92 | * IntNet=>IntNet [label="Route packet -> drop", linecolor="green", textcolor="green" ];
|
---|
93 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
94 | *
|
---|
95 | * --- [label="The trunk interface is idle now, disconnect it"];
|
---|
96 | * IntNet=>IntNet [label="Lock Network"];
|
---|
97 | * IntNet=>IntNet [label="Unlink Trunk"];
|
---|
98 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
99 | * IntNet=>NetFlt [label="pfnDisconnectAndRelease"];
|
---|
100 | * NetFlt=>Kernel [label="iflt_detach"];
|
---|
101 | * NetFlt<<=Kernel [label="iff_detached"];
|
---|
102 | * NetFlt>>Kernel [label="iff_detached"];
|
---|
103 | * NetFlt<<Kernel [label="iflt_detach"];
|
---|
104 | * NetFlt=>NetFlt [label="Release"];
|
---|
105 | * IntNet<<NetFlt [label="pfnDisconnectAndRelease"];
|
---|
106 | *
|
---|
107 | * @endmsc
|
---|
108 | *
|
---|
109 | *
|
---|
110 | *
|
---|
111 | * @subsection subsec_netflt_msc_hif_rm Host Interface Removal
|
---|
112 | *
|
---|
113 | * The ifnet_t (pIf) is a tricky customer as any reference to it can potentially
|
---|
114 | * race the filter detaching. The simple way of solving it on Darwin is to guard
|
---|
115 | * all access to the pIf member with a spinlock. The other host systems will
|
---|
116 | * probably have similar race conditions, so the spinlock is a generic thing.
|
---|
117 | *
|
---|
118 | * @msc
|
---|
119 | * VM, IntNet, NetFlt, Kernel;
|
---|
120 | *
|
---|
121 | * VM->IntNet [label="pkt0", linecolor="green", textcolor="green"];
|
---|
122 | * IntNet=>IntNet [label="Lock Network", linecolor="green", textcolor="green" ];
|
---|
123 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="green", textcolor="green" ];
|
---|
124 | * IntNet=>IntNet [label="Unlock Network", linecolor="green", textcolor="green" ];
|
---|
125 | * IntNet=>NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green" ];
|
---|
126 | * NetFlt=>Kernel [label="ifnet_reference w/ spinlock", linecolor="green", textcolor="green" ];
|
---|
127 | * NetFlt<<Kernel [label="ifnet_reference", linecolor="green", textcolor="green" ];
|
---|
128 | * NetFlt=>Kernel [label="pkt0 to wire (blocks)", linecolor="green", textcolor="green" ];
|
---|
129 | *
|
---|
130 | * --- [label="The host interface is being disconnected"];
|
---|
131 | * Kernel->NetFlt [label="iff_detached"];
|
---|
132 | * NetFlt=>Kernel [label="ifnet_release w/ spinlock"];
|
---|
133 | * NetFlt<<Kernel [label="ifnet_release"];
|
---|
134 | * NetFlt=>NetFlt [label="fDisconnectedFromHost=true"];
|
---|
135 | * NetFlt>>Kernel [label="iff_detached"];
|
---|
136 | *
|
---|
137 | * NetFlt<<Kernel [label="dropped", linecolor="green", textcolor="green"];
|
---|
138 | * NetFlt=>NetFlt [label="Acquire spinlock", linecolor="green", textcolor="green"];
|
---|
139 | * NetFlt=>Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
|
---|
140 | * NetFlt<<Kernel [label="ifnet_release", linecolor="green", textcolor="green"];
|
---|
141 | * NetFlt=>NetFlt [label="pIf=NULL", linecolor="green", textcolor="green"];
|
---|
142 | * NetFlt=>NetFlt [label="Release spinlock", linecolor="green", textcolor="green"];
|
---|
143 | * IntNet<=NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
144 | * IntNet>>NetFlt [label="pfnSGRelease", linecolor="green", textcolor="green"];
|
---|
145 | * IntNet<<NetFlt [label="pkt0 to wire", linecolor="green", textcolor="green"];
|
---|
146 | *
|
---|
147 | * @endmsc
|
---|
148 | *
|
---|
149 | *
|
---|
150 | *
|
---|
151 | * @subsection subsec_netflt_msc_hif_rm Host Interface Rediscovery
|
---|
152 | *
|
---|
153 | * The rediscovery is performed when we receive a send request and a certain
|
---|
154 | * period have elapsed since the last attempt, i.e. we're polling it. We
|
---|
155 | * synchronize the rediscovery with disconnection from the internal network
|
---|
156 | * by means of the pfnWaitForIdle call, so no special handling is required.
|
---|
157 | *
|
---|
158 | * @msc
|
---|
159 | * VM2, VM1, IntNet, NetFlt, Kernel, Wire;
|
---|
160 | *
|
---|
161 | * --- [label="Rediscovery conditions are not met"];
|
---|
162 | * VM1->IntNet [label="pkt0"];
|
---|
163 | * IntNet=>IntNet [label="Lock Network"];
|
---|
164 | * IntNet=>IntNet [label="Route packet -> wire"];
|
---|
165 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
166 | * IntNet=>NetFlt [label="pkt0 to wire"];
|
---|
167 | * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
|
---|
168 | * IntNet<<NetFlt [label="pkt0 to wire (dropped)"];
|
---|
169 | *
|
---|
170 | * --- [label="Rediscovery conditions"];
|
---|
171 | * VM1->IntNet [label="pkt1"];
|
---|
172 | * IntNet=>IntNet [label="Lock Network"];
|
---|
173 | * IntNet=>IntNet [label="Route packet -> wire"];
|
---|
174 | * IntNet=>IntNet [label="Unlock Network"];
|
---|
175 | * IntNet=>NetFlt [label="pkt1 to wire"];
|
---|
176 | * NetFlt=>NetFlt [label="Read pIf(==NULL) w/ spinlock"];
|
---|
177 | * NetFlt=>NetFlt [label="fRediscoveryPending=true w/ spinlock"];
|
---|
178 | * NetFlt=>Kernel [label="ifnet_find_by_name"];
|
---|
179 | * NetFlt<<Kernel [label="ifnet_find_by_name (success)"];
|
---|
180 | *
|
---|
181 | * VM2->IntNet [label="pkt2", linecolor="red", textcolor="red"];
|
---|
182 | * IntNet=>IntNet [label="Lock Network", linecolor="red", textcolor="red"];
|
---|
183 | * IntNet=>IntNet [label="Route packet -> wire", linecolor="red", textcolor="red"];
|
---|
184 | * IntNet=>IntNet [label="Unlock Network", linecolor="red", textcolor="red"];
|
---|
185 | * IntNet=>NetFlt [label="pkt2 to wire", linecolor="red", textcolor="red"];
|
---|
186 | * NetFlt=>NetFlt [label="!pIf || fRediscoveryPending (w/ spinlock)", linecolor="red", textcolor="red"];
|
---|
187 | * IntNet<<NetFlt [label="pkt2 to wire (dropped)", linecolor="red", textcolor="red"];
|
---|
188 |
|
---|
189 | * NetFlt=>Kernel [label="iflt_attach"];
|
---|
190 | * NetFlt<<Kernel [label="iflt_attach (success)"];
|
---|
191 | * NetFlt=>NetFlt [label="Acquire spinlock"];
|
---|
192 | * NetFlt=>NetFlt [label="Set pIf and update flags"];
|
---|
193 | * NetFlt=>NetFlt [label="Release spinlock"];
|
---|
194 | *
|
---|
195 | * NetFlt=>Kernel [label="pkt1 to wire"];
|
---|
196 | * Kernel->Wire [label="pkt1 to wire"];
|
---|
197 | * NetFlt<<Kernel [label="pkt1 to wire"];
|
---|
198 | * IntNet<<NetFlt [label="pkt1 to wire"];
|
---|
199 | *
|
---|
200 | *
|
---|
201 | * @endmsc
|
---|
202 | *
|
---|
203 | */
|
---|
204 |
|
---|
205 | /*******************************************************************************
|
---|
206 | * Header Files *
|
---|
207 | *******************************************************************************/
|
---|
208 | #define LOG_GROUP LOG_GROUP_NET_FLT_DRV
|
---|
209 | #include "VBoxNetFltInternal.h"
|
---|
210 |
|
---|
211 | #include <VBox/sup.h>
|
---|
212 | #include <VBox/log.h>
|
---|
213 | #include <VBox/err.h>
|
---|
214 | #include <iprt/assert.h>
|
---|
215 | #include <iprt/mem.h>
|
---|
216 | #include <iprt/string.h>
|
---|
217 | #include <iprt/spinlock.h>
|
---|
218 | #include <iprt/semaphore.h>
|
---|
219 | #include <iprt/time.h>
|
---|
220 | #include <iprt/uuid.h>
|
---|
221 |
|
---|
222 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
223 | # error "The static setup needs a full check up wrt assumptions and incomplete code."
|
---|
224 | #endif
|
---|
225 |
|
---|
226 |
|
---|
227 | /*******************************************************************************
|
---|
228 | * Defined Constants And Macros *
|
---|
229 | *******************************************************************************/
|
---|
230 | #define IFPORT_2_VBOXNETFLTINS(pIfPort) \
|
---|
231 | ( (PVBOXNETFLTINS)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETFLTINS, MyPort)) )
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Finds a instance by its name, the caller does the locking.
|
---|
236 | *
|
---|
237 | *
|
---|
238 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
239 | * @param pGlobals The globals.
|
---|
240 | * @param pszName The name of the instance.
|
---|
241 | */
|
---|
242 | static PVBOXNETFLTINS vboxNetFltFindInstanceLocked(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
243 | {
|
---|
244 | PVBOXNETFLTINS pCur;
|
---|
245 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
246 | if (!strcmp(pszName, pCur->szName))
|
---|
247 | return pCur;
|
---|
248 | return NULL;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Finds a instance by its name, will request the mutex.
|
---|
254 | *
|
---|
255 | * @returns Pointer to the instance by the given name. NULL if not found.
|
---|
256 | * @param pGlobals The globals.
|
---|
257 | * @param pszName The name of the instance.
|
---|
258 | */
|
---|
259 | DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName)
|
---|
260 | {
|
---|
261 | PVBOXNETFLTINS pRet;
|
---|
262 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
263 | AssertRCReturn(rc, NULL);
|
---|
264 |
|
---|
265 | pRet = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
266 |
|
---|
267 | rc = RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
268 | AssertRC(rc);
|
---|
269 | return pRet;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Unlinks an instance from the chain.
|
---|
275 | *
|
---|
276 | * @param pGlobals The globals.
|
---|
277 | * @param pToUnlink The instance to unlink.
|
---|
278 | */
|
---|
279 | static void vboxNetFltUnlinkLocked(PVBOXNETFLTGLOBALS pGlobals, PVBOXNETFLTINS pToUnlink)
|
---|
280 | {
|
---|
281 | if (pGlobals->pInstanceHead == pToUnlink)
|
---|
282 | pGlobals->pInstanceHead = pToUnlink->pNext;
|
---|
283 | else
|
---|
284 | {
|
---|
285 | PVBOXNETFLTINS pCur;
|
---|
286 | for (pCur = pGlobals->pInstanceHead; pCur; pCur = pCur->pNext)
|
---|
287 | if (pCur->pNext == pToUnlink)
|
---|
288 | {
|
---|
289 | pCur->pNext = pToUnlink->pNext;
|
---|
290 | break;
|
---|
291 | }
|
---|
292 | Assert(pCur);
|
---|
293 | }
|
---|
294 | pToUnlink->pNext = NULL;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | AssertCompileMemberSize(VBOXNETFLTINS, enmState, sizeof(uint32_t));
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Sets the enmState member atomically.
|
---|
302 | *
|
---|
303 | * Used for all updates.
|
---|
304 | *
|
---|
305 | * @param pThis The instance.
|
---|
306 | * @param enmNewState The new value.
|
---|
307 | */
|
---|
308 | DECLINLINE(void) vboxNetFltSetState(PVBOXNETFLTINS pThis, VBOXNETFTLINSSTATE enmNewState)
|
---|
309 | {
|
---|
310 | ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Gets the enmState member atomically.
|
---|
316 | *
|
---|
317 | * Used for all reads.
|
---|
318 | *
|
---|
319 | * @returns The enmState value.
|
---|
320 | * @param pThis The instance.
|
---|
321 | */
|
---|
322 | DECLINLINE(VBOXNETFTLINSSTATE) vboxNetFltGetState(PVBOXNETFLTINS pThis)
|
---|
323 | {
|
---|
324 | return (VBOXNETFTLINSSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Performs interface rediscovery if it was disconnected from the host.
|
---|
330 | *
|
---|
331 | * @returns true if successfully rediscovered and connected, false if not.
|
---|
332 | * @param pThis The instance.
|
---|
333 | */
|
---|
334 | static bool vboxNetFltMaybeRediscovered(PVBOXNETFLTINS pThis)
|
---|
335 | {
|
---|
336 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
337 | uint64_t Now = RTTimeNanoTS();
|
---|
338 | bool fRediscovered;
|
---|
339 | bool fDoIt;
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Rediscovered already? Time to try again?
|
---|
343 | */
|
---|
344 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
345 |
|
---|
346 | fRediscovered = !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
|
---|
347 | fDoIt = !fRediscovered
|
---|
348 | && !ASMAtomicUoReadBool(&pThis->fRediscoveryPending)
|
---|
349 | && Now - ASMAtomicUoReadU64(&pThis->NanoTSLastRediscovery) > UINT64_C(5000000000); /* 5 sec */
|
---|
350 | if (fDoIt)
|
---|
351 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, true);
|
---|
352 |
|
---|
353 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
354 |
|
---|
355 | /*
|
---|
356 | * Call the OS specific code to do the job.
|
---|
357 | * Update the state when the call returns, that is everything except for
|
---|
358 | * the fDisconnectedFromHost flag which the OS specific code shall set.
|
---|
359 | */
|
---|
360 | if (fDoIt)
|
---|
361 | {
|
---|
362 | fRediscovered = vboxNetFltOsMaybeRediscovered(pThis);
|
---|
363 |
|
---|
364 | Assert(!fRediscovered || !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost));
|
---|
365 |
|
---|
366 | ASMAtomicUoWriteU64(&pThis->NanoTSLastRediscovery, RTTimeNanoTS());
|
---|
367 | ASMAtomicWriteBool(&pThis->fRediscoveryPending, false);
|
---|
368 |
|
---|
369 | if (fRediscovered)
|
---|
370 | vboxNetFltPortOsSetActive(pThis, pThis->fActive);
|
---|
371 | }
|
---|
372 |
|
---|
373 | return fRediscovered;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * @copydoc INTNETTRUNKIFPORT::pfnXmit
|
---|
379 | */
|
---|
380 | static DECLCALLBACK(int) vboxNetFltPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
|
---|
381 | {
|
---|
382 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
383 | int rc;
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Input validation.
|
---|
387 | */
|
---|
388 | AssertPtr(pThis);
|
---|
389 | AssertPtr(pSG);
|
---|
390 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
391 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
392 | Assert(pThis->fActive);
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Do a busy retain and then make sure we're connected to the interface
|
---|
396 | * before invoking the OS specific code.
|
---|
397 | */
|
---|
398 | vboxNetFltRetain(pThis, true /* fBusy */);
|
---|
399 | if ( !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)
|
---|
400 | || vboxNetFltMaybeRediscovered(pThis))
|
---|
401 | rc = vboxNetFltPortOsXmit(pThis, pSG, fDst);
|
---|
402 | vboxNetFltRelease(pThis, true /* fBusy */);
|
---|
403 |
|
---|
404 | return rc;
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
|
---|
410 | */
|
---|
411 | static DECLCALLBACK(bool) vboxNetFltPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
|
---|
412 | {
|
---|
413 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * Input validation.
|
---|
417 | */
|
---|
418 | AssertPtr(pThis);
|
---|
419 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
420 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
421 | Assert(pThis->fActive);
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * Ask the OS specific code.
|
---|
425 | */
|
---|
426 | return vboxNetFltPortOsIsPromiscuous(pThis);
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
|
---|
432 | */
|
---|
433 | static DECLCALLBACK(void) vboxNetFltPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
|
---|
434 | {
|
---|
435 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Input validation.
|
---|
439 | */
|
---|
440 | AssertPtr(pThis);
|
---|
441 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
442 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
443 | Assert(pThis->fActive);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Forward the question to the OS specific code.
|
---|
447 | */
|
---|
448 | vboxNetFltPortOsGetMacAddress(pThis, pMac);
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * @copydoc INTNETTRUNKIFPORT::pfnIsHostMac
|
---|
454 | */
|
---|
455 | static DECLCALLBACK(bool) vboxNetFltPortIsHostMac(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac)
|
---|
456 | {
|
---|
457 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * Input validation.
|
---|
461 | */
|
---|
462 | AssertPtr(pThis);
|
---|
463 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
464 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
465 | Assert(pThis->fActive);
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Ask the OS specific code.
|
---|
469 | */
|
---|
470 | return vboxNetFltPortOsIsHostMac(pThis, pMac);
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
|
---|
476 | */
|
---|
477 | static DECLCALLBACK(int) vboxNetFltPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
|
---|
478 | {
|
---|
479 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
480 | int rc;
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * Input validation.
|
---|
484 | */
|
---|
485 | AssertPtr(pThis);
|
---|
486 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
487 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, VERR_INVALID_STATE);
|
---|
488 | AssertReturn(!pThis->fActive, VERR_INVALID_STATE);
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * Go to sleep on the semaphore after checking the busy count.
|
---|
492 | */
|
---|
493 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
494 |
|
---|
495 | rc = VINF_SUCCESS;
|
---|
496 | while (pThis->cBusy && RT_SUCCESS(rc))
|
---|
497 | rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
|
---|
498 |
|
---|
499 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
500 |
|
---|
501 | return rc;
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * @copydoc INTNETTRUNKIFPORT::pfnSetActive
|
---|
507 | */
|
---|
508 | static DECLCALLBACK(bool) vboxNetFltPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
|
---|
509 | {
|
---|
510 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Input validation.
|
---|
514 | */
|
---|
515 | AssertPtr(pThis);
|
---|
516 | AssertPtr(pThis->pGlobals);
|
---|
517 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
518 | AssertReturn(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected, false);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * We're assuming that the caller is serializing the calls, so we don't
|
---|
522 | * have to be extremely careful here. Just update first and then call
|
---|
523 | * the OS specific code, the update must be serialized for various reasons.
|
---|
524 | */
|
---|
525 | if (ASMAtomicReadBool(&pThis->fActive) != fActive)
|
---|
526 | {
|
---|
527 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
528 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
529 | ASMAtomicWriteBool(&pThis->fActive, fActive);
|
---|
530 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
531 |
|
---|
532 | vboxNetFltPortOsSetActive(pThis, fActive);
|
---|
533 | }
|
---|
534 | else
|
---|
535 | fActive = !fActive;
|
---|
536 | return !fActive;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
|
---|
542 | */
|
---|
543 | static DECLCALLBACK(void) vboxNetFltPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
544 | {
|
---|
545 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
546 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Serious paranoia.
|
---|
550 | */
|
---|
551 | AssertPtr(pThis);
|
---|
552 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
553 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
554 | AssertPtr(pThis->pGlobals);
|
---|
555 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
556 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
557 | Assert(pThis->szName[0]);
|
---|
558 |
|
---|
559 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Connected);
|
---|
560 | Assert(!pThis->fActive);
|
---|
561 | Assert(!pThis->fRediscoveryPending);
|
---|
562 | Assert(!pThis->cBusy);
|
---|
563 |
|
---|
564 | /*
|
---|
565 | * Disconnect and release it.
|
---|
566 | */
|
---|
567 | RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
|
---|
568 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
569 | RTSpinlockRelease(pThis->hSpinlock, &Tmp);
|
---|
570 |
|
---|
571 | vboxNetFltOsDisconnectIt(pThis);
|
---|
572 | pThis->pSwitchPort = NULL;
|
---|
573 |
|
---|
574 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Destroy a device that has been disconnected from the switch.
|
---|
580 | *
|
---|
581 | * @param pThis The instance to be destroyed. This is
|
---|
582 | * no longer valid when this function returns.
|
---|
583 | */
|
---|
584 | static void vboxNetFltDestroyInstance(PVBOXNETFLTINS pThis)
|
---|
585 | {
|
---|
586 | PVBOXNETFLTGLOBALS pGlobals = pThis->pGlobals;
|
---|
587 | int rc;
|
---|
588 | LogFlow(("vboxNetFltDestroyInstance: pThis=%p (%s)\n", pThis, pThis->szName));
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Validate the state.
|
---|
592 | */
|
---|
593 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
594 | Assert( vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting
|
---|
595 | || vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
596 | #else
|
---|
597 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Disconnecting);
|
---|
598 | #endif
|
---|
599 | Assert(!pThis->fActive);
|
---|
600 | Assert(!pThis->fRediscoveryPending);
|
---|
601 | Assert(!pThis->cRefs);
|
---|
602 | Assert(!pThis->cBusy);
|
---|
603 | Assert(!pThis->pSwitchPort);
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Make sure the state is 'disconnecting' and let the OS specific code
|
---|
607 | * do its part of the cleanup outside the mutex.
|
---|
608 | */
|
---|
609 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
610 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Disconnecting);
|
---|
611 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
612 |
|
---|
613 | vboxNetFltOsDeleteInstance(pThis);
|
---|
614 |
|
---|
615 | /*
|
---|
616 | * Unlink the instance and free up its resources.
|
---|
617 | */
|
---|
618 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx); AssertRC(rc);
|
---|
619 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Destroyed);
|
---|
620 | vboxNetFltUnlinkLocked(pGlobals, pThis);
|
---|
621 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
622 |
|
---|
623 | RTSemEventDestroy(pThis->hEventIdle);
|
---|
624 | pThis->hEventIdle = NIL_RTSEMEVENT;
|
---|
625 | RTSpinlockDestroy(pThis->hSpinlock);
|
---|
626 | pThis->hSpinlock = NIL_RTSPINLOCK;
|
---|
627 | RTMemFree(pThis);
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Releases a reference to the specified instance.
|
---|
633 | *
|
---|
634 | * This method will destroy the instance when the count reaches 0.
|
---|
635 | * It will also take care of decrementing the counter and idle wakeup.
|
---|
636 | *
|
---|
637 | * @param pThis The instance.
|
---|
638 | * @param fBusy Whether the busy counter should be decremented too.
|
---|
639 | */
|
---|
640 | DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
641 | {
|
---|
642 | uint32_t cRefs;
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * Paranoid Android.
|
---|
646 | */
|
---|
647 | AssertPtr(pThis);
|
---|
648 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
649 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
650 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
651 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
652 | AssertPtr(pThis->pGlobals);
|
---|
653 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
654 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
655 | Assert(pThis->szName[0]);
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * Work the busy counter.
|
---|
659 | */
|
---|
660 | if (fBusy)
|
---|
661 | {
|
---|
662 | cRefs = ASMAtomicDecU32(&pThis->cBusy);
|
---|
663 | if (!cRefs)
|
---|
664 | {
|
---|
665 | int rc = RTSemEventSignal(pThis->hEventIdle);
|
---|
666 | AssertRC(rc);
|
---|
667 | }
|
---|
668 | else
|
---|
669 | Assert(cRefs < UINT32_MAX / 2);
|
---|
670 | }
|
---|
671 |
|
---|
672 | /*
|
---|
673 | * The object reference counting.
|
---|
674 | */
|
---|
675 | cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
676 | if (!cRefs)
|
---|
677 | vboxNetFltDestroyInstance(pThis);
|
---|
678 | else
|
---|
679 | Assert(cRefs < UINT32_MAX / 2);
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
685 | */
|
---|
686 | static DECLCALLBACK(void) vboxNetFltPortRelease(PINTNETTRUNKIFPORT pIfPort)
|
---|
687 | {
|
---|
688 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
689 | vboxNetFltRelease(pThis, false /* fBusy */);
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Retains a reference to the specified instance and a busy reference too.
|
---|
695 | *
|
---|
696 | * @param pThis The instance.
|
---|
697 | * @param fBusy Whether the busy counter should be incremented as well.
|
---|
698 | */
|
---|
699 | DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy)
|
---|
700 | {
|
---|
701 | uint32_t cRefs;
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Paranoid Android.
|
---|
705 | */
|
---|
706 | AssertPtr(pThis);
|
---|
707 | Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
|
---|
708 | Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
|
---|
709 | Assert( vboxNetFltGetState(pThis) > kVBoxNetFltInsState_Invalid
|
---|
710 | && vboxNetFltGetState(pThis) < kVBoxNetFltInsState_Destroyed);
|
---|
711 | AssertPtr(pThis->pGlobals);
|
---|
712 | Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
|
---|
713 | Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
|
---|
714 | Assert(pThis->szName[0]);
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * Retain the object.
|
---|
718 | */
|
---|
719 | cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
720 | Assert(cRefs > 0 && cRefs < UINT32_MAX / 2);
|
---|
721 |
|
---|
722 | /*
|
---|
723 | * Work the busy counter.
|
---|
724 | */
|
---|
725 | if (fBusy)
|
---|
726 | {
|
---|
727 | cRefs = ASMAtomicIncU32(&pThis->cBusy);
|
---|
728 | Assert(cRefs > 0 && cRefs < UINT32_MAX / 2);
|
---|
729 | }
|
---|
730 |
|
---|
731 | NOREF(cRefs);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * @copydoc INTNETTRUNKIFPORT::pfnRetain
|
---|
737 | */
|
---|
738 | static DECLCALLBACK(void) vboxNetFltPortRetain(PINTNETTRUNKIFPORT pIfPort)
|
---|
739 | {
|
---|
740 | PVBOXNETFLTINS pThis = IFPORT_2_VBOXNETFLTINS(pIfPort);
|
---|
741 | vboxNetFltRetain(pThis, false /* fBusy */);
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * Connects the instance to the specified switch port.
|
---|
747 | *
|
---|
748 | * Called while owning the lock. We're ASSUMING that the internal
|
---|
749 | * networking code is already owning an recursive mutex, so, there
|
---|
750 | * will be no deadlocks when vboxNetFltOsConnectIt calls back into
|
---|
751 | * it for setting preferences.
|
---|
752 | *
|
---|
753 | * @returns VBox status code.
|
---|
754 | * @param pThis The instance.
|
---|
755 | * @param pSwitchPort The port on the internal network 'switch'.
|
---|
756 | * @param ppIfPort Where to return our port interface.
|
---|
757 | */
|
---|
758 | static int vboxNetFltConnectIt(PVBOXNETFLTINS pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
759 | {
|
---|
760 | int rc;
|
---|
761 |
|
---|
762 | /*
|
---|
763 | * Validate state.
|
---|
764 | */
|
---|
765 | Assert(!pThis->fActive);
|
---|
766 | Assert(!pThis->fRediscoveryPending);
|
---|
767 | Assert(!pThis->cBusy);
|
---|
768 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
769 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Unconnected);
|
---|
770 | #else
|
---|
771 | Assert(vboxNetFltGetState(pThis) == kVBoxNetFltInsState_Initializing);
|
---|
772 | #endif
|
---|
773 |
|
---|
774 | /*
|
---|
775 | * Do the job.
|
---|
776 | * Note that we're calling the os stuff while owning the semaphore here.
|
---|
777 | */
|
---|
778 | pThis->pSwitchPort = pSwitchPort;
|
---|
779 | rc = vboxNetFltOsConnectIt(pThis);
|
---|
780 | if (RT_SUCCESS(rc))
|
---|
781 | vboxNetFltSetState(pThis, kVBoxNetFltInsState_Connected);
|
---|
782 | else
|
---|
783 | pThis->pSwitchPort = NULL;
|
---|
784 |
|
---|
785 | Assert(!pThis->fActive);
|
---|
786 | return rc;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Creates a new instance.
|
---|
792 | *
|
---|
793 | * The new instance will be in the suspended state in a dynamic config and in
|
---|
794 | * the inactive in a static one.
|
---|
795 | *
|
---|
796 | * Called without owning the lock, but will request is several times.
|
---|
797 | *
|
---|
798 | * @returns VBox status code.
|
---|
799 | * @param pGlobals The globals.
|
---|
800 | * @param pszName The instance name.
|
---|
801 | * @param pSwitchPort The port on the switch that we're connected with (dynamic only).
|
---|
802 | * @param ppIfPort Where to store the pointer to our port interface (dynamic only).
|
---|
803 | */
|
---|
804 | static int vboxNetFltNewInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
805 | {
|
---|
806 | /*
|
---|
807 | * Allocate and initialize a new instance before requesting the mutex.
|
---|
808 | */
|
---|
809 | int rc;
|
---|
810 | size_t const cchName = strlen(pszName);
|
---|
811 | PVBOXNETFLTINS pNew = (PVBOXNETFLTINS)RTMemAllocZ(RT_OFFSETOF(VBOXNETFLTINS, szName[cchName + 1]));
|
---|
812 | if (!pNew)
|
---|
813 | return VERR_INTNET_FLT_IF_FAILED;
|
---|
814 | pNew->pNext = NULL;
|
---|
815 | pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
|
---|
816 | pNew->MyPort.pfnRetain = vboxNetFltPortRetain;
|
---|
817 | pNew->MyPort.pfnRelease = vboxNetFltPortRelease;
|
---|
818 | pNew->MyPort.pfnDisconnectAndRelease= vboxNetFltPortDisconnectAndRelease;
|
---|
819 | pNew->MyPort.pfnSetActive = vboxNetFltPortSetActive;
|
---|
820 | pNew->MyPort.pfnWaitForIdle = vboxNetFltPortWaitForIdle;
|
---|
821 | pNew->MyPort.pfnGetMacAddress = vboxNetFltPortGetMacAddress;
|
---|
822 | pNew->MyPort.pfnIsHostMac = vboxNetFltPortIsHostMac;
|
---|
823 | pNew->MyPort.pfnIsPromiscuous = vboxNetFltPortIsPromiscuous;
|
---|
824 | pNew->MyPort.pfnXmit = vboxNetFltPortXmit;
|
---|
825 | pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
|
---|
826 | pNew->pSwitchPort = NULL;
|
---|
827 | pNew->pGlobals = pGlobals;
|
---|
828 | pNew->hSpinlock = NIL_RTSPINLOCK;
|
---|
829 | pNew->enmState = kVBoxNetFltInsState_Initializing;
|
---|
830 | pNew->fActive = false;
|
---|
831 | pNew->fDisconnectedFromHost = false;
|
---|
832 | pNew->fRediscoveryPending = false;
|
---|
833 | pNew->NanoTSLastRediscovery = INT64_MAX;
|
---|
834 | pNew->cRefs = 1;
|
---|
835 | pNew->cBusy = 0;
|
---|
836 | pNew->hEventIdle = NIL_RTSEMEVENT;
|
---|
837 | memcpy(pNew->szName, pszName, cchName + 1);
|
---|
838 |
|
---|
839 | rc = RTSpinlockCreate(&pNew->hSpinlock);
|
---|
840 | if (RT_SUCCESS(rc))
|
---|
841 | {
|
---|
842 | rc = RTSemEventCreate(&pNew->hEventIdle);
|
---|
843 | if (RT_SUCCESS(rc))
|
---|
844 | {
|
---|
845 | rc = vboxNetFltOsPreInitInstance(pNew);
|
---|
846 | if (RT_SUCCESS(rc))
|
---|
847 | {
|
---|
848 | /*
|
---|
849 | * Insert the instance into the chain, checking for
|
---|
850 | * duplicates first of course (race).
|
---|
851 | */
|
---|
852 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
853 | if (RT_SUCCESS(rc))
|
---|
854 | {
|
---|
855 | if (!vboxNetFltFindInstanceLocked(pGlobals, pszName))
|
---|
856 | {
|
---|
857 | pNew->pNext = pGlobals->pInstanceHead;
|
---|
858 | pGlobals->pInstanceHead = pNew;
|
---|
859 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
860 |
|
---|
861 | /*
|
---|
862 | * Call the OS specific initialization code.
|
---|
863 | */
|
---|
864 | rc = vboxNetFltOsInitInstance(pNew);
|
---|
865 | RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
866 | if (RT_SUCCESS(rc))
|
---|
867 | {
|
---|
868 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
869 | pNew->enmState = kVBoxNetFltInsState_Inactive;
|
---|
870 | Assert(!pSwitchPort); Assert(!ppIfPort);
|
---|
871 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
872 | return rc;
|
---|
873 |
|
---|
874 | #else
|
---|
875 | /*
|
---|
876 | * Connect it as well, the OS specific bits has to be done outside
|
---|
877 | * the lock as they may call back to into intnet.
|
---|
878 | */
|
---|
879 | rc = vboxNetFltConnectIt(pNew, pSwitchPort, ppIfPort);
|
---|
880 | if (RT_SUCCESS(rc))
|
---|
881 | {
|
---|
882 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
883 | *ppIfPort = &pNew->MyPort;
|
---|
884 | return rc;
|
---|
885 | }
|
---|
886 |
|
---|
887 | /* Bail out (failed). */
|
---|
888 | vboxNetFltOsDeleteInstance(pNew);
|
---|
889 | #endif
|
---|
890 | }
|
---|
891 | vboxNetFltUnlinkLocked(pGlobals, pNew);
|
---|
892 | }
|
---|
893 | else
|
---|
894 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
895 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
896 | }
|
---|
897 | }
|
---|
898 | RTSemEventDestroy(pNew->hEventIdle);
|
---|
899 | }
|
---|
900 | RTSpinlockDestroy(pNew->hSpinlock);
|
---|
901 | }
|
---|
902 |
|
---|
903 | RTMemFree(pNew);
|
---|
904 | return rc;
|
---|
905 | }
|
---|
906 |
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
|
---|
910 | */
|
---|
911 | static DECLCALLBACK(int) vboxNetFltFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
|
---|
912 | PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
|
---|
913 | {
|
---|
914 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
915 | PVBOXNETFLTINS pCur;
|
---|
916 | int rc;
|
---|
917 |
|
---|
918 | LogFlow(("vboxNetFltFactoryCreateAndConnect: pszName=%p:{%s}\n", pszName, pszName));
|
---|
919 | Assert(pGlobals->cFactoryRefs > 0);
|
---|
920 |
|
---|
921 | /*
|
---|
922 | * Static: Find instance, check if busy, connect if not.
|
---|
923 | * Dynamic: Check for duplicate / busy interface instance.
|
---|
924 | */
|
---|
925 | rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
926 | AssertRCReturn(rc, rc);
|
---|
927 |
|
---|
928 | pCur = vboxNetFltFindInstanceLocked(pGlobals, pszName);
|
---|
929 | if (pCur)
|
---|
930 | {
|
---|
931 | rc = VERR_INTNET_FLT_IF_BUSY;
|
---|
932 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
933 | if (vboxNetFltGetState(pCur) == kVBoxNetFltInsState_Unconnected)
|
---|
934 | rc = vboxNetFltConnectIt(pCur, pSwitchPort, ppIfPort);
|
---|
935 | #endif
|
---|
936 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
937 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
938 | return rc;
|
---|
939 | }
|
---|
940 |
|
---|
941 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
942 |
|
---|
943 | #ifdef VBOXNETFLT_STATIC_CONFIG
|
---|
944 | rc = VERR_INTNET_FLT_IF_NOT_FOUND;
|
---|
945 | #else
|
---|
946 | /*
|
---|
947 | * Dynamically create a new instance.
|
---|
948 | */
|
---|
949 | rc = vboxNetFltNewInstance(pGlobals, pszName, pSwitchPort, ppIfPort);
|
---|
950 | #endif
|
---|
951 | LogFlow(("vboxNetFltFactoryCreateAndConnect: returns %Rrc\n", rc));
|
---|
952 | return rc;
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * @copydoc INTNETTRUNKFACTORY::pfnRelease
|
---|
958 | */
|
---|
959 | static DECLCALLBACK(void) vboxNetFltFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
|
---|
960 | {
|
---|
961 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, TrunkFactory));
|
---|
962 |
|
---|
963 | int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
|
---|
964 | Assert(cRefs >= 0); NOREF(cRefs);
|
---|
965 | LogFlow(("vboxNetFltFactoryRelease: cRefs=%d (new)\n", cRefs));
|
---|
966 | }
|
---|
967 |
|
---|
968 |
|
---|
969 | /**
|
---|
970 | * Implements the SUPDRV component factor interface query method.
|
---|
971 | *
|
---|
972 | * @returns Pointer to an interface. NULL if not supported.
|
---|
973 | *
|
---|
974 | * @param pSupDrvFactory Pointer to the componet factory registration structure.
|
---|
975 | * @param pSession The session - unused.
|
---|
976 | * @param pszInterfaceUuid The factory interface id.
|
---|
977 | */
|
---|
978 | static DECLCALLBACK(void *) vboxNetFltQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
|
---|
979 | {
|
---|
980 | PVBOXNETFLTGLOBALS pGlobals = (PVBOXNETFLTGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETFLTGLOBALS, SupDrvFactory));
|
---|
981 |
|
---|
982 | /*
|
---|
983 | * Convert the UUID strings and compare them.
|
---|
984 | */
|
---|
985 | RTUUID UuidReq;
|
---|
986 | int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
|
---|
987 | if (RT_SUCCESS(rc))
|
---|
988 | {
|
---|
989 | if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
|
---|
990 | {
|
---|
991 | ASMAtomicIncS32(&pGlobals->cFactoryRefs);
|
---|
992 | return &pGlobals->TrunkFactory;
|
---|
993 | }
|
---|
994 | #ifdef LOG_ENABLED
|
---|
995 | /* log legacy queries */
|
---|
996 | /* else if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_V1_UUID_STR))
|
---|
997 | Log(("VBoxNetFlt: V1 factory query\n"));
|
---|
998 | */
|
---|
999 | else
|
---|
1000 | Log(("VBoxNetFlt: unknown factory interface query (%s)\n", pszInterfaceUuid));
|
---|
1001 | #endif
|
---|
1002 | }
|
---|
1003 | else
|
---|
1004 | Log(("VBoxNetFlt: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
|
---|
1005 |
|
---|
1006 | return NULL;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * Checks whether the VBoxNetFlt wossname can be unloaded.
|
---|
1012 | *
|
---|
1013 | * This will return false if someone is currently using the module.
|
---|
1014 | *
|
---|
1015 | * @returns true if it's relatively safe to unload it, otherwise false.
|
---|
1016 | * @param pGlobals Pointer to the globals.
|
---|
1017 | */
|
---|
1018 | DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1019 | {
|
---|
1020 | int rc = RTSemFastMutexRequest(pGlobals->hFastMtx);
|
---|
1021 | bool fRc = !pGlobals->pInstanceHead
|
---|
1022 | && pGlobals->cFactoryRefs <= 0;
|
---|
1023 | RTSemFastMutexRelease(pGlobals->hFastMtx);
|
---|
1024 | AssertRC(rc);
|
---|
1025 | return fRc;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 |
|
---|
1029 | /**
|
---|
1030 | * Called by the native part when the OS wants the driver to unload.
|
---|
1031 | *
|
---|
1032 | * @returns VINF_SUCCESS on succes, VERR_WRONG_ORDER if we're busy.
|
---|
1033 | *
|
---|
1034 | * @param pGlobals Pointer to the globals.
|
---|
1035 | */
|
---|
1036 | DECLHIDDEN(int) vboxNetFltTryDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1037 | {
|
---|
1038 | int rc;
|
---|
1039 | Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
|
---|
1040 |
|
---|
1041 | /*
|
---|
1042 | * Check before trying to deregister the factory.
|
---|
1043 | */
|
---|
1044 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1045 | return VERR_WRONG_ORDER;
|
---|
1046 |
|
---|
1047 | /*
|
---|
1048 | * Disconnect from SUPDRV and check that nobody raced us,
|
---|
1049 | * reconnect if that should happen.
|
---|
1050 | */
|
---|
1051 | rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1052 | AssertRC(rc);
|
---|
1053 | if (!vboxNetFltCanUnload(pGlobals))
|
---|
1054 | {
|
---|
1055 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1056 | AssertRC(rc);
|
---|
1057 | return VERR_WRONG_ORDER;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1061 |
|
---|
1062 | /*
|
---|
1063 | * Release resources.
|
---|
1064 | */
|
---|
1065 | RTSemFastMutexDestroy(pGlobals->hFastMtx);
|
---|
1066 | pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
|
---|
1067 |
|
---|
1068 | return VINF_SUCCESS;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 |
|
---|
1072 | /**
|
---|
1073 | * Called by the native driver/kext module initialization routine.
|
---|
1074 | *
|
---|
1075 | * It will initialize the common parts of the globals, assuming the caller
|
---|
1076 | * has already taken care of the OS specific bits.
|
---|
1077 | *
|
---|
1078 | * @returns VBox status code.
|
---|
1079 | * @param pGlobals Pointer to the globals.
|
---|
1080 | */
|
---|
1081 | DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals)
|
---|
1082 | {
|
---|
1083 | /*
|
---|
1084 | * Initialize the common portions of the structure.
|
---|
1085 | */
|
---|
1086 | int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
|
---|
1087 | if (RT_SUCCESS(rc))
|
---|
1088 | {
|
---|
1089 | pGlobals->pInstanceHead = NULL;
|
---|
1090 |
|
---|
1091 | pGlobals->TrunkFactory.pfnRelease = vboxNetFltFactoryRelease;
|
---|
1092 | pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetFltFactoryCreateAndConnect;
|
---|
1093 |
|
---|
1094 | strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetFlt");
|
---|
1095 | pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetFltQueryFactoryInterface;
|
---|
1096 |
|
---|
1097 | /*
|
---|
1098 | * Establish a connection to SUPDRV and register our component factory.
|
---|
1099 | */
|
---|
1100 | rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
|
---|
1101 | if (RT_SUCCESS(rc))
|
---|
1102 | {
|
---|
1103 | rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
|
---|
1104 | if (RT_SUCCESS(rc))
|
---|
1105 | {
|
---|
1106 | Log(("VBoxNetFlt: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
|
---|
1107 | return rc;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /* bail out. */
|
---|
1111 | LogRel(("VBoxNetFlt: Failed to register component factory, rc=%Rrc\n", rc));
|
---|
1112 | SUPR0IdcClose(&pGlobals->SupDrvIDC);
|
---|
1113 | }
|
---|
1114 | RTSemFastMutexDestroy(pGlobals->hFastMtx);
|
---|
1115 | pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | return rc;
|
---|
1119 | }
|
---|
1120 |
|
---|