[TCPIP]
[reactos.git] / reactos / drivers / network / tcpip / tcpip / main.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/main.c
5 * PURPOSE: Driver entry point
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10 #include "precomp.h"
11
12 PDEVICE_OBJECT TCPDeviceObject = NULL;
13 PDEVICE_OBJECT UDPDeviceObject = NULL;
14 PDEVICE_OBJECT IPDeviceObject = NULL;
15 PDEVICE_OBJECT RawIPDeviceObject = NULL;
16 NDIS_HANDLE GlobalPacketPool = NULL;
17 NDIS_HANDLE GlobalBufferPool = NULL;
18 KSPIN_LOCK EntityListLock;
19 TDIEntityInfo *EntityList = NULL;
20 ULONG EntityCount = 0;
21 ULONG EntityMax = 0;
22 UDP_STATISTICS UDPStats;
23
24 /* Network timers */
25 KTIMER IPTimer;
26 KDPC IPTimeoutDpc;
27
28 VOID TiWriteErrorLog(
29 PDRIVER_OBJECT DriverContext,
30 NTSTATUS ErrorCode,
31 ULONG UniqueErrorValue,
32 NTSTATUS FinalStatus,
33 PWSTR String,
34 ULONG DumpDataCount,
35 PULONG DumpData)
36 /*
37 * FUNCTION: Writes an error log entry
38 * ARGUMENTS:
39 * DriverContext = Pointer to the driver or device object
40 * ErrorCode = An error code to put in the log entry
41 * UniqueErrorValue = UniqueErrorValue in the error log packet
42 * FinalStatus = FinalStatus in the error log packet
43 * String = If not NULL, a pointer to a string to put in log
44 * entry
45 * DumpDataCount = Number of ULONGs of dump data
46 * DumpData = Pointer to dump data for the log entry
47 */
48 {
49 #if 0
50 PIO_ERROR_LOG_PACKET LogEntry;
51 UCHAR EntrySize;
52 ULONG StringSize;
53 PUCHAR pString;
54 static WCHAR DriverName[] = L"TCP/IP";
55
56 EntrySize = sizeof(IO_ERROR_LOG_PACKET) +
57 (DumpDataCount * sizeof(ULONG)) + sizeof(DriverName);
58
59 if (String) {
60 StringSize = (wcslen(String) * sizeof(WCHAR)) + sizeof(UNICODE_NULL);
61 EntrySize += (UCHAR)StringSize;
62 }
63
64 LogEntry = (PIO_ERROR_LOG_PACKET)IoAllocateErrorLogEntry(
65 DriverContext, EntrySize);
66
67 if (LogEntry) {
68 LogEntry->MajorFunctionCode = -1;
69 LogEntry->RetryCount = -1;
70 LogEntry->DumpDataSize = (USHORT)(DumpDataCount * sizeof(ULONG));
71 LogEntry->NumberOfStrings = (String == NULL) ? 1 : 2;
72 LogEntry->StringOffset = sizeof(IO_ERROR_LOG_PACKET) + (DumpDataCount-1) * sizeof(ULONG);
73 LogEntry->EventCategory = 0;
74 LogEntry->ErrorCode = ErrorCode;
75 LogEntry->UniqueErrorValue = UniqueErrorValue;
76 LogEntry->FinalStatus = FinalStatus;
77 LogEntry->SequenceNumber = -1;
78 LogEntry->IoControlCode = 0;
79
80 if (DumpDataCount)
81 RtlCopyMemory(LogEntry->DumpData, DumpData, DumpDataCount * sizeof(ULONG));
82
83 pString = ((PUCHAR)LogEntry) + LogEntry->StringOffset;
84 RtlCopyMemory(pString, DriverName, sizeof(DriverName));
85 pString += sizeof(DriverName);
86
87 if (String)
88 RtlCopyMemory(pString, String, StringSize);
89
90 IoWriteErrorLogEntry(LogEntry);
91 }
92 #endif
93 }
94
95 /*
96 * FUNCTION: Creates a file object
97 * ARGUMENTS:
98 * DeviceObject = Pointer to a device object for this driver
99 * Irp = Pointer to a I/O request packet
100 * RETURNS:
101 * Status of the operation
102 */
103
104 NTSTATUS TiCreateFileObject(
105 PDEVICE_OBJECT DeviceObject,
106 PIRP Irp)
107 {
108 PFILE_FULL_EA_INFORMATION EaInfo;
109 PTRANSPORT_CONTEXT Context;
110 PIO_STACK_LOCATION IrpSp;
111 PTA_IP_ADDRESS Address;
112 TDI_REQUEST Request;
113 PVOID ClientContext;
114 NTSTATUS Status;
115 ULONG Protocol;
116
117 TI_DbgPrint(DEBUG_IRP, ("Called. DeviceObject is at (0x%X), IRP is at (0x%X).\n", DeviceObject, Irp));
118
119 EaInfo = Irp->AssociatedIrp.SystemBuffer;
120
121 /* Parameter check */
122 /* No EA information means that we're opening for SET/QUERY_INFORMATION
123 * style calls. */
124
125 /* Allocate resources here. We release them again if something failed */
126 Context = ExAllocatePoolWithTag(NonPagedPool, sizeof(TRANSPORT_CONTEXT),
127 TRANS_CONTEXT_TAG);
128 if (!Context)
129 {
130 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
131 return STATUS_INSUFFICIENT_RESOURCES;
132 }
133
134 Context->CancelIrps = FALSE;
135
136 IrpSp = IoGetCurrentIrpStackLocation(Irp);
137 IrpSp->FileObject->FsContext = Context;
138 Request.RequestContext = Irp;
139
140 /* Branch to the right handler */
141 if (EaInfo &&
142 (EaInfo->EaNameLength == TDI_TRANSPORT_ADDRESS_LENGTH) &&
143 (RtlCompareMemory(&EaInfo->EaName, TdiTransportAddress,
144 TDI_TRANSPORT_ADDRESS_LENGTH) == TDI_TRANSPORT_ADDRESS_LENGTH))
145 {
146 /* This is a request to open an address */
147
148
149 /* XXX This should probably be done in IoCreateFile() */
150 /* Parameter checks */
151
152 Address = (PTA_IP_ADDRESS)(EaInfo->EaName + EaInfo->EaNameLength + 1); //0-term
153
154 if ((EaInfo->EaValueLength < sizeof(TA_IP_ADDRESS)) ||
155 (Address->TAAddressCount != 1) ||
156 (Address->Address[0].AddressLength < TDI_ADDRESS_LENGTH_IP) ||
157 (Address->Address[0].AddressType != TDI_ADDRESS_TYPE_IP))
158 {
159 TI_DbgPrint(MIN_TRACE, ("Parameters are invalid:\n"));
160 TI_DbgPrint(MIN_TRACE, ("AddressCount: %d\n", Address->TAAddressCount));
161 if( Address->TAAddressCount == 1 )
162 {
163 TI_DbgPrint(MIN_TRACE, ("AddressLength: %\n",
164 Address->Address[0].AddressLength));
165 TI_DbgPrint(MIN_TRACE, ("AddressType: %\n",
166 Address->Address[0].AddressType));
167 }
168
169 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
170 return STATUS_INVALID_PARAMETER;
171 }
172
173 /* Open address file object */
174
175 /* Protocol depends on device object so find the protocol */
176 if (DeviceObject == TCPDeviceObject)
177 Protocol = IPPROTO_TCP;
178 else if (DeviceObject == UDPDeviceObject)
179 Protocol = IPPROTO_UDP;
180 else if (DeviceObject == IPDeviceObject)
181 Protocol = IPPROTO_RAW;
182 else if (DeviceObject == RawIPDeviceObject)
183 {
184 Status = TiGetProtocolNumber(&IrpSp->FileObject->FileName, &Protocol);
185 if (!NT_SUCCESS(Status))
186 {
187 TI_DbgPrint(MIN_TRACE, ("Raw IP protocol number is invalid.\n"));
188 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
189 return STATUS_INVALID_PARAMETER;
190 }
191 }
192 else
193 {
194 TI_DbgPrint(MIN_TRACE, ("Invalid device object at (0x%X).\n", DeviceObject));
195 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
196 return STATUS_INVALID_PARAMETER;
197 }
198
199 Status = FileOpenAddress(&Request, Address, Protocol, NULL);
200 if (NT_SUCCESS(Status))
201 {
202 IrpSp->FileObject->FsContext2 = (PVOID)TDI_TRANSPORT_ADDRESS_FILE;
203 Context->Handle.AddressHandle = Request.Handle.AddressHandle;
204 }
205
206 }
207 else if (EaInfo &&
208 (EaInfo->EaNameLength == TDI_CONNECTION_CONTEXT_LENGTH) &&
209 (RtlCompareMemory
210 (&EaInfo->EaName, TdiConnectionContext,
211 TDI_CONNECTION_CONTEXT_LENGTH) ==
212 TDI_CONNECTION_CONTEXT_LENGTH))
213 {
214 /* This is a request to open a connection endpoint */
215
216 /* Parameter checks */
217
218 if (EaInfo->EaValueLength < sizeof(PVOID))
219 {
220 TI_DbgPrint(MIN_TRACE, ("Parameters are invalid.\n"));
221 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
222 return STATUS_INVALID_PARAMETER;
223 }
224
225 /* Can only do connection oriented communication using TCP */
226
227 if (DeviceObject != TCPDeviceObject)
228 {
229 TI_DbgPrint(MIN_TRACE, ("Bad device object.\n"));
230 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
231 return STATUS_INVALID_PARAMETER;
232 }
233
234 ClientContext = *((PVOID*)(EaInfo->EaName + EaInfo->EaNameLength));
235
236 /* Open connection endpoint file object */
237
238 Status = FileOpenConnection(&Request, ClientContext);
239 if (NT_SUCCESS(Status))
240 {
241 IrpSp->FileObject->FsContext2 = (PVOID)TDI_CONNECTION_FILE;
242 Context->Handle.ConnectionContext = Request.Handle.ConnectionContext;
243 }
244 }
245 else
246 {
247 /* This is a request to open a control connection */
248 Status = FileOpenControlChannel(&Request);
249 if (NT_SUCCESS(Status))
250 {
251 IrpSp->FileObject->FsContext2 = (PVOID)TDI_CONTROL_CHANNEL_FILE;
252 Context->Handle.ControlChannel = Request.Handle.ControlChannel;
253 }
254 }
255
256 if (!NT_SUCCESS(Status))
257 ExFreePoolWithTag(Context, TRANS_CONTEXT_TAG);
258
259 TI_DbgPrint(DEBUG_IRP, ("Leaving. Status = (0x%X).\n", Status));
260
261 Irp->IoStatus.Status = Status;
262 return Status;
263 }
264
265
266 /*
267 * FUNCTION: Releases resources used by a file object
268 * ARGUMENTS:
269 * DeviceObject = Pointer to a device object for this driver
270 * Irp = Pointer to a I/O request packet
271 * RETURNS:
272 * Status of the operation
273 * NOTES:
274 * This function does not pend
275 */
276 NTSTATUS TiCloseFileObject(
277 PDEVICE_OBJECT DeviceObject,
278 PIRP Irp)
279 {
280 PIO_STACK_LOCATION IrpSp;
281 PTRANSPORT_CONTEXT Context;
282 TDI_REQUEST Request;
283 NTSTATUS Status;
284
285 IrpSp = IoGetCurrentIrpStackLocation(Irp);
286 Context = IrpSp->FileObject->FsContext;
287 if (!Context)
288 {
289 TI_DbgPrint(MIN_TRACE, ("Parameters are invalid.\n"));
290 return STATUS_INVALID_PARAMETER;
291 }
292
293 switch ((ULONG_PTR)IrpSp->FileObject->FsContext2)
294 {
295 case TDI_TRANSPORT_ADDRESS_FILE:
296 Request.Handle.AddressHandle = Context->Handle.AddressHandle;
297 Status = FileCloseAddress(&Request);
298 break;
299
300 case TDI_CONNECTION_FILE:
301 Request.Handle.ConnectionContext = Context->Handle.ConnectionContext;
302 Status = FileCloseConnection(&Request);
303 break;
304
305 case TDI_CONTROL_CHANNEL_FILE:
306 Request.Handle.ControlChannel = Context->Handle.ControlChannel;
307 Status = FileCloseControlChannel(&Request);
308 break;
309
310 default:
311 Status = STATUS_INVALID_PARAMETER;
312 break;
313 }
314
315 Irp->IoStatus.Status = Status;
316
317 return Irp->IoStatus.Status;
318 }
319
320
321 NTSTATUS NTAPI
322 TiDispatchOpenClose(
323 IN PDEVICE_OBJECT DeviceObject,
324 IN PIRP Irp)
325 /*
326 * FUNCTION: Main dispath routine
327 * ARGUMENTS:
328 * DeviceObject = Pointer to a device object for this driver
329 * Irp = Pointer to a I/O request packet
330 * RETURNS:
331 * Status of the operation
332 */
333 {
334 PIO_STACK_LOCATION IrpSp;
335 NTSTATUS Status;
336
337 // DbgPrint("Called. DeviceObject is at (0x%X), IRP is at (0x%X).\n", DeviceObject, Irp);
338
339 IrpSp = IoGetCurrentIrpStackLocation(Irp);
340
341 switch (IrpSp->MajorFunction) {
342 /* Open an address file, connection endpoint, or control connection */
343 case IRP_MJ_CREATE:
344 Status = TiCreateFileObject(DeviceObject, Irp);
345 break;
346
347 /* Close an address file, connection endpoint, or control connection */
348 case IRP_MJ_CLOSE:
349 Status = TiCloseFileObject(DeviceObject, Irp);
350 break;
351
352 default:
353 Status = STATUS_INVALID_DEVICE_REQUEST;
354 }
355
356 //DbgPrint("Leaving. Status is (0x%X)\n", Status);
357
358 return IRPFinish( Irp, Status );
359 }
360
361
362 NTSTATUS NTAPI
363 TiDispatchInternal(
364 PDEVICE_OBJECT DeviceObject,
365 PIRP Irp)
366 /*
367 * FUNCTION: Internal IOCTL dispatch routine
368 * ARGUMENTS:
369 * DeviceObject = Pointer to a device object for this driver
370 * Irp = Pointer to a I/O request packet
371 * RETURNS:
372 * Status of the operation
373 */
374 {
375 NTSTATUS Status;
376 BOOLEAN Complete = TRUE;
377 PIO_STACK_LOCATION IrpSp;
378
379 IrpSp = IoGetCurrentIrpStackLocation(Irp);
380
381 TI_DbgPrint(DEBUG_IRP, ("[TCPIP, TiDispatchInternal] Called. DeviceObject is at (0x%X), IRP is at (0x%X) MN (%d).\n",
382 DeviceObject, Irp, IrpSp->MinorFunction));
383
384 Irp->IoStatus.Status = STATUS_SUCCESS;
385 Irp->IoStatus.Information = 0;
386
387 switch (IrpSp->MinorFunction) {
388 case TDI_RECEIVE:
389 Status = DispTdiReceive(Irp);
390 Complete = FALSE;
391 break;
392
393 case TDI_RECEIVE_DATAGRAM:
394 Status = DispTdiReceiveDatagram(Irp);
395 Complete = FALSE;
396 break;
397
398 case TDI_SEND:
399 Status = DispTdiSend(Irp);
400 Complete = FALSE; /* Completed in DispTdiSend */
401 break;
402
403 case TDI_SEND_DATAGRAM:
404 Status = DispTdiSendDatagram(Irp);
405 Complete = FALSE;
406 break;
407
408 case TDI_ACCEPT:
409 Status = DispTdiAccept(Irp);
410 break;
411
412 case TDI_LISTEN:
413 Status = DispTdiListen(Irp);
414 Complete = FALSE;
415 break;
416
417 case TDI_CONNECT:
418 Status = DispTdiConnect(Irp);
419 Complete = FALSE; /* Completed by the TCP event handler */
420 break;
421
422 case TDI_DISCONNECT:
423 Status = DispTdiDisconnect(Irp);
424 Complete = FALSE;
425 break;
426
427 case TDI_ASSOCIATE_ADDRESS:
428 Status = DispTdiAssociateAddress(Irp);
429 break;
430
431 case TDI_DISASSOCIATE_ADDRESS:
432 Status = DispTdiDisassociateAddress(Irp);
433 break;
434
435 case TDI_QUERY_INFORMATION:
436 Status = DispTdiQueryInformation(DeviceObject, Irp);
437 break;
438
439 case TDI_SET_INFORMATION:
440 Status = DispTdiSetInformation(Irp);
441 break;
442
443 case TDI_SET_EVENT_HANDLER:
444 Status = DispTdiSetEventHandler(Irp);
445 break;
446
447 case TDI_ACTION:
448 Status = STATUS_SUCCESS;
449 break;
450
451 /* An unsupported IOCTL code was submitted */
452 default:
453 Status = STATUS_INVALID_DEVICE_REQUEST;
454 }
455
456 TI_DbgPrint(DEBUG_IRP, ("[TCPIP, TiDispatchInternal] Leaving. Status = (0x%X).\n", Status));
457
458 if( Complete )
459 IRPFinish( Irp, Status );
460
461 return Status;
462 }
463
464
465 NTSTATUS NTAPI
466 TiDispatch(
467 PDEVICE_OBJECT DeviceObject,
468 PIRP Irp)
469 /*
470 * FUNCTION: Dispatch routine for IRP_MJ_DEVICE_CONTROL requests
471 * ARGUMENTS:
472 * DeviceObject = Pointer to a device object for this driver
473 * Irp = Pointer to a I/O request packet
474 * RETURNS:
475 * Status of the operation
476 */
477 {
478 NTSTATUS Status;
479 PIO_STACK_LOCATION IrpSp;
480
481 IrpSp = IoGetCurrentIrpStackLocation(Irp);
482
483 TI_DbgPrint(DEBUG_IRP, ("[TCPIP, TiDispatch] Called. IRP is at (0x%X).\n", Irp));
484
485 Irp->IoStatus.Information = 0;
486
487 #if 0
488 Status = TdiMapUserRequest(DeviceObject, Irp, IrpSp);
489 if (NT_SUCCESS(Status)) {
490 TiDispatchInternal(DeviceObject, Irp);
491 Status = STATUS_PENDING;
492 } else {
493 #else
494 if (TRUE) {
495 #endif
496 /* See if this request is TCP/IP specific */
497 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
498 case IOCTL_TCP_QUERY_INFORMATION_EX:
499 TI_DbgPrint(MIN_TRACE, ("TCP_QUERY_INFORMATION_EX\n"));
500 Status = DispTdiQueryInformationEx(Irp, IrpSp);
501 break;
502
503 case IOCTL_TCP_SET_INFORMATION_EX:
504 TI_DbgPrint(MIN_TRACE, ("TCP_SET_INFORMATION_EX\n"));
505 Status = DispTdiSetInformationEx(Irp, IrpSp);
506 break;
507
508 case IOCTL_SET_IP_ADDRESS:
509 TI_DbgPrint(MIN_TRACE, ("SET_IP_ADDRESS\n"));
510 Status = DispTdiSetIPAddress(Irp, IrpSp);
511 break;
512
513 case IOCTL_DELETE_IP_ADDRESS:
514 TI_DbgPrint(MIN_TRACE, ("DELETE_IP_ADDRESS\n"));
515 Status = DispTdiDeleteIPAddress(Irp, IrpSp);
516 break;
517
518 default:
519 TI_DbgPrint(MIN_TRACE, ("Unknown IOCTL 0x%X\n",
520 IrpSp->Parameters.DeviceIoControl.IoControlCode));
521 Status = STATUS_NOT_IMPLEMENTED;
522 break;
523 }
524 }
525
526 TI_DbgPrint(DEBUG_IRP, ("[TCPIP, TiDispatch] Leaving. Status = (0x%X).\n", Status));
527
528 return IRPFinish( Irp, Status );
529 }
530
531
532 VOID NTAPI TiUnload(
533 PDRIVER_OBJECT DriverObject)
534 /*
535 * FUNCTION: Unloads the driver
536 * ARGUMENTS:
537 * DriverObject = Pointer to driver object created by the system
538 */
539 {
540 #if DBG
541 KIRQL OldIrql;
542
543 TcpipAcquireSpinLock(&AddressFileListLock, &OldIrql);
544 if (!IsListEmpty(&AddressFileListHead)) {
545 TI_DbgPrint(MIN_TRACE, ("[TCPIP, TiUnload] Called. Open address file objects exists.\n"));
546 }
547 TcpipReleaseSpinLock(&AddressFileListLock, OldIrql);
548 #endif
549 /* Cancel timer */
550 KeCancelTimer(&IPTimer);
551
552 /* Unregister loopback adapter */
553 LoopUnregisterAdapter(NULL);
554
555 /* Unregister protocol with NDIS */
556 LANUnregisterProtocol();
557
558 /* Shutdown transport level protocol subsystems */
559 TCPShutdown();
560 UDPShutdown();
561 RawIPShutdown();
562 ICMPShutdown();
563
564 /* Shutdown network level protocol subsystem */
565 IPShutdown();
566
567 /* Free NDIS buffer descriptors */
568 if (GlobalBufferPool)
569 NdisFreeBufferPool(GlobalBufferPool);
570
571 /* Free NDIS packet descriptors */
572 if (GlobalPacketPool)
573 NdisFreePacketPool(GlobalPacketPool);
574
575 /* Release all device objects */
576
577 if (TCPDeviceObject)
578 IoDeleteDevice(TCPDeviceObject);
579
580 if (UDPDeviceObject)
581 IoDeleteDevice(UDPDeviceObject);
582
583 if (RawIPDeviceObject)
584 IoDeleteDevice(RawIPDeviceObject);
585
586 if (IPDeviceObject) {
587 ChewShutdown();
588 IoDeleteDevice(IPDeviceObject);
589 }
590
591 if (EntityList)
592 ExFreePoolWithTag(EntityList, TDI_ENTITY_TAG);
593
594 TI_DbgPrint(MAX_TRACE, ("[TCPIP, TiUnload] Leaving.\n"));
595 }
596
597 NTSTATUS NTAPI
598 DriverEntry(
599 PDRIVER_OBJECT DriverObject,
600 PUNICODE_STRING RegistryPath)
601 /*
602 * FUNCTION: Main driver entry point
603 * ARGUMENTS:
604 * DriverObject = Pointer to a driver object for this driver
605 * RegistryPath = Registry node for configuration parameters
606 * RETURNS:
607 * Status of driver initialization
608 */
609 {
610 NTSTATUS Status;
611 UNICODE_STRING strIpDeviceName = RTL_CONSTANT_STRING(DD_IP_DEVICE_NAME);
612 UNICODE_STRING strRawDeviceName = RTL_CONSTANT_STRING(DD_RAWIP_DEVICE_NAME);
613 UNICODE_STRING strUdpDeviceName = RTL_CONSTANT_STRING(DD_UDP_DEVICE_NAME);
614 UNICODE_STRING strTcpDeviceName = RTL_CONSTANT_STRING(DD_TCP_DEVICE_NAME);
615 UNICODE_STRING strNdisDeviceName = RTL_CONSTANT_STRING(TCPIP_PROTOCOL_NAME);
616 NDIS_STATUS NdisStatus;
617 LARGE_INTEGER DueTime;
618
619 TI_DbgPrint(MAX_TRACE, ("[TCPIP, DriverEntry] Called\n"));
620
621 /* TdiInitialize() ? */
622
623 /* FIXME: Create symbolic links in Win32 namespace */
624
625 /* Initialize our periodic timer and its associated DPC object. When the
626 timer expires, the IPTimeout deferred procedure call (DPC) is queued */
627 KeInitializeDpc(&IPTimeoutDpc, IPTimeoutDpcFn, NULL);
628 KeInitializeTimer(&IPTimer);
629
630 /* Create IP device object */
631 Status = IoCreateDevice(DriverObject, 0, &strIpDeviceName,
632 FILE_DEVICE_NETWORK, 0, FALSE, &IPDeviceObject);
633 if (!NT_SUCCESS(Status)) {
634 TI_DbgPrint(MIN_TRACE, ("Failed to create IP device object. Status (0x%X).\n", Status));
635 TiUnload(DriverObject);
636 return Status;
637 }
638
639 ChewInit( IPDeviceObject );
640
641 /* Create RawIP device object */
642 Status = IoCreateDevice(DriverObject, 0, &strRawDeviceName,
643 FILE_DEVICE_NETWORK, 0, FALSE, &RawIPDeviceObject);
644 if (!NT_SUCCESS(Status)) {
645 TI_DbgPrint(MIN_TRACE, ("Failed to create RawIP device object. Status (0x%X).\n", Status));
646 TiUnload(DriverObject);
647 return Status;
648 }
649
650 /* Create UDP device object */
651 Status = IoCreateDevice(DriverObject, 0, &strUdpDeviceName,
652 FILE_DEVICE_NETWORK, 0, FALSE, &UDPDeviceObject);
653 if (!NT_SUCCESS(Status)) {
654 TI_DbgPrint(MIN_TRACE, ("Failed to create UDP device object. Status (0x%X).\n", Status));
655 TiUnload(DriverObject);
656 return Status;
657 }
658
659 /* Create TCP device object */
660 Status = IoCreateDevice(DriverObject, 0, &strTcpDeviceName,
661 FILE_DEVICE_NETWORK, 0, FALSE, &TCPDeviceObject);
662 if (!NT_SUCCESS(Status)) {
663 TI_DbgPrint(MIN_TRACE, ("Failed to create TCP device object. Status (0x%X).\n", Status));
664 TiUnload(DriverObject);
665 return Status;
666 }
667
668 /* Setup network layer and transport layer entities */
669 KeInitializeSpinLock(&EntityListLock);
670 EntityList = ExAllocatePoolWithTag(NonPagedPool,
671 sizeof(TDIEntityID) * MAX_TDI_ENTITIES,
672 TDI_ENTITY_TAG );
673 if (!EntityList) {
674 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
675 TiUnload(DriverObject);
676 return STATUS_INSUFFICIENT_RESOURCES;
677 }
678
679 EntityCount = 0;
680 EntityMax = MAX_TDI_ENTITIES;
681
682 /* Allocate NDIS packet descriptors */
683 NdisAllocatePacketPoolEx(&NdisStatus, &GlobalPacketPool, 500, 1500, sizeof(PACKET_CONTEXT));
684 if (NdisStatus != NDIS_STATUS_SUCCESS) {
685 TiUnload(DriverObject);
686 return STATUS_INSUFFICIENT_RESOURCES;
687 }
688
689 /* Allocate NDIS buffer descriptors */
690 NdisAllocateBufferPool(&NdisStatus, &GlobalBufferPool, 2000);
691 if (NdisStatus != NDIS_STATUS_SUCCESS) {
692 TiUnload(DriverObject);
693 return STATUS_INSUFFICIENT_RESOURCES;
694 }
695
696 /* Initialize address file list and protecting spin lock */
697 InitializeListHead(&AddressFileListHead);
698 KeInitializeSpinLock(&AddressFileListLock);
699
700 /* Initialize connection endpoint list and protecting spin lock */
701 InitializeListHead(&ConnectionEndpointListHead);
702 KeInitializeSpinLock(&ConnectionEndpointListLock);
703
704 /* Initialize interface list and protecting spin lock */
705 InitializeListHead(&InterfaceListHead);
706 KeInitializeSpinLock(&InterfaceListLock);
707
708 /* Initialize network level protocol subsystem */
709 IPStartup(RegistryPath);
710
711 /* Initialize transport level protocol subsystems */
712 Status = RawIPStartup();
713 if( !NT_SUCCESS(Status) ) {
714 TiUnload(DriverObject);
715 return Status;
716 }
717
718 Status = UDPStartup();
719 if( !NT_SUCCESS(Status) ) {
720 TiUnload(DriverObject);
721 return Status;
722 }
723
724 Status = TCPStartup();
725 if( !NT_SUCCESS(Status) ) {
726 TiUnload(DriverObject);
727 return Status;
728 }
729
730 Status = ICMPStartup();
731 if( !NT_SUCCESS(Status) ) {
732 TiUnload(DriverObject);
733 return Status;
734 }
735
736 /* Use direct I/O */
737 IPDeviceObject->Flags |= DO_DIRECT_IO;
738 RawIPDeviceObject->Flags |= DO_DIRECT_IO;
739 UDPDeviceObject->Flags |= DO_DIRECT_IO;
740 TCPDeviceObject->Flags |= DO_DIRECT_IO;
741
742 /* Initialize the driver object with this driver's entry points */
743 DriverObject->MajorFunction[IRP_MJ_CREATE] = TiDispatchOpenClose;
744 DriverObject->MajorFunction[IRP_MJ_CLOSE] = TiDispatchOpenClose;
745 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = TiDispatchInternal;
746 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = TiDispatch;
747
748 DriverObject->DriverUnload = TiUnload;
749
750 /* Open loopback adapter */
751 Status = LoopRegisterAdapter(NULL, NULL);
752 if (!NT_SUCCESS(Status)) {
753 TI_DbgPrint(MIN_TRACE, ("Failed to create loopback adapter. Status (0x%X).\n", Status));
754 TiUnload(DriverObject);
755 return Status;
756 }
757
758 /* Register protocol with NDIS */
759 /* This used to be IP_DEVICE_NAME but the DDK says it has to match your entry in the SCM */
760 Status = LANRegisterProtocol(&strNdisDeviceName);
761 if (!NT_SUCCESS(Status)) {
762 TI_DbgPrint(MIN_TRACE,("Failed to register protocol with NDIS; status 0x%x\n", Status));
763 TiWriteErrorLog(
764 DriverObject,
765 EVENT_TRANSPORT_REGISTER_FAILED,
766 TI_ERROR_DRIVERENTRY,
767 Status,
768 NULL,
769 0,
770 NULL);
771 TiUnload(DriverObject);
772 return Status;
773 }
774
775 /* Start the periodic timer with an initial and periodic
776 relative expiration time of IP_TIMEOUT milliseconds */
777 DueTime.QuadPart = -(LONGLONG)IP_TIMEOUT * 10000;
778 KeSetTimerEx(&IPTimer, DueTime, IP_TIMEOUT, &IPTimeoutDpc);
779
780 TI_DbgPrint(MAX_TRACE, ("[TCPIP, DriverEntry] Finished\n"));
781
782
783 return STATUS_SUCCESS;
784 }
785
786 VOID NTAPI
787 IPAddInterface(
788 ULONG Unknown0,
789 ULONG Unknown1,
790 ULONG Unknown2,
791 ULONG Unknown3,
792 ULONG Unknown4)
793 {
794 UNIMPLEMENTED
795 }
796
797
798 VOID NTAPI
799 IPDelInterface(
800 ULONG Unknown0)
801 {
802 UNIMPLEMENTED
803 }
804
805
806 VOID NTAPI
807 LookupRoute(
808 ULONG Unknown0,
809 ULONG Unknown1)
810 {
811 UNIMPLEMENTED
812 }
813
814 /* EOF */