2 * Copyright (c) 1999, 2000
3 * Politecnico di Torino. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the Politecnico
13 * di Torino, and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <ddk/ntddk.h>
33 static NDIS_MEDIUM MediumArray[] = {
37 NdisMediumArcnet878_2,
42 #define NUM_NDIS_MEDIA (sizeof MediumArray / sizeof MediumArray[0])
44 ULONG NamedEventsCounter=0;
46 //Itoa. Replaces the buggy RtlIntegerToUnicodeString
47 void PacketItoa(UINT n,PUCHAR buf){
58 /// Global start time. Used as an absolute reference for timestamp conversion.
59 struct time_conv G_Start_Time = {
64 UINT n_Opened_Instances = 0;
66 NDIS_SPIN_LOCK Opened_Instances_Lock;
68 //-------------------------------------------------------------------
71 NPF_Open(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
74 PDEVICE_EXTENSION DeviceExtension;
78 PIO_STACK_LOCATION IrpSp;
81 NDIS_STATUS ErrorStatus;
84 PLIST_ENTRY PacketListEntry;
88 IF_LOUD(DbgPrint("NPF: OpenAdapter\n");)
90 DeviceExtension = DeviceObject->DeviceExtension;
93 IrpSp = IoGetCurrentIrpStackLocation(Irp);
95 // allocate some memory for the open structure
96 Open=ExAllocatePoolWithTag(NonPagedPool, sizeof(OPEN_INSTANCE), '0OWA');
101 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
102 IoCompleteRequest(Irp, IO_NO_INCREMENT);
103 return STATUS_INSUFFICIENT_RESOURCES;
108 sizeof(OPEN_INSTANCE)
112 EvName=ExAllocatePoolWithTag(NonPagedPool, sizeof(L"\\BaseNamedObjects\\NPF0000000000"), '1OWA');
117 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
118 IoCompleteRequest(Irp, IO_NO_INCREMENT);
119 return STATUS_INSUFFICIENT_RESOURCES;
123 IrpSp->FileObject->FsContext=Open;
125 Open->DeviceExtension=DeviceExtension;
128 // Save the Irp here for the completeion routine to retrieve
129 Open->OpenCloseIrp=Irp;
131 // Allocate a packet pool for our xmit and receive packets
132 NdisAllocatePacketPool(
136 sizeof(PACKET_RESERVED));
139 if (Status != NDIS_STATUS_SUCCESS) {
141 IF_LOUD(DbgPrint("NPF: Failed to allocate packet pool\n");)
145 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
146 IoCompleteRequest(Irp, IO_NO_INCREMENT);
147 return STATUS_INSUFFICIENT_RESOURCES;
151 RtlCopyBytes(EvName,L"\\BaseNamedObjects\\NPF0000000000",sizeof(L"\\BaseNamedObjects\\NPF0000000000"));
153 //Create the string containing the name of the read event
154 RtlInitUnicodeString(&Open->ReadEventName,(PCWSTR) EvName);
156 PacketItoa(NamedEventsCounter,(PUCHAR)(Open->ReadEventName.Buffer+21));
158 InterlockedIncrement(&NamedEventsCounter);
160 IF_LOUD(DbgPrint("\nCreated the named event for the read; name=%ws, counter=%d\n", Open->ReadEventName.Buffer,NamedEventsCounter-1);)
162 //allocate the event objects
163 Open->ReadEvent=IoCreateNotificationEvent(&Open->ReadEventName,&Open->ReadEventHandle);
164 if(Open->ReadEvent==NULL){
167 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
168 IoCompleteRequest(Irp, IO_NO_INCREMENT);
169 return STATUS_INSUFFICIENT_RESOURCES;
172 KeInitializeEvent(Open->ReadEvent, NotificationEvent, FALSE);
173 KeClearEvent(Open->ReadEvent);
174 NdisInitializeEvent(&Open->WriteEvent);
175 NdisInitializeEvent(&Open->IOEvent);
176 NdisInitializeEvent(&Open->DumpEvent);
177 NdisInitializeEvent(&Open->IOEvent);
178 NdisAllocateSpinLock(&Open->machine_lock);
181 // list to hold irp's want to reset the adapter
182 InitializeListHead(&Open->ResetIrpList);
185 // Initialize the request list
186 KeInitializeSpinLock(&Open->RequestSpinLock);
187 InitializeListHead(&Open->RequestList);
189 // Initializes the extended memory of the NPF machine
190 Open->mem_ex.buffer = ExAllocatePoolWithTag(NonPagedPool, DEFAULT_MEM_EX_SIZE, '2OWA');
191 if((Open->mem_ex.buffer) == NULL)
196 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
197 IoCompleteRequest(Irp, IO_NO_INCREMENT);
198 return STATUS_INSUFFICIENT_RESOURCES;
201 Open->mem_ex.size = DEFAULT_MEM_EX_SIZE;
202 RtlZeroMemory(Open->mem_ex.buffer, DEFAULT_MEM_EX_SIZE);
205 // Initialize the open instance
212 Open->Dropped = 0; //reset the dropped packets counter
213 Open->Received = 0; //reset the received packets counter
214 Open->Accepted = 0; //reset the accepted packets counter
215 Open->bpfprogram = NULL; //reset the filter
216 Open->mode = MODE_CAPT;
217 Open->Nbytes.QuadPart = 0;
218 Open->Npackets.QuadPart = 0;
220 Open->Multiple_Write_Counter = 0;
222 Open->TimeOut.QuadPart = (LONGLONG)1;
224 Open->DumpFileName.Buffer = NULL;
225 Open->DumpFileHandle = NULL;
226 Open->tme.active = TME_NONE_ACTIVE;
227 Open->DumpLimitReached = FALSE;
229 //allocate the spinlock for the statistic counters
230 NdisAllocateSpinLock(&Open->CountersLock);
232 //allocate the spinlock for the buffer pointers
233 NdisAllocateSpinLock(&Open->BufLock);
236 // link up the request stored in our open block
238 for (i=0;i<MAX_REQUESTS;i++) {
239 ExInterlockedInsertTailList(
241 &Open->Requests[i].ListElement,
242 &Open->RequestSpinLock);
247 IoMarkIrpPending(Irp);
250 // Try to open the MAC
252 IF_LOUD(DbgPrint("NPF: Openinig the device %ws, BindingContext=%d\n",DeviceExtension->AdapterName.Buffer, Open);)
257 &Open->AdapterHandle,
261 DeviceExtension->NdisProtocolHandle,
263 &DeviceExtension->AdapterName,
267 IF_LOUD(DbgPrint("NPF: Opened the device, Status=%x\n",Status);)
269 if (Status != NDIS_STATUS_PENDING)
271 NPF_OpenAdapterComplete(Open,Status,NDIS_STATUS_SUCCESS);
274 return(STATUS_PENDING);
277 //-------------------------------------------------------------------
279 VOID NPF_OpenAdapterComplete(
280 IN NDIS_HANDLE ProtocolBindingContext,
281 IN NDIS_STATUS Status,
282 IN NDIS_STATUS OpenErrorStatus)
288 IF_LOUD(DbgPrint("NPF: OpenAdapterComplete\n");)
290 Open= (POPEN_INSTANCE)ProtocolBindingContext;
295 Irp=Open->OpenCloseIrp;
297 if (Status != NDIS_STATUS_SUCCESS) {
299 IF_LOUD(DbgPrint("NPF: OpenAdapterComplete-FAILURE\n");)
301 NdisFreePacketPool(Open->PacketPool);
304 Open->mem_ex.size = 0;
305 if(Open->mem_ex.buffer != NULL)ExFreePool(Open->mem_ex.buffer);
307 ExFreePool(Open->ReadEventName.Buffer);
312 NdisAcquireSpinLock(&Opened_Instances_Lock);
313 n_Opened_Instances++;
314 NdisReleaseSpinLock(&Opened_Instances_Lock);
316 IF_LOUD(DbgPrint("Opened Instances:%d", n_Opened_Instances);)
318 // Get the absolute value of the system boot time.
319 // This is used for timestamp conversion.
320 TIME_SYNCHRONIZE(&G_Start_Time);
323 Irp->IoStatus.Status = Status;
324 Irp->IoStatus.Information = 0;
325 IoCompleteRequest(Irp, IO_NO_INCREMENT);
331 //-------------------------------------------------------------------
334 NPF_Close(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
339 PIO_STACK_LOCATION IrpSp;
340 LARGE_INTEGER ThreadDelay;
342 IF_LOUD(DbgPrint("NPF: CloseAdapter\n");)
344 IrpSp = IoGetCurrentIrpStackLocation(Irp);
346 Open=IrpSp->FileObject->FsContext;
348 // Reset the buffer size. This tells the dump thread to stop.
351 if( Open->Bound == FALSE){
353 NdisWaitEvent(&Open->IOEvent,10000);
355 // Free the filter if it's present
356 if(Open->bpfprogram != NULL)
357 ExFreePool(Open->bpfprogram);
359 // Free the jitted filter if it's present
360 if(Open->Filter != NULL)
361 BPF_Destroy_JIT_Filter(Open->Filter);
365 if(Open->Buffer != NULL)ExFreePool(Open->Buffer);
368 Open->mem_ex.size = 0;
369 if(Open->mem_ex.buffer != NULL)ExFreePool(Open->mem_ex.buffer);
371 NdisFreePacketPool(Open->PacketPool);
373 // Free the string with the name of the dump file
374 if(Open->DumpFileName.Buffer!=NULL)
375 ExFreePool(Open->DumpFileName.Buffer);
377 ExFreePool(Open->ReadEventName.Buffer);
380 Irp->IoStatus.Information = 0;
381 Irp->IoStatus.Status = STATUS_SUCCESS;
382 IoCompleteRequest(Irp, IO_NO_INCREMENT);
384 return(STATUS_SUCCESS);
387 // Unfreeze the consumer
388 if(Open->mode & MODE_DUMP)
389 NdisSetEvent(&Open->DumpEvent);
391 KeSetEvent(Open->ReadEvent,0,FALSE);
394 Open->OpenCloseIrp = Irp;
396 IoMarkIrpPending(Irp);
398 // If this instance is in dump mode, complete the dump and close the file
399 if((Open->mode & MODE_DUMP) && Open->DumpFileHandle != NULL){
403 ThreadDelay.QuadPart = -50000000;
404 // Wait the completion of the thread
405 wres = KeWaitForSingleObject(Open->DumpThreadObject,
411 ObDereferenceObject(Open->DumpThreadObject);
414 // Flush and close the dump file
415 NPF_CloseDumpFile(Open);
419 // Destroy the read Event
420 ZwClose(Open->ReadEventHandle);
428 if (Status != NDIS_STATUS_PENDING) {
430 NPF_CloseAdapterComplete(
434 return STATUS_SUCCESS;
438 return(STATUS_PENDING);
441 //-------------------------------------------------------------------
444 NPF_CloseAdapterComplete(IN NDIS_HANDLE ProtocolBindingContext,IN NDIS_STATUS Status)
449 IF_LOUD(DbgPrint("NPF: CloseAdapterComplete\n");)
451 Open= (POPEN_INSTANCE)ProtocolBindingContext;
453 // free the allocated structures only if the instance is still bound to the adapter
454 if(Open->Bound == TRUE){
456 // Free the filter if it's present
457 if(Open->bpfprogram != NULL)
458 ExFreePool(Open->bpfprogram);
460 // Free the jitted filter if it's present
461 if(Open->Filter != NULL)
462 BPF_Destroy_JIT_Filter(Open->Filter);
466 if(Open->Buffer!=NULL)ExFreePool(Open->Buffer);
469 Open->mem_ex.size = 0;
470 if(Open->mem_ex.buffer != NULL)ExFreePool(Open->mem_ex.buffer);
472 NdisFreePacketPool(Open->PacketPool);
474 Irp=Open->OpenCloseIrp;
476 // Free the string with the name of the dump file
477 if(Open->DumpFileName.Buffer!=NULL)
478 ExFreePool(Open->DumpFileName.Buffer);
480 ExFreePool(Open->ReadEventName.Buffer);
483 // Complete the request only if the instance is still bound to the adapter
484 Irp->IoStatus.Status = STATUS_SUCCESS;
485 Irp->IoStatus.Information = 0;
486 IoCompleteRequest(Irp, IO_NO_INCREMENT);
489 NdisSetEvent(&Open->IOEvent);
491 // Decrease the counter of open instances
492 NdisAcquireSpinLock(&Opened_Instances_Lock);
493 n_Opened_Instances--;
494 NdisReleaseSpinLock(&Opened_Instances_Lock);
496 IF_LOUD(DbgPrint("Opened Instances:%d", n_Opened_Instances);)
498 if(n_Opened_Instances == 0){
499 // Force a synchronization at the next NPF_Open().
500 // This hopefully avoids the synchronization issues caused by hibernation or standby.
501 TIME_DESYNCHRONIZE(&G_Start_Time);
507 //-------------------------------------------------------------------
511 NPF_PowerChange(IN NDIS_HANDLE ProtocolBindingContext, IN PNET_PNP_EVENT pNetPnPEvent)
513 IF_LOUD(DbgPrint("NPF: PowerChange\n");)
515 TIME_DESYNCHRONIZE(&G_Start_Time);
517 TIME_SYNCHRONIZE(&G_Start_Time);
519 return STATUS_SUCCESS;
523 //-------------------------------------------------------------------
527 OUT PNDIS_STATUS Status,
528 IN NDIS_HANDLE BindContext,
529 IN PNDIS_STRING DeviceName,
530 IN PVOID SystemSpecific1,
531 IN PVOID SystemSpecific2
534 IF_LOUD(DbgPrint("NPF: NPF_BindAdapter\n");)
537 //-------------------------------------------------------------------
541 OUT PNDIS_STATUS Status,
542 IN NDIS_HANDLE ProtocolBindingContext,
543 IN NDIS_HANDLE UnbindContext
546 POPEN_INSTANCE Open =(POPEN_INSTANCE)ProtocolBindingContext;
549 IF_LOUD(DbgPrint("NPF: NPF_UnbindAdapter\n");)
551 // Reset the buffer size. This tells the dump thread to stop.
554 NdisResetEvent(&Open->IOEvent);
556 // This open instance is no more bound to the adapter, set Bound to False
557 InterlockedExchange( (PLONG) &Open->Bound, FALSE );
559 // Awake a possible pending read on this instance
560 if(Open->mode & MODE_DUMP)
561 NdisSetEvent(&Open->DumpEvent);
563 KeSetEvent(Open->ReadEvent,0,FALSE);
565 // If this instance is in dump mode, complete the dump and close the file
566 if((Open->mode & MODE_DUMP) && Open->DumpFileHandle != NULL)
567 NPF_CloseDumpFile(Open);
569 // Destroy the read Event
570 ZwClose(Open->ReadEventHandle);
578 if (lStatus != NDIS_STATUS_PENDING) {
580 NPF_CloseAdapterComplete(
585 *Status = NDIS_STATUS_SUCCESS;
590 *Status = NDIS_STATUS_SUCCESS;
594 //-------------------------------------------------------------------
597 NPF_ResetComplete(IN NDIS_HANDLE ProtocolBindingContext,IN NDIS_STATUS Status)
603 PLIST_ENTRY ResetListEntry;
605 IF_LOUD(DbgPrint("NPF: PacketResetComplte\n");)
607 Open= (POPEN_INSTANCE)ProtocolBindingContext;
611 // remove the reset IRP from the list
613 ResetListEntry=ExInterlockedRemoveHeadList(
615 &Open->RequestSpinLock
619 if (ResetListEntry == NULL) {
625 Irp=CONTAINING_RECORD(ResetListEntry,IRP,Tail.Overlay.ListEntry);
627 Irp->IoStatus.Status = STATUS_SUCCESS;
628 IoCompleteRequest(Irp, IO_NO_INCREMENT);
630 IF_LOUD(DbgPrint("NPF: PacketResetComplte exit\n");)