2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
5 * PURPOSE: Local Area Network media routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/08-2000 Created
13 UINT TransferDataCalled
= 0;
14 UINT TransferDataCompleteCalled
= 0;
16 #define CCS_ROOT L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet"
17 #define TCPIP_GUID L"{4D36E972-E325-11CE-BFC1-08002BE10318}"
19 typedef struct _LAN_WQ_ITEM
{
23 UINT BytesTransferred
;
24 BOOLEAN LegacyReceive
;
25 } LAN_WQ_ITEM
, *PLAN_WQ_ITEM
;
27 NDIS_HANDLE NdisProtocolHandle
= (NDIS_HANDLE
)NULL
;
28 BOOLEAN ProtocolRegistered
= FALSE
;
29 LIST_ENTRY AdapterListHead
;
30 KSPIN_LOCK AdapterListLock
;
34 NDIS_REQUEST_TYPE Type
,
39 * FUNCTION: Send a request to NDIS
41 * Adapter = Pointer to a LAN_ADAPTER structure
42 * Type = Type of request (Set or Query)
43 * OID = Value to be set/queried for
44 * Buffer = Pointer to a buffer to use
45 * Length = Number of bytes in Buffer
51 NDIS_STATUS NdisStatus
;
53 Request
.RequestType
= Type
;
54 if (Type
== NdisRequestSetInformation
) {
55 Request
.DATA
.SET_INFORMATION
.Oid
= OID
;
56 Request
.DATA
.SET_INFORMATION
.InformationBuffer
= Buffer
;
57 Request
.DATA
.SET_INFORMATION
.InformationBufferLength
= Length
;
59 Request
.DATA
.QUERY_INFORMATION
.Oid
= OID
;
60 Request
.DATA
.QUERY_INFORMATION
.InformationBuffer
= Buffer
;
61 Request
.DATA
.QUERY_INFORMATION
.InformationBufferLength
= Length
;
64 if (Adapter
->State
!= LAN_STATE_RESETTING
) {
65 NdisRequest(&NdisStatus
, Adapter
->NdisHandle
, &Request
);
67 NdisStatus
= NDIS_STATUS_NOT_ACCEPTED
;
70 /* Wait for NDIS to complete the request */
71 if (NdisStatus
== NDIS_STATUS_PENDING
) {
72 KeWaitForSingleObject(&Adapter
->Event
,
77 NdisStatus
= Adapter
->NdisStatus
;
83 /* Used by legacy ProtocolReceive for packet type */
85 GetPacketTypeFromHeaderBuffer(PLAN_ADAPTER Adapter
,
87 ULONG HeaderBufferSize
,
90 PETH_HEADER EthHeader
= HeaderBuffer
;
92 if (HeaderBufferSize
< Adapter
->HeaderSize
)
94 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", HeaderBufferSize
));
95 return NDIS_STATUS_NOT_ACCEPTED
;
98 switch (Adapter
->Media
)
100 case NdisMedium802_3
:
101 /* Ethernet and IEEE 802.3 frames can be destinguished by
102 looking at the IEEE 802.3 length field. This field is
103 less than or equal to 1500 for a valid IEEE 802.3 frame
104 and larger than 1500 is it's a valid EtherType value.
105 See RFC 1122, section 2.3.3 for more information */
107 *PacketType
= EthHeader
->EType
;
111 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
113 /* FIXME: Support other medias */
114 return NDIS_STATUS_NOT_ACCEPTED
;
117 TI_DbgPrint(DEBUG_DATALINK
, ("EtherType (0x%X).\n", *PacketType
));
119 return NDIS_STATUS_SUCCESS
;
122 /* Used by ProtocolReceivePacket for packet type */
124 GetPacketTypeFromNdisPacket(PLAN_ADAPTER Adapter
,
125 PNDIS_PACKET NdisPacket
,
132 HeaderBuffer
= ExAllocatePool(NonPagedPool
,
133 Adapter
->HeaderSize
);
135 return NDIS_STATUS_RESOURCES
;
137 /* Copy the media header */
138 BytesCopied
= CopyPacketToBuffer(HeaderBuffer
,
141 Adapter
->HeaderSize
);
142 if (BytesCopied
!= Adapter
->HeaderSize
)
145 ExFreePool(HeaderBuffer
);
146 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", BytesCopied
));
147 return NDIS_STATUS_NOT_ACCEPTED
;
150 Status
= GetPacketTypeFromHeaderBuffer(Adapter
,
155 ExFreePool(HeaderBuffer
);
162 PLAN_ADAPTER Adapter
)
164 * FUNCTION: Frees memory for a LAN_ADAPTER structure
166 * Adapter = Pointer to LAN_ADAPTER structure to free
169 ExFreePoolWithTag(Adapter
, LAN_ADAPTER_TAG
);
173 NTSTATUS TcpipLanGetDwordOid
174 ( PIP_INTERFACE Interface
,
177 /* Get maximum frame size */
178 if( Interface
->Context
) {
179 return NDISCall((PLAN_ADAPTER
)Interface
->Context
,
180 NdisRequestQueryInformation
,
184 } else switch( Oid
) { /* Loopback Case */
185 case OID_GEN_HARDWARE_STATUS
:
186 *Result
= NdisHardwareStatusReady
;
187 return STATUS_SUCCESS
;
190 return STATUS_INVALID_PARAMETER
;
195 VOID NTAPI
ProtocolOpenAdapterComplete(
196 NDIS_HANDLE BindingContext
,
198 NDIS_STATUS OpenErrorStatus
)
200 * FUNCTION: Called by NDIS to complete opening of an adapter
202 * BindingContext = Pointer to a device context (LAN_ADAPTER)
203 * Status = Status of the operation
204 * OpenErrorStatus = Additional status information
207 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
209 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
211 Adapter
->NdisStatus
= Status
;
213 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
217 VOID NTAPI
ProtocolCloseAdapterComplete(
218 NDIS_HANDLE BindingContext
,
221 * FUNCTION: Called by NDIS to complete closing an adapter
223 * BindingContext = Pointer to a device context (LAN_ADAPTER)
224 * Status = Status of the operation
227 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
229 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
231 Adapter
->NdisStatus
= Status
;
233 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
237 VOID NTAPI
ProtocolResetComplete(
238 NDIS_HANDLE BindingContext
,
241 * FUNCTION: Called by NDIS to complete resetting an adapter
243 * BindingContext = Pointer to a device context (LAN_ADAPTER)
244 * Status = Status of the operation
247 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
249 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
251 Adapter
->NdisStatus
= Status
;
253 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
257 VOID NTAPI
ProtocolRequestComplete(
258 NDIS_HANDLE BindingContext
,
259 PNDIS_REQUEST NdisRequest
,
262 * FUNCTION: Called by NDIS to complete a request
264 * BindingContext = Pointer to a device context (LAN_ADAPTER)
265 * NdisRequest = Pointer to an object describing the request
266 * Status = Status of the operation
269 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
271 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
273 /* Save status of request and signal an event */
274 Adapter
->NdisStatus
= Status
;
276 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
280 VOID NTAPI
ProtocolSendComplete(
281 NDIS_HANDLE BindingContext
,
285 * FUNCTION: Called by NDIS to complete sending process
287 * BindingContext = Pointer to a device context (LAN_ADAPTER)
288 * Packet = Pointer to a packet descriptor
289 * Status = Status of the operation
292 FreeNdisPacket(Packet
);
295 VOID
LanReceiveWorker( PVOID Context
) {
297 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
299 PLAN_ADAPTER Adapter
;
300 UINT BytesTransferred
;
302 BOOLEAN LegacyReceive
;
303 PIP_INTERFACE Interface
;
305 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
307 Packet
= WorkItem
->Packet
;
308 Adapter
= WorkItem
->Adapter
;
309 BytesTransferred
= WorkItem
->BytesTransferred
;
310 LegacyReceive
= WorkItem
->LegacyReceive
;
312 ExFreePoolWithTag(WorkItem
, WQ_CONTEXT_TAG
);
314 Interface
= Adapter
->Context
;
316 IPInitializePacket(&IPPacket
, 0);
318 IPPacket
.NdisPacket
= Packet
;
319 IPPacket
.ReturnPacket
= !LegacyReceive
;
323 /* Packet type is precomputed */
324 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
326 /* Data is at position 0 */
327 IPPacket
.Position
= 0;
329 /* Packet size is determined by bytes transferred */
330 IPPacket
.TotalSize
= BytesTransferred
;
334 /* Determine packet type from media header */
335 if (GetPacketTypeFromNdisPacket(Adapter
,
337 &PacketType
) != NDIS_STATUS_SUCCESS
)
340 IPPacket
.Free(&IPPacket
);
344 /* Data is at the end of the media header */
345 IPPacket
.Position
= Adapter
->HeaderSize
;
347 /* Calculate packet size (excluding media header) */
348 NdisQueryPacketLength(IPPacket
.NdisPacket
, &IPPacket
.TotalSize
);
353 ("Ether Type = %x Total = %d\n",
354 PacketType
, IPPacket
.TotalSize
));
356 Interface
->Stats
.InBytes
+= IPPacket
.TotalSize
;
358 /* NDIS packet is freed in all of these cases */
359 switch (PacketType
) {
362 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
363 IPReceive(Adapter
->Context
, &IPPacket
);
366 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
367 ARPReceive(Adapter
->Context
, &IPPacket
);
370 IPPacket
.Free(&IPPacket
);
375 VOID
LanSubmitReceiveWork(
376 NDIS_HANDLE BindingContext
,
378 UINT BytesTransferred
,
379 BOOLEAN LegacyReceive
) {
380 PLAN_WQ_ITEM WQItem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_WQ_ITEM
),
382 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
384 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
388 WQItem
->Packet
= Packet
;
389 WQItem
->Adapter
= Adapter
;
390 WQItem
->BytesTransferred
= BytesTransferred
;
391 WQItem
->LegacyReceive
= LegacyReceive
;
393 if (!ChewCreate( LanReceiveWorker
, WQItem
))
394 ExFreePoolWithTag(WQItem
, WQ_CONTEXT_TAG
);
397 VOID NTAPI
ProtocolTransferDataComplete(
398 NDIS_HANDLE BindingContext
,
401 UINT BytesTransferred
)
403 * FUNCTION: Called by NDIS to complete reception of data
405 * BindingContext = Pointer to a device context (LAN_ADAPTER)
406 * Packet = Pointer to a packet descriptor
407 * Status = Status of the operation
408 * BytesTransferred = Number of bytes transferred
410 * If the packet was successfully received, determine the protocol
411 * type and pass it to the correct receive handler
414 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
416 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
418 TransferDataCompleteCalled
++;
419 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
421 if( Status
!= NDIS_STATUS_SUCCESS
) return;
423 LanSubmitReceiveWork(BindingContext
,
429 INT NTAPI
ProtocolReceivePacket(
430 NDIS_HANDLE BindingContext
,
431 PNDIS_PACKET NdisPacket
)
433 PLAN_ADAPTER Adapter
= BindingContext
;
435 if (Adapter
->State
!= LAN_STATE_STARTED
) {
436 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
440 LanSubmitReceiveWork(BindingContext
,
445 /* Hold 1 reference on this packet */
449 NDIS_STATUS NTAPI
ProtocolReceive(
450 NDIS_HANDLE BindingContext
,
451 NDIS_HANDLE MacReceiveContext
,
453 UINT HeaderBufferSize
,
454 PVOID LookaheadBuffer
,
455 UINT LookaheadBufferSize
,
458 * FUNCTION: Called by NDIS when a packet has been received on the physical link
460 * BindingContext = Pointer to a device context (LAN_ADAPTER)
461 * MacReceiveContext = Handle used by underlying NIC driver
462 * HeaderBuffer = Pointer to a buffer containing the packet header
463 * HeaderBufferSize = Number of bytes in HeaderBuffer
464 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
465 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
466 * PacketSize = Overall size of the packet (not including header)
468 * Status of operation
472 UINT BytesTransferred
;
474 NDIS_STATUS NdisStatus
;
475 PNDIS_PACKET NdisPacket
;
476 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
478 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
480 if (Adapter
->State
!= LAN_STATE_STARTED
) {
481 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
482 return NDIS_STATUS_NOT_ACCEPTED
;
485 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
486 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
487 return NDIS_STATUS_NOT_ACCEPTED
;
490 NdisStatus
= GetPacketTypeFromHeaderBuffer(Adapter
,
494 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
495 return NDIS_STATUS_NOT_ACCEPTED
;
497 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
498 Adapter
, Adapter
->MTU
));
500 /* Get a transfer data packet */
501 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
503 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
504 return NDIS_STATUS_NOT_ACCEPTED
;
507 PC(NdisPacket
)->PacketType
= PacketType
;
509 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
511 GetDataPtr( NdisPacket
, 0, &BufferData
, &PacketSize
);
513 TransferDataCalled
++;
515 if (LookaheadBufferSize
== PacketSize
)
517 /* Optimized code path for packets that are fully contained in
518 * the lookahead buffer. */
519 NdisCopyLookaheadData(BufferData
,
522 Adapter
->MacOptions
);
526 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
527 MacReceiveContext
, 0, PacketSize
,
528 NdisPacket
, &BytesTransferred
);
530 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
532 if (NdisStatus
!= NDIS_STATUS_PENDING
)
533 ProtocolTransferDataComplete(BindingContext
,
538 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
540 return NDIS_STATUS_SUCCESS
;
544 VOID NTAPI
ProtocolReceiveComplete(
545 NDIS_HANDLE BindingContext
)
547 * FUNCTION: Called by NDIS when we're done receiving data
549 * BindingContext = Pointer to a device context (LAN_ADAPTER)
552 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
556 VOID NTAPI
ProtocolStatus(
557 NDIS_HANDLE BindingContext
,
558 NDIS_STATUS GeneralStatus
,
560 UINT StatusBufferSize
)
562 * FUNCTION: Called by NDIS when the underlying driver has changed state
564 * BindingContext = Pointer to a device context (LAN_ADAPTER)
565 * GeneralStatus = A general status code
566 * StatusBuffer = Pointer to a buffer with medium-specific data
567 * StatusBufferSize = Number of bytes in StatusBuffer
570 PLAN_ADAPTER Adapter
= BindingContext
;
572 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
574 switch(GeneralStatus
)
576 case NDIS_STATUS_MEDIA_CONNECT
:
577 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
580 case NDIS_STATUS_MEDIA_DISCONNECT
:
581 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
584 case NDIS_STATUS_RESET_START
:
585 Adapter
->State
= LAN_STATE_RESETTING
;
588 case NDIS_STATUS_RESET_END
:
589 Adapter
->State
= LAN_STATE_STARTED
;
593 DbgPrint("Unhandled status: %x", GeneralStatus
);
600 NDIS_HANDLE NdisBindingContext
,
601 PNET_PNP_EVENT PnPEvent
)
603 switch(PnPEvent
->NetEvent
)
605 case NetEventSetPower
:
606 DbgPrint("Device transitioned to power state %ld\n", PnPEvent
->Buffer
);
607 return NDIS_STATUS_SUCCESS
;
609 case NetEventQueryPower
:
610 DbgPrint("Device wants to go into power state %ld\n", PnPEvent
->Buffer
);
611 return NDIS_STATUS_SUCCESS
;
613 case NetEventQueryRemoveDevice
:
614 DbgPrint("Device is about to be removed\n");
615 return NDIS_STATUS_SUCCESS
;
617 case NetEventCancelRemoveDevice
:
618 DbgPrint("Device removal cancelled\n");
619 return NDIS_STATUS_SUCCESS
;
622 DbgPrint("Unhandled event type: %ld\n", PnPEvent
->NetEvent
);
623 return NDIS_STATUS_SUCCESS
;
627 VOID NTAPI
ProtocolStatusComplete(
628 NDIS_HANDLE NdisBindingContext
)
630 * FUNCTION: Called by NDIS when a status-change has occurred
632 * BindingContext = Pointer to a device context (LAN_ADAPTER)
635 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
638 VOID NTAPI
ProtocolBindAdapter(
639 OUT PNDIS_STATUS Status
,
640 IN NDIS_HANDLE BindContext
,
641 IN PNDIS_STRING DeviceName
,
642 IN PVOID SystemSpecific1
,
643 IN PVOID SystemSpecific2
)
645 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
646 * bindings, and periodically thereafer as new adapters come online
648 * Status: Return value to NDIS
649 * BindContext: Handle provided by NDIS to track pending binding operations
650 * DeviceName: Name of the miniport device to bind to
651 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
652 * SystemSpecific2: Unused & must not be touched
655 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
656 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
662 PNDIS_PACKET NdisPacket
,
667 * FUNCTION: Transmits a packet
669 * Context = Pointer to context information (LAN_ADAPTER)
670 * NdisPacket = Pointer to NDIS packet to send
671 * Offset = Offset in packet where data starts
672 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
673 * Type = LAN protocol type (LAN_PROTO_*)
676 NDIS_STATUS NdisStatus
;
680 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
682 PNDIS_PACKET XmitPacket
;
684 TI_DbgPrint(DEBUG_DATALINK
,
685 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
686 NdisPacket
, Offset
, Adapter
));
688 if (Adapter
->State
!= LAN_STATE_STARTED
) {
689 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
693 TI_DbgPrint(DEBUG_DATALINK
,
694 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
695 Adapter
->HWAddress
[0] & 0xff,
696 Adapter
->HWAddress
[1] & 0xff,
697 Adapter
->HWAddress
[2] & 0xff,
698 Adapter
->HWAddress
[3] & 0xff,
699 Adapter
->HWAddress
[4] & 0xff,
700 Adapter
->HWAddress
[5] & 0xff));
702 GetDataPtr( NdisPacket
, 0, &OldData
, &OldSize
);
704 NdisStatus
= AllocatePacketWithBuffer(&XmitPacket
, NULL
, OldSize
+ Adapter
->HeaderSize
);
705 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
706 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_RESOURCES
);
710 GetDataPtr(XmitPacket
, 0, &Data
, &Size
);
712 RtlCopyMemory(Data
+ Adapter
->HeaderSize
, OldData
, OldSize
);
714 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_SUCCESS
);
716 switch (Adapter
->Media
) {
717 case NdisMedium802_3
:
718 EHeader
= (PETH_HEADER
)Data
;
721 /* Unicast address */
722 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
724 /* Broadcast address */
725 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
728 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
732 EHeader
->EType
= ETYPE_IPv4
;
735 EHeader
->EType
= ETYPE_ARP
;
738 EHeader
->EType
= ETYPE_IPv6
;
747 /* FIXME: Support other medias */
751 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
755 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
756 ((PCHAR
)LinkAddress
)[0] & 0xff,
757 ((PCHAR
)LinkAddress
)[1] & 0xff,
758 ((PCHAR
)LinkAddress
)[2] & 0xff,
759 ((PCHAR
)LinkAddress
)[3] & 0xff,
760 ((PCHAR
)LinkAddress
)[4] & 0xff,
761 ((PCHAR
)LinkAddress
)[5] & 0xff));
764 if (Adapter
->MTU
< Size
) {
765 /* This is NOT a pointer. MSDN explicitly says so. */
766 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket
,
767 TcpLargeSendPacketInfo
) = (PVOID
)((ULONG_PTR
)Adapter
->MTU
);
770 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
771 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
772 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, XmitPacket
);
773 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
774 NdisStatus
== NDIS_STATUS_PENDING
?
775 "Pending" : "Complete"));
776 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
778 /* I had a talk with vizzini: these really ought to be here.
779 * we're supposed to see these completed by ndis *only* when
780 * status_pending is returned. Note that this is different from
781 * the situation with IRPs. */
782 if (NdisStatus
!= NDIS_STATUS_PENDING
)
783 ProtocolSendComplete((NDIS_HANDLE
)Context
, XmitPacket
, NdisStatus
);
787 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
788 OBJECT_ATTRIBUTES Attributes
;
791 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, 0, 0);
792 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
796 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
797 PWCHAR RegistryValue
,
798 PUNICODE_STRING String
) {
799 UNICODE_STRING ValueName
;
800 UNICODE_STRING UnicodeString
;
804 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
806 RtlInitUnicodeString(&ValueName
, RegistryValue
);
808 ZwQueryValueKey(RegHandle
,
810 KeyValuePartialInformation
,
815 if (!NT_SUCCESS(Status
))
817 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
818 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
820 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
821 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
822 UnicodeString
.MaximumLength
= Information
->DataLength
;
825 (PWCHAR
)ExAllocatePool( NonPagedPool
,
826 UnicodeString
.MaximumLength
+ sizeof(WCHAR
) );
828 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
830 String
->MaximumLength
= UnicodeString
.MaximumLength
;
831 RtlCopyUnicodeString( String
, &UnicodeString
);
833 return STATUS_SUCCESS
;
837 * Utility to copy and append two unicode strings.
839 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
840 * IN PUNICODE_STRING Second -> Second string to append
841 * IN BOOL Deallocate -> TRUE: Deallocate First string before
847 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
848 PUNICODE_STRING Second
,
849 BOOLEAN Deallocate
) {
851 UNICODE_STRING Ustr
= *ResultFirst
;
852 PWSTR new_string
= ExAllocatePool
854 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)));
856 return STATUS_NO_MEMORY
;
858 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
859 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
860 Second
->Buffer
, Second
->Length
);
861 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
862 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
863 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
864 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
865 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
866 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
867 ExFreePool(new_string
);
871 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
872 PUNICODE_STRING TargetKeyName
,
873 PUNICODE_STRING Name
,
874 PUNICODE_STRING DeviceDesc
) {
875 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
876 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
877 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
878 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
881 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
883 RtlInitUnicodeString(&BackSlash
, L
"\\");
884 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
886 RtlInitUnicodeString(&DescKeyName
, L
"");
887 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
888 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
889 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
891 RtlInitUnicodeString(&LinkageKeyName
, L
"");
892 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
893 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
895 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
896 if( !NT_SUCCESS(Status
) ) goto cleanup
;
898 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
899 if( !NT_SUCCESS(Status
) ) goto cleanup
;
901 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
902 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
903 if( !NT_SUCCESS(Status
) ) goto cleanup
;
905 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
906 if( !NT_SUCCESS(Status
) ) goto cleanup
;
908 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
909 } else Status
= STATUS_UNSUCCESSFUL
;
912 RtlFreeUnicodeString( &RootDevice
);
913 RtlFreeUnicodeString( &LinkageKeyName
);
914 RtlFreeUnicodeString( &DescKeyName
);
915 if( LinkageKey
) NtClose( LinkageKey
);
916 if( DescKey
) NtClose( DescKey
);
918 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
923 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
924 PUNICODE_STRING DeviceDesc
) {
925 UNICODE_STRING EnumKeyName
, TargetKeyName
;
929 KEY_BASIC_INFORMATION
*Kbio
=
930 ExAllocatePool(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
));
931 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
933 RtlInitUnicodeString( DeviceDesc
, NULL
);
935 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
938 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
940 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
942 if( !NT_SUCCESS(Status
) ) {
943 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
944 &EnumKeyName
, Status
));
949 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
950 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
951 Kbio
, KbioLength
, &ResultLength
);
953 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
955 KbioLength
= ResultLength
;
956 Kbio
= ExAllocatePool( NonPagedPool
, KbioLength
);
958 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
960 return STATUS_NO_MEMORY
;
963 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
964 Kbio
, KbioLength
, &ResultLength
);
966 if( !NT_SUCCESS(Status
) ) {
967 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
974 if( NT_SUCCESS(Status
) ) {
975 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
977 TargetKeyName
.Buffer
= Kbio
->Name
;
979 Status
= CheckForDeviceDesc
980 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
981 if( NT_SUCCESS(Status
) ) {
985 } else Status
= STATUS_SUCCESS
;
991 return STATUS_UNSUCCESSFUL
;
994 VOID
GetName( PUNICODE_STRING RegistryKey
,
995 PUNICODE_STRING OutName
) {
997 UNICODE_STRING PartialRegistryKey
;
999 PartialRegistryKey
.Buffer
=
1000 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
1001 Ptr
= PartialRegistryKey
.Buffer
;
1003 while( *Ptr
!= L
'\\' &&
1004 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
1007 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
1008 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
1010 RtlInitUnicodeString( OutName
, L
"" );
1011 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
1014 BOOLEAN
BindAdapter(
1015 PLAN_ADAPTER Adapter
,
1016 PNDIS_STRING RegistryPath
)
1018 * FUNCTION: Binds a LAN adapter to IP layer
1020 * Adapter = Pointer to LAN_ADAPTER structure
1022 * We set the lookahead buffer size, set the packet filter and
1023 * bind the adapter to IP layer
1027 NDIS_STATUS NdisStatus
;
1028 LLIP_BIND_INFO BindInfo
;
1029 IP_ADDRESS DefaultMask
, Router
;
1030 ULONG Lookahead
= LOOKAHEAD_SIZE
, Unused
;
1032 OBJECT_ATTRIBUTES ObjectAttributes
;
1033 HANDLE ParameterHandle
;
1034 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo
;
1036 UNICODE_STRING IPAddress
= RTL_CONSTANT_STRING(L
"IPAddress");
1037 UNICODE_STRING Netmask
= RTL_CONSTANT_STRING(L
"SubnetMask");
1038 UNICODE_STRING Gateway
= RTL_CONSTANT_STRING(L
"DefaultGateway");
1039 UNICODE_STRING EnableDhcp
= RTL_CONSTANT_STRING(L
"EnableDHCP");
1040 UNICODE_STRING Prefix
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
1041 UNICODE_STRING TcpipRegistryPath
;
1042 UNICODE_STRING RegistryDataU
;
1043 ANSI_STRING RegistryDataA
;
1045 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1047 Adapter
->State
= LAN_STATE_OPENING
;
1049 NdisStatus
= NDISCall(Adapter
,
1050 NdisRequestSetInformation
,
1051 OID_GEN_CURRENT_LOOKAHEAD
,
1054 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1055 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
1059 /* Bind the adapter to IP layer */
1060 BindInfo
.Context
= Adapter
;
1061 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
1062 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
1063 BindInfo
.MTU
= Adapter
->MTU
;
1064 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
1065 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
1066 BindInfo
.Transmit
= LANTransmit
;
1068 IF
= IPCreateInterface(&BindInfo
);
1071 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1076 * Query per-adapter configuration from the registry
1077 * In case anyone is curious: there *is* an Ndis configuration api
1078 * for this sort of thing, but it doesn't really support things like
1079 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
1080 * protocol drivers developed for win2k and above just use the native
1081 * services (ZwOpenKey, etc).
1084 GetName( RegistryPath
, &IF
->Name
);
1086 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
1087 if (!NT_SUCCESS(Status
)) {
1088 TI_DbgPrint(MIN_TRACE
, ("Failed to get device description.\n"));
1089 IPDestroyInterface(IF
);
1093 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
1096 TcpipRegistryPath
.MaximumLength
= sizeof(WCHAR
) * 150;
1097 TcpipRegistryPath
.Length
= 0;
1098 TcpipRegistryPath
.Buffer
= Buffer
;
1100 RtlAppendUnicodeStringToString(&TcpipRegistryPath
,
1103 RtlAppendUnicodeStringToString(&TcpipRegistryPath
,
1106 InitializeObjectAttributes(&ObjectAttributes
,
1108 OBJ_CASE_INSENSITIVE
,
1112 AddrInitIPv4(&DefaultMask
, 0);
1114 Status
= ZwOpenKey(&ParameterHandle
, KEY_READ
, &ObjectAttributes
);
1116 if (!NT_SUCCESS(Status
))
1118 IF
->Unicast
= DefaultMask
;
1119 IF
->Netmask
= DefaultMask
;
1123 KeyValueInfo
= ExAllocatePool(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
));
1126 ZwClose(ParameterHandle
);
1127 IPDestroyInterface(IF
);
1131 Status
= ZwQueryValueKey(ParameterHandle
,
1133 KeyValuePartialInformation
,
1135 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
),
1137 if (NT_SUCCESS(Status
) && KeyValueInfo
->DataLength
== sizeof(ULONG
) && (*(PULONG
)KeyValueInfo
->Data
) == 0)
1139 RegistryDataU
.MaximumLength
= 16 + sizeof(WCHAR
);
1140 RegistryDataU
.Buffer
= (PWCHAR
)KeyValueInfo
->Data
;
1142 Status
= ZwQueryValueKey(ParameterHandle
,
1144 KeyValuePartialInformation
,
1146 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1148 if (NT_SUCCESS(Status
))
1150 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1152 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1156 AddrInitIPv4(&IF
->Unicast
, inet_addr(RegistryDataA
.Buffer
));
1158 RtlFreeAnsiString(&RegistryDataA
);
1163 IF
->Unicast
= DefaultMask
;
1166 Status
= ZwQueryValueKey(ParameterHandle
,
1168 KeyValuePartialInformation
,
1170 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1172 if (NT_SUCCESS(Status
))
1174 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1176 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1180 AddrInitIPv4(&IF
->Netmask
, inet_addr(RegistryDataA
.Buffer
));
1182 RtlFreeAnsiString(&RegistryDataA
);
1186 IF
->Netmask
= DefaultMask
;
1189 Status
= ZwQueryValueKey(ParameterHandle
,
1191 KeyValuePartialInformation
,
1193 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1195 if (NT_SUCCESS(Status
))
1197 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1199 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1203 AddrInitIPv4(&Router
, inet_addr(RegistryDataA
.Buffer
));
1205 RtlFreeAnsiString(&RegistryDataA
);
1207 if (!AddrIsUnspecified(&Router
)) RouterCreateRoute(&DefaultMask
, &DefaultMask
, &Router
, IF
, 1);
1212 IF
->Unicast
= DefaultMask
;
1213 IF
->Netmask
= DefaultMask
;
1216 ZwClose(ParameterHandle
);
1219 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
1220 IF
->Broadcast
.Address
.IPv4Address
=
1221 IF
->Unicast
.Address
.IPv4Address
|
1222 ~IF
->Netmask
.Address
.IPv4Address
;
1224 TI_DbgPrint(DEBUG_DATALINK
,("BCAST(IF) %s\n", A2S(&IF
->Broadcast
)));
1226 /* Get maximum link speed */
1227 NdisStatus
= NDISCall(Adapter
,
1228 NdisRequestQueryInformation
,
1233 if( !NT_SUCCESS(NdisStatus
) )
1234 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
1236 /* Register interface with IP layer */
1237 IPRegisterInterface(IF
);
1239 /* Set packet filter so we can send and receive packets */
1240 NdisStatus
= NDISCall(Adapter
,
1241 NdisRequestSetInformation
,
1242 OID_GEN_CURRENT_PACKET_FILTER
,
1243 &Adapter
->PacketFilter
,
1246 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1247 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
1248 IPUnregisterInterface(IF
);
1249 IPDestroyInterface(IF
);
1253 Adapter
->Context
= IF
;
1254 Adapter
->State
= LAN_STATE_STARTED
;
1260 PLAN_ADAPTER Adapter
)
1262 * FUNCTION: Unbinds a LAN adapter from IP layer
1264 * Adapter = Pointer to LAN_ADAPTER structure
1267 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1269 if (Adapter
->State
== LAN_STATE_STARTED
) {
1270 PIP_INTERFACE IF
= Adapter
->Context
;
1272 IPUnregisterInterface(IF
);
1274 IPDestroyInterface(IF
);
1279 NDIS_STATUS
LANRegisterAdapter(
1280 PNDIS_STRING AdapterName
,
1281 PNDIS_STRING RegistryPath
)
1283 * FUNCTION: Registers protocol with an NDIS adapter
1285 * AdapterName = Pointer to string with name of adapter to register
1286 * Adapter = Address of pointer to a LAN_ADAPTER structure
1288 * Status of operation
1292 NDIS_STATUS NdisStatus
;
1293 NDIS_STATUS OpenStatus
;
1295 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1299 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1301 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_ADAPTER
), LAN_ADAPTER_TAG
);
1303 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1304 return NDIS_STATUS_RESOURCES
;
1307 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1309 /* Put adapter in stopped state */
1310 IF
->State
= LAN_STATE_STOPPED
;
1312 /* Initialize protecting spin lock */
1313 KeInitializeSpinLock(&IF
->Lock
);
1315 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1317 /* Initialize array with media IDs we support */
1318 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1320 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1321 /* Open the adapter. */
1322 NdisOpenAdapter(&NdisStatus
,
1334 /* Wait until the adapter is opened */
1335 if (NdisStatus
== NDIS_STATUS_PENDING
)
1336 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1337 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1338 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1339 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1343 IF
->Media
= MediaArray
[MediaIndex
];
1345 /* Fill LAN_ADAPTER structure with some adapter specific information */
1346 switch (IF
->Media
) {
1347 case NdisMedium802_3
:
1348 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1349 IF
->BCastMask
= BCAST_ETH_MASK
;
1350 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1351 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1352 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1353 IF
->MinFrameSize
= 60;
1354 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1356 NDIS_PACKET_TYPE_BROADCAST
|
1357 NDIS_PACKET_TYPE_DIRECTED
|
1358 NDIS_PACKET_TYPE_MULTICAST
;
1362 /* Unsupported media */
1363 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1364 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1365 return NDIS_STATUS_NOT_SUPPORTED
;
1368 /* Get maximum frame size */
1369 NdisStatus
= NDISCall(IF
,
1370 NdisRequestQueryInformation
,
1371 OID_GEN_MAXIMUM_FRAME_SIZE
,
1374 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1375 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (NDISCall)\n", AdapterName
));
1376 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1380 /* Get maximum packet size */
1381 NdisStatus
= NDISCall(IF
,
1382 NdisRequestQueryInformation
,
1383 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1386 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1387 TI_DbgPrint(MIN_TRACE
, ("Query for maximum packet size failed.\n"));
1388 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1392 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1393 NdisStatus
= NDISCall(IF
,
1394 NdisRequestQueryInformation
,
1395 OID_GEN_MAXIMUM_SEND_PACKETS
,
1396 &IF
->MaxSendPackets
,
1398 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1399 /* Legacy NIC drivers may not support this query, if it fails we
1400 assume it can send at least one packet per call to NdisSend(Packets) */
1401 IF
->MaxSendPackets
= 1;
1403 /* Get current hardware address */
1404 NdisStatus
= NDISCall(IF
,
1405 NdisRequestQueryInformation
,
1408 IF
->HWAddressLength
);
1409 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1410 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1411 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1415 /* Get maximum link speed */
1416 NdisStatus
= NDISCall(IF
,
1417 NdisRequestQueryInformation
,
1421 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1422 TI_DbgPrint(MIN_TRACE
, ("Query for maximum link speed failed.\n"));
1423 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1427 /* Convert returned link speed to bps (it is in 100bps increments) */
1428 IF
->Speed
= Speed
* 100L;
1430 /* Bind adapter to IP layer */
1431 if( !BindAdapter(IF
, RegistryPath
) ) {
1432 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1433 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1434 return NDIS_STATUS_NOT_ACCEPTED
;
1437 /* Add adapter to the adapter list */
1438 ExInterlockedInsertTailList(&AdapterListHead
,
1442 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1444 return NDIS_STATUS_SUCCESS
;
1448 NDIS_STATUS
LANUnregisterAdapter(
1449 PLAN_ADAPTER Adapter
)
1451 * FUNCTION: Unregisters protocol with NDIS adapter
1453 * Adapter = Pointer to a LAN_ADAPTER structure
1455 * Status of operation
1459 NDIS_HANDLE NdisHandle
;
1460 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1462 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1464 /* Unlink the adapter from the list */
1465 RemoveEntryList(&Adapter
->ListEntry
);
1467 /* Unbind adapter from IP layer */
1468 UnbindAdapter(Adapter
);
1470 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1471 NdisHandle
= Adapter
->NdisHandle
;
1473 Adapter
->NdisHandle
= NULL
;
1474 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1476 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1477 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1478 TcpipWaitForSingleObject(&Adapter
->Event
,
1483 NdisStatus
= Adapter
->NdisStatus
;
1486 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1488 FreeAdapter(Adapter
);
1495 LANUnregisterProtocol(VOID
)
1497 * FUNCTION: Unregisters this protocol driver with NDIS
1498 * NOTES: Does not care wether we are already registered
1501 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1503 if (ProtocolRegistered
) {
1504 NDIS_STATUS NdisStatus
;
1505 PLIST_ENTRY CurrentEntry
;
1506 PLIST_ENTRY NextEntry
;
1507 PLAN_ADAPTER Current
;
1510 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1512 /* Search the list and remove every adapter we find */
1513 CurrentEntry
= AdapterListHead
.Flink
;
1514 while (CurrentEntry
!= &AdapterListHead
) {
1515 NextEntry
= CurrentEntry
->Flink
;
1516 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1518 LANUnregisterAdapter(Current
);
1519 CurrentEntry
= NextEntry
;
1522 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1524 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1525 ProtocolRegistered
= FALSE
;
1531 ProtocolUnbindAdapter(
1532 PNDIS_STATUS Status
,
1533 NDIS_HANDLE ProtocolBindingContext
,
1534 NDIS_HANDLE UnbindContext
)
1536 /* We don't pend any unbinding so we can just ignore UnbindContext */
1537 *Status
= LANUnregisterAdapter((PLAN_ADAPTER
)ProtocolBindingContext
);
1540 NTSTATUS
LANRegisterProtocol(
1543 * FUNCTION: Registers this protocol driver with NDIS
1545 * Name = Name of this protocol driver
1547 * Status of operation
1550 NDIS_STATUS NdisStatus
;
1551 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1553 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1555 InitializeListHead(&AdapterListHead
);
1556 KeInitializeSpinLock(&AdapterListLock
);
1558 /* Set up protocol characteristics */
1559 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1560 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1561 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1562 ProtChars
.Name
.Length
= Name
->Length
;
1563 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1564 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1565 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1566 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1567 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1568 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1569 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1570 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1571 ProtChars
.ReceivePacketHandler
= ProtocolReceivePacket
;
1572 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1573 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1574 ProtChars
.StatusHandler
= ProtocolStatus
;
1575 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1576 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1577 ProtChars
.PnPEventHandler
= ProtocolPnPEvent
;
1578 ProtChars
.UnbindAdapterHandler
= ProtocolUnbindAdapter
;
1579 ProtChars
.UnloadHandler
= LANUnregisterProtocol
;
1581 /* Try to register protocol */
1582 NdisRegisterProtocol(&NdisStatus
,
1583 &NdisProtocolHandle
,
1585 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1586 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1588 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1589 return (NTSTATUS
)NdisStatus
;
1592 ProtocolRegistered
= TRUE
;
1594 return STATUS_SUCCESS
;