Skip to content

Commit aeb8b3c

Browse files
committed
move code
1 parent 58f88c8 commit aeb8b3c

32 files changed

+4151
-13
lines changed

.gitignore

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
# Generated by Cargo
2-
# will have compiled files and executables
3-
debug/
4-
target/
1+
*.suo
2+
*.user
3+
*.userosscache
4+
*.sln.docstates
55

6-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8-
Cargo.lock
6+
# Build results
7+
[Dd]ebug/
8+
[Dd]ebugPublic/
9+
[Rr]elease/
10+
[Rr]eleases/
11+
build/
12+
bld/
13+
[Bb]in/
14+
[Oo]bj/
15+
x64
916

10-
# These are backup files generated by rustfmt
11-
**/*.rs.bk
1217

13-
# MSVC Windows builds of rustc generate these, which store debugging information
14-
*.pdb
18+
# Visual C++ cache files
19+
ipch/
20+
*.aps
21+
*.ncb
22+
*.opensdf
23+
*.sdf
24+
*.cachefile
25+
26+
# Visual Studio profiler
27+
*.psess
28+
*.vsp
29+
*.vspx
30+
31+
.vs/
32+
33+
README.md

ForTun/Adapter.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "Adapter.h"
2+
#include "rxqueue.h"
3+
#include "txqueue.h"
4+
#include "Adapter.tmh"
5+
6+
#define FOR_TUN_MEDIA_MAX_SPEED 1'000'000'000
7+
8+
#if (NETADAPTER_VERSION_MAJOR>=2) && (NETADAPTER_VERSION_MINOR>=1)
9+
EVT_NET_ADAPTER_OFFLOAD_SET_RX_CHECKSUM AdapterOffloadSetRxCHecksum;
10+
11+
12+
VOID
13+
AdapterOffloadSetRxChecksum(NETADAPTER Adapter, NETOFFLOAD offload) {
14+
UNREFERENCED_PARAMETER(Adapter);
15+
UNREFERENCED_PARAMETER(offload);
16+
}
17+
#endif
18+
19+
20+
NTSTATUS
21+
ForTunAdapterCreate(PDEVICE_CONTEXT DeviceContext, WDFDEVICE Device) {
22+
23+
NTSTATUS status = STATUS_SUCCESS;
24+
25+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_ADAPTER, "%!FUNC! Entry");
26+
27+
NETADAPTER_INIT* adapterInit = NetAdapterInitAllocate(Device);
28+
29+
NET_ADAPTER_DATAPATH_CALLBACKS datapathCallbacks;
30+
NET_ADAPTER_DATAPATH_CALLBACKS_INIT(&datapathCallbacks,
31+
ForTunAdapterCreateTXQueue,
32+
ForTunAdapterCreateRXQueue);
33+
NetAdapterInitSetDatapathCallbacks(adapterInit, &datapathCallbacks);
34+
35+
WDF_OBJECT_ATTRIBUTES adapterAttributes;
36+
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&adapterAttributes, ADAPTER_CONTEXT);
37+
38+
NETADAPTER netAdapter;
39+
status = NetAdapterCreate(adapterInit, &adapterAttributes, &netAdapter);
40+
if (!NT_SUCCESS(status)) {
41+
goto adapterFail;
42+
}
43+
44+
NetAdapterInitFree(adapterInit);
45+
adapterInit = NULL;
46+
47+
48+
DeviceContext->Adapter = netAdapter;
49+
PADAPTER_CONTEXT context = AdapterGetContext(netAdapter);
50+
context->DeviceContext = DeviceContext;
51+
52+
// Capabilities
53+
// Limit
54+
NET_ADAPTER_RX_CAPABILITIES rxCapabilities;
55+
NET_ADAPTER_RX_CAPABILITIES_INIT_SYSTEM_MANAGED(&rxCapabilities, 65536,1);
56+
NET_ADAPTER_TX_CAPABILITIES txCapabilities;
57+
NET_ADAPTER_TX_CAPABILITIES_INIT(&txCapabilities, 1);
58+
NetAdapterSetDataPathCapabilities(netAdapter, &txCapabilities, &rxCapabilities);
59+
60+
//linkLayer
61+
NET_ADAPTER_LINK_LAYER_CAPABILITIES linkLayerCapabilities;
62+
NET_ADAPTER_LINK_LAYER_CAPABILITIES_INIT(&linkLayerCapabilities, FOR_TUN_MEDIA_MAX_SPEED, FOR_TUN_MEDIA_MAX_SPEED);
63+
NetAdapterSetLinkLayerCapabilities(netAdapter, &linkLayerCapabilities);
64+
NetAdapterSetLinkLayerMtuSize(netAdapter, 0xFFFF);
65+
66+
//LinkState
67+
ForTunAdapterSetLinkState(netAdapter, MediaConnectStateDisconnected);
68+
69+
#if(NETADAPTER_VERSION_MAJOR>=2) && (NETADAPTER_VERSION_MINOR>=1)
70+
AdapterOffloadSetRxChecksum(netAdapter);
71+
#endif
72+
73+
status = NetAdapterStart(netAdapter);
74+
75+
goto done;
76+
77+
adapterFail:
78+
NetAdapterInitFree(adapterInit);
79+
adapterInit = NULL;
80+
done:
81+
return status;
82+
}
83+
84+
85+
NTSTATUS ForTunAdapterNotifyRx(NETADAPTER NetAdapter)
86+
{
87+
if (NetAdapter == WDF_NO_HANDLE) {
88+
TraceEvents(TRACE_LEVEL_ERROR, TRACE_ADAPTER, "Adapter no initialized");
89+
return STATUS_DEVICE_NOT_READY;
90+
}
91+
92+
NETPACKETQUEUE rxQueue = AdapterGetContext(NetAdapter)->RxQueue;
93+
PRXQUEUE_CONTEXT rxContext = RxQueueGetContext(rxQueue);
94+
if (InterlockedExchange(&rxContext->NotificationEnabled, FALSE) == TRUE) {
95+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_ADAPTER, "begin to notify rxQueue");
96+
NetRxQueueNotifyMoreReceivedPacketsAvailable(rxQueue);
97+
}
98+
99+
return STATUS_SUCCESS;
100+
}
101+
102+
VOID ForTunAdapterSetLinkState(NETADAPTER Adapter, NET_IF_MEDIA_CONNECT_STATE State)
103+
{
104+
NET_ADAPTER_LINK_STATE linkState;
105+
NET_ADAPTER_LINK_STATE_INIT(&linkState,
106+
FOR_TUN_MEDIA_MAX_SPEED,
107+
State,
108+
MediaDuplexStateFull,
109+
NetAdapterPauseFunctionTypeUnsupported,
110+
NetAdapterAutoNegotiationFlagNone);
111+
NetAdapterSetLinkState(Adapter, &linkState);
112+
113+
114+
}

ForTun/Adapter.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
4+
#include "precomp.h"
5+
#include "Device.h"
6+
7+
EXTERN_C_START
8+
9+
typedef struct _ADAPTER_CONTEXT {
10+
NETPACKETQUEUE RxQueue;
11+
PDEVICE_CONTEXT DeviceContext;
12+
} ADAPTER_CONTEXT, *PADAPTER_CONTEXT;
13+
14+
15+
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(ADAPTER_CONTEXT, AdapterGetContext)
16+
17+
18+
19+
NTSTATUS
20+
ForTunAdapterCreate(PDEVICE_CONTEXT DeviceContext, WDFDEVICE Device);
21+
22+
NTSTATUS ForTunAdapterNotifyRx(NETADAPTER NetAdapter);
23+
24+
VOID ForTunAdapterSetLinkState(NETADAPTER Adapter, NET_IF_MEDIA_CONNECT_STATE State);
25+
26+
EXTERN_C_END

ForTun/Device.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*++
2+
3+
Module Name:
4+
5+
device.c - Device handling events for example driver.
6+
7+
Abstract:
8+
9+
This file contains the device entry points and callbacks.
10+
11+
Environment:
12+
13+
Kernel-mode Driver Framework
14+
15+
--*/
16+
17+
#include "Device.h"
18+
#include "Adapter.h"
19+
#include "Queue.h"
20+
#include "Public.h"
21+
#include "device.tmh"
22+
23+
#ifdef ALLOC_PRAGMA
24+
#pragma alloc_text (PAGE, ForTunCreateDevice)
25+
#endif
26+
27+
NTSTATUS
28+
ForTunCreateDevice(
29+
_Inout_ PWDFDEVICE_INIT DeviceInit
30+
)
31+
/*++
32+
33+
Routine Description:
34+
35+
Worker routine called to create a device and its software resources.
36+
37+
Arguments:
38+
39+
DeviceInit - Pointer to an opaque init structure. Memory for this
40+
structure will be freed by the framework when the WdfDeviceCreate
41+
succeeds. So don't access the structure after that point.
42+
43+
Return Value:
44+
45+
NTSTATUS
46+
47+
--*/
48+
{
49+
WDF_OBJECT_ATTRIBUTES deviceAttributes;
50+
PDEVICE_CONTEXT deviceContext;
51+
WDFDEVICE device;
52+
NTSTATUS status;
53+
54+
55+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! Entry");
56+
57+
PAGED_CODE();
58+
59+
WdfDeviceInitSetExclusive(DeviceInit, TRUE);
60+
61+
62+
WDF_FILEOBJECT_CONFIG fileConfig;
63+
WDF_FILEOBJECT_CONFIG_INIT(&fileConfig, WDF_NO_EVENT_CALLBACK, WDF_NO_EVENT_CALLBACK, ForTunFileCleanUp);
64+
WdfDeviceInitSetFileObjectConfig(DeviceInit, &fileConfig, WDF_NO_OBJECT_ATTRIBUTES);
65+
66+
67+
status = NetDeviceInitConfig(DeviceInit);
68+
if (!NT_SUCCESS(status)) {
69+
goto done;
70+
}
71+
72+
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_CONTEXT);
73+
74+
75+
deviceAttributes.EvtCleanupCallback = ForTunDeviceCleanUp;
76+
77+
status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
78+
79+
if (!NT_SUCCESS(status)) {
80+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "WdfDeviceCreate failure: %x", status);
81+
goto done;
82+
}
83+
84+
85+
deviceContext = DeviceGetContext(device);
86+
DECLARE_CONST_UNICODE_STRING(symLink, L"\\DosDevices\\ForTun");
87+
88+
//
89+
// Create a device interface so that applications can find and talk
90+
// to us.
91+
//
92+
// If the client driver calls WdfDeviceCreateDeviceInterface with the ReferenceString parameter equal to NULL,
93+
// NDIS intercepts I/O requests sent to the device interface. To avoid this behavior, specify any reference string.
94+
95+
status = WdfDeviceCreateSymbolicLink(device, &symLink);
96+
if (!NT_SUCCESS(status)) {
97+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "create symLink failure: %x", status);
98+
}
99+
100+
UNICODE_STRING referenceString;
101+
RtlInitUnicodeString(&referenceString, L"ForTun");
102+
103+
104+
status = WdfDeviceCreateDeviceInterface(
105+
device,
106+
&GUID_DEVINTERFACE_ForTun,
107+
&referenceString
108+
);
109+
110+
if (!NT_SUCCESS(status)) {
111+
//This would fail if multiple device create..
112+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "create device interface failure: %x", status);
113+
goto done;
114+
}
115+
// Initialize the I/O Package and any Queues
116+
117+
status = ForTunQueueInitialize(device);
118+
119+
if (!NT_SUCCESS(status)) {
120+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "WdfDeviceCreate failure: %x", status);
121+
goto done;
122+
}
123+
124+
status = ForTunAdapterCreate(deviceContext, device);
125+
126+
127+
done:
128+
if (!NT_SUCCESS(status)) {
129+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "create device failure: %x", status);
130+
}
131+
return status;
132+
}
133+
134+
135+
136+
VOID ForTunDeviceCleanUp(WDFOBJECT Obj) {
137+
138+
UNREFERENCED_PARAMETER(Obj);
139+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! Entry");
140+
141+
PDEVICE_CONTEXT pDeviceContext = DeviceGetContext(Obj);
142+
PoolQueueFree(pDeviceContext->PoolQueue);
143+
//pDeviceContext->Adapter = WDF_NO_HANDLE;
144+
145+
}
146+
147+
VOID ForTunFileCleanUp(
148+
_In_
149+
WDFFILEOBJECT FileObject
150+
) {
151+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! Entry");
152+
PDEVICE_CONTEXT context = DeviceGetContext(WdfFileObjectGetDevice(FileObject));
153+
154+
if (context->Adapter != NULL) {
155+
ForTunAdapterSetLinkState(context->Adapter, MediaConnectStateDisconnected);
156+
}
157+
158+
}

0 commit comments

Comments
 (0)