2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS library
4 * FILE: ndis/protocol.c
5 * PURPOSE: Routines used by NDIS protocol drivers
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Vizzini (vizzini@plasmic.com)
9 * CSH 01/08-2000 Created
10 * 09-13-2003 Vizzini Updates for SendPackets support
19 IN NDIS_HANDLE MiniportAdapterHandle
,
20 IN PNDIS_PACKET Packet
,
21 IN NDIS_STATUS Status
);
23 #define SERVICES_KEY L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"
24 #define LINKAGE_KEY L"\\Linkage"
25 #define PARAMETERS_KEY L"\\Parameters\\"
27 LIST_ENTRY ProtocolListHead
;
28 KSPIN_LOCK ProtocolListLock
;
36 NdisCompleteBindAdapter(
37 IN NDIS_HANDLE BindAdapterContext
,
38 IN NDIS_STATUS Status
,
39 IN NDIS_STATUS OpenStatus
)
41 * FUNCTION: Indicates a packet to bound protocols
43 * Adapter = Pointer to logical adapter
44 * Packet = Pointer to packet to indicate
48 * - FIXME: partially-implemented
51 PROTOCOL_BINDING
*Protocol
= (PROTOCOL_BINDING
*)BindAdapterContext
;
53 /* Put protocol binding struct on global list */
54 ExInterlockedInsertTailList(&ProtocolListHead
, &Protocol
->ListEntry
, &ProtocolListLock
);
60 PLOGICAL_ADAPTER Adapter
,
63 * FUNCTION: Indicates a packet to bound protocols
65 * Adapter = Pointer to logical adapter
66 * Packet = Pointer to packet to indicate
68 * STATUS_SUCCESS in all cases
70 * - XXX ATM, this only handles loopback packets - is that its designed function?
77 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
80 MiniDisplayPacket(Packet
);
83 NdisQueryPacket(Packet
, NULL
, NULL
, NULL
, &PacketLength
);
85 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
86 KeAcquireSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, &OldIrql
);
88 Adapter
->LoopPacket
= Packet
;
89 BufferedLength
= CopyPacketToBuffer(Adapter
->LookaheadBuffer
, Packet
, 0, Adapter
->NdisMiniportBlock
.CurrentLookahead
);
91 KeReleaseSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, OldIrql
);
93 if (BufferedLength
> Adapter
->MediumHeaderSize
)
95 /* XXX Change this to call SendPackets so we don't have to duplicate this wacky logic */
96 MiniIndicateData(Adapter
, NULL
, Adapter
->LookaheadBuffer
, Adapter
->MediumHeaderSize
,
97 &Adapter
->LookaheadBuffer
[Adapter
->MediumHeaderSize
], BufferedLength
- Adapter
->MediumHeaderSize
,
98 PacketLength
- Adapter
->MediumHeaderSize
);
102 MiniIndicateData(Adapter
, NULL
, Adapter
->LookaheadBuffer
, Adapter
->MediumHeaderSize
, NULL
, 0, 0);
105 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
106 KeAcquireSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, &OldIrql
);
108 Adapter
->LoopPacket
= NULL
;
110 KeReleaseSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, OldIrql
);
112 return STATUS_SUCCESS
;
118 IN NDIS_HANDLE MacBindingHandle
,
119 IN PNDIS_REQUEST NdisRequest
)
121 * FUNCTION: Forwards a request to an NDIS miniport
123 * MacBindingHandle = Adapter binding handle
124 * NdisRequest = Pointer to request to perform
126 * Status of operation
130 BOOLEAN QueueWorkItem
= FALSE
;
131 NDIS_STATUS NdisStatus
;
132 PADAPTER_BINDING AdapterBinding
;
133 PLOGICAL_ADAPTER Adapter
;
135 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
137 ASSERT(MacBindingHandle
);
138 AdapterBinding
= GET_ADAPTER_BINDING(MacBindingHandle
);
140 ASSERT(AdapterBinding
->Adapter
);
141 Adapter
= AdapterBinding
->Adapter
;
144 * If the miniport is already busy, queue a workitem
146 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
147 KeAcquireSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, &OldIrql
);
149 if(Adapter
->MiniportBusy
)
150 QueueWorkItem
= TRUE
;
153 NDIS_DbgPrint(MAX_TRACE
, ("Setting adapter 0x%x to busy\n"));
154 Adapter
->MiniportBusy
= TRUE
;
158 /* MiniQueueWorkItem must be called at IRQL >= DISPATCH_LEVEL */
161 MiniQueueWorkItem(Adapter
, NdisWorkItemRequest
, (PVOID
)NdisRequest
);
162 return NDIS_STATUS_PENDING
;
165 KeReleaseSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, OldIrql
);
167 /* MiniportQueryInformation (called by MiniDoRequest) runs at DISPATCH_LEVEL */
168 /* TODO (?): move the irql raise into MiniDoRequest */
169 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
171 NdisStatus
= MiniDoRequest(Adapter
, NdisRequest
);
173 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
174 KeAcquireSpinLockAtDpcLevel(&Adapter
->NdisMiniportBlock
.Lock
);
176 NDIS_DbgPrint(MAX_TRACE
, ("Setting adapter 0x%x to free\n"));
177 Adapter
->MiniportBusy
= FALSE
;
179 if (Adapter
->WorkQueueHead
)
180 KeInsertQueueDpc(&Adapter
->MiniportDpc
, NULL
, NULL
);
182 KeReleaseSpinLockFromDpcLevel(&Adapter
->NdisMiniportBlock
.Lock
);
184 KeLowerIrql(OldIrql
);
192 IN NDIS_HANDLE MacBindingHandle
)
196 return NDIS_STATUS_FAILURE
;
202 IN NDIS_HANDLE MacBindingHandle
,
203 IN PNDIS_PACKET Packet
)
205 * FUNCTION: Forwards a request to send a packet to an NDIS miniport
207 * MacBindingHandle = Adapter binding handle
208 * Packet = Pointer to NDIS packet descriptor
210 * NDIS_STATUS_SUCCESS always
213 * - Fix return values
214 * - Should queue packet if miniport returns NDIS_STATUS_RESOURCES
215 * - Queue packets directly on the adapters when possible (i.e.
216 * when miniports not busy)
220 KIRQL RaiseOldIrql
, SpinOldIrql
;
221 BOOLEAN QueueWorkItem
= FALSE
;
222 NDIS_STATUS NdisStatus
;
223 PADAPTER_BINDING AdapterBinding
;
224 PLOGICAL_ADAPTER Adapter
;
226 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
228 ASSERT(MacBindingHandle
);
229 AdapterBinding
= GET_ADAPTER_BINDING(MacBindingHandle
);
231 ASSERT(AdapterBinding
);
232 Adapter
= AdapterBinding
->Adapter
;
236 /* if the following is not true, KeRaiseIrql() below will break */
237 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
239 /* XXX what is this crazy black magic? */
240 Packet
->Reserved
[0] = (ULONG_PTR
)MacBindingHandle
;
243 * Acquire this lock in order to see if the miniport is busy.
244 * If it is not busy, we mark it as busy and release the lock.
245 * Else we don't do anything because we have to queue a workitem
248 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
249 KeAcquireSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, &SpinOldIrql
);
252 * if the miniport is marked as busy, we queue the packet as a work item,
253 * else we send the packet directly to the miniport. Sending to the miniport
256 if (Adapter
->MiniportBusy
)
257 QueueWorkItem
= TRUE
;
260 NDIS_DbgPrint(MAX_TRACE
, ("Setting adapter 0x%x to busy\n"));
261 Adapter
->MiniportBusy
= TRUE
;
264 KeReleaseSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, SpinOldIrql
);
267 * Test the packet to see if it is a MAC loopback.
269 * We may have to loop this packet if miniport cannot.
270 * If dest MAC address of packet == MAC address of adapter,
271 * this is a loopback frame.
273 if ((Adapter
->NdisMiniportBlock
.MacOptions
& NDIS_MAC_OPTION_NO_LOOPBACK
) &&
274 MiniAdapterHasAddress(Adapter
, Packet
))
276 NDIS_DbgPrint(MIN_TRACE
, ("Looping packet.\n"));
280 MiniQueueWorkItem(Adapter
, NdisWorkItemSendLoopback
, (PVOID
)Packet
);
281 return NDIS_STATUS_PENDING
;
284 KeRaiseIrql(DISPATCH_LEVEL
, &RaiseOldIrql
);
287 * atm this *only* handles loopback packets - it calls MiniIndicateData to
288 * send back to the protocol. FIXME: this will need to be adjusted a bit.
289 * Also, I'm not sure you really have to be at dispatch level for this. It
290 * might use a ReceivePackets handler, which can run <= DISPATCH_LEVEL.
292 NdisStatus
= ProIndicatePacket(Adapter
, Packet
);
294 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
295 KeAcquireSpinLockAtDpcLevel(&Adapter
->NdisMiniportBlock
.Lock
);
297 NDIS_DbgPrint(MAX_TRACE
, ("Setting adapter 0x%x to free\n"));
298 Adapter
->MiniportBusy
= FALSE
;
300 if (Adapter
->WorkQueueHead
)
301 KeInsertQueueDpc(&Adapter
->MiniportDpc
, NULL
, NULL
);
303 NDIS_DbgPrint(MID_TRACE
,("Failed to insert packet into work queue\n"));
305 KeReleaseSpinLockFromDpcLevel(&Adapter
->NdisMiniportBlock
.Lock
);
307 KeLowerIrql(RaiseOldIrql
);
312 NDIS_DbgPrint(MID_TRACE
,("Not a loopback packet\n"));
314 /* This is a normal send packet, not a loopback packet. */
317 MiniQueueWorkItem(Adapter
, NdisWorkItemSend
, (PVOID
)Packet
);
318 NDIS_DbgPrint(MAX_TRACE
, ("Queued a work item and returning\n"));
319 return NDIS_STATUS_PENDING
;
322 ASSERT(Adapter
->Miniport
);
325 * Call the appropriate send handler
327 * If a miniport provides a SendPackets handler, we always call it. If not, we call the
330 if(Adapter
->Miniport
->Chars
.SendPacketsHandler
)
332 if(Adapter
->NdisMiniportBlock
.Flags
& NDIS_ATTRIBUTE_DESERIALIZE
)
334 NDIS_DbgPrint(MAX_TRACE
, ("Calling miniport's SendPackets handler\n"));
335 (*Adapter
->Miniport
->Chars
.SendPacketsHandler
)(Adapter
->NdisMiniportBlock
.MiniportAdapterContext
, &Packet
, 1);
339 /* SendPackets is called at DISPATCH_LEVEL for all serialized miniports */
340 KeRaiseIrql(DISPATCH_LEVEL
, &RaiseOldIrql
);
342 NDIS_DbgPrint(MAX_TRACE
, ("Calling miniport's SendPackets handler\n"));
343 (*Adapter
->Miniport
->Chars
.SendPacketsHandler
)(Adapter
->NdisMiniportBlock
.MiniportAdapterContext
, &Packet
, 1);
345 KeLowerIrql(RaiseOldIrql
);
348 /* SendPackets handlers return void - they always "succeed" */
349 NdisStatus
= NDIS_STATUS_SUCCESS
;
353 /* XXX FIXME THIS IS WRONG */
354 /* uh oh... forgot why i thought that... */
355 if(Adapter
->NdisMiniportBlock
.Flags
& NDIS_ATTRIBUTE_DESERIALIZE
)
357 NDIS_DbgPrint(MAX_TRACE
, ("Calling miniport's Send handler\n"));
358 NdisStatus
= (*Adapter
->Miniport
->Chars
.SendHandler
)(Adapter
->NdisMiniportBlock
.MiniportAdapterContext
, Packet
, 0);
359 NDIS_DbgPrint(MAX_TRACE
, ("back from miniport's send handler\n"));
363 /* Send handlers always run at DISPATCH_LEVEL so we raise here */
364 KeRaiseIrql(DISPATCH_LEVEL
, &RaiseOldIrql
);
366 NDIS_DbgPrint(MAX_TRACE
, ("Calling miniport's Send handler\n"));
367 NdisStatus
= (*Adapter
->Miniport
->Chars
.SendHandler
)(Adapter
->NdisMiniportBlock
.MiniportAdapterContext
, Packet
, 0);
368 NDIS_DbgPrint(MAX_TRACE
, ("back from miniport's send handler\n"));
369 if( NdisStatus
!= NDIS_STATUS_PENDING
) {
370 NdisMSendComplete( Adapter
, Packet
, NdisStatus
);
371 Adapter
->MiniportBusy
= FALSE
;
373 KeLowerIrql(RaiseOldIrql
);
377 /* XXX why the hell do we do this? */
378 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
379 KeAcquireSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, &SpinOldIrql
);
381 if (Adapter
->WorkQueueHead
)
383 KeInsertQueueDpc(&Adapter
->MiniportDpc
, NULL
, NULL
);
384 NDIS_DbgPrint(MAX_TRACE
, ("MiniportDpc queued; returning NDIS_STATUS_SUCCESS\n"));
387 KeReleaseSpinLock(&Adapter
->NdisMiniportBlock
.Lock
, SpinOldIrql
);
389 NDIS_DbgPrint(MAX_TRACE
, ("returning 0x%x\n", NdisStatus
));
396 IN NDIS_HANDLE NdisBindingHandle
,
397 IN PPNDIS_PACKET PacketArray
,
398 IN UINT NumberOfPackets
)
406 IN NDIS_HANDLE MacBindingHandle
,
407 IN NDIS_HANDLE MacReceiveContext
,
409 IN UINT BytesToTransfer
,
410 IN OUT PNDIS_PACKET Packet
,
411 OUT PUINT BytesTransferred
)
413 * FUNCTION: Forwards a request to copy received data into a protocol-supplied packet
415 * MacBindingHandle = Adapter binding handle
416 * MacReceiveContext = MAC receive context
417 * ByteOffset = Offset in packet to place data
418 * BytesToTransfer = Number of bytes to copy into packet
419 * Packet = Pointer to NDIS packet descriptor
420 * BytesTransferred = Address of buffer to place number of bytes copied
423 PADAPTER_BINDING AdapterBinding
= GET_ADAPTER_BINDING(MacBindingHandle
);
424 PLOGICAL_ADAPTER Adapter
= AdapterBinding
->Adapter
;
426 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
428 /* FIXME: Interrupts must be disabled for adapter */
429 /* XXX sd - why is that true? */
431 if (Packet
== Adapter
->LoopPacket
) {
432 NDIS_DbgPrint(MAX_TRACE
, ("LoopPacket\n"));
433 /* NDIS is responsible for looping this packet */
434 NdisCopyFromPacketToPacket(Packet
,
440 return NDIS_STATUS_SUCCESS
;
443 return (*Adapter
->Miniport
->Chars
.TransferDataHandler
)(
446 Adapter
->NdisMiniportBlock
.MiniportAdapterContext
,
460 OUT PNDIS_STATUS Status
,
461 IN NDIS_HANDLE NdisBindingHandle
)
463 * FUNCTION: Closes an adapter opened with NdisOpenAdapter
465 * Status = Address of buffer for status information
466 * NdisBindingHandle = Handle returned by NdisOpenAdapter
470 PADAPTER_BINDING AdapterBinding
= GET_ADAPTER_BINDING(NdisBindingHandle
);
472 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
474 /* Remove from protocol's bound adapters list */
475 KeAcquireSpinLock(&AdapterBinding
->ProtocolBinding
->Lock
, &OldIrql
);
476 RemoveEntryList(&AdapterBinding
->ProtocolListEntry
);
477 KeReleaseSpinLock(&AdapterBinding
->ProtocolBinding
->Lock
, OldIrql
);
479 /* Remove protocol from adapter's bound protocols list */
480 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
481 KeAcquireSpinLock(&AdapterBinding
->Adapter
->NdisMiniportBlock
.Lock
, &OldIrql
);
482 RemoveEntryList(&AdapterBinding
->AdapterListEntry
);
483 KeReleaseSpinLock(&AdapterBinding
->Adapter
->NdisMiniportBlock
.Lock
, OldIrql
);
485 ExFreePool(AdapterBinding
);
487 *Status
= NDIS_STATUS_SUCCESS
;
496 NdisDeregisterProtocol(
497 OUT PNDIS_STATUS Status
,
498 IN NDIS_HANDLE NdisProtocolHandle
)
500 * FUNCTION: Releases the resources allocated by NdisRegisterProtocol
502 * Status = Address of buffer for status information
503 * NdisProtocolHandle = Handle returned by NdisRegisterProtocol
507 PPROTOCOL_BINDING Protocol
= GET_PROTOCOL_BINDING(NdisProtocolHandle
);
509 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
511 /* FIXME: Make sure no adapter bindings exist */
513 /* Remove protocol from global list */
514 KeAcquireSpinLock(&ProtocolListLock
, &OldIrql
);
515 RemoveEntryList(&Protocol
->ListEntry
);
516 KeReleaseSpinLock(&ProtocolListLock
, OldIrql
);
518 ExFreePool(Protocol
);
520 *Status
= NDIS_STATUS_SUCCESS
;
530 OUT PNDIS_STATUS Status
,
531 OUT PNDIS_STATUS OpenErrorStatus
,
532 OUT PNDIS_HANDLE NdisBindingHandle
,
533 OUT PUINT SelectedMediumIndex
,
534 IN PNDIS_MEDIUM MediumArray
,
535 IN UINT MediumArraySize
,
536 IN NDIS_HANDLE NdisProtocolHandle
,
537 IN NDIS_HANDLE ProtocolBindingContext
,
538 IN PNDIS_STRING AdapterName
,
540 IN PSTRING AddressingInformation OPTIONAL
)
542 * FUNCTION: Opens an adapter for communication
544 * Status = Address of buffer for status information
545 * OpenErrorStatus = Address of buffer for secondary error code
546 * NdisBindingHandle = Address of buffer for adapter binding handle
547 * SelectedMediumIndex = Address of buffer for selected medium
548 * MediumArray = Pointer to an array of NDIS_MEDIUMs called can support
549 * MediumArraySize = Number of elements in MediumArray
550 * NdisProtocolHandle = Handle returned by NdisRegisterProtocol
551 * ProtocolBindingContext = Pointer to caller suplied context area
552 * AdapterName = Pointer to buffer with name of adapter
553 * OpenOptions = Bitmask with flags passed to next-lower driver
554 * AddressingInformation = Optional pointer to buffer with NIC specific information
559 PLOGICAL_ADAPTER Adapter
;
560 PADAPTER_BINDING AdapterBinding
;
561 PPROTOCOL_BINDING Protocol
= GET_PROTOCOL_BINDING(NdisProtocolHandle
);
563 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
565 if(!NdisProtocolHandle
)
567 NDIS_DbgPrint(MAX_TRACE
, ("NdisProtocolHandle is NULL\n"));
568 *OpenErrorStatus
= *Status
= NDIS_STATUS_FAILURE
;
572 Adapter
= MiniLocateDevice(AdapterName
);
575 NDIS_DbgPrint(MIN_TRACE
, ("Adapter not found.\n"));
576 *Status
= NDIS_STATUS_ADAPTER_NOT_FOUND
;
580 /* Find the media type in the list provided by the protocol driver */
582 for (i
= 0; i
< MediumArraySize
; i
++)
584 if (Adapter
->NdisMiniportBlock
.MediaType
== MediumArray
[i
])
586 *SelectedMediumIndex
= i
;
594 NDIS_DbgPrint(MIN_TRACE
, ("Medium is not supported.\n"));
595 *Status
= NDIS_STATUS_UNSUPPORTED_MEDIA
;
599 /* Now that we have confirmed that the adapter can be opened, create a binding */
601 AdapterBinding
= ExAllocatePool(NonPagedPool
, sizeof(ADAPTER_BINDING
));
604 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
605 *Status
= NDIS_STATUS_RESOURCES
;
609 RtlZeroMemory(AdapterBinding
, sizeof(ADAPTER_BINDING
));
611 AdapterBinding
->ProtocolBinding
= Protocol
;
612 AdapterBinding
->Adapter
= Adapter
;
613 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.ProtocolBindingContext
= ProtocolBindingContext
;
615 /* Set fields required by some NDIS macros */
616 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.BindingHandle
= (NDIS_HANDLE
)AdapterBinding
;
618 /* Set handlers (some NDIS macros require these) */
620 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.RequestHandler
= ProRequest
;
621 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.ResetHandler
= ProReset
;
622 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.SendHandler
= ProSend
;
623 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.SendPacketsHandler
= ProSendPackets
;
624 AdapterBinding
->NdisOpenBlock
.NdisCommonOpenBlock
.TransferDataHandler
= ProTransferData
;
627 /* XXX this looks fishy */
628 /* OK, this really *is* fishy - it bugchecks */
629 /* Put on protocol's bound adapters list */
630 ExInterlockedInsertTailList(&Protocol
->AdapterListHead
, &AdapterBinding
->ProtocolListEntry
, &Protocol
->Lock
);
633 /* XXX so does this */
634 /* Put protocol on adapter's bound protocols list */
635 NDIS_DbgPrint(MAX_TRACE
, ("acquiring miniport block lock\n"));
636 ExInterlockedInsertTailList(&Adapter
->ProtocolListHead
, &AdapterBinding
->AdapterListEntry
, &Adapter
->NdisMiniportBlock
.Lock
);
638 *NdisBindingHandle
= (NDIS_HANDLE
)AdapterBinding
;
640 *Status
= NDIS_STATUS_SUCCESS
;
649 NdisRegisterProtocol(
650 OUT PNDIS_STATUS Status
,
651 OUT PNDIS_HANDLE NdisProtocolHandle
,
652 IN PNDIS_PROTOCOL_CHARACTERISTICS ProtocolCharacteristics
,
653 IN UINT CharacteristicsLength
)
655 * FUNCTION: Registers an NDIS driver's ProtocolXxx entry points
657 * Status = Address of buffer for status information
658 * NdisProtocolHandle = Address of buffer for handle used to identify the driver
659 * ProtocolCharacteristics = Pointer to NDIS_PROTOCOL_CHARACTERISTICS structure
660 * CharacteristicsLength = Size of structure which ProtocolCharacteristics targets
662 * - you *must* set NdisProtocolHandle before doing anything that could wind up
663 * getting BindAdapterHandler, as it will probably call OpenAdapter with this handle
664 * - the above implies that the initialization of the protocol block must be complete
667 * - break this function up - probably do a 'ndisRefreshProtocolBindings' function
668 * - make this thing able to handle >1 protocol
671 PPROTOCOL_BINDING Protocol
;
674 HANDLE DriverKeyHandle
= NULL
;
675 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation
= NULL
;
678 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
680 /* first validate the PROTOCOL_CHARACTERISTICS */
681 switch (ProtocolCharacteristics
->MajorNdisVersion
)
684 /* we don't really want to support ndis3 drivers - so we complain for now */
685 NDIS_DbgPrint(MID_TRACE
, ("NDIS 3 protocol attempting to register\n"));
686 MinSize
= sizeof(NDIS30_PROTOCOL_CHARACTERISTICS
);
690 MinSize
= sizeof(NDIS40_PROTOCOL_CHARACTERISTICS
);
694 MinSize
= sizeof(NDIS50_PROTOCOL_CHARACTERISTICS
);
698 *Status
= NDIS_STATUS_BAD_VERSION
;
699 NDIS_DbgPrint(MIN_TRACE
, ("Incorrect characteristics size\n"));
703 if (CharacteristicsLength
< MinSize
)
705 NDIS_DbgPrint(DEBUG_PROTOCOL
, ("Bad protocol characteristics.\n"));
706 *Status
= NDIS_STATUS_BAD_CHARACTERISTICS
;
710 /* set up the protocol block */
711 Protocol
= ExAllocatePool(NonPagedPool
, sizeof(PROTOCOL_BINDING
));
714 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
715 *Status
= NDIS_STATUS_RESOURCES
;
719 RtlZeroMemory(Protocol
, sizeof(PROTOCOL_BINDING
));
720 RtlCopyMemory(&Protocol
->Chars
, ProtocolCharacteristics
, MinSize
);
722 NtStatus
= RtlUpcaseUnicodeString(&Protocol
->Chars
.Name
, &ProtocolCharacteristics
->Name
, TRUE
);
723 if (!NT_SUCCESS(NtStatus
))
725 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
726 ExFreePool(Protocol
);
727 *Status
= NDIS_STATUS_RESOURCES
;
731 KeInitializeSpinLock(&Protocol
->Lock
);
733 Protocol
->RefCount
= 1;
735 InitializeListHead(&Protocol
->AdapterListHead
);
738 * bind the protocol to all of its miniports
741 * get list of devices from Bind key
742 * call BindAdapterHandler for each
745 OBJECT_ATTRIBUTES ObjectAttributes
;
746 UNICODE_STRING RegistryPath
;
747 WCHAR
*RegistryPathStr
;
749 RegistryPathStr
= ExAllocatePoolWithTag(PagedPool
, sizeof(SERVICES_KEY
) + ProtocolCharacteristics
->Name
.Length
+ sizeof(LINKAGE_KEY
), NDIS_TAG
+ __LINE__
);
752 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
753 ExFreePool(Protocol
);
754 *Status
= NDIS_STATUS_RESOURCES
;
758 wcscpy(RegistryPathStr
, SERVICES_KEY
);
759 wcsncat(RegistryPathStr
, ((WCHAR
*)ProtocolCharacteristics
->Name
.Buffer
), ProtocolCharacteristics
->Name
.Length
/ sizeof(WCHAR
));
760 RegistryPathStr
[wcslen(SERVICES_KEY
)+ProtocolCharacteristics
->Name
.Length
/sizeof(WCHAR
)] = 0;
761 wcscat(RegistryPathStr
, LINKAGE_KEY
);
763 RtlInitUnicodeString(&RegistryPath
, RegistryPathStr
);
764 NDIS_DbgPrint(MAX_TRACE
, ("Opening configuration key: %wZ\n", &RegistryPath
));
766 InitializeObjectAttributes(&ObjectAttributes
, &RegistryPath
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
767 NtStatus
= ZwOpenKey(&DriverKeyHandle
, KEY_READ
, &ObjectAttributes
);
769 ExFreePool(RegistryPathStr
);
771 if(!NT_SUCCESS(NtStatus
))
773 NDIS_DbgPrint(MID_TRACE
, ("Unable to open protocol configuration\n"));
774 ExFreePool(Protocol
);
775 *Status
= NDIS_STATUS_FAILURE
;
780 NDIS_DbgPrint(MAX_TRACE
, ("Successfully opened the registry configuration\n"));
783 UNICODE_STRING ValueName
;
786 RtlInitUnicodeString(&ValueName
, L
"Bind");
788 NtStatus
= ZwQueryValueKey(DriverKeyHandle
, &ValueName
, KeyValuePartialInformation
, NULL
, 0, &ResultLength
);
789 if(NtStatus
!= STATUS_BUFFER_OVERFLOW
&& NtStatus
!= STATUS_BUFFER_TOO_SMALL
)
791 NDIS_DbgPrint(MID_TRACE
, ("Unable to query the Bind value for size\n"));
792 ZwClose(DriverKeyHandle
);
793 ExFreePool(Protocol
);
794 *Status
= NDIS_STATUS_FAILURE
;
798 KeyInformation
= ExAllocatePoolWithTag(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + ResultLength
, NDIS_TAG
+ __LINE__
);
801 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
802 ZwClose(DriverKeyHandle
);
803 ExFreePool(Protocol
);
804 *Status
= NDIS_STATUS_FAILURE
;
808 NtStatus
= ZwQueryValueKey(DriverKeyHandle
, &ValueName
, KeyValuePartialInformation
, KeyInformation
,
809 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + ResultLength
, &ResultLength
);
811 if(!NT_SUCCESS(NtStatus
))
813 NDIS_DbgPrint(MIN_TRACE
, ("Unable to query the Bind value\n"));
814 ZwClose(DriverKeyHandle
);
815 ExFreePool(KeyInformation
);
816 ExFreePool(Protocol
);
817 *Status
= NDIS_STATUS_FAILURE
;
822 for (DataPtr
= (WCHAR
*)KeyInformation
->Data
;
824 DataPtr
+= wcslen(DataPtr
) + 1)
826 /* BindContext is for tracking pending binding operations */
827 VOID
*BindContext
= 0;
828 NDIS_STRING DeviceName
;
829 NDIS_STRING RegistryPath
;
830 WCHAR
*RegistryPathStr
= NULL
;
831 ULONG PathLength
= 0;
833 RtlInitUnicodeString(&DeviceName
, DataPtr
); /* we know this is 0-term */
836 * RegistryPath should be:
837 * \Registry\Machine\System\CurrentControlSet\Services\Nic1\Parameters\Tcpip
839 * This is constructed as follows:
840 * SERVICES_KEY + extracted device name + Protocol name from characteristics
843 PathLength
= sizeof(SERVICES_KEY
) + /* \Registry\Machine\System\CurrentControlSet\Services\ */
844 wcslen( DataPtr
+ 8 ) * sizeof(WCHAR
) + /* Adapter1 (extracted from \Device\Adapter1) */
845 sizeof(PARAMETERS_KEY
) + /* \Parameters\ */
846 ProtocolCharacteristics
->Name
.Length
+ sizeof(WCHAR
); /* Tcpip */
848 RegistryPathStr
= ExAllocatePool(PagedPool
, PathLength
);
851 NDIS_DbgPrint(MIN_TRACE
, ("insufficient resources.\n"));
852 ExFreePool(KeyInformation
);
853 ExFreePool(Protocol
);
854 *Status
= NDIS_STATUS_RESOURCES
;
858 wcscpy(RegistryPathStr
, SERVICES_KEY
);
859 wcscat(RegistryPathStr
, (((WCHAR
*)(KeyInformation
->Data
)) +8 ));
860 wcscat(RegistryPathStr
, PARAMETERS_KEY
);
861 wcsncat(RegistryPathStr
, ProtocolCharacteristics
->Name
.Buffer
, ProtocolCharacteristics
->Name
.Length
/ sizeof(WCHAR
) );
863 RegistryPathStr
[PathLength
/sizeof(WCHAR
) - 1] = 0;
865 RtlInitUnicodeString(&RegistryPath
, RegistryPathStr
);
867 NDIS_DbgPrint(MAX_TRACE
, ("Calling protocol's BindAdapter handler with DeviceName %wZ and RegistryPath %wZ\n",
868 &DeviceName
, &RegistryPath
));
870 /* XXX SD must do something with bind context */
871 *NdisProtocolHandle
= Protocol
;
874 BIND_HANDLER BindHandler
= ProtocolCharacteristics
->BindAdapterHandler
;
876 BindHandler(Status
, BindContext
, &DeviceName
, &RegistryPath
, 0);
878 NDIS_DbgPrint(MID_TRACE
, ("No protocol bind handler specified\n"));
882 (*(Protocol->Chars.BindAdapterHandler))(Status, BindContext, &DeviceName, &RegistryPath, 0);
885 if(*Status
== NDIS_STATUS_SUCCESS
)
887 /* Put protocol binding struct on global list */
888 ExInterlockedInsertTailList(&ProtocolListHead
, &Protocol
->ListEntry
, &ProtocolListLock
);
892 else if(*Status != NDIS_STATUS_PENDING)
899 *Status
= NDIS_STATUS_SUCCESS
;
909 OUT PNDIS_STATUS Status
,
910 IN NDIS_HANDLE NdisBindingHandle
,
911 IN PNDIS_REQUEST NdisRequest
)
913 * FUNCTION: Forwards a request to an NDIS driver
915 * Status = Address of buffer for status information
916 * NdisBindingHandle = Adapter binding handle
917 * NdisRequest = Pointer to request to perform
920 *Status
= ProRequest(NdisBindingHandle
, NdisRequest
);
930 OUT PNDIS_STATUS Status
,
931 IN NDIS_HANDLE NdisBindingHandle
)
933 *Status
= ProReset(NdisBindingHandle
);
943 OUT PNDIS_STATUS Status
,
944 IN NDIS_HANDLE NdisBindingHandle
,
945 IN PNDIS_PACKET Packet
)
947 * FUNCTION: Forwards a request to send a packet
949 * Status = Address of buffer for status information
950 * NdisBindingHandle = Adapter binding handle
951 * Packet = Pointer to NDIS packet descriptor
954 *Status
= ProSend(NdisBindingHandle
, Packet
);
964 IN NDIS_HANDLE NdisBindingHandle
,
965 IN PPNDIS_PACKET PacketArray
,
966 IN UINT NumberOfPackets
)
968 ProSendPackets(NdisBindingHandle
, PacketArray
, NumberOfPackets
);
978 OUT PNDIS_STATUS Status
,
979 IN NDIS_HANDLE NdisBindingHandle
,
980 IN NDIS_HANDLE MacReceiveContext
,
982 IN UINT BytesToTransfer
,
983 IN OUT PNDIS_PACKET Packet
,
984 OUT PUINT BytesTransferred
)
986 * FUNCTION: Forwards a request to copy received data into a protocol-supplied packet
988 * Status = Address of buffer for status information
989 * NdisBindingHandle = Adapter binding handle
990 * MacReceiveContext = MAC receive context
991 * ByteOffset = Offset in packet to place data
992 * BytesToTransfer = Number of bytes to copy into packet
993 * Packet = Pointer to NDIS packet descriptor
994 * BytesTransferred = Address of buffer to place number of bytes copied
997 *Status
= ProTransferData(NdisBindingHandle
,