1 // SoftEther VPN Source Code - Stable Edition Repository
2 // Kernel Device Driver
3 //
4 // SoftEther VPN Server, Client and Bridge are free software under the Apache License, Version 2.0.
5 //
6 // Copyright (c) Daiyuu Nobori.
7 // Copyright (c) SoftEther VPN Project, University of Tsukuba, Japan.
8 // Copyright (c) SoftEther Corporation.
9 // Copyright (c) all contributors on SoftEther VPN project in GitHub.
10 //
11 // All Rights Reserved.
12 //
13 // http://www.softether.org/
14 //
15 // This stable branch is officially managed by Daiyuu Nobori, the owner of SoftEther VPN Project.
16 // Pull requests should be sent to the Developer Edition Master Repository on https://github.com/SoftEtherVPN/SoftEtherVPN
17 //
18 // License: The Apache License, Version 2.0
19 // https://www.apache.org/licenses/LICENSE-2.0
20 //
21 // DISCLAIMER
22 // ==========
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
31 //
32 // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN, UNDER
33 // JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY, MERGE, PUBLISH,
34 // DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS SOFTWARE, THAT ANY
35 // JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS SOFTWARE OR ITS CONTENTS,
36 // AGAINST US (SOFTETHER PROJECT, SOFTETHER CORPORATION, DAIYUU NOBORI OR OTHER
37 // SUPPLIERS), OR ANY JURIDICAL DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND
38 // OF USING, COPYING, MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING,
39 // AND/OR SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
40 // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO EXCLUSIVE
41 // JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO, JAPAN. YOU MUST WAIVE
42 // ALL DEFENSES OF LACK OF PERSONAL JURISDICTION AND FORUM NON CONVENIENS.
43 // PROCESS MAY BE SERVED ON EITHER PARTY IN THE MANNER AUTHORIZED BY APPLICABLE
44 // LAW OR COURT RULE.
45 //
46 // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS YOU HAVE
47 // A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY CRIMINAL LAWS OR CIVIL
48 // RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS SOFTWARE IN OTHER COUNTRIES IS
49 // COMPLETELY AT YOUR OWN RISK. THE SOFTETHER VPN PROJECT HAS DEVELOPED AND
50 // DISTRIBUTED THIS SOFTWARE TO COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING
51 // CIVIL RIGHTS INCLUDING PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER
52 // COUNTRIES' LAWS OR CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES.
53 // WE HAVE NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
54 // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+ COUNTRIES
55 // AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE WORLD, WITH
56 // DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY COUNTRIES' LAWS, REGULATIONS
57 // AND CIVIL RIGHTS TO MAKE THE SOFTWARE COMPLY WITH ALL COUNTRIES' LAWS BY THE
58 // PROJECT. EVEN IF YOU WILL BE SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A
59 // PUBLIC SERVANT IN YOUR COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE
60 // LIABLE TO RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
61 // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT JUST A
62 // STATEMENT FOR WARNING AND DISCLAIMER.
63 //
64 // READ AND UNDERSTAND THE 'WARNING.TXT' FILE BEFORE USING THIS SOFTWARE.
65 // SOME SOFTWARE PROGRAMS FROM THIRD PARTIES ARE INCLUDED ON THIS SOFTWARE WITH
66 // LICENSE CONDITIONS WHICH ARE DESCRIBED ON THE 'THIRD_PARTY.TXT' FILE.
67 //
68 //
69 // SOURCE CODE CONTRIBUTION
70 // ------------------------
71 //
72 // Your contribution to SoftEther VPN Project is much appreciated.
73 // Please send patches to us through GitHub.
74 // Read the SoftEther VPN Patch Acceptance Policy in advance:
75 // http://www.softether.org/5-download/src/9.patch
76 //
77 //
78 // DEAR SECURITY EXPERTS
79 // ---------------------
80 //
81 // If you find a bug or a security vulnerability please kindly inform us
82 // about the problem immediately so that we can fix the security problem
83 // to protect a lot of users around the world as soon as possible.
84 //
85 // Our e-mail address for security reports is:
86 // softether-vpn-security [at] softether.org
87 //
88 // Please note that the above e-mail address is not a technical support
89 // inquiry address. If you need technical assistance, please visit
90 // http://www.softether.org/ and ask your question on the users forum.
91 //
92 // Thank you for your cooperation.
93 //
94 //
95 // NO MEMORY OR RESOURCE LEAKS
96 // ---------------------------
97 //
98 // The memory-leaks and resource-leaks verification under the stress
99 // test has been passed before release this source code.
100 
101 
102 // Neo.c
103 // Driver main program
104 
105 #include <GlobalConst.h>
106 
107 #define	NEO_DEVICE_DRIVER
108 
109 #include "Neo.h"
110 
111 // Whether Win8
112 extern bool g_is_win8;
113 
114 // Neo driver context
115 static NEO_CTX static_ctx;
116 NEO_CTX *ctx = &static_ctx;
117 
118 // Read the packet data from the transmit packet queue
NeoRead(void * buf)119 void NeoRead(void *buf)
120 {
121 	NEO_QUEUE *q;
122 	UINT num;
123 	BOOL left;
124 	// Validate arguments
125 	if (buf == NULL)
126 	{
127 		return;
128 	}
129 
130 	// Copy the packets one by one from the queue
131 	num = 0;
132 	left = TRUE;
133 	NeoLockPacketQueue();
134 	{
135 		while (TRUE)
136 		{
137 			if (num >= NEO_MAX_PACKET_EXCHANGE)
138 			{
139 				if (ctx->PacketQueue == NULL)
140 				{
141 					left = FALSE;
142 				}
143 				break;
144 			}
145 			q = NeoGetNextQueue();
146 			if (q == NULL)
147 			{
148 				left = FALSE;
149 				break;
150 			}
151 			NEO_SIZE_OF_PACKET(buf, num) = q->Size;
152 			NeoCopy(NEO_ADDR_OF_PACKET(buf, num), q->Buf, q->Size);
153 			num++;
154 			NeoFreeQueue(q);
155 		}
156 	}
157 	NeoUnlockPacketQueue();
158 
159 	NEO_NUM_PACKET(buf) = num;
160 	NEO_LEFT_FLAG(buf) = left;
161 
162 	if (left == FALSE)
163 	{
164 		NeoReset(ctx->Event);
165 	}
166 	else
167 	{
168 		NeoSet(ctx->Event);
169 	}
170 
171 	return;
172 }
173 
174 // Process the received packet
NeoWrite(void * buf)175 void NeoWrite(void *buf)
176 {
177 	UINT num, i, size;
178 	void *packet_buf;
179 	// Validate arguments
180 	if (buf == NULL)
181 	{
182 		return;
183 	}
184 
185 	// Number of packets
186 	num = NEO_NUM_PACKET(buf);
187 	if (num > NEO_MAX_PACKET_EXCHANGE)
188 	{
189 		// Number of packets is too many
190 		return;
191 	}
192 	if (num == 0)
193 	{
194 		// No packet
195 		return;
196 	}
197 
198 	if (ctx->Halting != FALSE)
199 	{
200 		// Halting
201 		return;
202 	}
203 
204 	if (ctx->Opened == FALSE)
205 	{
206 		// Not connected
207 		return;
208 	}
209 
210 	for (i = 0;i < num;i++)
211 	{
212 		PACKET_BUFFER *p = ctx->PacketBuffer[i];
213 
214 		size = NEO_SIZE_OF_PACKET(buf, i);
215 		if (size > NEO_MAX_PACKET_SIZE)
216 		{
217 			size = NEO_MAX_PACKET_SIZE;
218 		}
219 		if (size < NEO_PACKET_HEADER_SIZE)
220 		{
221 			size = NEO_PACKET_HEADER_SIZE;
222 		}
223 
224 		packet_buf = NEO_ADDR_OF_PACKET(buf, i);
225 
226 		// Buffer copy
227 		NeoCopy(p->Buf, packet_buf, size);
228 
229 		if (g_is_win8 == false)
230 		{
231 			// Adjust the buffer size
232 			NdisAdjustBufferLength(p->NdisBuffer, size);
233 			// Set the packet information
234 			NDIS_SET_PACKET_STATUS(p->NdisPacket, NDIS_STATUS_RESOURCES);
235 			NDIS_SET_PACKET_HEADER_SIZE(p->NdisPacket, NEO_PACKET_HEADER_SIZE);
236 		}
237 		else
238 		{
239 			NdisMEthIndicateReceive(ctx->NdisMiniport, ctx,
240 				p->Buf, NEO_PACKET_HEADER_SIZE,
241 				((UCHAR *)p->Buf) + NEO_PACKET_HEADER_SIZE, size - NEO_PACKET_HEADER_SIZE,
242 				size - NEO_PACKET_HEADER_SIZE);
243 			NdisMEthIndicateReceiveComplete(ctx->NdisMiniport);
244 		}
245 	}
246 
247 	// Notify that packets have received
248 	ctx->Status.NumPacketRecv += num;
249 
250 	if (g_is_win8 == false)
251 	{
252 		NdisMIndicateReceivePacket(ctx->NdisMiniport, ctx->PacketBufferArray, num);
253 	}
254 }
255 
256 // Get the number of queue items
NeoGetNumQueue()257 UINT NeoGetNumQueue()
258 {
259 	return ctx->NumPacketQueue;
260 }
261 
262 // Insert the queue
NeoInsertQueue(void * buf,UINT size)263 void NeoInsertQueue(void *buf, UINT size)
264 {
265 	NEO_QUEUE *p;
266 	// Validate arguments
267 	if (buf == NULL || size == 0)
268 	{
269 		return;
270 	}
271 
272 	// Prevent the packet accumulation in large quantities in the queue
273 	if (ctx->NumPacketQueue > NEO_MAX_PACKET_QUEUED)
274 	{
275 		NeoFree(buf);
276 		return;
277 	}
278 
279 	// Create a queue
280 	p = NeoMalloc(sizeof(NEO_QUEUE));
281 	p->Next = NULL;
282 	p->Size = size;
283 	p->Buf = buf;
284 
285 	// Append to the queue
286 	if (ctx->PacketQueue == NULL)
287 	{
288 		ctx->PacketQueue = p;
289 	}
290 	else
291 	{
292 		NEO_QUEUE *q = ctx->Tail;
293 		q->Next = p;
294 	}
295 
296 	ctx->Tail = p;
297 
298 	ctx->NumPacketQueue++;
299 }
300 
301 // Get the next queued item
NeoGetNextQueue()302 NEO_QUEUE *NeoGetNextQueue()
303 {
304 	NEO_QUEUE *q;
305 	if (ctx->PacketQueue == NULL)
306 	{
307 		// No item queued
308 		return NULL;
309 	}
310 
311 	// Get the next queued item
312 	q = ctx->PacketQueue;
313 	ctx->PacketQueue = ctx->PacketQueue->Next;
314 	q->Next = NULL;
315 	ctx->NumPacketQueue--;
316 
317 	if (ctx->PacketQueue == NULL)
318 	{
319 		ctx->Tail = NULL;
320 	}
321 
322 	return q;
323 }
324 
325 // Release the buffer queue
NeoFreeQueue(NEO_QUEUE * q)326 void NeoFreeQueue(NEO_QUEUE *q)
327 {
328 	// Validate arguments
329 	if (q == NULL)
330 	{
331 		return;
332 	}
333 	NeoFree(q->Buf);
334 	NeoFree(q);
335 }
336 
337 // Lock the packet queue
NeoLockPacketQueue()338 void NeoLockPacketQueue()
339 {
340 	NeoLock(ctx->PacketQueueLock);
341 }
342 
343 // Unlock the packet queue
NeoUnlockPacketQueue()344 void NeoUnlockPacketQueue()
345 {
346 	NeoUnlock(ctx->PacketQueueLock);
347 }
348 
349 // Initialize the packet queue
NeoInitPacketQueue()350 void NeoInitPacketQueue()
351 {
352 	// Create a lock
353 	ctx->PacketQueueLock = NeoNewLock();
354 	// Initialize the packet queue
355 	ctx->PacketQueue = NULL;
356 	ctx->NumPacketQueue = 0;
357 	ctx->Tail = NULL;
358 }
359 
360 // Delete all the packets from the packet queue
NeoClearPacketQueue()361 void NeoClearPacketQueue()
362 {
363 	// Release the memory of the packet queue
364 	NeoLock(ctx->PacketQueueLock);
365 	{
366 		NEO_QUEUE *q = ctx->PacketQueue;
367 		NEO_QUEUE *qn;
368 		while (q != NULL)
369 		{
370 			qn = q->Next;
371 			NeoFree(q->Buf);
372 			NeoFree(q);
373 			q = qn;
374 		}
375 		ctx->PacketQueue = NULL;
376 		ctx->Tail = NULL;
377 		ctx->NumPacketQueue = 0;
378 	}
379 	NeoUnlock(ctx->PacketQueueLock);
380 }
381 
382 // Release the packet queue
NeoFreePacketQueue()383 void NeoFreePacketQueue()
384 {
385 	// Delete all packets
386 	NeoClearPacketQueue();
387 
388 	// Delete the lock
389 	NeoFreeLock(ctx->PacketQueueLock);
390 	ctx->PacketQueueLock = NULL;
391 }
392 
393 // Start the adapter
NeoStartAdapter()394 void NeoStartAdapter()
395 {
396 	// Initialize the packet queue
397 	NeoInitPacketQueue();
398 }
399 
400 // Stop the adapter
NeoStopAdapter()401 void NeoStopAdapter()
402 {
403 	// Delete the packet queue
404 	NeoFreePacketQueue();
405 }
406 
407 // Initialization
NeoInit()408 BOOL NeoInit()
409 {
410 	// Initialize the context
411 	NeoZero(ctx, sizeof(NEO_CTX));
412 
413 	// Initialize the status information
414 	NeoNewStatus(&ctx->Status);
415 
416 	return TRUE;
417 }
418 
419 // Shutdown
NeoShutdown()420 void NeoShutdown()
421 {
422 	if (ctx == NULL)
423 	{
424 		// Uninitialized
425 		return;
426 	}
427 
428 	// Relaese the status information
429 	NeoFreeStatus(&ctx->Status);
430 
431 	NeoZero(ctx, sizeof(NEO_CTX));
432 }
433 
434 // Create a status information
NeoNewStatus(NEO_STATUS * s)435 void NeoNewStatus(NEO_STATUS *s)
436 {
437 	// Validate arguments
438 	if (s == NULL)
439 	{
440 		return;
441 	}
442 
443 	// Memory initialization
444 	NeoZero(s, sizeof(NEO_STATUS));
445 }
446 
447 // Release the status information
NeoFreeStatus(NEO_STATUS * s)448 void NeoFreeStatus(NEO_STATUS *s)
449 {
450 	// Validate arguments
451 	if (s == NULL)
452 	{
453 		return;
454 	}
455 
456 	// Memory initialization
457 	NeoZero(s, sizeof(NEO_STATUS));
458 }
459 
460