VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvNetSniffer.cpp@ 8140

Last change on this file since 8140 was 7831, checked in by vboxsync, 17 years ago

net: moved the wait/notify mechanism for receive buffers into the device

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/** @file
2 *
3 * VBox network devices:
4 * Network sniffer filter driver
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_DRV_NAT
24#include <VBox/pdmdrv.h>
25
26#include <VBox/log.h>
27#include <iprt/assert.h>
28#include <iprt/file.h>
29#include <iprt/process.h>
30#include <iprt/string.h>
31#include <iprt/time.h>
32#include <iprt/critsect.h>
33#include <VBox/param.h>
34
35#include "Builtins.h"
36
37
38/*******************************************************************************
39* Structures and Typedefs *
40*******************************************************************************/
41/**
42 * Block driver instance data.
43 */
44typedef struct DRVNETSNIFFER
45{
46 /** The network interface. */
47 PDMINETWORKCONNECTOR INetworkConnector;
48 /** The network interface. */
49 PDMINETWORKPORT INetworkPort;
50 /** The port we're attached to. */
51 PPDMINETWORKPORT pPort;
52 /** The connector that's attached to us. */
53 PPDMINETWORKCONNECTOR pConnector;
54 /** The filename. */
55 char szFilename[RTPATH_MAX];
56 /** The filehandle. */
57 RTFILE File;
58 /** The lock serializing the file access. */
59 RTCRITSECT Lock;
60 /** Pointer to the driver instance. */
61 PPDMDRVINS pDrvIns;
62
63} DRVNETSNIFFER, *PDRVNETSNIFFER;
64
65/** Converts a pointer to NAT::INetworkConnector to a PDRVNETSNIFFER. */
66#define PDMINETWORKCONNECTOR_2_DRVNETSNIFFER(pInterface) ( (PDRVNETSNIFFER)((uintptr_t)pInterface - RT_OFFSETOF(DRVNETSNIFFER, INetworkConnector)) )
67
68/** Converts a pointer to NAT::INetworkPort to a PDRVNETSNIFFER. */
69#define PDMINETWORKPORT_2_DRVNETSNIFFER(pInterface) ( (PDRVNETSNIFFER)((uintptr_t)pInterface - RT_OFFSETOF(DRVNETSNIFFER, INetworkPort)) )
70
71
72/* "libpcap" magic */
73#define PCAP_MAGIC 0xa1b2c3d4
74
75/* "libpcap" file header (minus magic number). */
76struct pcap_hdr
77{
78 uint16_t version_major; /* major version number = 2 */
79 uint16_t version_minor; /* minor version number = 4 */
80 int32_t thiszone; /* GMT to local correction = 0 */
81 uint32_t sigfigs; /* accuracy of timestamps = 0 */
82 uint32_t snaplen; /* max length of captured packets, in octets = 0xffff */
83 uint32_t network; /* data link type = 01 */
84};
85
86/* "libpcap" record header. */
87struct pcaprec_hdr
88{
89 uint32_t ts_sec; /* timestamp seconds */
90 uint32_t ts_usec; /* timestamp microseconds */
91 uint32_t incl_len; /* number of octets of packet saved in file */
92 uint32_t orig_len; /* actual length of packet */
93};
94
95struct pcaprec_hdr_init
96{
97 uint32_t u32Magic;
98 struct pcap_hdr pcap;
99#ifdef LOG_ENABLED
100 pcaprec_hdr rec;
101#endif
102};
103
104
105/**
106 * Send data to the network.
107 *
108 * @returns VBox status code.
109 * @param pInterface Pointer to the interface structure containing the called function pointer.
110 * @param pvBuf Data to send.
111 * @param cb Number of bytes to send.
112 * @thread EMT
113 */
114static DECLCALLBACK(int) drvNetSnifferSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
115{
116 PDRVNETSNIFFER pData = PDMINETWORKCONNECTOR_2_DRVNETSNIFFER(pInterface);
117
118 /* output to sniffer */
119 struct pcaprec_hdr Hdr;
120 uint64_t u64TS = RTTimeProgramNanoTS();
121 Hdr.ts_sec = (uint32_t)(u64TS / 1000000000);
122 Hdr.ts_usec = (uint32_t)((u64TS / 1000) % 1000000);
123 Hdr.incl_len = cb;
124 Hdr.orig_len = cb;
125 RTCritSectEnter(&pData->Lock);
126 RTFileWrite(pData->File, &Hdr, sizeof(Hdr), NULL);
127 RTFileWrite(pData->File, pvBuf, cb, NULL);
128 RTCritSectLeave(&pData->Lock);
129
130 /* pass down */
131 if (pData->pConnector)
132 {
133 int rc = pData->pConnector->pfnSend(pData->pConnector, pvBuf, cb);
134#if 0
135 RTCritSectEnter(&pData->Lock);
136 u64TS = RTTimeProgramNanoTS();
137 Hdr.ts_sec = (uint32_t)(u64TS / 1000000000);
138 Hdr.ts_usec = (uint32_t)((u64TS / 1000) % 1000000);
139 Hdr.incl_len = 0;
140 RTFileWrite(pData->File, &Hdr, sizeof(Hdr), NULL);
141 RTCritSectLeave(&pData->Lock);
142#endif
143 return rc;
144 }
145 return VINF_SUCCESS;
146}
147
148
149/**
150 * Set promiscuous mode.
151 *
152 * This is called when the promiscuous mode is set. This means that there doesn't have
153 * to be a mode change when it's called.
154 *
155 * @param pInterface Pointer to the interface structure containing the called function pointer.
156 * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
157 * @thread EMT
158 */
159static DECLCALLBACK(void) drvNetSnifferSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
160{
161 LogFlow(("drvNetSnifferSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
162 PDRVNETSNIFFER pData = PDMINETWORKCONNECTOR_2_DRVNETSNIFFER(pInterface);
163 if (pData->pConnector)
164 pData->pConnector->pfnSetPromiscuousMode(pData->pConnector, fPromiscuous);
165}
166
167
168/**
169 * Notification on link status changes.
170 *
171 * @param pInterface Pointer to the interface structure containing the called function pointer.
172 * @param enmLinkState The new link state.
173 * @thread EMT
174 */
175static DECLCALLBACK(void) drvNetSnifferNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
176{
177 LogFlow(("drvNetSnifferNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
178 PDRVNETSNIFFER pData = PDMINETWORKCONNECTOR_2_DRVNETSNIFFER(pInterface);
179 if (pData->pConnector)
180 pData->pConnector->pfnNotifyLinkChanged(pData->pConnector, enmLinkState);
181}
182
183
184/**
185 * Check how much data the device/driver can receive data now.
186 * This must be called before the pfnRecieve() method is called.
187 *
188 * @returns Number of bytes the device can receive now.
189 * @param pInterface Pointer to the interface structure containing the called function pointer.
190 * @thread EMT
191 */
192static DECLCALLBACK(int) drvNetSnifferWaitReceiveAvail(PPDMINETWORKPORT pInterface, unsigned cMillies)
193{
194 PDRVNETSNIFFER pData = PDMINETWORKPORT_2_DRVNETSNIFFER(pInterface);
195 return pData->pPort->pfnWaitReceiveAvail(pData->pPort, cMillies);
196}
197
198
199/**
200 * Receive data from the network.
201 *
202 * @returns VBox status code.
203 * @param pInterface Pointer to the interface structure containing the called function pointer.
204 * @param pvBuf The available data.
205 * @param cb Number of bytes available in the buffer.
206 * @thread EMT
207 */
208static DECLCALLBACK(int) drvNetSnifferReceive(PPDMINETWORKPORT pInterface, const void *pvBuf, size_t cb)
209{
210 PDRVNETSNIFFER pData = PDMINETWORKPORT_2_DRVNETSNIFFER(pInterface);
211
212 /* output to sniffer */
213 struct pcaprec_hdr Hdr;
214 uint64_t u64TS = RTTimeProgramNanoTS();
215 Hdr.ts_sec = (uint32_t)(u64TS / 1000000000);
216 Hdr.ts_usec = (uint32_t)((u64TS / 1000) % 1000000);
217 Hdr.incl_len = cb;
218 Hdr.orig_len = cb;
219 RTCritSectEnter(&pData->Lock);
220 RTFileWrite(pData->File, &Hdr, sizeof(Hdr), NULL);
221 RTFileWrite(pData->File, pvBuf, cb, NULL);
222 RTCritSectLeave(&pData->Lock);
223
224 /* pass up */
225 int rc = pData->pPort->pfnReceive(pData->pPort, pvBuf, cb);
226#if 0
227 RTCritSectEnter(&pData->Lock);
228 u64TS = RTTimeProgramNanoTS();
229 Hdr.ts_sec = (uint32_t)(u64TS / 1000000000);
230 Hdr.ts_usec = (uint32_t)((u64TS / 1000) % 1000000);
231 Hdr.incl_len = 0;
232 RTFileWrite(pData->File, &Hdr, sizeof(Hdr), NULL);
233 RTCritSectLeave(&pData->Lock);
234#endif
235 return rc;
236}
237
238/**
239 * Queries an interface to the driver.
240 *
241 * @returns Pointer to interface.
242 * @returns NULL if the interface was not supported by the driver.
243 * @param pInterface Pointer to this interface structure.
244 * @param enmInterface The requested interface identification.
245 * @thread Any thread.
246 */
247static DECLCALLBACK(void *) drvNetSnifferQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
248{
249 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
250 PDRVNETSNIFFER pData = PDMINS2DATA(pDrvIns, PDRVNETSNIFFER);
251 switch (enmInterface)
252 {
253 case PDMINTERFACE_BASE:
254 return &pDrvIns->IBase;
255 case PDMINTERFACE_NETWORK_CONNECTOR:
256 return &pData->INetworkConnector;
257 case PDMINTERFACE_NETWORK_PORT:
258 return &pData->INetworkPort;
259 default:
260 return NULL;
261 }
262}
263
264
265/**
266 * Destruct a driver instance.
267 *
268 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
269 * resources can be freed correctly.
270 *
271 * @param pDrvIns The driver instance data.
272 */
273static DECLCALLBACK(void) drvNetSnifferDestruct(PPDMDRVINS pDrvIns)
274{
275 PDRVNETSNIFFER pData = PDMINS2DATA(pDrvIns, PDRVNETSNIFFER);
276
277 if (RTCritSectIsInitialized(&pData->Lock))
278 RTCritSectDelete(&pData->Lock);
279
280 if (pData->File != NIL_RTFILE)
281 {
282 RTFileClose(pData->File);
283 pData->File = NIL_RTFILE;
284 }
285}
286
287
288/**
289 * Construct a NAT network transport driver instance.
290 *
291 * @returns VBox status.
292 * @param pDrvIns The driver instance data.
293 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
294 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
295 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
296 * iInstance it's expected to be used a bit in this function.
297 */
298static DECLCALLBACK(int) drvNetSnifferConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
299{
300 PDRVNETSNIFFER pData = PDMINS2DATA(pDrvIns, PDRVNETSNIFFER);
301 LogFlow(("drvNetSnifferConstruct:\n"));
302
303 /*
304 * Validate the config.
305 */
306 if (!CFGMR3AreValuesValid(pCfgHandle, "File\0"))
307 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
308
309 /*
310 * Init the static parts.
311 */
312 pData->pDrvIns = pDrvIns;
313 pData->File = NIL_RTFILE;
314 /* IBase */
315 pDrvIns->IBase.pfnQueryInterface = drvNetSnifferQueryInterface;
316 /* INetworkConnector */
317 pData->INetworkConnector.pfnSend = drvNetSnifferSend;
318 pData->INetworkConnector.pfnSetPromiscuousMode = drvNetSnifferSetPromiscuousMode;
319 pData->INetworkConnector.pfnNotifyLinkChanged = drvNetSnifferNotifyLinkChanged;
320 /* INetworkPort */
321 pData->INetworkPort.pfnWaitReceiveAvail = drvNetSnifferWaitReceiveAvail;
322 pData->INetworkPort.pfnReceive = drvNetSnifferReceive;
323
324 /*
325 * Get the filename.
326 */
327 int rc = CFGMR3QueryString(pCfgHandle, "File", pData->szFilename, sizeof(pData->szFilename));
328 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
329 RTStrPrintf(pData->szFilename, sizeof(pData->szFilename), "./VBox-%x.pcap", RTProcSelf());
330 else if (VBOX_FAILURE(rc))
331 {
332 AssertMsgFailed(("Failed to query \"File\", rc=%Vrc.\n", rc));
333 return rc;
334 }
335
336 /*
337 * Query the network port interface.
338 */
339 pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
340 if (!pData->pPort)
341 {
342 AssertMsgFailed(("Configuration error: the above device/driver didn't export the network port interface!\n"));
343 return VERR_PDM_MISSING_INTERFACE_ABOVE;
344 }
345
346 /*
347 * Query the network connector interface.
348 */
349 PPDMIBASE pBaseDown;
350 rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseDown);
351 if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
352 pData->pConnector = NULL;
353 else if (VBOX_SUCCESS(rc))
354 {
355 pData->pConnector = (PPDMINETWORKCONNECTOR)pBaseDown->pfnQueryInterface(pBaseDown, PDMINTERFACE_NETWORK_CONNECTOR);
356 if (!pData->pConnector)
357 {
358 AssertMsgFailed(("Configuration error: the driver below didn't export the network connector interface!\n"));
359 return VERR_PDM_MISSING_INTERFACE_BELOW;
360 }
361 }
362 else
363 {
364 AssertMsgFailed(("Failed to attach to driver below! rc=%Vrc\n", rc));
365 return rc;
366 }
367
368 /*
369 * Create the lock.
370 */
371 rc = RTCritSectInit(&pData->Lock);
372 if (VBOX_FAILURE(rc))
373 return rc;
374
375 /*
376 * Open output file / pipe.
377 */
378 rc = RTFileOpen(&pData->File, pData->szFilename,
379 RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE);
380 if (VBOX_FAILURE(rc))
381 {
382 AssertMsgFailed(("Failed to create file '%s' for writing. rc=%Vrc\n", pData->szFilename, rc));
383 return rc;
384 }
385
386 /*
387 * Write pcap header.
388 */
389#ifdef LOG_ENABLED
390 pcaprec_hdr_init Hdr =
391 {
392 PCAP_MAGIC,
393 { 2, 4, 0, 0, 0xffff, 1 },
394 { 0, 1, 0, 60}
395 }; /* force ethereal to start at 0.000000. */
396#else
397 pcaprec_hdr_init Hdr =
398 {
399 PCAP_MAGIC,
400 { 2, 4, 0, 0, 0xffff, 1 }
401 }; /* this is just to make it happy, not to be correct. */
402#endif
403
404 RTFileWrite(pData->File, &Hdr, sizeof(Hdr), NULL);
405
406 return VINF_SUCCESS;
407}
408
409
410
411/**
412 * Network sniffer filter driver registration record.
413 */
414const PDMDRVREG g_DrvNetSniffer =
415{
416 /* u32Version */
417 PDM_DRVREG_VERSION,
418 /* szDriverName */
419 "NetSniffer",
420 /* pszDescription */
421 "Network Sniffer Filter Driver",
422 /* fFlags */
423 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
424 /* fClass. */
425 PDM_DRVREG_CLASS_NETWORK,
426 /* cMaxInstances */
427 1,
428 /* cbInstance */
429 sizeof(DRVNETSNIFFER),
430 /* pfnConstruct */
431 drvNetSnifferConstruct,
432 /* pfnDestruct */
433 drvNetSnifferDestruct,
434 /* pfnIOCtl */
435 NULL,
436 /* pfnPowerOn */
437 NULL,
438 /* pfnReset */
439 NULL,
440 /* pfnSuspend */
441 NULL,
442 /* pfnResume */
443 NULL,
444 /* pfnDetach */
445 NULL,
446 /* pfnPowerOff */
447 NULL
448};
449
Note: See TracBrowser for help on using the repository browser.

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