VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/drv/VBoxNetLwf-win.cpp@ 62714

Last change on this file since 62714 was 62714, checked in by vboxsync, 8 years ago

HostDrivers: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 115.0 KB
Line 
1/* $Id: VBoxNetLwf-win.cpp 62714 2016-07-29 21:41:09Z vboxsync $ */
2/** @file
3 * VBoxNetLwf-win.cpp - NDIS6 Bridged Networking Driver, Windows-specific code.
4 */
5/*
6 * Copyright (C) 2014-2016 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
17
18/*
19 * If VBOXNETLWF_SYNC_SEND is defined we won't allocate data buffers, but use
20 * the original buffers coming from IntNet to build MDLs around them. This
21 * also means that we need to wait for send operation to complete before
22 * returning the buffers, which hinders performance way too much.
23 */
24//#define VBOXNETLWF_SYNC_SEND
25
26/*
27 * If VBOXNETLWF_FIXED_SIZE_POOLS is defined we pre-allocate data buffers of
28 * fixed size in five pools. Each pool uses different size to accomodate packets
29 * of various sizes. We allocate these buffers once and re-use them when send
30 * operation is complete.
31 * If VBOXNETLWF_FIXED_SIZE_POOLS is not defined we allocate data buffers before
32 * each send operation and free then upon completion.
33 */
34#define VBOXNETLWF_FIXED_SIZE_POOLS
35
36/*
37 * Don't ask me why it is 42. Empirically this is what goes down the stack.
38 * OTOH, as we know from trustworthy sources, 42 is the answer, so be it.
39 */
40#define VBOXNETLWF_MAX_FRAME_SIZE(mtu) (mtu + 42)
41
42#include <VBox/version.h>
43#include <VBox/err.h>
44#include <iprt/initterm.h>
45#include <iprt/net.h>
46#include <iprt/list.h>
47#include <VBox/intnetinline.h>
48
49#include <iprt/nt/ntddk.h>
50#include <iprt/nt/ndis.h>
51#include <iprt/win/netioapi.h>
52#include <mstcpip.h>
53
54#define LogError(x) DbgPrint x
55
56#if 0
57#undef Log
58#define Log(x) DbgPrint x
59#undef LogFlow
60#define LogFlow(x) DbgPrint x
61#endif
62
63/** We have an entirely different structure than the one defined in VBoxNetFltCmn-win.h */
64typedef struct VBOXNETFLTWIN
65{
66 /** filter module context handle */
67 NDIS_HANDLE hModuleCtx;
68 /** IP address change notifier handle */
69 HANDLE hNotifier; /* Must be here as hModuleCtx may already be NULL when vboxNetFltOsDeleteInstance is called */
70} VBOXNETFLTWIN, *PVBOXNETFLTWIN;
71#define VBOXNETFLT_NO_PACKET_QUEUE
72#define VBOXNETFLT_OS_SPECFIC 1
73#include "VBoxNetFltInternal.h"
74
75#include "VBoxNetLwf-win.h"
76#include "VBox/VBoxNetCmn-win.h"
77
78typedef enum {
79 LwfState_Detached = 0,
80 LwfState_Attaching,
81 LwfState_Paused,
82 LwfState_Restarting,
83 LwfState_Running,
84 LwfState_Pausing,
85 LwfState_32BitHack = 0x7fffffff
86} VBOXNETLWFSTATE;
87
88/*
89 * Valid state transitions are:
90 * 1) Disconnected -> Connecting : start the worker thread, attempting to init IDC;
91 * 2) Connecting -> Disconnected : failed to start IDC init worker thread;
92 * 3) Connecting -> Connected : IDC init successful, terminate the worker;
93 * 4) Connecting -> Stopping : IDC init incomplete, but the driver is being unloaded, terminate the worker;
94 * 5) Connected -> Stopping : IDC init was successful, no worker, the driver is being unloaded;
95 *
96 * Driver terminates in Stopping state.
97 */
98typedef enum {
99 LwfIdcState_Disconnected = 0, /* Initial state */
100 LwfIdcState_Connecting, /* Attemping to init IDC, worker thread running */
101 LwfIdcState_Connected, /* Successfully connected to IDC, worker thread terminated */
102 LwfIdcState_Stopping /* Terminating the worker thread and disconnecting IDC */
103} VBOXNETLWFIDCSTATE;
104
105struct _VBOXNETLWF_MODULE;
106
107typedef struct VBOXNETLWFGLOBALS
108{
109 /** synch event used for device creation synchronization */
110 //KEVENT SynchEvent;
111 /** Device reference count */
112 //int cDeviceRefs;
113 /** ndis device */
114 NDIS_HANDLE hDevice;
115 /** device object */
116 PDEVICE_OBJECT pDevObj;
117 /** our filter driver handle */
118 NDIS_HANDLE hFilterDriver;
119 /** lock protecting the module list */
120 NDIS_SPIN_LOCK Lock;
121 /** the head of module list */
122 RTLISTANCHOR listModules;
123 /** IDC initialization state */
124 volatile uint32_t enmIdcState;
125 /** IDC init thread handle */
126 HANDLE hInitIdcThread;
127} VBOXNETLWFGLOBALS, *PVBOXNETLWFGLOBALS;
128
129/**
130 * The (common) global data.
131 */
132static VBOXNETFLTGLOBALS g_VBoxNetFltGlobals;
133/* win-specific global data */
134VBOXNETLWFGLOBALS g_VBoxNetLwfGlobals;
135
136#ifdef VBOXNETLWF_FIXED_SIZE_POOLS
137static ULONG g_cbPool[] = { 576+56, 1556, 4096+56, 6192+56, 9056 };
138#endif /* VBOXNETLWF_FIXED_SIZE_POOLS */
139
140typedef struct _VBOXNETLWF_MODULE {
141 RTLISTNODE node;
142
143 NDIS_HANDLE hFilter;
144#ifndef VBOXNETLWF_FIXED_SIZE_POOLS
145 NDIS_HANDLE hPool;
146#else /* VBOXNETLWF_FIXED_SIZE_POOLS */
147 NDIS_HANDLE hPool[RT_ELEMENTS(g_cbPool)];
148#endif /* VBOXNETLWF_FIXED_SIZE_POOLS */
149 PVBOXNETLWFGLOBALS pGlobals;
150 /** Associated instance of NetFlt, one-to-one relationship */
151 PVBOXNETFLTINS pNetFlt; /// @todo Consider automic access!
152 /** Module state as described in http://msdn.microsoft.com/en-us/library/windows/hardware/ff550017(v=vs.85).aspx */
153 volatile uint32_t enmState; /* No lock needed yet, atomic should suffice. */
154 /** Mutex to prevent pausing while transmitting on behalf of NetFlt */
155 NDIS_MUTEX InTransmit;
156#ifdef VBOXNETLWF_SYNC_SEND
157 /** Event signalled when sending to the wire is complete */
158 KEVENT EventWire;
159 /** Event signalled when NDIS returns our receive notification */
160 KEVENT EventHost;
161#else /* !VBOXNETLWF_SYNC_SEND */
162 /** Event signalled when all pending sends (both to wire and host) have completed */
163 NDIS_EVENT EventSendComplete;
164 /** Counter for pending sends (both to wire and host) */
165 int32_t cPendingBuffers;
166 /** Work Item to deliver offloading indications at passive IRQL */
167 NDIS_HANDLE hWorkItem;
168#endif /* !VBOXNETLWF_SYNC_SEND */
169 /** MAC address of underlying adapter */
170 RTMAC MacAddr;
171 /** Saved offload configuration */
172 NDIS_OFFLOAD SavedOffloadConfig;
173 /** the cloned request we have passed down */
174 PNDIS_OID_REQUEST pPendingRequest;
175 /** true if the underlying miniport supplied offloading config */
176 bool fOffloadConfigValid;
177 /** true if the trunk expects data from us */
178 bool fActive;
179 /** true if the host wants the adapter to be in promisc mode */
180 bool fHostPromisc;
181 /** Name of underlying adapter */
182 char szMiniportName[1];
183} VBOXNETLWF_MODULE;
184typedef VBOXNETLWF_MODULE *PVBOXNETLWF_MODULE;
185
186/*
187 * A structure to wrap OID requests in.
188 */
189typedef struct _VBOXNETLWF_OIDREQ {
190 NDIS_OID_REQUEST Request;
191 NDIS_STATUS Status;
192 NDIS_EVENT Event;
193} VBOXNETLWF_OIDREQ;
194typedef VBOXNETLWF_OIDREQ *PVBOXNETLWF_OIDREQ;
195
196
197/*********************************************************************************************************************************
198* Internal Functions *
199*********************************************************************************************************************************/
200static FILTER_ATTACH vboxNetLwfWinAttach;
201static FILTER_DETACH vboxNetLwfWinDetach;
202static FILTER_RESTART vboxNetLwfWinRestart;
203static FILTER_PAUSE vboxNetLwfWinPause;
204static FILTER_OID_REQUEST vboxNetLwfWinOidRequest;
205static FILTER_OID_REQUEST_COMPLETE vboxNetLwfWinOidRequestComplete;
206//static FILTER_CANCEL_OID_REQUEST vboxNetLwfWinCancelOidRequest;
207static FILTER_STATUS vboxNetLwfWinStatus;
208//static FILTER_NET_PNP_EVENT vboxNetLwfWinPnPEvent;
209static FILTER_SEND_NET_BUFFER_LISTS vboxNetLwfWinSendNetBufferLists;
210static FILTER_SEND_NET_BUFFER_LISTS_COMPLETE vboxNetLwfWinSendNetBufferListsComplete;
211static FILTER_RECEIVE_NET_BUFFER_LISTS vboxNetLwfWinReceiveNetBufferLists;
212static FILTER_RETURN_NET_BUFFER_LISTS vboxNetLwfWinReturnNetBufferLists;
213static KSTART_ROUTINE vboxNetLwfWinInitIdcWorker;
214
215static VOID vboxNetLwfWinUnloadDriver(IN PDRIVER_OBJECT pDriver);
216static int vboxNetLwfWinInitBase(void);
217static int vboxNetLwfWinFini(void);
218
219
220
221/**
222 * Logs an error to the system event log.
223 *
224 * @param ErrCode Error to report to event log.
225 * @param ReturnedStatus Error that was reported by the driver to the caller.
226 * @param uErrId Unique error id representing the location in the driver.
227 * @param cbDumpData Number of bytes at pDumpData.
228 * @param pDumpData Pointer to data that will be added to the message (see 'details' tab).
229 */
230static void vboxNetLwfLogErrorEvent(NTSTATUS uErrCode, NTSTATUS uReturnedStatus, ULONG uErrId)
231{
232 /* Figure out how many modules are attached and if they are going to fit into the dump data. */
233 unsigned cMaxModules = (ERROR_LOG_MAXIMUM_SIZE - FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData)) / sizeof(RTMAC);
234 unsigned cModules = 0;
235 PVBOXNETLWF_MODULE pModuleCtx = NULL;
236 NdisAcquireSpinLock(&g_VBoxNetLwfGlobals.Lock);
237 RTListForEach(&g_VBoxNetLwfGlobals.listModules, pModuleCtx, VBOXNETLWF_MODULE, node)
238 ++cModules;
239 NdisReleaseSpinLock(&g_VBoxNetLwfGlobals.Lock);
240 /* Prevent overflow */
241 if (cModules > cMaxModules)
242 cModules = cMaxModules;
243
244 /* DumpDataSize must be a multiple of sizeof(ULONG). */
245 unsigned cbDumpData = (cModules * sizeof(RTMAC) + 3) & ~3;
246 /* Prevent underflow */
247 unsigned cbTotal = RT_MAX(FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData) + cbDumpData,
248 sizeof(IO_ERROR_LOG_PACKET));
249
250 PIO_ERROR_LOG_PACKET pErrEntry;
251 pErrEntry = (PIO_ERROR_LOG_PACKET)IoAllocateErrorLogEntry(g_VBoxNetLwfGlobals.pDevObj,
252 (UCHAR)cbTotal);
253 if (pErrEntry)
254 {
255 PRTMAC pDump = (PRTMAC)pErrEntry->DumpData;
256 /*
257 * Initialize the whole structure with zeros in case we are suddenly short
258 * of data because the list is empty or has become smaller.
259 */
260 memset(pErrEntry, 0, cbTotal);
261
262 NdisAcquireSpinLock(&g_VBoxNetLwfGlobals.Lock);
263 RTListForEach(&g_VBoxNetLwfGlobals.listModules, pModuleCtx, VBOXNETLWF_MODULE, node)
264 {
265 /* The list could have been modified while we were allocating the entry, rely on cModules instead! */
266 if (cModules-- == 0)
267 break;
268 *pDump++ = pModuleCtx->MacAddr;
269 }
270 NdisReleaseSpinLock(&g_VBoxNetLwfGlobals.Lock);
271
272 pErrEntry->DumpDataSize = cbDumpData;
273 pErrEntry->ErrorCode = uErrCode;
274 pErrEntry->UniqueErrorValue = uErrId;
275 pErrEntry->FinalStatus = uReturnedStatus;
276 IoWriteErrorLogEntry(pErrEntry);
277 }
278 else
279 {
280 DbgPrint("Failed to allocate error log entry (cb=%u)\n", cbTotal);
281 }
282}
283
284#ifdef DEBUG
285
286static const char *vboxNetLwfWinStatusToText(NDIS_STATUS code)
287{
288 switch (code)
289 {
290 case NDIS_STATUS_MEDIA_CONNECT: return "NDIS_STATUS_MEDIA_CONNECT";
291 case NDIS_STATUS_MEDIA_DISCONNECT: return "NDIS_STATUS_MEDIA_DISCONNECT";
292 case NDIS_STATUS_RESET_START: return "NDIS_STATUS_RESET_START";
293 case NDIS_STATUS_RESET_END: return "NDIS_STATUS_RESET_END";
294 case NDIS_STATUS_MEDIA_BUSY: return "NDIS_STATUS_MEDIA_BUSY";
295 case NDIS_STATUS_MEDIA_SPECIFIC_INDICATION: return "NDIS_STATUS_MEDIA_SPECIFIC_INDICATION";
296 case NDIS_STATUS_LINK_SPEED_CHANGE: return "NDIS_STATUS_LINK_SPEED_CHANGE";
297 case NDIS_STATUS_LINK_STATE: return "NDIS_STATUS_LINK_STATE";
298 case NDIS_STATUS_PORT_STATE: return "NDIS_STATUS_PORT_STATE";
299 case NDIS_STATUS_OPER_STATUS: return "NDIS_STATUS_OPER_STATUS";
300 case NDIS_STATUS_NETWORK_CHANGE: return "NDIS_STATUS_NETWORK_CHANGE";
301 case NDIS_STATUS_PACKET_FILTER: return "NDIS_STATUS_PACKET_FILTER";
302 case NDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG: return "NDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG";
303 case NDIS_STATUS_TASK_OFFLOAD_HARDWARE_CAPABILITIES: return "NDIS_STATUS_TASK_OFFLOAD_HARDWARE_CAPABILITIES";
304 case NDIS_STATUS_OFFLOAD_ENCASPULATION_CHANGE: return "NDIS_STATUS_OFFLOAD_ENCASPULATION_CHANGE";
305 case NDIS_STATUS_TCP_CONNECTION_OFFLOAD_HARDWARE_CAPABILITIES: return "NDIS_STATUS_TCP_CONNECTION_OFFLOAD_HARDWARE_CAPABILITIES";
306 }
307 return "unknown";
308}
309
310static void vboxNetLwfWinDumpFilterTypes(ULONG uFlags)
311{
312 if (uFlags & NDIS_PACKET_TYPE_DIRECTED) Log5((" NDIS_PACKET_TYPE_DIRECTED\n"));
313 if (uFlags & NDIS_PACKET_TYPE_MULTICAST) Log5((" NDIS_PACKET_TYPE_MULTICAST\n"));
314 if (uFlags & NDIS_PACKET_TYPE_ALL_MULTICAST) Log5((" NDIS_PACKET_TYPE_ALL_MULTICAST\n"));
315 if (uFlags & NDIS_PACKET_TYPE_BROADCAST) Log5((" NDIS_PACKET_TYPE_BROADCAST\n"));
316 if (uFlags & NDIS_PACKET_TYPE_PROMISCUOUS) Log5((" NDIS_PACKET_TYPE_PROMISCUOUS\n"));
317 if (uFlags & NDIS_PACKET_TYPE_ALL_FUNCTIONAL) Log5((" NDIS_PACKET_TYPE_ALL_FUNCTIONAL\n"));
318 if (uFlags & NDIS_PACKET_TYPE_ALL_LOCAL) Log5((" NDIS_PACKET_TYPE_ALL_LOCAL\n"));
319 if (uFlags & NDIS_PACKET_TYPE_FUNCTIONAL) Log5((" NDIS_PACKET_TYPE_FUNCTIONAL\n"));
320 if (uFlags & NDIS_PACKET_TYPE_GROUP) Log5((" NDIS_PACKET_TYPE_GROUP\n"));
321 if (uFlags & NDIS_PACKET_TYPE_MAC_FRAME) Log5((" NDIS_PACKET_TYPE_MAC_FRAME\n"));
322 if (uFlags & NDIS_PACKET_TYPE_SMT) Log5((" NDIS_PACKET_TYPE_SMT\n"));
323 if (uFlags & NDIS_PACKET_TYPE_SOURCE_ROUTING) Log5((" NDIS_PACKET_TYPE_SOURCE_ROUTING\n"));
324 if (uFlags == 0) Log5((" NONE\n"));
325}
326
327DECLINLINE(void) vboxNetLwfWinDumpEncapsulation(const char *pcszText, ULONG uEncapsulation)
328{
329 if (uEncapsulation == NDIS_ENCAPSULATION_NOT_SUPPORTED)
330 Log5(("%s not supported\n", pcszText));
331 else
332 {
333 Log5(("%s", pcszText));
334 if (uEncapsulation & NDIS_ENCAPSULATION_NULL)
335 Log5((" null"));
336 if (uEncapsulation & NDIS_ENCAPSULATION_IEEE_802_3)
337 Log5((" 802.3"));
338 if (uEncapsulation & NDIS_ENCAPSULATION_IEEE_802_3_P_AND_Q)
339 Log5((" 802.3pq"));
340 if (uEncapsulation & NDIS_ENCAPSULATION_IEEE_802_3_P_AND_Q_IN_OOB)
341 Log5((" 802.3pq(oob)"));
342 if (uEncapsulation & NDIS_ENCAPSULATION_IEEE_LLC_SNAP_ROUTED)
343 Log5((" LLC"));
344 Log5(("\n"));
345 }
346}
347
348DECLINLINE(const char *) vboxNetLwfWinSetOnOffText(ULONG uOnOff)
349{
350 switch (uOnOff)
351 {
352 case NDIS_OFFLOAD_SET_NO_CHANGE: return "no change";
353 case NDIS_OFFLOAD_SET_ON: return "on";
354 case NDIS_OFFLOAD_SET_OFF: return "off";
355 }
356 return "unknown";
357}
358
359DECLINLINE(const char *) vboxNetLwfWinOnOffText(ULONG uOnOff)
360{
361 switch (uOnOff)
362 {
363 case NDIS_OFFLOAD_NOT_SUPPORTED: return "off";
364 case NDIS_OFFLOAD_SUPPORTED: return "on";
365 }
366 return "unknown";
367}
368
369DECLINLINE(const char *) vboxNetLwfWinSupportedText(ULONG uSupported)
370{
371 switch (uSupported)
372 {
373 case NDIS_OFFLOAD_NOT_SUPPORTED: return "not supported";
374 case NDIS_OFFLOAD_SUPPORTED: return "supported";
375 }
376 return "unknown";
377}
378
379static void vboxNetLwfWinDumpSetOffloadSettings(PNDIS_OFFLOAD pOffloadConfig)
380{
381 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv4Transmit.Encapsulation =", pOffloadConfig->Checksum.IPv4Transmit.Encapsulation);
382 Log5((" Checksum.IPv4Transmit.IpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Transmit.IpOptionsSupported)));
383 Log5((" Checksum.IPv4Transmit.TcpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Transmit.TcpOptionsSupported)));
384 Log5((" Checksum.IPv4Transmit.TcpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Transmit.TcpChecksum)));
385 Log5((" Checksum.IPv4Transmit.UdpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Transmit.UdpChecksum)));
386 Log5((" Checksum.IPv4Transmit.IpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Transmit.IpChecksum)));
387 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv4Receive.Encapsulation =", pOffloadConfig->Checksum.IPv4Receive.Encapsulation);
388 Log5((" Checksum.IPv4Receive.IpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Receive.IpOptionsSupported)));
389 Log5((" Checksum.IPv4Receive.TcpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Receive.TcpOptionsSupported)));
390 Log5((" Checksum.IPv4Receive.TcpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Receive.TcpChecksum)));
391 Log5((" Checksum.IPv4Receive.UdpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Receive.UdpChecksum)));
392 Log5((" Checksum.IPv4Receive.IpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv4Receive.IpChecksum)));
393 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv6Transmit.Encapsulation =", pOffloadConfig->Checksum.IPv6Transmit.Encapsulation);
394 Log5((" Checksum.IPv6Transmit.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Transmit.IpExtensionHeadersSupported)));
395 Log5((" Checksum.IPv6Transmit.TcpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Transmit.TcpOptionsSupported)));
396 Log5((" Checksum.IPv6Transmit.TcpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Transmit.TcpChecksum)));
397 Log5((" Checksum.IPv6Transmit.UdpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Transmit.UdpChecksum)));
398 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv6Receive.Encapsulation =", pOffloadConfig->Checksum.IPv6Receive.Encapsulation);
399 Log5((" Checksum.IPv6Receive.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Receive.IpExtensionHeadersSupported)));
400 Log5((" Checksum.IPv6Receive.TcpOptionsSupported = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Receive.TcpOptionsSupported)));
401 Log5((" Checksum.IPv6Receive.TcpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Receive.TcpChecksum)));
402 Log5((" Checksum.IPv6Receive.UdpChecksum = %s\n", vboxNetLwfWinSetOnOffText(pOffloadConfig->Checksum.IPv6Receive.UdpChecksum)));
403 vboxNetLwfWinDumpEncapsulation(" LsoV1.IPv4.Encapsulation =", pOffloadConfig->LsoV1.IPv4.Encapsulation);
404 Log5((" LsoV1.IPv4.TcpOptions = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV1.IPv4.TcpOptions)));
405 Log5((" LsoV1.IPv4.IpOptions = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV1.IPv4.IpOptions)));
406 vboxNetLwfWinDumpEncapsulation(" LsoV2.IPv4.Encapsulation =", pOffloadConfig->LsoV2.IPv4.Encapsulation);
407 vboxNetLwfWinDumpEncapsulation(" LsoV2.IPv6.Encapsulation =", pOffloadConfig->LsoV2.IPv6.Encapsulation);
408 Log5((" LsoV2.IPv6.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV2.IPv6.IpExtensionHeadersSupported)));
409 Log5((" LsoV2.IPv6.TcpOptionsSupported = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV2.IPv6.TcpOptionsSupported)));
410}
411
412static void vboxNetLwfWinDumpOffloadSettings(PNDIS_OFFLOAD pOffloadConfig)
413{
414 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv4Transmit.Encapsulation =", pOffloadConfig->Checksum.IPv4Transmit.Encapsulation);
415 Log5((" Checksum.IPv4Transmit.IpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Transmit.IpOptionsSupported)));
416 Log5((" Checksum.IPv4Transmit.TcpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Transmit.TcpOptionsSupported)));
417 Log5((" Checksum.IPv4Transmit.TcpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Transmit.TcpChecksum)));
418 Log5((" Checksum.IPv4Transmit.UdpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Transmit.UdpChecksum)));
419 Log5((" Checksum.IPv4Transmit.IpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Transmit.IpChecksum)));
420 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv4Receive.Encapsulation =", pOffloadConfig->Checksum.IPv4Receive.Encapsulation);
421 Log5((" Checksum.IPv4Receive.IpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Receive.IpOptionsSupported)));
422 Log5((" Checksum.IPv4Receive.TcpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Receive.TcpOptionsSupported)));
423 Log5((" Checksum.IPv4Receive.TcpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Receive.TcpChecksum)));
424 Log5((" Checksum.IPv4Receive.UdpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Receive.UdpChecksum)));
425 Log5((" Checksum.IPv4Receive.IpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv4Receive.IpChecksum)));
426 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv6Transmit.Encapsulation =", pOffloadConfig->Checksum.IPv6Transmit.Encapsulation);
427 Log5((" Checksum.IPv6Transmit.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Transmit.IpExtensionHeadersSupported)));
428 Log5((" Checksum.IPv6Transmit.TcpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Transmit.TcpOptionsSupported)));
429 Log5((" Checksum.IPv6Transmit.TcpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Transmit.TcpChecksum)));
430 Log5((" Checksum.IPv6Transmit.UdpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Transmit.UdpChecksum)));
431 vboxNetLwfWinDumpEncapsulation(" Checksum.IPv6Receive.Encapsulation =", pOffloadConfig->Checksum.IPv6Receive.Encapsulation);
432 Log5((" Checksum.IPv6Receive.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Receive.IpExtensionHeadersSupported)));
433 Log5((" Checksum.IPv6Receive.TcpOptionsSupported = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Receive.TcpOptionsSupported)));
434 Log5((" Checksum.IPv6Receive.TcpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Receive.TcpChecksum)));
435 Log5((" Checksum.IPv6Receive.UdpChecksum = %s\n", vboxNetLwfWinOnOffText(pOffloadConfig->Checksum.IPv6Receive.UdpChecksum)));
436 vboxNetLwfWinDumpEncapsulation(" LsoV1.IPv4.Encapsulation =", pOffloadConfig->LsoV1.IPv4.Encapsulation);
437 Log5((" LsoV1.IPv4.TcpOptions = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV1.IPv4.TcpOptions)));
438 Log5((" LsoV1.IPv4.IpOptions = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV1.IPv4.IpOptions)));
439 vboxNetLwfWinDumpEncapsulation(" LsoV2.IPv4.Encapsulation =", pOffloadConfig->LsoV2.IPv4.Encapsulation);
440 vboxNetLwfWinDumpEncapsulation(" LsoV2.IPv6.Encapsulation =", pOffloadConfig->LsoV2.IPv6.Encapsulation);
441 Log5((" LsoV2.IPv6.IpExtensionHeadersSupported = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV2.IPv6.IpExtensionHeadersSupported)));
442 Log5((" LsoV2.IPv6.TcpOptionsSupported = %s\n", vboxNetLwfWinSupportedText(pOffloadConfig->LsoV2.IPv6.TcpOptionsSupported)));
443}
444
445static const char *vboxNetLwfWinStateToText(uint32_t enmState)
446{
447 switch (enmState)
448 {
449 case LwfState_Detached: return "Detached";
450 case LwfState_Attaching: return "Attaching";
451 case LwfState_Paused: return "Paused";
452 case LwfState_Restarting: return "Restarting";
453 case LwfState_Running: return "Running";
454 case LwfState_Pausing: return "Pausing";
455 }
456 return "invalid";
457}
458
459static void vboxNetLwfWinDumpPackets(const char *pszMsg, PNET_BUFFER_LIST pBufLists)
460{
461 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = NET_BUFFER_LIST_NEXT_NBL(pList))
462 {
463 for (PNET_BUFFER pBuf = NET_BUFFER_LIST_FIRST_NB(pList); pBuf; pBuf = NET_BUFFER_NEXT_NB(pBuf))
464 {
465 Log6(("%s packet: cb=%d offset=%d", pszMsg, NET_BUFFER_DATA_LENGTH(pBuf), NET_BUFFER_DATA_OFFSET(pBuf)));
466 for (PMDL pMdl = NET_BUFFER_FIRST_MDL(pBuf);
467 pMdl != NULL;
468 pMdl = NDIS_MDL_LINKAGE(pMdl))
469 {
470 Log6((" MDL: cb=%d", MmGetMdlByteCount(pMdl)));
471 }
472 Log6(("\n"));
473 }
474 }
475}
476
477DECLINLINE(const char *) vboxNetLwfWinEthTypeStr(uint16_t uType)
478{
479 switch (uType)
480 {
481 case RTNET_ETHERTYPE_IPV4: return "IP";
482 case RTNET_ETHERTYPE_IPV6: return "IPv6";
483 case RTNET_ETHERTYPE_ARP: return "ARP";
484 }
485 return "unknown";
486}
487
488#define VBOXNETLWF_PKTDMPSIZE 0x50
489
490/**
491 * Dump a packet to debug log.
492 *
493 * @param cpPacket The packet.
494 * @param cb The size of the packet.
495 * @param cszText A string denoting direction of packet transfer.
496 */
497DECLINLINE(void) vboxNetLwfWinDumpPacket(PCINTNETSG pSG, const char *cszText)
498{
499 uint8_t bPacket[VBOXNETLWF_PKTDMPSIZE];
500
501 uint32_t cb = pSG->cbTotal < VBOXNETLWF_PKTDMPSIZE ? pSG->cbTotal : VBOXNETLWF_PKTDMPSIZE;
502 IntNetSgReadEx(pSG, 0, cb, bPacket);
503
504 AssertReturnVoid(cb >= 14);
505
506 uint8_t *pHdr = bPacket;
507 uint8_t *pEnd = bPacket + cb;
508 AssertReturnVoid(pEnd - pHdr >= 14);
509 uint16_t uEthType = RT_N2H_U16(*(uint16_t*)(pHdr+12));
510 Log2(("NetLWF: %s (%d bytes), %RTmac => %RTmac, EthType=%s(0x%x)\n",
511 cszText, pSG->cbTotal, pHdr+6, pHdr, vboxNetLwfWinEthTypeStr(uEthType), uEthType));
512 pHdr += sizeof(RTNETETHERHDR);
513 if (uEthType == RTNET_ETHERTYPE_VLAN)
514 {
515 AssertReturnVoid(pEnd - pHdr >= 4);
516 uEthType = RT_N2H_U16(*(uint16_t*)(pHdr+2));
517 Log2((" + VLAN: id=%d EthType=%s(0x%x)\n", RT_N2H_U16(*(uint16_t*)(pHdr)) & 0xFFF,
518 vboxNetLwfWinEthTypeStr(uEthType), uEthType));
519 pHdr += 2 * sizeof(uint16_t);
520 }
521 uint8_t uProto = 0xFF;
522 switch (uEthType)
523 {
524 case RTNET_ETHERTYPE_IPV6:
525 AssertReturnVoid(pEnd - pHdr >= 40);
526 uProto = pHdr[6];
527 Log2((" + IPv6: %RTnaipv6 => %RTnaipv6\n", pHdr+8, pHdr+24));
528 pHdr += 40;
529 break;
530 case RTNET_ETHERTYPE_IPV4:
531 AssertReturnVoid(pEnd - pHdr >= 20);
532 uProto = pHdr[9];
533 Log2((" + IP: %RTnaipv4 => %RTnaipv4\n", *(uint32_t*)(pHdr+12), *(uint32_t*)(pHdr+16)));
534 pHdr += (pHdr[0] & 0xF) * 4;
535 break;
536 case RTNET_ETHERTYPE_ARP:
537 AssertReturnVoid(pEnd - pHdr >= 28);
538 AssertReturnVoid(RT_N2H_U16(*(uint16_t*)(pHdr+2)) == RTNET_ETHERTYPE_IPV4);
539 switch (RT_N2H_U16(*(uint16_t*)(pHdr+6)))
540 {
541 case 1: /* ARP request */
542 Log2((" + ARP-REQ: who-has %RTnaipv4 tell %RTnaipv4\n",
543 *(uint32_t*)(pHdr+24), *(uint32_t*)(pHdr+14)));
544 break;
545 case 2: /* ARP reply */
546 Log2((" + ARP-RPL: %RTnaipv4 is-at %RTmac\n",
547 *(uint32_t*)(pHdr+14), pHdr+8));
548 break;
549 default:
550 Log2((" + ARP: unknown op %d\n", RT_N2H_U16(*(uint16_t*)(pHdr+6))));
551 break;
552 }
553 break;
554 /* There is no default case as uProto is initialized with 0xFF */
555 }
556 while (uProto != 0xFF)
557 {
558 switch (uProto)
559 {
560 case 0: /* IPv6 Hop-by-Hop option*/
561 case 60: /* IPv6 Destination option*/
562 case 43: /* IPv6 Routing option */
563 case 44: /* IPv6 Fragment option */
564 Log2((" + IPv6 option (%d): <not implemented>\n", uProto));
565 uProto = pHdr[0];
566 pHdr += pHdr[1] * 8 + 8; /* Skip to the next extension/protocol */
567 break;
568 case 51: /* IPv6 IPsec AH */
569 Log2((" + IPv6 IPsec AH: <not implemented>\n"));
570 uProto = pHdr[0];
571 pHdr += (pHdr[1] + 2) * 4; /* Skip to the next extension/protocol */
572 break;
573 case 50: /* IPv6 IPsec ESP */
574 /* Cannot decode IPsec, fall through */
575 Log2((" + IPv6 IPsec ESP: <not implemented>\n"));
576 uProto = 0xFF;
577 break;
578 case 59: /* No Next Header */
579 Log2((" + IPv6 No Next Header\n"));
580 uProto = 0xFF;
581 break;
582 case 58: /* IPv6-ICMP */
583 switch (pHdr[0])
584 {
585 case 1: Log2((" + IPv6-ICMP: destination unreachable, code %d\n", pHdr[1])); break;
586 case 128: Log2((" + IPv6-ICMP: echo request\n")); break;
587 case 129: Log2((" + IPv6-ICMP: echo reply\n")); break;
588 default: Log2((" + IPv6-ICMP: unknown type %d, code %d\n", pHdr[0], pHdr[1])); break;
589 }
590 uProto = 0xFF;
591 break;
592 case 1: /* ICMP */
593 switch (pHdr[0])
594 {
595 case 0: Log2((" + ICMP: echo reply\n")); break;
596 case 8: Log2((" + ICMP: echo request\n")); break;
597 case 3: Log2((" + ICMP: destination unreachable, code %d\n", pHdr[1])); break;
598 default: Log2((" + ICMP: unknown type %d, code %d\n", pHdr[0], pHdr[1])); break;
599 }
600 uProto = 0xFF;
601 break;
602 case 6: /* TCP */
603 Log2((" + TCP: src=%d dst=%d seq=%x ack=%x\n",
604 RT_N2H_U16(*(uint16_t*)(pHdr)), RT_N2H_U16(*(uint16_t*)(pHdr+2)),
605 RT_N2H_U32(*(uint32_t*)(pHdr+4)), RT_N2H_U32(*(uint32_t*)(pHdr+8))));
606 uProto = 0xFF;
607 break;
608 case 17: /* UDP */
609 Log2((" + UDP: src=%d dst=%d\n",
610 RT_N2H_U16(*(uint16_t*)(pHdr)), RT_N2H_U16(*(uint16_t*)(pHdr+2))));
611 uProto = 0xFF;
612 break;
613 default:
614 Log2((" + Unknown: proto=0x%x\n", uProto));
615 uProto = 0xFF;
616 break;
617 }
618 }
619 Log3(("%.*Rhxd\n", cb, bPacket));
620}
621
622#else /* !DEBUG */
623# define vboxNetLwfWinDumpFilterTypes(uFlags) do { } while (0)
624# define vboxNetLwfWinDumpOffloadSettings(p) do { } while (0)
625# define vboxNetLwfWinDumpSetOffloadSettings(p) do { } while (0)
626# define vboxNetLwfWinDumpPackets(m,l) do { } while (0)
627# define vboxNetLwfWinDumpPacket(p,t) do { } while (0)
628#endif /* !DEBUG */
629
630DECLINLINE(bool) vboxNetLwfWinChangeState(PVBOXNETLWF_MODULE pModuleCtx, uint32_t enmNew, uint32_t enmOld = LwfState_32BitHack)
631{
632 AssertReturn(pModuleCtx, false);
633
634 bool fSuccess = true;
635 if (enmOld != LwfState_32BitHack)
636 {
637 fSuccess = ASMAtomicCmpXchgU32(&pModuleCtx->enmState, enmNew, enmOld);
638 if (fSuccess)
639 Log(("vboxNetLwfWinChangeState: state change %s -> %s\n",
640 vboxNetLwfWinStateToText(enmOld),
641 vboxNetLwfWinStateToText(enmNew)));
642 else
643 Log(("ERROR! vboxNetLwfWinChangeState: failed state change %s (actual=%s) -> %s\n",
644 vboxNetLwfWinStateToText(enmOld),
645 vboxNetLwfWinStateToText(ASMAtomicReadU32(&pModuleCtx->enmState)),
646 vboxNetLwfWinStateToText(enmNew)));
647 Assert(fSuccess);
648 }
649 else
650 {
651 uint32_t enmPrevState = ASMAtomicXchgU32(&pModuleCtx->enmState, enmNew);
652 Log(("vboxNetLwfWinChangeState: state change %s -> %s\n",
653 vboxNetLwfWinStateToText(enmPrevState),
654 vboxNetLwfWinStateToText(enmNew)));
655 NOREF(enmPrevState);
656 }
657 return fSuccess;
658}
659
660DECLINLINE(void) vboxNetLwfWinInitOidRequest(PVBOXNETLWF_OIDREQ pRequest)
661{
662 NdisZeroMemory(pRequest, sizeof(VBOXNETLWF_OIDREQ));
663
664 NdisInitializeEvent(&pRequest->Event);
665
666 pRequest->Request.Header.Type = NDIS_OBJECT_TYPE_OID_REQUEST;
667 pRequest->Request.Header.Revision = NDIS_OID_REQUEST_REVISION_1;
668 pRequest->Request.Header.Size = NDIS_SIZEOF_OID_REQUEST_REVISION_1;
669
670 pRequest->Request.RequestId = (PVOID)VBOXNETLWF_REQ_ID;
671}
672
673static NDIS_STATUS vboxNetLwfWinSyncOidRequest(PVBOXNETLWF_MODULE pModuleCtx, PVBOXNETLWF_OIDREQ pRequest)
674{
675 NDIS_STATUS Status = NdisFOidRequest(pModuleCtx->hFilter, &pRequest->Request);
676 if (Status == NDIS_STATUS_PENDING)
677 {
678 NdisWaitEvent(&pRequest->Event, 0);
679 Status = pRequest->Status;
680 }
681 return Status;
682}
683
684DECLINLINE(void) vboxNetLwfWinCopyOidRequestResults(PNDIS_OID_REQUEST pFrom, PNDIS_OID_REQUEST pTo)
685{
686 switch (pFrom->RequestType)
687 {
688 case NdisRequestSetInformation:
689 pTo->DATA.SET_INFORMATION.BytesRead = pFrom->DATA.SET_INFORMATION.BytesRead;
690 pTo->DATA.SET_INFORMATION.BytesNeeded = pFrom->DATA.SET_INFORMATION.BytesNeeded;
691 break;
692 case NdisRequestMethod:
693 pTo->DATA.METHOD_INFORMATION.OutputBufferLength = pFrom->DATA.METHOD_INFORMATION.OutputBufferLength;
694 pTo->DATA.METHOD_INFORMATION.BytesWritten = pFrom->DATA.METHOD_INFORMATION.BytesWritten;
695 pTo->DATA.METHOD_INFORMATION.BytesRead = pFrom->DATA.METHOD_INFORMATION.BytesRead;
696 pTo->DATA.METHOD_INFORMATION.BytesNeeded = pFrom->DATA.METHOD_INFORMATION.BytesNeeded;
697 break;
698 case NdisRequestQueryInformation:
699 case NdisRequestQueryStatistics:
700 default:
701 pTo->DATA.QUERY_INFORMATION.BytesWritten = pFrom->DATA.QUERY_INFORMATION.BytesWritten;
702 pTo->DATA.QUERY_INFORMATION.BytesNeeded = pFrom->DATA.QUERY_INFORMATION.BytesNeeded;
703 }
704}
705
706void inline vboxNetLwfWinOverridePacketFiltersUp(PVBOXNETLWF_MODULE pModuleCtx, ULONG *pFilters)
707{
708 if (ASMAtomicReadBool(&pModuleCtx->fActive) && !ASMAtomicReadBool(&pModuleCtx->fHostPromisc))
709 *pFilters &= ~NDIS_PACKET_TYPE_PROMISCUOUS;
710}
711
712NDIS_STATUS vboxNetLwfWinOidRequest(IN NDIS_HANDLE hModuleCtx,
713 IN PNDIS_OID_REQUEST pOidRequest)
714{
715 LogFlow(("==>vboxNetLwfWinOidRequest: module=%p\n", hModuleCtx));
716 vboxNetCmnWinDumpOidRequest(__FUNCTION__, pOidRequest);
717 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
718 PNDIS_OID_REQUEST pClone = NULL;
719 NDIS_STATUS Status = NdisAllocateCloneOidRequest(pModuleCtx->hFilter,
720 pOidRequest,
721 VBOXNETLWF_MEM_TAG,
722 &pClone);
723 if (Status == NDIS_STATUS_SUCCESS)
724 {
725 /* Save the pointer to the original */
726 *((PNDIS_OID_REQUEST*)(pClone->SourceReserved)) = pOidRequest;
727
728 pClone->RequestId = pOidRequest->RequestId;
729 /* We are not supposed to get another request until we are through with the one we "postponed" */
730 PNDIS_OID_REQUEST pPrev = ASMAtomicXchgPtrT(&pModuleCtx->pPendingRequest, pClone, PNDIS_OID_REQUEST);
731 Assert(pPrev == NULL);
732 pModuleCtx->pPendingRequest = pClone;
733 if (pOidRequest->RequestType == NdisRequestSetInformation
734 && pOidRequest->DATA.SET_INFORMATION.Oid == OID_GEN_CURRENT_PACKET_FILTER)
735 {
736 ASMAtomicWriteBool(&pModuleCtx->fHostPromisc, !!(*(ULONG*)pOidRequest->DATA.SET_INFORMATION.InformationBuffer & NDIS_PACKET_TYPE_PROMISCUOUS));
737 Log(("vboxNetLwfWinOidRequest: host wanted to set packet filter value to:\n"));
738 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pOidRequest->DATA.SET_INFORMATION.InformationBuffer);
739 /* Keep adapter in promisc mode as long as we are active. */
740 if (ASMAtomicReadBool(&pModuleCtx->fActive))
741 *(ULONG*)pClone->DATA.SET_INFORMATION.InformationBuffer |= NDIS_PACKET_TYPE_PROMISCUOUS;
742 Log5(("vboxNetLwfWinOidRequest: pass the following packet filters to miniport:\n"));
743 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pOidRequest->DATA.SET_INFORMATION.InformationBuffer);
744 }
745 if (pOidRequest->RequestType == NdisRequestSetInformation
746 && pOidRequest->DATA.SET_INFORMATION.Oid == OID_TCP_OFFLOAD_CURRENT_CONFIG)
747 {
748 Log5(("vboxNetLwfWinOidRequest: offloading set to:\n"));
749 vboxNetLwfWinDumpSetOffloadSettings((PNDIS_OFFLOAD)pOidRequest->DATA.SET_INFORMATION.InformationBuffer);
750 }
751
752 /* Forward the clone to underlying filters/miniport */
753 Status = NdisFOidRequest(pModuleCtx->hFilter, pClone);
754 if (Status != NDIS_STATUS_PENDING)
755 {
756 /* Synchronous completion */
757 pPrev = ASMAtomicXchgPtrT(&pModuleCtx->pPendingRequest, NULL, PNDIS_OID_REQUEST);
758 Assert(pPrev == pClone);
759 Log5(("vboxNetLwfWinOidRequest: got the following packet filters from miniport:\n"));
760 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pOidRequest->DATA.QUERY_INFORMATION.InformationBuffer);
761 /*
762 * The host does not expect the adapter to be in promisc mode,
763 * unless it enabled the mode. Let's not disillusion it.
764 */
765 if ( pOidRequest->RequestType == NdisRequestQueryInformation
766 && pOidRequest->DATA.QUERY_INFORMATION.Oid == OID_GEN_CURRENT_PACKET_FILTER)
767 vboxNetLwfWinOverridePacketFiltersUp(pModuleCtx, (ULONG*)pOidRequest->DATA.QUERY_INFORMATION.InformationBuffer);
768 Log5(("vboxNetLwfWinOidRequest: reporting to the host the following packet filters:\n"));
769 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pOidRequest->DATA.QUERY_INFORMATION.InformationBuffer);
770 vboxNetLwfWinCopyOidRequestResults(pClone, pOidRequest);
771 NdisFreeCloneOidRequest(pModuleCtx->hFilter, pClone);
772 }
773 /* In case of async completion we do the rest in vboxNetLwfWinOidRequestComplete() */
774 }
775 else
776 {
777 LogError(("vboxNetLwfWinOidRequest: NdisAllocateCloneOidRequest failed with 0x%x\n", Status));
778 }
779 LogFlow(("<==vboxNetLwfWinOidRequest: Status=0x%x\n", Status));
780 return Status;
781}
782
783VOID vboxNetLwfWinOidRequestComplete(IN NDIS_HANDLE hModuleCtx,
784 IN PNDIS_OID_REQUEST pRequest,
785 IN NDIS_STATUS Status)
786{
787 LogFlow(("==>vboxNetLwfWinOidRequestComplete: module=%p req=%p status=0x%x\n", hModuleCtx, pRequest, Status));
788 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
789 PNDIS_OID_REQUEST pOriginal = *((PNDIS_OID_REQUEST*)(pRequest->SourceReserved));
790 if (pOriginal)
791 {
792 /* NDIS is supposed to serialize requests */
793 PNDIS_OID_REQUEST pPrev = ASMAtomicXchgPtrT(&pModuleCtx->pPendingRequest, NULL, PNDIS_OID_REQUEST);
794 Assert(pPrev == pRequest); NOREF(pPrev);
795
796 Log5(("vboxNetLwfWinOidRequestComplete: completed rq type=%d oid=%x\n", pRequest->RequestType, pRequest->DATA.QUERY_INFORMATION.Oid));
797 vboxNetLwfWinCopyOidRequestResults(pRequest, pOriginal);
798 if ( pRequest->RequestType == NdisRequestQueryInformation
799 && pRequest->DATA.QUERY_INFORMATION.Oid == OID_GEN_CURRENT_PACKET_FILTER)
800 {
801 Log5(("vboxNetLwfWinOidRequestComplete: underlying miniport reports its packet filters:\n"));
802 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pRequest->DATA.QUERY_INFORMATION.InformationBuffer);
803 vboxNetLwfWinOverridePacketFiltersUp(pModuleCtx, (ULONG*)pRequest->DATA.QUERY_INFORMATION.InformationBuffer);
804 Log5(("vboxNetLwfWinOidRequestComplete: reporting the following packet filters to upper protocol:\n"));
805 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pRequest->DATA.QUERY_INFORMATION.InformationBuffer);
806 }
807 NdisFreeCloneOidRequest(pModuleCtx->hFilter, pRequest);
808 NdisFOidRequestComplete(pModuleCtx->hFilter, pOriginal, Status);
809 }
810 else
811 {
812 /* This is not a clone, we originated it */
813 Log(("vboxNetLwfWinOidRequestComplete: locally originated request (%p) completed, status=0x%x\n", pRequest, Status));
814 PVBOXNETLWF_OIDREQ pRqWrapper = RT_FROM_MEMBER(pRequest, VBOXNETLWF_OIDREQ, Request);
815 pRqWrapper->Status = Status;
816 NdisSetEvent(&pRqWrapper->Event);
817 }
818 LogFlow(("<==vboxNetLwfWinOidRequestComplete\n"));
819}
820
821
822static bool vboxNetLwfWinIsPromiscuous(PVBOXNETLWF_MODULE pModuleCtx)
823{
824 return ASMAtomicReadBool(&pModuleCtx->fHostPromisc);
825}
826
827#if 0
828static NDIS_STATUS vboxNetLwfWinGetPacketFilter(PVBOXNETLWF_MODULE pModuleCtx)
829{
830 LogFlow(("==>vboxNetLwfWinGetPacketFilter: module=%p\n", pModuleCtx));
831 VBOXNETLWF_OIDREQ Rq;
832 vboxNetLwfWinInitOidRequest(&Rq);
833 Rq.Request.RequestType = NdisRequestQueryInformation;
834 Rq.Request.DATA.QUERY_INFORMATION.Oid = OID_GEN_CURRENT_PACKET_FILTER;
835 Rq.Request.DATA.QUERY_INFORMATION.InformationBuffer = &pModuleCtx->uPacketFilter;
836 Rq.Request.DATA.QUERY_INFORMATION.InformationBufferLength = sizeof(pModuleCtx->uPacketFilter);
837 NDIS_STATUS Status = vboxNetLwfWinSyncOidRequest(pModuleCtx, &Rq);
838 if (Status != NDIS_STATUS_SUCCESS)
839 {
840 LogError(("vboxNetLwfWinGetPacketFilter: vboxNetLwfWinSyncOidRequest(query, OID_GEN_CURRENT_PACKET_FILTER) failed with 0x%x\n", Status));
841 return FALSE;
842 }
843 if (Rq.Request.DATA.QUERY_INFORMATION.BytesWritten != sizeof(pModuleCtx->uPacketFilter))
844 {
845 LogError(("vboxNetLwfWinGetPacketFilter: vboxNetLwfWinSyncOidRequest(query, OID_GEN_CURRENT_PACKET_FILTER) failed to write neccessary amount (%d bytes), actually written %d bytes\n", sizeof(pModuleCtx->uPacketFilter), Rq.Request.DATA.QUERY_INFORMATION.BytesWritten));
846 }
847
848 Log5(("vboxNetLwfWinGetPacketFilter: OID_GEN_CURRENT_PACKET_FILTER query returned the following filters:\n"));
849 vboxNetLwfWinDumpFilterTypes(pModuleCtx->uPacketFilter);
850
851 LogFlow(("<==vboxNetLwfWinGetPacketFilter: status=0x%x\n", Status));
852 return Status;
853}
854#endif
855
856static NDIS_STATUS vboxNetLwfWinSetPacketFilter(PVBOXNETLWF_MODULE pModuleCtx, bool fPromisc)
857{
858 LogFlow(("==>vboxNetLwfWinSetPacketFilter: module=%p %s\n", pModuleCtx, fPromisc ? "promiscuous" : "normal"));
859 ULONG uFilter = 0;
860 VBOXNETLWF_OIDREQ Rq;
861 vboxNetLwfWinInitOidRequest(&Rq);
862 Rq.Request.RequestType = NdisRequestQueryInformation;
863 Rq.Request.DATA.QUERY_INFORMATION.Oid = OID_GEN_CURRENT_PACKET_FILTER;
864 Rq.Request.DATA.QUERY_INFORMATION.InformationBuffer = &uFilter;
865 Rq.Request.DATA.QUERY_INFORMATION.InformationBufferLength = sizeof(uFilter);
866 NDIS_STATUS Status = vboxNetLwfWinSyncOidRequest(pModuleCtx, &Rq);
867 if (Status != NDIS_STATUS_SUCCESS)
868 {
869 LogError(("vboxNetLwfWinSetPacketFilter: vboxNetLwfWinSyncOidRequest(query, OID_GEN_CURRENT_PACKET_FILTER) failed with 0x%x\n", Status));
870 return Status;
871 }
872 if (Rq.Request.DATA.QUERY_INFORMATION.BytesWritten != sizeof(uFilter))
873 {
874 LogError(("vboxNetLwfWinSetPacketFilter: vboxNetLwfWinSyncOidRequest(query, OID_GEN_CURRENT_PACKET_FILTER) failed to write neccessary amount (%d bytes), actually written %d bytes\n", sizeof(uFilter), Rq.Request.DATA.QUERY_INFORMATION.BytesWritten));
875 return NDIS_STATUS_FAILURE;
876 }
877
878 Log5(("vboxNetLwfWinSetPacketFilter: OID_GEN_CURRENT_PACKET_FILTER query returned the following filters:\n"));
879 vboxNetLwfWinDumpFilterTypes(uFilter);
880
881 if (fPromisc)
882 {
883 /* If we about to go promiscuous, save the state before we change it. */
884 ASMAtomicWriteBool(&pModuleCtx->fHostPromisc, !!(uFilter & NDIS_PACKET_TYPE_PROMISCUOUS));
885 uFilter |= NDIS_PACKET_TYPE_PROMISCUOUS;
886 }
887 else
888 {
889 /* Reset promisc only if it was not enabled before we had changed it. */
890 if (!ASMAtomicReadBool(&pModuleCtx->fHostPromisc))
891 uFilter &= ~NDIS_PACKET_TYPE_PROMISCUOUS;
892 }
893
894 Log5(("vboxNetLwfWinSetPacketFilter: OID_GEN_CURRENT_PACKET_FILTER about to set the following filters:\n"));
895 vboxNetLwfWinDumpFilterTypes(uFilter);
896
897 NdisResetEvent(&Rq.Event); /* need to reset as it has been set by query op */
898 Rq.Request.RequestType = NdisRequestSetInformation;
899 Rq.Request.DATA.SET_INFORMATION.Oid = OID_GEN_CURRENT_PACKET_FILTER;
900 Rq.Request.DATA.SET_INFORMATION.InformationBuffer = &uFilter;
901 Rq.Request.DATA.SET_INFORMATION.InformationBufferLength = sizeof(uFilter);
902 Status = vboxNetLwfWinSyncOidRequest(pModuleCtx, &Rq);
903 if (Status != NDIS_STATUS_SUCCESS)
904 {
905 LogError(("vboxNetLwfWinSetPacketFilter: vboxNetLwfWinSyncOidRequest(set, OID_GEN_CURRENT_PACKET_FILTER, vvv below vvv) failed with 0x%x\n", Status));
906 vboxNetLwfWinDumpFilterTypes(uFilter);
907 }
908 LogFlow(("<==vboxNetLwfWinSetPacketFilter: status=0x%x\n", Status));
909 return Status;
910}
911
912
913static NTSTATUS vboxNetLwfWinDevDispatch(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
914{
915 RT_NOREF1(pDevObj);
916 PIO_STACK_LOCATION pIrpSl = IoGetCurrentIrpStackLocation(pIrp);;
917 NTSTATUS Status = STATUS_SUCCESS;
918
919 switch (pIrpSl->MajorFunction)
920 {
921 case IRP_MJ_DEVICE_CONTROL:
922 Status = STATUS_NOT_SUPPORTED;
923 break;
924 case IRP_MJ_CREATE:
925 case IRP_MJ_CLEANUP:
926 case IRP_MJ_CLOSE:
927 break;
928 default:
929 AssertFailed();
930 break;
931 }
932
933 pIrp->IoStatus.Status = Status;
934 IoCompleteRequest(pIrp, IO_NO_INCREMENT);
935
936 return Status;
937}
938
939/** @todo So far we had no use for device, should we even bother to create it? */
940static NDIS_STATUS vboxNetLwfWinDevCreate(PVBOXNETLWFGLOBALS pGlobals)
941{
942 NDIS_STRING DevName, LinkName;
943 PDRIVER_DISPATCH aMajorFunctions[IRP_MJ_MAXIMUM_FUNCTION+1];
944 NdisInitUnicodeString(&DevName, VBOXNETLWF_NAME_DEVICE);
945 NdisInitUnicodeString(&LinkName, VBOXNETLWF_NAME_LINK);
946
947 Assert(!pGlobals->hDevice);
948 Assert(!pGlobals->pDevObj);
949 NdisZeroMemory(aMajorFunctions, sizeof (aMajorFunctions));
950 aMajorFunctions[IRP_MJ_CREATE] = vboxNetLwfWinDevDispatch;
951 aMajorFunctions[IRP_MJ_CLEANUP] = vboxNetLwfWinDevDispatch;
952 aMajorFunctions[IRP_MJ_CLOSE] = vboxNetLwfWinDevDispatch;
953 aMajorFunctions[IRP_MJ_DEVICE_CONTROL] = vboxNetLwfWinDevDispatch;
954
955 NDIS_DEVICE_OBJECT_ATTRIBUTES DeviceAttributes;
956 NdisZeroMemory(&DeviceAttributes, sizeof(DeviceAttributes));
957 DeviceAttributes.Header.Type = NDIS_OBJECT_TYPE_DEVICE_OBJECT_ATTRIBUTES;
958 DeviceAttributes.Header.Revision = NDIS_DEVICE_OBJECT_ATTRIBUTES_REVISION_1;
959 DeviceAttributes.Header.Size = sizeof(DeviceAttributes);
960 DeviceAttributes.DeviceName = &DevName;
961 DeviceAttributes.SymbolicName = &LinkName;
962 DeviceAttributes.MajorFunctions = aMajorFunctions;
963 //DeviceAttributes.ExtensionSize = sizeof(FILTER_DEVICE_EXTENSION);
964
965 NDIS_STATUS Status = NdisRegisterDeviceEx(pGlobals->hFilterDriver,
966 &DeviceAttributes,
967 &pGlobals->pDevObj,
968 &pGlobals->hDevice);
969 Log(("vboxNetLwfWinDevCreate: NdisRegisterDeviceEx returned 0x%x\n", Status));
970 Assert(Status == NDIS_STATUS_SUCCESS);
971#if 0
972 if (Status == NDIS_STATUS_SUCCESS)
973 {
974 PFILTER_DEVICE_EXTENSION pExtension;
975 pExtension = NdisGetDeviceReservedExtension(pGlobals->pDevObj);
976 pExtension->Signature = VBOXNETLWF_MEM_TAG;
977 pExtension->Handle = pGlobals->hFilterDriver;
978 }
979#endif
980 return Status;
981}
982
983static void vboxNetLwfWinDevDestroy(PVBOXNETLWFGLOBALS pGlobals)
984{
985 Assert(pGlobals->hDevice);
986 Assert(pGlobals->pDevObj);
987 NdisDeregisterDeviceEx(pGlobals->hDevice);
988 pGlobals->hDevice = NULL;
989 pGlobals->pDevObj = NULL;
990}
991
992static void vboxNetLwfWinUpdateSavedOffloadConfig(PVBOXNETLWF_MODULE pModuleCtx, PNDIS_OFFLOAD pOffload)
993{
994 pModuleCtx->SavedOffloadConfig = *pOffload;
995 pModuleCtx->fOffloadConfigValid = true;
996}
997
998#ifdef VBOXNETLWF_FIXED_SIZE_POOLS
999static void vboxNetLwfWinFreePools(PVBOXNETLWF_MODULE pModuleCtx, int cPools)
1000{
1001 for (int i = 0; i < cPools; ++i)
1002 {
1003 if (pModuleCtx->hPool[i])
1004 {
1005 NdisFreeNetBufferListPool(pModuleCtx->hPool[i]);
1006 Log4(("vboxNetLwfWinAttach: freeed NBL+NB pool 0x%p\n", pModuleCtx->hPool[i]));
1007 }
1008 }
1009}
1010#endif /* VBOXNETLWF_FIXED_SIZE_POOLS */
1011
1012static NDIS_STATUS vboxNetLwfWinAttach(IN NDIS_HANDLE hFilter, IN NDIS_HANDLE hDriverCtx,
1013 IN PNDIS_FILTER_ATTACH_PARAMETERS pParameters)
1014{
1015 LogFlow(("==>vboxNetLwfWinAttach: filter=%p\n", hFilter));
1016
1017 PVBOXNETLWFGLOBALS pGlobals = (PVBOXNETLWFGLOBALS)hDriverCtx;
1018 if (!pGlobals)
1019 {
1020 vboxNetLwfLogErrorEvent(IO_ERR_INTERNAL_ERROR, NDIS_STATUS_FAILURE, 1);
1021 return NDIS_STATUS_FAILURE;
1022 }
1023
1024 ANSI_STRING strMiniportName;
1025 /* We use the miniport name to associate this filter module with the netflt instance */
1026 NTSTATUS rc = RtlUnicodeStringToAnsiString(&strMiniportName,
1027 pParameters->BaseMiniportName,
1028 TRUE);
1029 if (rc != STATUS_SUCCESS)
1030 {
1031 LogError(("vboxNetLwfWinAttach: RtlUnicodeStringToAnsiString(%ls) failed with 0x%x\n",
1032 pParameters->BaseMiniportName, rc));
1033 vboxNetLwfLogErrorEvent(IO_ERR_INTERNAL_ERROR, NDIS_STATUS_FAILURE, 2);
1034 return NDIS_STATUS_FAILURE;
1035 }
1036 DbgPrint("vboxNetLwfWinAttach: friendly name=%wZ\n", pParameters->BaseMiniportInstanceName);
1037 DbgPrint("vboxNetLwfWinAttach: name=%Z\n", &strMiniportName);
1038
1039 UINT cbModuleWithNameExtra = sizeof(VBOXNETLWF_MODULE) + strMiniportName.Length;
1040 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)NdisAllocateMemoryWithTagPriority(hFilter,
1041 cbModuleWithNameExtra,
1042 VBOXNETLWF_MEM_TAG,
1043 LowPoolPriority);
1044 if (!pModuleCtx)
1045 {
1046 LogError(("vboxNetLwfWinAttach: Failed to allocate module context for %ls\n", pParameters->BaseMiniportName));
1047 RtlFreeAnsiString(&strMiniportName);
1048 vboxNetLwfLogErrorEvent(IO_ERR_INSUFFICIENT_RESOURCES, NDIS_STATUS_RESOURCES, 3);
1049 return NDIS_STATUS_RESOURCES;
1050 }
1051 Log4(("vboxNetLwfWinAttach: allocated module context 0x%p\n", pModuleCtx));
1052
1053 NdisZeroMemory(pModuleCtx, cbModuleWithNameExtra);
1054 NdisMoveMemory(pModuleCtx->szMiniportName, strMiniportName.Buffer, strMiniportName.Length);
1055 RtlFreeAnsiString(&strMiniportName);
1056
1057 pModuleCtx->hWorkItem = NdisAllocateIoWorkItem(g_VBoxNetLwfGlobals.hFilterDriver);
1058 if (!pModuleCtx->hWorkItem)
1059 {
1060 LogError(("vboxNetLwfWinAttach: Failed to allocate work item for %ls\n",
1061 pParameters->BaseMiniportName));
1062 NdisFreeMemory(pModuleCtx, 0, 0);
1063 vboxNetLwfLogErrorEvent(IO_ERR_INSUFFICIENT_RESOURCES, NDIS_STATUS_RESOURCES, 4);
1064 return NDIS_STATUS_RESOURCES;
1065 }
1066
1067 Assert(pParameters->MacAddressLength == sizeof(RTMAC));
1068 NdisMoveMemory(&pModuleCtx->MacAddr, pParameters->CurrentMacAddress, RT_MIN(sizeof(RTMAC), pParameters->MacAddressLength));
1069 if (pParameters->DefaultOffloadConfiguration)
1070 vboxNetLwfWinUpdateSavedOffloadConfig(pModuleCtx, pParameters->DefaultOffloadConfiguration);
1071
1072 pModuleCtx->pGlobals = pGlobals;
1073 pModuleCtx->hFilter = hFilter;
1074 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Attaching);
1075 /* Initialize transmission mutex and events */
1076 NDIS_INIT_MUTEX(&pModuleCtx->InTransmit);
1077#ifdef VBOXNETLWF_SYNC_SEND
1078 KeInitializeEvent(&pModuleCtx->EventWire, SynchronizationEvent, FALSE);
1079 KeInitializeEvent(&pModuleCtx->EventHost, SynchronizationEvent, FALSE);
1080#else /* !VBOXNETLWF_SYNC_SEND */
1081 NdisInitializeEvent(&pModuleCtx->EventSendComplete);
1082 pModuleCtx->cPendingBuffers = 0;
1083#endif /* !VBOXNETLWF_SYNC_SEND */
1084
1085#ifdef VBOXNETLWF_FIXED_SIZE_POOLS
1086 for (int i = 0; i < RT_ELEMENTS(g_cbPool); ++i)
1087 {
1088 /* Allocate buffer pools */
1089 NET_BUFFER_LIST_POOL_PARAMETERS PoolParams;
1090 NdisZeroMemory(&PoolParams, sizeof(PoolParams));
1091 PoolParams.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
1092 PoolParams.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1;
1093 PoolParams.Header.Size = sizeof(PoolParams);
1094 PoolParams.ProtocolId = NDIS_PROTOCOL_ID_DEFAULT;
1095 PoolParams.fAllocateNetBuffer = TRUE;
1096 PoolParams.ContextSize = 0; /** @todo Do we need to consider underlying drivers? I think not. */
1097 PoolParams.PoolTag = VBOXNETLWF_MEM_TAG;
1098 PoolParams.DataSize = g_cbPool[i];
1099 pModuleCtx->hPool[i] = NdisAllocateNetBufferListPool(hFilter, &PoolParams);
1100 if (!pModuleCtx->hPool[i])
1101 {
1102 LogError(("vboxNetLwfWinAttach: NdisAllocateNetBufferListPool failed\n"));
1103 vboxNetLwfWinFreePools(pModuleCtx, i);
1104 NdisFreeIoWorkItem(pModuleCtx->hWorkItem);
1105 NdisFreeMemory(pModuleCtx, 0, 0);
1106 return NDIS_STATUS_RESOURCES;
1107 }
1108 Log4(("vboxNetLwfWinAttach: allocated NBL+NB pool (data size=%u) 0x%p\n",
1109 PoolParams.DataSize, pModuleCtx->hPool[i]));
1110 }
1111#else /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1112 /* Allocate buffer pools */
1113 NET_BUFFER_LIST_POOL_PARAMETERS PoolParams;
1114 NdisZeroMemory(&PoolParams, sizeof(PoolParams));
1115 PoolParams.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
1116 PoolParams.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1;
1117 PoolParams.Header.Size = sizeof(PoolParams);
1118 PoolParams.ProtocolId = NDIS_PROTOCOL_ID_DEFAULT;
1119 PoolParams.fAllocateNetBuffer = TRUE;
1120 PoolParams.ContextSize = 0; /** @todo Do we need to consider underlying drivers? I think not. */
1121 PoolParams.PoolTag = VBOXNETLWF_MEM_TAG;
1122 pModuleCtx->hPool = NdisAllocateNetBufferListPool(hFilter, &PoolParams);
1123 if (!pModuleCtx->hPool)
1124 {
1125 LogError(("vboxNetLwfWinAttach: NdisAllocateNetBufferListPool failed\n"));
1126 NdisFreeIoWorkItem(pModuleCtx->hWorkItem);
1127 NdisFreeMemory(pModuleCtx, 0, 0);
1128 return NDIS_STATUS_RESOURCES;
1129 }
1130 Log4(("vboxNetLwfWinAttach: allocated NBL+NB pool 0x%p\n", pModuleCtx->hPool));
1131#endif /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1132
1133 NDIS_FILTER_ATTRIBUTES Attributes;
1134 NdisZeroMemory(&Attributes, sizeof(Attributes));
1135 Attributes.Header.Revision = NDIS_FILTER_ATTRIBUTES_REVISION_1;
1136 Attributes.Header.Size = sizeof(Attributes);
1137 Attributes.Header.Type = NDIS_OBJECT_TYPE_FILTER_ATTRIBUTES;
1138 Attributes.Flags = 0;
1139 NDIS_STATUS Status = NdisFSetAttributes(hFilter, pModuleCtx, &Attributes);
1140 if (Status != NDIS_STATUS_SUCCESS)
1141 {
1142 LogError(("vboxNetLwfWinAttach: NdisFSetAttributes failed with 0x%x\n", Status));
1143#ifdef VBOXNETLWF_FIXED_SIZE_POOLS
1144 vboxNetLwfWinFreePools(pModuleCtx, RT_ELEMENTS(g_cbPool));
1145#else /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1146 NdisFreeNetBufferListPool(pModuleCtx->hPool);
1147 Log4(("vboxNetLwfWinAttach: freed NBL+NB pool 0x%p\n", pModuleCtx->hPool));
1148#endif /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1149 NdisFreeIoWorkItem(pModuleCtx->hWorkItem);
1150 NdisFreeMemory(pModuleCtx, 0, 0);
1151 vboxNetLwfLogErrorEvent(IO_ERR_INTERNAL_ERROR, NDIS_STATUS_RESOURCES, 5);
1152 return NDIS_STATUS_RESOURCES;
1153 }
1154 /* Insert into module chain */
1155 NdisAcquireSpinLock(&pGlobals->Lock);
1156 RTListPrepend(&pGlobals->listModules, &pModuleCtx->node);
1157 NdisReleaseSpinLock(&pGlobals->Lock);
1158
1159 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Paused);
1160
1161 /// @todo Somehow the packet filter is 0 at this point: Status = vboxNetLwfWinGetPacketFilter(pModuleCtx);
1162 /// @todo We actually update it later in status handler, perhaps we should not do anything here.
1163
1164 LogFlow(("<==vboxNetLwfWinAttach: Status = 0x%x\n", Status));
1165 return Status;
1166}
1167
1168static VOID vboxNetLwfWinDetach(IN NDIS_HANDLE hModuleCtx)
1169{
1170 LogFlow(("==>vboxNetLwfWinDetach: module=%p\n", hModuleCtx));
1171 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
1172 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Detached, LwfState_Paused);
1173
1174 /* Remove from module chain */
1175 NdisAcquireSpinLock(&pModuleCtx->pGlobals->Lock);
1176 RTListNodeRemove(&pModuleCtx->node);
1177 NdisReleaseSpinLock(&pModuleCtx->pGlobals->Lock);
1178
1179 PVBOXNETFLTINS pNetFltIns = pModuleCtx->pNetFlt; /// @todo Atomic?
1180 if (pNetFltIns && vboxNetFltTryRetainBusyNotDisconnected(pNetFltIns))
1181 {
1182 /*
1183 * Set hModuleCtx to null now in order to prevent filter restart,
1184 * OID requests and other stuff associated with NetFlt deactivation.
1185 */
1186 pNetFltIns->u.s.WinIf.hModuleCtx = NULL;
1187 /* Notify NetFlt that we are going down */
1188 pNetFltIns->pSwitchPort->pfnDisconnect(pNetFltIns->pSwitchPort, &pNetFltIns->MyPort, vboxNetFltPortReleaseBusy);
1189 /* We do not 'release' netflt instance since it has been done by pfnDisconnect */
1190 }
1191 pModuleCtx->pNetFlt = NULL;
1192
1193 /*
1194 * We have to make sure that all NET_BUFFER_LIST structures have been freed by now, but
1195 * it does not require us to do anything here since it has already been taken care of
1196 * by vboxNetLwfWinPause().
1197 */
1198#ifdef VBOXNETLWF_FIXED_SIZE_POOLS
1199 vboxNetLwfWinFreePools(pModuleCtx, RT_ELEMENTS(g_cbPool));
1200#else /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1201 if (pModuleCtx->hPool)
1202 {
1203 NdisFreeNetBufferListPool(pModuleCtx->hPool);
1204 Log4(("vboxNetLwfWinDetach: freed NBL+NB pool 0x%p\n", pModuleCtx->hPool));
1205 }
1206#endif /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1207 NdisFreeIoWorkItem(pModuleCtx->hWorkItem);
1208 NdisFreeMemory(hModuleCtx, 0, 0);
1209 Log4(("vboxNetLwfWinDetach: freed module context 0x%p\n", pModuleCtx));
1210 LogFlow(("<==vboxNetLwfWinDetach\n"));
1211}
1212
1213
1214static NDIS_STATUS vboxNetLwfWinPause(IN NDIS_HANDLE hModuleCtx, IN PNDIS_FILTER_PAUSE_PARAMETERS pParameters)
1215{
1216 RT_NOREF1(pParameters);
1217 LogFlow(("==>vboxNetLwfWinPause: module=%p\n", hModuleCtx));
1218 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
1219 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Pausing, LwfState_Running);
1220 /* Wait for pending send/indication operations to complete. */
1221 NDIS_WAIT_FOR_MUTEX(&pModuleCtx->InTransmit);
1222#ifndef VBOXNETLWF_SYNC_SEND
1223 NdisWaitEvent(&pModuleCtx->EventSendComplete, 1000 /* ms */);
1224#endif /* !VBOXNETLWF_SYNC_SEND */
1225 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Paused, LwfState_Pausing);
1226 NDIS_RELEASE_MUTEX(&pModuleCtx->InTransmit);
1227 LogFlow(("<==vboxNetLwfWinPause\n"));
1228 return NDIS_STATUS_SUCCESS; /* Failure is not an option */
1229}
1230
1231
1232static void vboxNetLwfWinIndicateOffload(PVBOXNETLWF_MODULE pModuleCtx, PNDIS_OFFLOAD pOffload)
1233{
1234 Log5(("vboxNetLwfWinIndicateOffload: offload config changed to:\n"));
1235 vboxNetLwfWinDumpOffloadSettings(pOffload);
1236 NDIS_STATUS_INDICATION OffloadingIndication;
1237 NdisZeroMemory(&OffloadingIndication, sizeof(OffloadingIndication));
1238 OffloadingIndication.Header.Type = NDIS_OBJECT_TYPE_STATUS_INDICATION;
1239 OffloadingIndication.Header.Revision = NDIS_STATUS_INDICATION_REVISION_1;
1240 OffloadingIndication.Header.Size = NDIS_SIZEOF_STATUS_INDICATION_REVISION_1;
1241 OffloadingIndication.SourceHandle = pModuleCtx->hFilter;
1242 OffloadingIndication.StatusCode = NDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG;
1243 OffloadingIndication.StatusBuffer = pOffload;
1244 OffloadingIndication.StatusBufferSize = sizeof(NDIS_OFFLOAD);
1245 NdisFIndicateStatus(pModuleCtx->hFilter, &OffloadingIndication);
1246}
1247
1248
1249static NDIS_STATUS vboxNetLwfWinRestart(IN NDIS_HANDLE hModuleCtx, IN PNDIS_FILTER_RESTART_PARAMETERS pParameters)
1250{
1251 RT_NOREF1(pParameters);
1252 LogFlow(("==>vboxNetLwfWinRestart: module=%p\n", hModuleCtx));
1253 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
1254 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Restarting, LwfState_Paused);
1255 vboxNetLwfWinChangeState(pModuleCtx, LwfState_Running, LwfState_Restarting);
1256 NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
1257 LogFlow(("<==vboxNetLwfWinRestart: Status = 0x%x\n", Status));
1258 return Status;
1259}
1260
1261
1262static void vboxNetLwfWinDestroySG(PINTNETSG pSG)
1263{
1264 NdisFreeMemory(pSG, 0, 0);
1265 Log4(("vboxNetLwfWinDestroySG: freed SG 0x%p\n", pSG));
1266}
1267
1268DECLINLINE(ULONG) vboxNetLwfWinCalcSegments(PNET_BUFFER pNetBuf)
1269{
1270 ULONG cSegs = 0;
1271 for (PMDL pMdl = NET_BUFFER_CURRENT_MDL(pNetBuf); pMdl; pMdl = NDIS_MDL_LINKAGE(pMdl))
1272 cSegs++;
1273 return cSegs;
1274}
1275
1276DECLINLINE(void) vboxNetLwfWinFreeMdlChain(PMDL pMdl)
1277{
1278#ifndef VBOXNETLWF_FIXED_SIZE_POOLS
1279 PMDL pMdlNext;
1280 while (pMdl)
1281 {
1282 pMdlNext = pMdl->Next;
1283# ifndef VBOXNETLWF_SYNC_SEND
1284 PUCHAR pDataBuf;
1285 ULONG cb = 0;
1286 NdisQueryMdl(pMdl, &pDataBuf, &cb, NormalPagePriority);
1287# endif /* !VBOXNETLWF_SYNC_SEND */
1288 NdisFreeMdl(pMdl);
1289 Log4(("vboxNetLwfWinFreeMdlChain: freed MDL 0x%p\n", pMdl));
1290# ifndef VBOXNETLWF_SYNC_SEND
1291 NdisFreeMemory(pDataBuf, 0, 0);
1292 Log4(("vboxNetLwfWinFreeMdlChain: freed data buffer 0x%p\n", pDataBuf));
1293# endif /* !VBOXNETLWF_SYNC_SEND */
1294 pMdl = pMdlNext;
1295 }
1296#else /* VBOXNETLWF_FIXED_SIZE_POOLS */
1297 RT_NOREF1(pMdl);
1298#endif /* VBOXNETLWF_FIXED_SIZE_POOLS */
1299}
1300
1301/** @todo
1302 * 1) Copy data from SG to MDL (if we decide to complete asynchronously).
1303 * 2) Provide context/backfill space. Nobody does it, should we?
1304 * 3) We always get a single segment from intnet. Simplify?
1305 */
1306static PNET_BUFFER_LIST vboxNetLwfWinSGtoNB(PVBOXNETLWF_MODULE pModule, PINTNETSG pSG)
1307{
1308 AssertReturn(pSG->cSegsUsed >= 1, NULL);
1309 LogFlow(("==>vboxNetLwfWinSGtoNB: segments=%d hPool=%p cb=%u\n", pSG->cSegsUsed,
1310 pModule->hPool, pSG->cbTotal));
1311 AssertReturn(pModule->hPool, NULL);
1312
1313#ifdef VBOXNETLWF_SYNC_SEND
1314 PINTNETSEG pSeg = pSG->aSegs;
1315 PMDL pMdl = NdisAllocateMdl(pModule->hFilter, pSeg->pv, pSeg->cb);
1316 if (!pMdl)
1317 {
1318 LogError(("vboxNetLwfWinSGtoNB: failed to allocate an MDL\n"));
1319 LogFlow(("<==vboxNetLwfWinSGtoNB: return NULL\n"));
1320 return NULL;
1321 }
1322 Log4(("vboxNetLwfWinSGtoNB: allocated Mdl 0x%p\n", pMdl));
1323 PMDL pMdlCurr = pMdl;
1324 for (int i = 1; i < pSG->cSegsUsed; i++)
1325 {
1326 pSeg = &pSG->aSegs[i];
1327 pMdlCurr->Next = NdisAllocateMdl(pModule->hFilter, pSeg->pv, pSeg->cb);
1328 if (!pMdlCurr->Next)
1329 {
1330 LogError(("vboxNetLwfWinSGtoNB: failed to allocate an MDL\n"));
1331 /* Tear down all MDL we chained so far */
1332 vboxNetLwfWinFreeMdlChain(pMdl);
1333 return NULL;
1334 }
1335 pMdlCurr = pMdlCurr->Next;
1336 Log4(("vboxNetLwfWinSGtoNB: allocated Mdl 0x%p\n", pMdlCurr));
1337 }
1338 PNET_BUFFER_LIST pBufList = NdisAllocateNetBufferAndNetBufferList(pModule->hPool,
1339 0 /* ContextSize */,
1340 0 /* ContextBackFill */,
1341 pMdl,
1342 0 /* DataOffset */,
1343 pSG->cbTotal);
1344 if (pBufList)
1345 {
1346 Log4(("vboxNetLwfWinSGtoNB: allocated NBL+NB 0x%p\n", pBufList));
1347 pBufList->SourceHandle = pModule->hFilter;
1348 /** @todo Do we need to initialize anything else? */
1349 }
1350 else
1351 {
1352 LogError(("vboxNetLwfWinSGtoNB: failed to allocate an NBL+NB\n"));
1353 vboxNetLwfWinFreeMdlChain(pMdl);
1354 }
1355#else /* !VBOXNETLWF_SYNC_SEND */
1356
1357# ifdef VBOXNETLWF_FIXED_SIZE_POOLS
1358 int iPool = 0;
1359 ULONG cbFrame = VBOXNETLWF_MAX_FRAME_SIZE(pSG->cbTotal);
1360 /* Let's find the appropriate pool first */
1361 for (iPool = 0; iPool < RT_ELEMENTS(g_cbPool); ++iPool)
1362 if (cbFrame <= g_cbPool[iPool])
1363 break;
1364 if (iPool >= RT_ELEMENTS(g_cbPool))
1365 {
1366 LogError(("vboxNetLwfWinSGtoNB: frame is too big (%u > %u), drop it.\n", cbFrame, g_cbPool[RT_ELEMENTS(g_cbPool)-1]));
1367 LogFlow(("<==vboxNetLwfWinSGtoNB: return NULL\n"));
1368 return NULL;
1369 }
1370 PNET_BUFFER_LIST pBufList = NdisAllocateNetBufferList(pModule->hPool[iPool],
1371 0 /** @todo ContextSize */,
1372 0 /** @todo ContextBackFill */);
1373 if (!pBufList)
1374 {
1375 LogError(("vboxNetLwfWinSGtoNB: failed to allocate netbuffer (cb=%u) from pool %d\n", cbFrame, iPool));
1376 LogFlow(("<==vboxNetLwfWinSGtoNB: return NULL\n"));
1377 return NULL;
1378 }
1379 NET_BUFFER *pBuffer = NET_BUFFER_LIST_FIRST_NB(pBufList);
1380 NDIS_STATUS Status = NdisRetreatNetBufferDataStart(pBuffer, pSG->cbTotal, 0 /** @todo DataBackfill */, NULL);
1381 if (Status == NDIS_STATUS_SUCCESS)
1382 {
1383 uint8_t *pDst = (uint8_t*)NdisGetDataBuffer(pBuffer, pSG->cbTotal, NULL, 1, 0);
1384 if (pDst)
1385 {
1386 for (int i = 0; i < pSG->cSegsUsed; i++)
1387 {
1388 NdisMoveMemory(pDst, pSG->aSegs[i].pv, pSG->aSegs[i].cb);
1389 pDst += pSG->aSegs[i].cb;
1390 }
1391 Log4(("vboxNetLwfWinSGtoNB: allocated NBL+NB 0x%p\n", pBufList));
1392 pBufList->SourceHandle = pModule->hFilter;
1393 }
1394 else
1395 {
1396 LogError(("vboxNetLwfWinSGtoNB: failed to obtain the buffer pointer (size=%u)\n", pSG->cbTotal));
1397 NdisAdvanceNetBufferDataStart(pBuffer, pSG->cbTotal, false, NULL); /** @todo why bother? */
1398 NdisFreeNetBufferList(pBufList);
1399 pBufList = NULL;
1400 }
1401 }
1402 else
1403 {
1404 LogError(("vboxNetLwfWinSGtoNB: NdisRetreatNetBufferDataStart failed with 0x%x (size=%u)\n", Status, pSG->cbTotal));
1405 NdisFreeNetBufferList(pBufList);
1406 pBufList = NULL;
1407 }
1408# else /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1409 PNET_BUFFER_LIST pBufList = NULL;
1410 ULONG cbMdl = VBOXNETLWF_MAX_FRAME_SIZE(pSG->cbTotal);
1411 ULONG uDataOffset = cbMdl - pSG->cbTotal;
1412 PUCHAR pDataBuf = (PUCHAR)NdisAllocateMemoryWithTagPriority(pModule->hFilter, cbMdl,
1413 VBOXNETLWF_MEM_TAG, NormalPoolPriority);
1414 if (pDataBuf)
1415 {
1416 Log4(("vboxNetLwfWinSGtoNB: allocated data buffer (cb=%u) 0x%p\n", cbMdl, pDataBuf));
1417 PMDL pMdl = NdisAllocateMdl(pModule->hFilter, pDataBuf, cbMdl);
1418 if (!pMdl)
1419 {
1420 NdisFreeMemory(pDataBuf, 0, 0);
1421 Log4(("vboxNetLwfWinSGtoNB: freed data buffer 0x%p\n", pDataBuf));
1422 LogError(("vboxNetLwfWinSGtoNB: failed to allocate an MDL (cb=%u)\n", cbMdl));
1423 LogFlow(("<==vboxNetLwfWinSGtoNB: return NULL\n"));
1424 return NULL;
1425 }
1426 PUCHAR pDst = pDataBuf + uDataOffset;
1427 for (int i = 0; i < pSG->cSegsUsed; i++)
1428 {
1429 NdisMoveMemory(pDst, pSG->aSegs[i].pv, pSG->aSegs[i].cb);
1430 pDst += pSG->aSegs[i].cb;
1431 }
1432 pBufList = NdisAllocateNetBufferAndNetBufferList(pModule->hPool,
1433 0 /* ContextSize */,
1434 0 /* ContextBackFill */,
1435 pMdl,
1436 uDataOffset,
1437 pSG->cbTotal);
1438 if (pBufList)
1439 {
1440 Log4(("vboxNetLwfWinSGtoNB: allocated NBL+NB 0x%p\n", pBufList));
1441 pBufList->SourceHandle = pModule->hFilter;
1442 /** @todo Do we need to initialize anything else? */
1443 }
1444 else
1445 {
1446 LogError(("vboxNetLwfWinSGtoNB: failed to allocate an NBL+NB\n"));
1447 vboxNetLwfWinFreeMdlChain(pMdl);
1448 }
1449 }
1450 else
1451 {
1452 LogError(("vboxNetLwfWinSGtoNB: failed to allocate data buffer (size=%u)\n", cbMdl));
1453 }
1454# endif /* !VBOXNETLWF_FIXED_SIZE_POOLS */
1455
1456#endif /* !VBOXNETLWF_SYNC_SEND */
1457 LogFlow(("<==vboxNetLwfWinSGtoNB: return %p\n", pBufList));
1458 return pBufList;
1459}
1460
1461static PINTNETSG vboxNetLwfWinNBtoSG(PVBOXNETLWF_MODULE pModule, PNET_BUFFER pNetBuf)
1462{
1463 ULONG cbPacket = NET_BUFFER_DATA_LENGTH(pNetBuf);
1464 UINT cSegs = vboxNetLwfWinCalcSegments(pNetBuf);
1465 /* Allocate and initialize SG */
1466 PINTNETSG pSG = (PINTNETSG)NdisAllocateMemoryWithTagPriority(pModule->hFilter,
1467 RT_OFFSETOF(INTNETSG, aSegs[cSegs]),
1468 VBOXNETLWF_MEM_TAG,
1469 NormalPoolPriority);
1470 AssertReturn(pSG, pSG);
1471 Log4(("vboxNetLwfWinNBtoSG: allocated SG 0x%p\n", pSG));
1472 IntNetSgInitTempSegs(pSG, cbPacket /*cbTotal*/, cSegs, cSegs /*cSegsUsed*/);
1473
1474 int rc = NDIS_STATUS_SUCCESS;
1475 ULONG uOffset = NET_BUFFER_CURRENT_MDL_OFFSET(pNetBuf);
1476 cSegs = 0;
1477 for (PMDL pMdl = NET_BUFFER_CURRENT_MDL(pNetBuf);
1478 pMdl != NULL && cbPacket > 0;
1479 pMdl = NDIS_MDL_LINKAGE(pMdl))
1480 {
1481 PUCHAR pSrc = (PUCHAR)MmGetSystemAddressForMdlSafe(pMdl, LowPagePriority);
1482 if (!pSrc)
1483 {
1484 rc = NDIS_STATUS_RESOURCES;
1485 break;
1486 }
1487 ULONG cbSrc = MmGetMdlByteCount(pMdl);
1488 if (uOffset)
1489 {
1490 Assert(uOffset < cbSrc);
1491 pSrc += uOffset;
1492 cbSrc -= uOffset;
1493 uOffset = 0;
1494 }
1495
1496 if (cbSrc > cbPacket)
1497 cbSrc = cbPacket;
1498
1499 pSG->aSegs[cSegs].pv = pSrc;
1500 pSG->aSegs[cSegs].cb = cbSrc;
1501 pSG->aSegs[cSegs].Phys = NIL_RTHCPHYS;
1502 cSegs++;
1503 cbPacket -= cbSrc;
1504 }
1505
1506 Assert(cSegs <= pSG->cSegsAlloc);
1507
1508 if (RT_FAILURE(rc))
1509 {
1510 vboxNetLwfWinDestroySG(pSG);
1511 pSG = NULL;
1512 }
1513 else
1514 {
1515 Assert(cbPacket == 0);
1516 Assert(pSG->cSegsUsed == cSegs);
1517 }
1518 return pSG;
1519}
1520
1521static void vboxNetLwfWinDisableOffloading(PNDIS_OFFLOAD pOffloadConfig)
1522{
1523 pOffloadConfig->Checksum.IPv4Transmit.Encapsulation = NDIS_ENCAPSULATION_NOT_SUPPORTED;
1524 pOffloadConfig->Checksum.IPv4Transmit.IpOptionsSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1525 pOffloadConfig->Checksum.IPv4Transmit.TcpOptionsSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1526 pOffloadConfig->Checksum.IPv4Transmit.TcpChecksum = NDIS_OFFLOAD_NOT_SUPPORTED;
1527 pOffloadConfig->Checksum.IPv4Transmit.UdpChecksum = NDIS_OFFLOAD_NOT_SUPPORTED;
1528 pOffloadConfig->Checksum.IPv4Transmit.IpChecksum = NDIS_OFFLOAD_NOT_SUPPORTED;
1529 pOffloadConfig->Checksum.IPv6Transmit.Encapsulation = NDIS_ENCAPSULATION_NOT_SUPPORTED;
1530 pOffloadConfig->Checksum.IPv6Transmit.IpExtensionHeadersSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1531 pOffloadConfig->Checksum.IPv6Transmit.TcpOptionsSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1532 pOffloadConfig->Checksum.IPv6Transmit.TcpChecksum = NDIS_OFFLOAD_NOT_SUPPORTED;
1533 pOffloadConfig->Checksum.IPv6Transmit.UdpChecksum = NDIS_OFFLOAD_NOT_SUPPORTED;
1534 pOffloadConfig->LsoV1.IPv4.Encapsulation = NDIS_ENCAPSULATION_NOT_SUPPORTED;
1535 pOffloadConfig->LsoV1.IPv4.TcpOptions = NDIS_OFFLOAD_NOT_SUPPORTED;
1536 pOffloadConfig->LsoV1.IPv4.IpOptions = NDIS_OFFLOAD_NOT_SUPPORTED;
1537 pOffloadConfig->LsoV2.IPv4.Encapsulation = NDIS_ENCAPSULATION_NOT_SUPPORTED;
1538 pOffloadConfig->LsoV2.IPv6.Encapsulation = NDIS_ENCAPSULATION_NOT_SUPPORTED;
1539 pOffloadConfig->LsoV2.IPv6.IpExtensionHeadersSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1540 pOffloadConfig->LsoV2.IPv6.TcpOptionsSupported = NDIS_OFFLOAD_NOT_SUPPORTED;
1541}
1542
1543VOID vboxNetLwfWinStatus(IN NDIS_HANDLE hModuleCtx, IN PNDIS_STATUS_INDICATION pIndication)
1544{
1545 LogFlow(("==>vboxNetLwfWinStatus: module=%p\n", hModuleCtx));
1546 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)hModuleCtx;
1547 Log(("vboxNetLwfWinStatus: Got status indication: %s\n", vboxNetLwfWinStatusToText(pIndication->StatusCode)));
1548 switch (pIndication->StatusCode)
1549 {
1550 case NDIS_STATUS_PACKET_FILTER:
1551 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pIndication->StatusBuffer);
1552 vboxNetLwfWinOverridePacketFiltersUp(pModuleCtx, (ULONG*)pIndication->StatusBuffer);
1553 Log(("vboxNetLwfWinStatus: Reporting status: %s\n", vboxNetLwfWinStatusToText(pIndication->StatusCode)));
1554 vboxNetLwfWinDumpFilterTypes(*(ULONG*)pIndication->StatusBuffer);
1555 break;
1556 case NDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG:
1557 Log5(("vboxNetLwfWinStatus: offloading currently set to:\n"));
1558 vboxNetLwfWinDumpOffloadSettings((PNDIS_OFFLOAD)pIndication->StatusBuffer);
1559 vboxNetLwfWinUpdateSavedOffloadConfig(pModuleCtx, (PNDIS_OFFLOAD)pIndication->StatusBuffer);
1560 if (ASMAtomicReadBool(&pModuleCtx->fActive))
1561 vboxNetLwfWinDisableOffloading((PNDIS_OFFLOAD)pIndication->StatusBuffer);
1562 Log5(("vboxNetLwfWinStatus: reporting offloading up as:\n"));
1563 vboxNetLwfWinDumpOffloadSettings((PNDIS_OFFLOAD)pIndication->StatusBuffer);
1564 break;
1565 }
1566 NdisFIndicateStatus(pModuleCtx->hFilter, pIndication);
1567 LogFlow(("<==vboxNetLwfWinStatus\n"));
1568}
1569
1570static bool vboxNetLwfWinForwardToIntNet(PVBOXNETLWF_MODULE pModuleCtx, PNET_BUFFER_LIST pBufLists, uint32_t fSrc)
1571{
1572 /* We must not forward anything to the trunk unless it is ready to receive. */
1573 if (!ASMAtomicReadBool(&pModuleCtx->fActive))
1574 {
1575 Log(("vboxNetLwfWinForwardToIntNet: trunk is inactive, won't forward\n"));
1576 return false;
1577 }
1578
1579 AssertReturn(pModuleCtx->pNetFlt, false);
1580 AssertReturn(pModuleCtx->pNetFlt->pSwitchPort, false);
1581 AssertReturn(pModuleCtx->pNetFlt->pSwitchPort->pfnRecv, false);
1582 LogFlow(("==>vboxNetLwfWinForwardToIntNet: module=%p\n", pModuleCtx));
1583 Assert(pBufLists); /* The chain must contain at least one list */
1584 Assert(NET_BUFFER_LIST_NEXT_NBL(pBufLists) == NULL); /* The caller is supposed to unlink the list from the chain */
1585 /*
1586 * Even if NBL contains more than one buffer we are prepared to deal with it.
1587 * When any of buffers should not be dropped we keep the whole list. It is
1588 * better to leak some "unexpected" packets to the wire/host than to loose any.
1589 */
1590 bool fDropIt = false;
1591 bool fDontDrop = false;
1592 int nLists = 0;
1593 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = NET_BUFFER_LIST_NEXT_NBL(pList))
1594 {
1595 int nBuffers = 0;
1596 nLists++;
1597 for (PNET_BUFFER pBuf = NET_BUFFER_LIST_FIRST_NB(pList); pBuf; pBuf = NET_BUFFER_NEXT_NB(pBuf))
1598 {
1599 nBuffers++;
1600 PINTNETSG pSG = vboxNetLwfWinNBtoSG(pModuleCtx, pBuf);
1601 if (pSG)
1602 {
1603 vboxNetLwfWinDumpPacket(pSG, (fSrc & INTNETTRUNKDIR_WIRE)?"intnet <-- wire":"intnet <-- host");
1604 /* A bit paranoid, but we do not use any locks, so... */
1605 if (ASMAtomicReadBool(&pModuleCtx->fActive))
1606 if (pModuleCtx->pNetFlt->pSwitchPort->pfnRecv(pModuleCtx->pNetFlt->pSwitchPort, NULL, pSG, fSrc))
1607 fDropIt = true;
1608 else
1609 fDontDrop = true;
1610 vboxNetLwfWinDestroySG(pSG);
1611 }
1612 }
1613 Log(("vboxNetLwfWinForwardToIntNet: list=%d buffers=%d\n", nLists, nBuffers));
1614 }
1615 Log(("vboxNetLwfWinForwardToIntNet: lists=%d drop=%s don't=%s\n", nLists, fDropIt ? "true":"false", fDontDrop ? "true":"false"));
1616 LogFlow(("<==vboxNetLwfWinForwardToIntNet: return '%s'\n",
1617 fDropIt ? (fDontDrop ? "do not drop (some)" : "drop it") : "do not drop (any)"));
1618 return fDropIt && !fDontDrop; /* Drop the list if ALL its buffers are being dropped! */
1619}
1620
1621DECLINLINE(bool) vboxNetLwfWinIsRunning(PVBOXNETLWF_MODULE pModule)
1622{
1623 Log(("vboxNetLwfWinIsRunning: state=%d\n", ASMAtomicReadU32(&pModule->enmState)));
1624 return ASMAtomicReadU32(&pModule->enmState) == LwfState_Running;
1625}
1626
1627VOID vboxNetLwfWinSendNetBufferLists(IN NDIS_HANDLE hModuleCtx, IN PNET_BUFFER_LIST pBufLists, IN NDIS_PORT_NUMBER nPort, IN ULONG fFlags)
1628{
1629 LogFlow(("==>vboxNetLwfWinSendNetBufferLists: module=%p\n", hModuleCtx));
1630 PVBOXNETLWF_MODULE pModule = (PVBOXNETLWF_MODULE)hModuleCtx;
1631 vboxNetLwfWinDumpPackets("vboxNetLwfWinSendNetBufferLists: got", pBufLists);
1632
1633 if (!ASMAtomicReadBool(&pModule->fActive))
1634 {
1635 /*
1636 * The trunk is inactive, jusp pass along all packets to the next
1637 * underlying driver.
1638 */
1639 NdisFSendNetBufferLists(pModule->hFilter, pBufLists, nPort, fFlags);
1640 return;
1641 }
1642
1643 if (vboxNetLwfWinIsRunning(pModule))
1644 {
1645 PNET_BUFFER_LIST pNext = NULL;
1646 PNET_BUFFER_LIST pDropHead = NULL;
1647 PNET_BUFFER_LIST pDropTail = NULL;
1648 PNET_BUFFER_LIST pPassHead = NULL;
1649 PNET_BUFFER_LIST pPassTail = NULL;
1650 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = pNext)
1651 {
1652 pNext = NET_BUFFER_LIST_NEXT_NBL(pList);
1653 NET_BUFFER_LIST_NEXT_NBL(pList) = NULL; /* Unlink */
1654 if (vboxNetLwfWinForwardToIntNet(pModule, pList, INTNETTRUNKDIR_HOST))
1655 {
1656 NET_BUFFER_LIST_STATUS(pList) = NDIS_STATUS_SUCCESS;
1657 if (pDropHead)
1658 {
1659 NET_BUFFER_LIST_NEXT_NBL(pDropTail) = pList;
1660 pDropTail = pList;
1661 }
1662 else
1663 pDropHead = pDropTail = pList;
1664 }
1665 else
1666 {
1667 if (pPassHead)
1668 {
1669 NET_BUFFER_LIST_NEXT_NBL(pPassTail) = pList;
1670 pPassTail = pList;
1671 }
1672 else
1673 pPassHead = pPassTail = pList;
1674 }
1675 }
1676 Assert((pBufLists == pPassHead) || (pBufLists == pDropHead));
1677 if (pPassHead)
1678 {
1679 vboxNetLwfWinDumpPackets("vboxNetLwfWinSendNetBufferLists: passing down", pPassHead);
1680 NdisFSendNetBufferLists(pModule->hFilter, pBufLists, nPort, fFlags);
1681 }
1682 if (pDropHead)
1683 {
1684 vboxNetLwfWinDumpPackets("vboxNetLwfWinSendNetBufferLists: consumed", pDropHead);
1685 NdisFSendNetBufferListsComplete(pModule->hFilter, pDropHead,
1686 fFlags & NDIS_SEND_FLAGS_DISPATCH_LEVEL ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0);
1687 }
1688 }
1689 else
1690 {
1691 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = NET_BUFFER_LIST_NEXT_NBL(pList))
1692 {
1693 NET_BUFFER_LIST_STATUS(pList) = NDIS_STATUS_PAUSED;
1694 }
1695 vboxNetLwfWinDumpPackets("vboxNetLwfWinSendNetBufferLists: consumed", pBufLists);
1696 NdisFSendNetBufferListsComplete(pModule->hFilter, pBufLists,
1697 fFlags & NDIS_SEND_FLAGS_DISPATCH_LEVEL ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0);
1698
1699 }
1700 LogFlow(("<==vboxNetLwfWinSendNetBufferLists\n"));
1701}
1702
1703VOID vboxNetLwfWinSendNetBufferListsComplete(IN NDIS_HANDLE hModuleCtx, IN PNET_BUFFER_LIST pBufLists, IN ULONG fFlags)
1704{
1705 LogFlow(("==>vboxNetLwfWinSendNetBufferListsComplete: module=%p\n", hModuleCtx));
1706 PVBOXNETLWF_MODULE pModule = (PVBOXNETLWF_MODULE)hModuleCtx;
1707 PNET_BUFFER_LIST pList = pBufLists;
1708 PNET_BUFFER_LIST pNextList;
1709 PNET_BUFFER_LIST pPrevList = NULL;
1710 while (pList)
1711 {
1712 pNextList = NET_BUFFER_LIST_NEXT_NBL(pList);
1713 if (pList->SourceHandle == pModule->hFilter)
1714 {
1715 /* We allocated this NET_BUFFER_LIST, let's free it up */
1716 Assert(NET_BUFFER_LIST_FIRST_NB(pList));
1717 Assert(NET_BUFFER_FIRST_MDL(NET_BUFFER_LIST_FIRST_NB(pList)));
1718 /*
1719 * All our NBLs hold a single NB each, no need to iterate over a list.
1720 * There is no need to free an associated NB explicitly either, as it was
1721 * preallocated with NBL structure.
1722 */
1723 Assert(!NET_BUFFER_NEXT_NB(NET_BUFFER_LIST_FIRST_NB(pList)));
1724 vboxNetLwfWinFreeMdlChain(NET_BUFFER_FIRST_MDL(NET_BUFFER_LIST_FIRST_NB(pList)));
1725 /* Unlink this list from the chain */
1726 if (pPrevList)
1727 NET_BUFFER_LIST_NEXT_NBL(pPrevList) = pNextList;
1728 else
1729 pBufLists = pNextList;
1730 Log(("vboxNetLwfWinSendNetBufferListsComplete: our list %p, next=%p, previous=%p, head=%p\n", pList, pNextList, pPrevList, pBufLists));
1731 NdisFreeNetBufferList(pList);
1732#ifdef VBOXNETLWF_SYNC_SEND
1733 Log4(("vboxNetLwfWinSendNetBufferListsComplete: freed NBL+NB 0x%p\n", pList));
1734 KeSetEvent(&pModule->EventWire, 0, FALSE);
1735#else /* !VBOXNETLWF_SYNC_SEND */
1736 Log4(("vboxNetLwfWinSendNetBufferListsComplete: freed NBL+NB+MDL+Data 0x%p\n", pList));
1737 Assert(ASMAtomicReadS32(&pModule->cPendingBuffers) > 0);
1738 if (ASMAtomicDecS32(&pModule->cPendingBuffers) == 0)
1739 NdisSetEvent(&pModule->EventSendComplete);
1740#endif /* !VBOXNETLWF_SYNC_SEND */
1741 }
1742 else
1743 {
1744 pPrevList = pList;
1745 Log(("vboxNetLwfWinSendNetBufferListsComplete: passing list %p, next=%p, previous=%p, head=%p\n", pList, pNextList, pPrevList, pBufLists));
1746 }
1747 pList = pNextList;
1748 }
1749 if (pBufLists)
1750 {
1751 /* There are still lists remaining in the chain, pass'em up */
1752 NdisFSendNetBufferListsComplete(pModule->hFilter, pBufLists, fFlags);
1753 }
1754 LogFlow(("<==vboxNetLwfWinSendNetBufferListsComplete\n"));
1755}
1756
1757VOID vboxNetLwfWinReceiveNetBufferLists(IN NDIS_HANDLE hModuleCtx,
1758 IN PNET_BUFFER_LIST pBufLists,
1759 IN NDIS_PORT_NUMBER nPort,
1760 IN ULONG nBufLists,
1761 IN ULONG fFlags)
1762{
1763 /// @todo Do we need loopback handling?
1764 LogFlow(("==>vboxNetLwfWinReceiveNetBufferLists: module=%p\n", hModuleCtx));
1765 PVBOXNETLWF_MODULE pModule = (PVBOXNETLWF_MODULE)hModuleCtx;
1766 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: got", pBufLists);
1767
1768 if (!ASMAtomicReadBool(&pModule->fActive))
1769 {
1770 /*
1771 * The trunk is inactive, jusp pass along all packets to the next
1772 * overlying driver.
1773 */
1774 NdisFIndicateReceiveNetBufferLists(pModule->hFilter, pBufLists, nPort, nBufLists, fFlags);
1775 LogFlow(("<==vboxNetLwfWinReceiveNetBufferLists: inactive trunk\n"));
1776 return;
1777 }
1778
1779 if (vboxNetLwfWinIsRunning(pModule))
1780 {
1781 if (NDIS_TEST_RECEIVE_CANNOT_PEND(fFlags))
1782 {
1783 /* We do not own NBLs so we do not need to return them */
1784 /* First we need to scan through the list to see if some packets must be dropped */
1785 bool bDropIt = false;
1786 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = NET_BUFFER_LIST_NEXT_NBL(pList))
1787 {
1788 PNET_BUFFER_LIST pNext = NET_BUFFER_LIST_NEXT_NBL(pList);
1789 NET_BUFFER_LIST_NEXT_NBL(pList) = NULL; /* Unlink temporarily */
1790 if (vboxNetLwfWinForwardToIntNet(pModule, pList, INTNETTRUNKDIR_WIRE))
1791 bDropIt = true;
1792 NET_BUFFER_LIST_NEXT_NBL(pList) = pNext; /* Restore the link */
1793 }
1794 if (bDropIt)
1795 {
1796 /* Some NBLs must be dropped, indicate selectively one by one */
1797 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = NET_BUFFER_LIST_NEXT_NBL(pList))
1798 {
1799 PNET_BUFFER_LIST pNext = NET_BUFFER_LIST_NEXT_NBL(pList);
1800 NET_BUFFER_LIST_NEXT_NBL(pList) = NULL; /* Unlink temporarily */
1801 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: passing up", pList);
1802 NdisFIndicateReceiveNetBufferLists(pModule->hFilter, pList, nPort, nBufLists, fFlags);
1803 NET_BUFFER_LIST_NEXT_NBL(pList) = pNext; /* Restore the link */
1804 }
1805 }
1806 else
1807 {
1808 /* All NBLs must be indicated, do it in bulk. */
1809 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: passing up", pBufLists);
1810 NdisFIndicateReceiveNetBufferLists(pModule->hFilter, pBufLists, nPort, nBufLists, fFlags);
1811 }
1812 }
1813 else
1814 {
1815 /* We collect dropped NBLs in a separate list in order to "return" them. */
1816 PNET_BUFFER_LIST pNext = NULL;
1817 PNET_BUFFER_LIST pDropHead = NULL;
1818 PNET_BUFFER_LIST pDropTail = NULL;
1819 PNET_BUFFER_LIST pPassHead = NULL;
1820 PNET_BUFFER_LIST pPassTail = NULL;
1821 ULONG nDrop = 0, nPass = 0;
1822 for (PNET_BUFFER_LIST pList = pBufLists; pList; pList = pNext)
1823 {
1824 pNext = NET_BUFFER_LIST_NEXT_NBL(pList);
1825 NET_BUFFER_LIST_NEXT_NBL(pList) = NULL; /* Unlink */
1826 if (vboxNetLwfWinForwardToIntNet(pModule, pList, INTNETTRUNKDIR_WIRE))
1827 {
1828 if (nDrop++)
1829 {
1830 NET_BUFFER_LIST_NEXT_NBL(pDropTail) = pList;
1831 pDropTail = pList;
1832 }
1833 else
1834 pDropHead = pDropTail = pList;
1835 }
1836 else
1837 {
1838 if (nPass++)
1839 {
1840 NET_BUFFER_LIST_NEXT_NBL(pPassTail) = pList;
1841 pPassTail = pList;
1842 }
1843 else
1844 pPassHead = pPassTail = pList;
1845 }
1846 }
1847 Assert((pBufLists == pPassHead) || (pBufLists == pDropHead));
1848 Assert(nDrop + nPass == nBufLists);
1849 if (pPassHead)
1850 {
1851 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: passing up", pPassHead);
1852 NdisFIndicateReceiveNetBufferLists(pModule->hFilter, pPassHead, nPort, nPass, fFlags);
1853 }
1854 if (pDropHead)
1855 {
1856 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: consumed", pDropHead);
1857 NdisFReturnNetBufferLists(pModule->hFilter, pDropHead,
1858 fFlags & NDIS_RECEIVE_FLAGS_DISPATCH_LEVEL ? NDIS_RETURN_FLAGS_DISPATCH_LEVEL : 0);
1859 }
1860 }
1861
1862 }
1863 else
1864 {
1865 vboxNetLwfWinDumpPackets("vboxNetLwfWinReceiveNetBufferLists: consumed", pBufLists);
1866 if ((fFlags & NDIS_RECEIVE_FLAGS_RESOURCES) == 0)
1867 NdisFReturnNetBufferLists(pModule->hFilter, pBufLists,
1868 fFlags & NDIS_RECEIVE_FLAGS_DISPATCH_LEVEL ? NDIS_RETURN_FLAGS_DISPATCH_LEVEL : 0);
1869 }
1870 LogFlow(("<==vboxNetLwfWinReceiveNetBufferLists\n"));
1871}
1872
1873VOID vboxNetLwfWinReturnNetBufferLists(IN NDIS_HANDLE hModuleCtx, IN PNET_BUFFER_LIST pBufLists, IN ULONG fFlags)
1874{
1875 LogFlow(("==>vboxNetLwfWinReturnNetBufferLists: module=%p\n", hModuleCtx));
1876 PVBOXNETLWF_MODULE pModule = (PVBOXNETLWF_MODULE)hModuleCtx;
1877 PNET_BUFFER_LIST pList = pBufLists;
1878 PNET_BUFFER_LIST pNextList;
1879 PNET_BUFFER_LIST pPrevList = NULL;
1880 /** @todo Move common part into a separate function to be used by vboxNetLwfWinSendNetBufferListsComplete() as well */
1881 while (pList)
1882 {
1883 pNextList = NET_BUFFER_LIST_NEXT_NBL(pList);
1884 if (pList->SourceHandle == pModule->hFilter)
1885 {
1886 /* We allocated this NET_BUFFER_LIST, let's free it up */
1887 Assert(NET_BUFFER_LIST_FIRST_NB(pList));
1888 Assert(NET_BUFFER_FIRST_MDL(NET_BUFFER_LIST_FIRST_NB(pList)));
1889 /*
1890 * All our NBLs hold a single NB each, no need to iterate over a list.
1891 * There is no need to free an associated NB explicitly either, as it was
1892 * preallocated with NBL structure.
1893 */
1894 vboxNetLwfWinFreeMdlChain(NET_BUFFER_FIRST_MDL(NET_BUFFER_LIST_FIRST_NB(pList)));
1895 /* Unlink this list from the chain */
1896 if (pPrevList)
1897 NET_BUFFER_LIST_NEXT_NBL(pPrevList) = pNextList;
1898 else
1899 pBufLists = pNextList;
1900 NdisFreeNetBufferList(pList);
1901#ifdef VBOXNETLWF_SYNC_SEND
1902 Log4(("vboxNetLwfWinReturnNetBufferLists: freed NBL+NB 0x%p\n", pList));
1903 KeSetEvent(&pModule->EventHost, 0, FALSE);
1904#else /* !VBOXNETLWF_SYNC_SEND */
1905 Log4(("vboxNetLwfWinReturnNetBufferLists: freed NBL+NB+MDL+Data 0x%p\n", pList));
1906 Assert(ASMAtomicReadS32(&pModule->cPendingBuffers) > 0);
1907 if (ASMAtomicDecS32(&pModule->cPendingBuffers) == 0)
1908 NdisSetEvent(&pModule->EventSendComplete);
1909#endif /* !VBOXNETLWF_SYNC_SEND */
1910 }
1911 else
1912 pPrevList = pList;
1913 pList = pNextList;
1914 }
1915 if (pBufLists)
1916 {
1917 /* There are still lists remaining in the chain, pass'em up */
1918 NdisFReturnNetBufferLists(pModule->hFilter, pBufLists, fFlags);
1919 }
1920 LogFlow(("<==vboxNetLwfWinReturnNetBufferLists\n"));
1921}
1922
1923/**
1924 * register the filter driver
1925 */
1926DECLHIDDEN(NDIS_STATUS) vboxNetLwfWinRegister(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPathStr)
1927{
1928 RT_NOREF1(pRegistryPathStr);
1929 NDIS_FILTER_DRIVER_CHARACTERISTICS FChars;
1930 NDIS_STRING FriendlyName;
1931 NDIS_STRING UniqueName;
1932 NDIS_STRING ServiceName;
1933
1934 NdisInitUnicodeString(&FriendlyName, VBOXNETLWF_NAME_FRIENDLY);
1935 NdisInitUnicodeString(&UniqueName, VBOXNETLWF_NAME_UNIQUE);
1936 NdisInitUnicodeString(&ServiceName, VBOXNETLWF_NAME_SERVICE);
1937
1938 NdisZeroMemory(&FChars, sizeof (FChars));
1939
1940 FChars.Header.Type = NDIS_OBJECT_TYPE_FILTER_DRIVER_CHARACTERISTICS;
1941 FChars.Header.Size = sizeof(NDIS_FILTER_DRIVER_CHARACTERISTICS);
1942 FChars.Header.Revision = NDIS_FILTER_CHARACTERISTICS_REVISION_1;
1943
1944 FChars.MajorNdisVersion = VBOXNETLWF_VERSION_NDIS_MAJOR;
1945 FChars.MinorNdisVersion = VBOXNETLWF_VERSION_NDIS_MINOR;
1946
1947 FChars.FriendlyName = FriendlyName;
1948 FChars.UniqueName = UniqueName;
1949 FChars.ServiceName = ServiceName;
1950
1951 /* Mandatory functions */
1952 FChars.AttachHandler = vboxNetLwfWinAttach;
1953 FChars.DetachHandler = vboxNetLwfWinDetach;
1954 FChars.RestartHandler = vboxNetLwfWinRestart;
1955 FChars.PauseHandler = vboxNetLwfWinPause;
1956
1957 /* Optional functions, non changeble at run-time */
1958 FChars.OidRequestHandler = vboxNetLwfWinOidRequest;
1959 FChars.OidRequestCompleteHandler = vboxNetLwfWinOidRequestComplete;
1960 //FChars.CancelOidRequestHandler = vboxNetLwfWinCancelOidRequest;
1961 FChars.StatusHandler = vboxNetLwfWinStatus;
1962 //FChars.NetPnPEventHandler = vboxNetLwfWinPnPEvent;
1963
1964 /* Datapath functions */
1965 FChars.SendNetBufferListsHandler = vboxNetLwfWinSendNetBufferLists;
1966 FChars.SendNetBufferListsCompleteHandler = vboxNetLwfWinSendNetBufferListsComplete;
1967 FChars.ReceiveNetBufferListsHandler = vboxNetLwfWinReceiveNetBufferLists;
1968 FChars.ReturnNetBufferListsHandler = vboxNetLwfWinReturnNetBufferLists;
1969
1970 pDriverObject->DriverUnload = vboxNetLwfWinUnloadDriver;
1971
1972 NDIS_STATUS Status;
1973 g_VBoxNetLwfGlobals.hFilterDriver = NULL;
1974 Log(("vboxNetLwfWinRegister: registering filter driver...\n"));
1975 Status = NdisFRegisterFilterDriver(pDriverObject,
1976 (NDIS_HANDLE)&g_VBoxNetLwfGlobals,
1977 &FChars,
1978 &g_VBoxNetLwfGlobals.hFilterDriver);
1979 Assert(Status == STATUS_SUCCESS);
1980 if (Status == STATUS_SUCCESS)
1981 {
1982 Log(("vboxNetLwfWinRegister: successfully registered filter driver; registering device...\n"));
1983 Status = vboxNetLwfWinDevCreate(&g_VBoxNetLwfGlobals);
1984 Assert(Status == STATUS_SUCCESS);
1985 Log(("vboxNetLwfWinRegister: vboxNetLwfWinDevCreate() returned 0x%x\n", Status));
1986 }
1987 else
1988 {
1989 LogError(("vboxNetLwfWinRegister: failed to register filter driver, status=0x%x", Status));
1990 }
1991 return Status;
1992}
1993
1994static int vboxNetLwfWinStartInitIdcThread()
1995{
1996 int rc = VERR_INVALID_STATE;
1997
1998 if (ASMAtomicCmpXchgU32(&g_VBoxNetLwfGlobals.enmIdcState, LwfIdcState_Connecting, LwfIdcState_Disconnected))
1999 {
2000 Log(("vboxNetLwfWinStartInitIdcThread: IDC state change Diconnected -> Connecting\n"));
2001
2002 NTSTATUS Status = PsCreateSystemThread(&g_VBoxNetLwfGlobals.hInitIdcThread,
2003 THREAD_ALL_ACCESS,
2004 NULL,
2005 NULL,
2006 NULL,
2007 vboxNetLwfWinInitIdcWorker,
2008 &g_VBoxNetLwfGlobals);
2009 Log(("vboxNetLwfWinStartInitIdcThread: create IDC initialization thread, status=0x%x\n", Status));
2010 if (Status != STATUS_SUCCESS)
2011 {
2012 LogError(("vboxNetLwfWinStartInitIdcThread: IDC initialization failed (system thread creation, status=0x%x)\n", Status));
2013 /*
2014 * We failed to init IDC and there will be no second chance.
2015 */
2016 Log(("vboxNetLwfWinStartInitIdcThread: IDC state change Connecting -> Diconnected\n"));
2017 ASMAtomicWriteU32(&g_VBoxNetLwfGlobals.enmIdcState, LwfIdcState_Disconnected);
2018 }
2019 rc = RTErrConvertFromNtStatus(Status);
2020 }
2021 return rc;
2022}
2023
2024static void vboxNetLwfWinStopInitIdcThread()
2025{
2026}
2027
2028
2029RT_C_DECLS_BEGIN
2030
2031NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath);
2032
2033RT_C_DECLS_END
2034
2035NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
2036{
2037 NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
2038 int rc;
2039
2040 /* the idc registration is initiated via IOCTL since our driver
2041 * can be loaded when the VBoxDrv is not in case we are a Ndis IM driver */
2042 rc = vboxNetLwfWinInitBase();
2043 AssertRC(rc);
2044 if (RT_SUCCESS(rc))
2045 {
2046 NdisZeroMemory(&g_VBoxNetLwfGlobals, sizeof (g_VBoxNetLwfGlobals));
2047 RTListInit(&g_VBoxNetLwfGlobals.listModules);
2048 NdisAllocateSpinLock(&g_VBoxNetLwfGlobals.Lock);
2049 /*
2050 * We choose to ignore IDC initialization errors here because if we fail to load
2051 * our filter the upper protocols won't bind to the associated adapter, causing
2052 * network failure at the host. Better to have non-working filter than broken
2053 * networking on the host.
2054 */
2055 rc = vboxNetLwfWinStartInitIdcThread();
2056 AssertRC(rc);
2057
2058 Status = vboxNetLwfWinRegister(pDriverObject, pRegistryPath);
2059 Assert(Status == STATUS_SUCCESS);
2060 if (Status == NDIS_STATUS_SUCCESS)
2061 {
2062 Log(("NETLWF: started successfully\n"));
2063 return STATUS_SUCCESS;
2064 }
2065 NdisFreeSpinLock(&g_VBoxNetLwfGlobals.Lock);
2066 vboxNetLwfWinFini();
2067 }
2068 else
2069 {
2070 Status = NDIS_STATUS_FAILURE;
2071 }
2072
2073 return Status;
2074}
2075
2076
2077static VOID vboxNetLwfWinUnloadDriver(IN PDRIVER_OBJECT pDriver)
2078{
2079 RT_NOREF1(pDriver);
2080 LogFlow(("==>vboxNetLwfWinUnloadDriver: driver=%p\n", pDriver));
2081 vboxNetLwfWinDevDestroy(&g_VBoxNetLwfGlobals);
2082 NdisFDeregisterFilterDriver(g_VBoxNetLwfGlobals.hFilterDriver);
2083 NdisFreeSpinLock(&g_VBoxNetLwfGlobals.Lock);
2084 LogFlow(("<==vboxNetLwfWinUnloadDriver\n"));
2085 vboxNetLwfWinFini();
2086}
2087
2088static const char *vboxNetLwfWinIdcStateToText(uint32_t enmState)
2089{
2090 switch (enmState)
2091 {
2092 case LwfIdcState_Disconnected: return "Disconnected";
2093 case LwfIdcState_Connecting: return "Connecting";
2094 case LwfIdcState_Connected: return "Connected";
2095 case LwfIdcState_Stopping: return "Stopping";
2096 }
2097 return "Unknown";
2098}
2099
2100static VOID vboxNetLwfWinInitIdcWorker(PVOID pvContext)
2101{
2102 int rc;
2103 PVBOXNETLWFGLOBALS pGlobals = (PVBOXNETLWFGLOBALS)pvContext;
2104
2105 while (ASMAtomicReadU32(&pGlobals->enmIdcState) == LwfIdcState_Connecting)
2106 {
2107 rc = vboxNetFltInitIdc(&g_VBoxNetFltGlobals);
2108 if (RT_SUCCESS(rc))
2109 {
2110 if (!ASMAtomicCmpXchgU32(&pGlobals->enmIdcState, LwfIdcState_Connected, LwfIdcState_Connecting))
2111 {
2112 /* The state has been changed (the only valid transition is to "Stopping"), undo init */
2113 rc = vboxNetFltTryDeleteIdc(&g_VBoxNetFltGlobals);
2114 Log(("vboxNetLwfWinInitIdcWorker: state change (Connecting -> %s) while initializing IDC, deleted IDC, rc=0x%x\n",
2115 vboxNetLwfWinIdcStateToText(ASMAtomicReadU32(&pGlobals->enmIdcState)), rc));
2116 }
2117 else
2118 {
2119 Log(("vboxNetLwfWinInitIdcWorker: IDC state change Connecting -> Connected\n"));
2120 }
2121 }
2122 else
2123 {
2124 LARGE_INTEGER WaitIn100nsUnits;
2125 WaitIn100nsUnits.QuadPart = -(LONGLONG)10000000; /* 1 sec */
2126 KeDelayExecutionThread(KernelMode, FALSE /* non-alertable */, &WaitIn100nsUnits);
2127 }
2128 }
2129 PsTerminateSystemThread(STATUS_SUCCESS);
2130}
2131
2132static int vboxNetLwfWinTryFiniIdc()
2133{
2134 int rc = VINF_SUCCESS;
2135 NTSTATUS Status;
2136 PKTHREAD pThread = NULL;
2137 uint32_t enmPrevState = ASMAtomicXchgU32(&g_VBoxNetLwfGlobals.enmIdcState, LwfIdcState_Stopping);
2138
2139 Log(("vboxNetLwfWinTryFiniIdc: IDC state change %s -> Stopping\n", vboxNetLwfWinIdcStateToText(enmPrevState)));
2140
2141 switch (enmPrevState)
2142 {
2143 case LwfIdcState_Disconnected:
2144 /* Have not even attempted to connect -- nothing to do. */
2145 break;
2146 case LwfIdcState_Stopping:
2147 /* Impossible, but another thread is alreading doing FiniIdc, bail out */
2148 LogError(("vboxNetLwfWinTryFiniIdc: called in 'Stopping' state\n"));
2149 rc = VERR_INVALID_STATE;
2150 break;
2151 case LwfIdcState_Connecting:
2152 /* the worker thread is running, let's wait for it to stop */
2153 Status = ObReferenceObjectByHandle(g_VBoxNetLwfGlobals.hInitIdcThread,
2154 THREAD_ALL_ACCESS, NULL, KernelMode,
2155 (PVOID*)&pThread, NULL);
2156 if (Status == STATUS_SUCCESS)
2157 {
2158 KeWaitForSingleObject(pThread, Executive, KernelMode, FALSE, NULL);
2159 ObDereferenceObject(pThread);
2160 }
2161 else
2162 {
2163 LogError(("vboxNetLwfWinTryFiniIdc: ObReferenceObjectByHandle(%p) failed with 0x%x\n",
2164 g_VBoxNetLwfGlobals.hInitIdcThread, Status));
2165 }
2166 rc = RTErrConvertFromNtStatus(Status);
2167 break;
2168 case LwfIdcState_Connected:
2169 /* the worker succeeded in IDC init and terminated */
2170 rc = vboxNetFltTryDeleteIdc(&g_VBoxNetFltGlobals);
2171 Log(("vboxNetLwfWinTryFiniIdc: deleted IDC, rc=0x%x\n", rc));
2172 break;
2173 }
2174 return rc;
2175}
2176
2177static void vboxNetLwfWinFiniBase()
2178{
2179 vboxNetFltDeleteGlobals(&g_VBoxNetFltGlobals);
2180
2181 /*
2182 * Undo the work done during start (in reverse order).
2183 */
2184 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
2185
2186 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
2187 RTLogDestroy(RTLogSetDefaultInstance(NULL));
2188
2189 RTR0Term();
2190}
2191
2192static int vboxNetLwfWinInitBase()
2193{
2194 int rc = RTR0Init(0);
2195 if (!RT_SUCCESS(rc))
2196 return rc;
2197
2198 memset(&g_VBoxNetFltGlobals, 0, sizeof(g_VBoxNetFltGlobals));
2199 rc = vboxNetFltInitGlobals(&g_VBoxNetFltGlobals);
2200 if (!RT_SUCCESS(rc))
2201 RTR0Term();
2202
2203 return rc;
2204}
2205
2206static int vboxNetLwfWinFini()
2207{
2208 int rc = vboxNetLwfWinTryFiniIdc();
2209 if (RT_SUCCESS(rc))
2210 {
2211 vboxNetLwfWinFiniBase();
2212 }
2213 return rc;
2214}
2215
2216
2217/*
2218 *
2219 * The OS specific interface definition
2220 *
2221 */
2222
2223
2224bool vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis)
2225{
2226 LogFlow(("==>vboxNetFltOsMaybeRediscovered: instance=%p\n", pThis));
2227 LogFlow(("<==vboxNetFltOsMaybeRediscovered: return %RTbool\n", !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost)));
2228 /* AttachToInterface true if disconnected */
2229 return !ASMAtomicUoReadBool(&pThis->fDisconnectedFromHost);
2230}
2231
2232int vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst)
2233{
2234 RT_NOREF1(pvIfData);
2235 int rc = VINF_SUCCESS;
2236
2237 PVBOXNETLWF_MODULE pModule = (PVBOXNETLWF_MODULE)pThis->u.s.WinIf.hModuleCtx;
2238 LogFlow(("==>vboxNetFltPortOsXmit: instance=%p module=%p\n", pThis, pModule));
2239 if (!pModule)
2240 {
2241 LogFlow(("<==vboxNetFltPortOsXmit: pModule is null, return %d\n", VERR_INTERNAL_ERROR));
2242 return VERR_INTERNAL_ERROR;
2243 }
2244 /* Prevent going into "paused" state until all transmissions have been completed. */
2245 NDIS_WAIT_FOR_MUTEX(&pModule->InTransmit);
2246 /* Ignore all sends if the stack is paused or being paused, etc... */
2247 if (!vboxNetLwfWinIsRunning(pModule))
2248 {
2249 NDIS_RELEASE_MUTEX(&pModule->InTransmit);
2250 return VINF_SUCCESS;
2251 }
2252
2253 vboxNetLwfWinDumpPacket(pSG, !(fDst & INTNETTRUNKDIR_WIRE) ? "intnet --> host"
2254 : !(fDst & INTNETTRUNKDIR_HOST) ? "intnet --> wire" : "intnet --> all");
2255
2256 /*
2257 * There are two possible strategies to deal with incoming SGs:
2258 * 1) make a copy of data and complete asynchronously;
2259 * 2) complete synchronously using the original data buffers.
2260 * Before we consider implementing (1) it is quite interesting to see
2261 * how well (2) performs. So we block until our requests are complete.
2262 * Actually there is third possibility -- to use SG retain/release
2263 * callbacks, but those seem not be fully implemented yet.
2264 * Note that ansynchronous completion will require different implementation
2265 * of vboxNetLwfWinPause(), not relying on InTransmit mutex.
2266 */
2267#ifdef VBOXNETLWF_SYNC_SEND
2268 PVOID aEvents[2]; /* To wire and to host */
2269 ULONG nEvents = 0;
2270 LARGE_INTEGER timeout;
2271 timeout.QuadPart = -(LONGLONG)10000000; /* 1 sec */
2272#endif /* VBOXNETLWF_SYNC_SEND */
2273 if (fDst & INTNETTRUNKDIR_WIRE)
2274 {
2275 PNET_BUFFER_LIST pBufList = vboxNetLwfWinSGtoNB(pModule, pSG);
2276 if (pBufList)
2277 {
2278 vboxNetLwfWinDumpPackets("vboxNetFltPortOsXmit: sending down", pBufList);
2279#ifdef VBOXNETLWF_SYNC_SEND
2280 aEvents[nEvents++] = &pModule->EventWire;
2281#else /* !VBOXNETLWF_SYNC_SEND */
2282 if (ASMAtomicIncS32(&pModule->cPendingBuffers) == 1)
2283 NdisResetEvent(&pModule->EventSendComplete);
2284#endif /* !VBOXNETLWF_SYNC_SEND */
2285 NdisFSendNetBufferLists(pModule->hFilter, pBufList, NDIS_DEFAULT_PORT_NUMBER, 0); /** @todo sendFlags! */
2286 }
2287 }
2288 if (fDst & INTNETTRUNKDIR_HOST)
2289 {
2290 PNET_BUFFER_LIST pBufList = vboxNetLwfWinSGtoNB(pModule, pSG);
2291 if (pBufList)
2292 {
2293 vboxNetLwfWinDumpPackets("vboxNetFltPortOsXmit: sending up", pBufList);
2294#ifdef VBOXNETLWF_SYNC_SEND
2295 aEvents[nEvents++] = &pModule->EventHost;
2296#else /* !VBOXNETLWF_SYNC_SEND */
2297 if (ASMAtomicIncS32(&pModule->cPendingBuffers) == 1)
2298 NdisResetEvent(&pModule->EventSendComplete);
2299#endif /* !VBOXNETLWF_SYNC_SEND */
2300 NdisFIndicateReceiveNetBufferLists(pModule->hFilter, pBufList, NDIS_DEFAULT_PORT_NUMBER, 1, 0);
2301 }
2302 }
2303#ifdef VBOXNETLWF_SYNC_SEND
2304 if (nEvents)
2305 {
2306 NTSTATUS Status = KeWaitForMultipleObjects(nEvents, aEvents, WaitAll, Executive, KernelMode, FALSE, &timeout, NULL);
2307 if (Status != STATUS_SUCCESS)
2308 {
2309 LogError(("vboxNetFltPortOsXmit: KeWaitForMultipleObjects() failed with 0x%x\n", Status));
2310 if (Status == STATUS_TIMEOUT)
2311 rc = VERR_TIMEOUT;
2312 else
2313 rc = RTErrConvertFromNtStatus(Status);
2314 }
2315 }
2316#endif /* VBOXNETLWF_SYNC_SEND */
2317 NDIS_RELEASE_MUTEX(&pModule->InTransmit);
2318
2319 LogFlow(("<==vboxNetFltPortOsXmit: return %d\n", rc));
2320 return rc;
2321}
2322
2323
2324NDIS_IO_WORKITEM_FUNCTION vboxNetLwfWinToggleOffloading;
2325
2326VOID vboxNetLwfWinToggleOffloading(PVOID WorkItemContext, NDIS_HANDLE NdisIoWorkItemHandle)
2327{
2328 /* WARNING! Call this with IRQL=Passive! */
2329 RT_NOREF1(NdisIoWorkItemHandle);
2330 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)WorkItemContext;
2331
2332 if (ASMAtomicReadBool(&pModuleCtx->fActive))
2333 {
2334 /* Disable offloading temporarily by indicating offload config change. */
2335 /** @todo Be sure to revise this when implementing offloading support! */
2336 NDIS_OFFLOAD OffloadConfig;
2337 if (pModuleCtx->fOffloadConfigValid)
2338 {
2339 OffloadConfig = pModuleCtx->SavedOffloadConfig;
2340 vboxNetLwfWinDisableOffloading(&OffloadConfig);
2341 }
2342 else
2343 {
2344 DbgPrint("VBoxNetLwf: no saved offload config to modify for %s\n", pModuleCtx->szMiniportName);
2345 NdisZeroMemory(&OffloadConfig, sizeof(OffloadConfig));
2346 OffloadConfig.Header.Type = NDIS_OBJECT_TYPE_OFFLOAD;
2347 OffloadConfig.Header.Revision = NDIS_OFFLOAD_REVISION_1;
2348 OffloadConfig.Header.Size = NDIS_SIZEOF_NDIS_OFFLOAD_REVISION_1;
2349 }
2350 vboxNetLwfWinIndicateOffload(pModuleCtx, &OffloadConfig);
2351 Log(("vboxNetLwfWinToggleOffloading: set offloading off\n"));
2352 }
2353 else
2354 {
2355 /* The filter is inactive -- restore offloading configuration. */
2356 if (pModuleCtx->fOffloadConfigValid)
2357 {
2358 vboxNetLwfWinIndicateOffload(pModuleCtx, &pModuleCtx->SavedOffloadConfig);
2359 Log(("vboxNetLwfWinToggleOffloading: restored offloading config\n"));
2360 }
2361 else
2362 DbgPrint("VBoxNetLwf: no saved offload config to restore for %s\n", pModuleCtx->szMiniportName);
2363 }
2364}
2365
2366
2367void vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive)
2368{
2369 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)pThis->u.s.WinIf.hModuleCtx;
2370 LogFlow(("==>vboxNetFltPortOsSetActive: instance=%p module=%p fActive=%RTbool\n", pThis, pModuleCtx, fActive));
2371 if (!pModuleCtx)
2372 {
2373 LogFlow(("<==vboxNetFltPortOsSetActive: pModuleCtx is null\n"));
2374 return;
2375 }
2376
2377 NDIS_STATUS Status = STATUS_SUCCESS;
2378 bool fOldActive = ASMAtomicXchgBool(&pModuleCtx->fActive, fActive);
2379 if (fOldActive != fActive)
2380 {
2381 NdisQueueIoWorkItem(pModuleCtx->hWorkItem, vboxNetLwfWinToggleOffloading, pModuleCtx);
2382 Status = vboxNetLwfWinSetPacketFilter(pModuleCtx, fActive);
2383 LogFlow(("<==vboxNetFltPortOsSetActive: vboxNetLwfWinSetPacketFilter() returned 0x%x\n", Status));
2384 }
2385 else
2386 LogFlow(("<==vboxNetFltPortOsSetActive: no change, remain %sactive\n", fActive ? "":"in"));
2387}
2388
2389int vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis)
2390{
2391 RT_NOREF1(pThis);
2392 LogFlow(("==>vboxNetFltOsDisconnectIt: instance=%p\n", pThis));
2393 LogFlow(("<==vboxNetFltOsDisconnectIt: return 0\n"));
2394 return VINF_SUCCESS;
2395}
2396
2397int vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis)
2398{
2399 RT_NOREF1(pThis);
2400 LogFlow(("==>vboxNetFltOsConnectIt: instance=%p\n", pThis));
2401 LogFlow(("<==vboxNetFltOsConnectIt: return 0\n"));
2402 return VINF_SUCCESS;
2403}
2404
2405/*
2406 * Uncommenting the following line produces debug log messages on IP address changes,
2407 * including wired interfaces. No actual calls to a switch port are made. This is for
2408 * debug purposes only!
2409 * #define VBOXNETLWFWIN_DEBUGIPADDRNOTIF 1
2410 */
2411static void __stdcall vboxNetLwfWinIpAddrChangeCallback(IN PVOID pvCtx,
2412 IN PMIB_UNICASTIPADDRESS_ROW pRow,
2413 IN MIB_NOTIFICATION_TYPE enmNotifType)
2414{
2415 PVBOXNETFLTINS pThis = (PVBOXNETFLTINS)pvCtx;
2416
2417 /* We are only interested in add or remove notifications. */
2418 bool fAdded;
2419 if (enmNotifType == MibAddInstance)
2420 fAdded = true;
2421 else if (enmNotifType == MibDeleteInstance)
2422 fAdded = false;
2423 else
2424 return;
2425
2426 if ( pRow
2427#ifndef VBOXNETLWFWIN_DEBUGIPADDRNOTIF
2428 && pThis->pSwitchPort->pfnNotifyHostAddress
2429#endif /* !VBOXNETLWFWIN_DEBUGIPADDRNOTIF */
2430 )
2431 {
2432 switch (pRow->Address.si_family)
2433 {
2434 case AF_INET:
2435 if ( IN4_IS_ADDR_LINKLOCAL(&pRow->Address.Ipv4.sin_addr)
2436 || pRow->Address.Ipv4.sin_addr.s_addr == IN4ADDR_LOOPBACK)
2437 {
2438 Log(("vboxNetLwfWinIpAddrChangeCallback: ignoring %s address (%RTnaipv4)\n",
2439 pRow->Address.Ipv4.sin_addr.s_addr == IN4ADDR_LOOPBACK ? "loopback" : "link-local",
2440 pRow->Address.Ipv4.sin_addr));
2441 break;
2442 }
2443 Log(("vboxNetLwfWinIpAddrChangeCallback: %s IPv4 addr=%RTnaipv4 on luid=(%u,%u)\n",
2444 fAdded ? "add" : "remove", pRow->Address.Ipv4.sin_addr,
2445 pRow->InterfaceLuid.Info.IfType, pRow->InterfaceLuid.Info.NetLuidIndex));
2446#ifndef VBOXNETLWFWIN_DEBUGIPADDRNOTIF
2447 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, fAdded, kIntNetAddrType_IPv4,
2448 &pRow->Address.Ipv4.sin_addr);
2449#endif /* !VBOXNETLWFWIN_DEBUGIPADDRNOTIF */
2450 break;
2451 case AF_INET6:
2452 if (Ipv6AddressScope(pRow->Address.Ipv6.sin6_addr.u.Byte) <= ScopeLevelLink)
2453 {
2454 Log(("vboxNetLwfWinIpAddrChangeCallback: ignoring link-local address (%RTnaipv6)\n",
2455 &pRow->Address.Ipv6.sin6_addr));
2456 break;
2457 }
2458 Log(("vboxNetLwfWinIpAddrChangeCallback: %s IPv6 addr=%RTnaipv6 scope=%d luid=(%u,%u)\n",
2459 fAdded ? "add" : "remove", &pRow->Address.Ipv6.sin6_addr,
2460 Ipv6AddressScope(pRow->Address.Ipv6.sin6_addr.u.Byte),
2461 pRow->InterfaceLuid.Info.IfType, pRow->InterfaceLuid.Info.NetLuidIndex));
2462#ifndef VBOXNETLWFWIN_DEBUGIPADDRNOTIF
2463 pThis->pSwitchPort->pfnNotifyHostAddress(pThis->pSwitchPort, fAdded, kIntNetAddrType_IPv6,
2464 &pRow->Address.Ipv6.sin6_addr);
2465#endif /* !VBOXNETLWFWIN_DEBUGIPADDRNOTIF */
2466 break;
2467 }
2468 }
2469 else
2470 Log(("vboxNetLwfWinIpAddrChangeCallback: pRow=%p pfnNotifyHostAddress=%p\n",
2471 pRow, pThis->pSwitchPort->pfnNotifyHostAddress));
2472}
2473
2474void vboxNetLwfWinRegisterIpAddrNotifier(PVBOXNETFLTINS pThis)
2475{
2476 LogFlow(("==>vboxNetLwfWinRegisterIpAddrNotifier: instance=%p\n", pThis));
2477 if ( pThis->pSwitchPort
2478#ifndef VBOXNETLWFWIN_DEBUGIPADDRNOTIF
2479 && pThis->pSwitchPort->pfnNotifyHostAddress
2480#endif /* !VBOXNETLWFWIN_DEBUGIPADDRNOTIF */
2481 )
2482 {
2483 NETIO_STATUS Status;
2484 /* First we need to go over all host IP addresses and add them via pfnNotifyHostAddress. */
2485 PMIB_UNICASTIPADDRESS_TABLE HostIpAddresses = NULL;
2486 Status = GetUnicastIpAddressTable(AF_UNSPEC, &HostIpAddresses);
2487 if (NETIO_SUCCESS(Status))
2488 {
2489 for (unsigned i = 0; i < HostIpAddresses->NumEntries; i++)
2490 vboxNetLwfWinIpAddrChangeCallback(pThis, &HostIpAddresses->Table[i], MibAddInstance);
2491 }
2492 else
2493 LogError(("vboxNetLwfWinRegisterIpAddrNotifier: GetUnicastIpAddressTable failed with %x\n", Status));
2494 /* Now we can register a callback function to keep track of address changes. */
2495 Status = NotifyUnicastIpAddressChange(AF_UNSPEC, vboxNetLwfWinIpAddrChangeCallback,
2496 pThis, false, &pThis->u.s.WinIf.hNotifier);
2497 if (NETIO_SUCCESS(Status))
2498 Log(("vboxNetLwfWinRegisterIpAddrNotifier: notifier=%p\n", pThis->u.s.WinIf.hNotifier));
2499 else
2500 LogError(("vboxNetLwfWinRegisterIpAddrNotifier: NotifyUnicastIpAddressChange failed with %x\n", Status));
2501 }
2502 else
2503 pThis->u.s.WinIf.hNotifier = NULL;
2504 LogFlow(("<==vboxNetLwfWinRegisterIpAddrNotifier\n"));
2505}
2506
2507void vboxNetLwfWinUnregisterIpAddrNotifier(PVBOXNETFLTINS pThis)
2508{
2509 Log(("vboxNetLwfWinUnregisterIpAddrNotifier: notifier=%p\n", pThis->u.s.WinIf.hNotifier));
2510 if (pThis->u.s.WinIf.hNotifier)
2511 CancelMibChangeNotify2(pThis->u.s.WinIf.hNotifier);
2512}
2513
2514void vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis)
2515{
2516 PVBOXNETLWF_MODULE pModuleCtx = (PVBOXNETLWF_MODULE)pThis->u.s.WinIf.hModuleCtx;
2517 LogFlow(("==>vboxNetFltOsDeleteInstance: instance=%p module=%p\n", pThis, pModuleCtx));
2518 /* Cancel IP address change notifications */
2519 vboxNetLwfWinUnregisterIpAddrNotifier(pThis);
2520 /* Technically it is possible that the module has already been gone by now. */
2521 if (pModuleCtx)
2522 {
2523 Assert(!pModuleCtx->fActive); /* Deactivation ensures bypass mode */
2524 pModuleCtx->pNetFlt = NULL;
2525 pThis->u.s.WinIf.hModuleCtx = NULL;
2526 }
2527 LogFlow(("<==vboxNetFltOsDeleteInstance\n"));
2528}
2529
2530static void vboxNetLwfWinReportCapabilities(PVBOXNETFLTINS pThis, PVBOXNETLWF_MODULE pModuleCtx)
2531{
2532 if (pThis->pSwitchPort
2533 && vboxNetFltTryRetainBusyNotDisconnected(pThis))
2534 {
2535 pThis->pSwitchPort->pfnReportMacAddress(pThis->pSwitchPort, &pModuleCtx->MacAddr);
2536 pThis->pSwitchPort->pfnReportPromiscuousMode(pThis->pSwitchPort,
2537 vboxNetLwfWinIsPromiscuous(pModuleCtx));
2538 pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0,
2539 INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
2540 pThis->pSwitchPort->pfnReportNoPreemptDsts(pThis->pSwitchPort, 0 /* none */);
2541 vboxNetFltRelease(pThis, true /*fBusy*/);
2542 }
2543}
2544
2545int vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext)
2546{
2547 RT_NOREF1(pvContext);
2548 LogFlow(("==>vboxNetFltOsInitInstance: instance=%p context=%p\n", pThis, pvContext));
2549 AssertReturn(pThis, VERR_INVALID_PARAMETER);
2550 Log(("vboxNetFltOsInitInstance: trunk name=%s\n", pThis->szName));
2551 PVBOXNETLWF_MODULE pModuleCtx = NULL;
2552 NdisAcquireSpinLock(&g_VBoxNetLwfGlobals.Lock);
2553 RTListForEach(&g_VBoxNetLwfGlobals.listModules, pModuleCtx, VBOXNETLWF_MODULE, node)
2554 {
2555 DbgPrint("vboxNetFltOsInitInstance: evaluating module, name=%s\n", pModuleCtx->szMiniportName);
2556 if (!RTStrICmp(pThis->szName, pModuleCtx->szMiniportName))
2557 {
2558 NdisReleaseSpinLock(&g_VBoxNetLwfGlobals.Lock);
2559 Log(("vboxNetFltOsInitInstance: found matching module, name=%s\n", pThis->szName));
2560 pThis->u.s.WinIf.hModuleCtx = pModuleCtx;
2561 pModuleCtx->pNetFlt = pThis;
2562 vboxNetLwfWinReportCapabilities(pThis, pModuleCtx);
2563 vboxNetLwfWinRegisterIpAddrNotifier(pThis);
2564 LogFlow(("<==vboxNetFltOsInitInstance: return 0\n"));
2565 return VINF_SUCCESS;
2566 }
2567 }
2568 NdisReleaseSpinLock(&g_VBoxNetLwfGlobals.Lock);
2569 vboxNetLwfLogErrorEvent(IO_ERR_INTERNAL_ERROR, STATUS_SUCCESS, 6);
2570 LogFlow(("<==vboxNetFltOsInitInstance: return VERR_INTNET_FLT_IF_NOT_FOUND\n"));
2571 return VERR_INTNET_FLT_IF_NOT_FOUND;
2572}
2573
2574int vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis)
2575{
2576 LogFlow(("==>vboxNetFltOsPreInitInstance: instance=%p\n", pThis));
2577 pThis->u.s.WinIf.hModuleCtx = 0;
2578 pThis->u.s.WinIf.hNotifier = NULL;
2579 LogFlow(("<==vboxNetFltOsPreInitInstance: return 0\n"));
2580 return VINF_SUCCESS;
2581}
2582
2583void vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac)
2584{
2585 RT_NOREF3(pThis, pvIfData, pMac);
2586 LogFlow(("==>vboxNetFltPortOsNotifyMacAddress: instance=%p data=%p mac=%RTmac\n", pThis, pvIfData, pMac));
2587 LogFlow(("<==vboxNetFltPortOsNotifyMacAddress\n"));
2588}
2589
2590int vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData)
2591{
2592 RT_NOREF3(pThis, pvIf, ppvIfData);
2593 LogFlow(("==>vboxNetFltPortOsConnectInterface: instance=%p if=%p data=%p\n", pThis, pvIf, ppvIfData));
2594 LogFlow(("<==vboxNetFltPortOsConnectInterface: return 0\n"));
2595 /* Nothing to do */
2596 return VINF_SUCCESS;
2597}
2598
2599int vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData)
2600{
2601 RT_NOREF2(pThis, pvIfData);
2602 LogFlow(("==>vboxNetFltPortOsDisconnectInterface: instance=%p data=%p\n", pThis, pvIfData));
2603 LogFlow(("<==vboxNetFltPortOsDisconnectInterface: return 0\n"));
2604 /* Nothing to do */
2605 return VINF_SUCCESS;
2606}
2607
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette