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
17 UINT TransferDataCalled
= 0;
18 UINT TransferDataCompleteCalled
= 0;
20 #define CCS_ROOT L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet"
21 #define TCPIP_GUID L"{4D36E972-E325-11CE-BFC1-08002BE10318}"
23 typedef struct _LAN_WQ_ITEM
{
27 UINT BytesTransferred
;
28 BOOLEAN LegacyReceive
;
29 } LAN_WQ_ITEM
, *PLAN_WQ_ITEM
;
31 typedef struct _RECONFIGURE_CONTEXT
{
34 } RECONFIGURE_CONTEXT
, *PRECONFIGURE_CONTEXT
;
36 NDIS_HANDLE NdisProtocolHandle
= (NDIS_HANDLE
)NULL
;
37 BOOLEAN ProtocolRegistered
= FALSE
;
38 LIST_ENTRY AdapterListHead
;
39 KSPIN_LOCK AdapterListLock
;
43 NDIS_REQUEST_TYPE Type
,
48 * FUNCTION: Send a request to NDIS
50 * Adapter = Pointer to a LAN_ADAPTER structure
51 * Type = Type of request (Set or Query)
52 * OID = Value to be set/queried for
53 * Buffer = Pointer to a buffer to use
54 * Length = Number of bytes in Buffer
60 NDIS_STATUS NdisStatus
;
62 Request
.RequestType
= Type
;
63 if (Type
== NdisRequestSetInformation
) {
64 Request
.DATA
.SET_INFORMATION
.Oid
= OID
;
65 Request
.DATA
.SET_INFORMATION
.InformationBuffer
= Buffer
;
66 Request
.DATA
.SET_INFORMATION
.InformationBufferLength
= Length
;
68 Request
.DATA
.QUERY_INFORMATION
.Oid
= OID
;
69 Request
.DATA
.QUERY_INFORMATION
.InformationBuffer
= Buffer
;
70 Request
.DATA
.QUERY_INFORMATION
.InformationBufferLength
= Length
;
73 if (Adapter
->State
!= LAN_STATE_RESETTING
) {
74 NdisRequest(&NdisStatus
, Adapter
->NdisHandle
, &Request
);
76 NdisStatus
= NDIS_STATUS_NOT_ACCEPTED
;
79 /* Wait for NDIS to complete the request */
80 if (NdisStatus
== NDIS_STATUS_PENDING
) {
81 KeWaitForSingleObject(&Adapter
->Event
,
86 NdisStatus
= Adapter
->NdisStatus
;
92 /* Used by legacy ProtocolReceive for packet type */
94 GetPacketTypeFromHeaderBuffer(PLAN_ADAPTER Adapter
,
96 ULONG HeaderBufferSize
,
99 PETH_HEADER EthHeader
= HeaderBuffer
;
101 if (HeaderBufferSize
< Adapter
->HeaderSize
)
103 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", HeaderBufferSize
));
104 return NDIS_STATUS_NOT_ACCEPTED
;
107 switch (Adapter
->Media
)
109 case NdisMedium802_3
:
110 /* Ethernet and IEEE 802.3 frames can be destinguished by
111 looking at the IEEE 802.3 length field. This field is
112 less than or equal to 1500 for a valid IEEE 802.3 frame
113 and larger than 1500 is it's a valid EtherType value.
114 See RFC 1122, section 2.3.3 for more information */
116 *PacketType
= EthHeader
->EType
;
120 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
122 /* FIXME: Support other medias */
123 return NDIS_STATUS_NOT_ACCEPTED
;
126 TI_DbgPrint(DEBUG_DATALINK
, ("EtherType (0x%X).\n", *PacketType
));
128 return NDIS_STATUS_SUCCESS
;
131 /* Used by ProtocolReceivePacket for packet type */
133 GetPacketTypeFromNdisPacket(PLAN_ADAPTER Adapter
,
134 PNDIS_PACKET NdisPacket
,
141 HeaderBuffer
= ExAllocatePool(NonPagedPool
,
142 Adapter
->HeaderSize
);
144 return NDIS_STATUS_RESOURCES
;
146 /* Copy the media header */
147 BytesCopied
= CopyPacketToBuffer(HeaderBuffer
,
150 Adapter
->HeaderSize
);
151 if (BytesCopied
!= Adapter
->HeaderSize
)
154 ExFreePool(HeaderBuffer
);
155 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", BytesCopied
));
156 return NDIS_STATUS_NOT_ACCEPTED
;
159 Status
= GetPacketTypeFromHeaderBuffer(Adapter
,
164 ExFreePool(HeaderBuffer
);
171 PLAN_ADAPTER Adapter
)
173 * FUNCTION: Frees memory for a LAN_ADAPTER structure
175 * Adapter = Pointer to LAN_ADAPTER structure to free
178 ExFreePoolWithTag(Adapter
, LAN_ADAPTER_TAG
);
182 NTSTATUS TcpipLanGetDwordOid
183 ( PIP_INTERFACE Interface
,
186 /* Get maximum frame size */
187 if( Interface
->Context
) {
188 return NDISCall((PLAN_ADAPTER
)Interface
->Context
,
189 NdisRequestQueryInformation
,
193 } else switch( Oid
) { /* Loopback Case */
194 case OID_GEN_HARDWARE_STATUS
:
195 *Result
= NdisHardwareStatusReady
;
196 return STATUS_SUCCESS
;
197 case OID_GEN_MEDIA_CONNECT_STATUS
:
198 *Result
= NdisMediaStateConnected
;
199 return STATUS_SUCCESS
;
201 return STATUS_INVALID_PARAMETER
;
206 VOID NTAPI
ProtocolOpenAdapterComplete(
207 NDIS_HANDLE BindingContext
,
209 NDIS_STATUS OpenErrorStatus
)
211 * FUNCTION: Called by NDIS to complete opening of an adapter
213 * BindingContext = Pointer to a device context (LAN_ADAPTER)
214 * Status = Status of the operation
215 * OpenErrorStatus = Additional status information
218 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
220 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
222 Adapter
->NdisStatus
= Status
;
224 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
228 VOID NTAPI
ProtocolCloseAdapterComplete(
229 NDIS_HANDLE BindingContext
,
232 * FUNCTION: Called by NDIS to complete closing an adapter
234 * BindingContext = Pointer to a device context (LAN_ADAPTER)
235 * Status = Status of the operation
238 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
240 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
242 Adapter
->NdisStatus
= Status
;
244 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
248 VOID NTAPI
ProtocolResetComplete(
249 NDIS_HANDLE BindingContext
,
252 * FUNCTION: Called by NDIS to complete resetting an adapter
254 * BindingContext = Pointer to a device context (LAN_ADAPTER)
255 * Status = Status of the operation
258 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
260 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
262 Adapter
->NdisStatus
= Status
;
264 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
268 VOID NTAPI
ProtocolRequestComplete(
269 NDIS_HANDLE BindingContext
,
270 PNDIS_REQUEST NdisRequest
,
273 * FUNCTION: Called by NDIS to complete a request
275 * BindingContext = Pointer to a device context (LAN_ADAPTER)
276 * NdisRequest = Pointer to an object describing the request
277 * Status = Status of the operation
280 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
282 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
284 /* Save status of request and signal an event */
285 Adapter
->NdisStatus
= Status
;
287 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
291 VOID NTAPI
ProtocolSendComplete(
292 NDIS_HANDLE BindingContext
,
296 * FUNCTION: Called by NDIS to complete sending process
298 * BindingContext = Pointer to a device context (LAN_ADAPTER)
299 * Packet = Pointer to a packet descriptor
300 * Status = Status of the operation
303 FreeNdisPacket(Packet
);
306 VOID
LanReceiveWorker( PVOID Context
) {
308 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
310 PLAN_ADAPTER Adapter
;
311 UINT BytesTransferred
;
313 BOOLEAN LegacyReceive
;
314 PIP_INTERFACE Interface
;
316 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
318 Packet
= WorkItem
->Packet
;
319 Adapter
= WorkItem
->Adapter
;
320 BytesTransferred
= WorkItem
->BytesTransferred
;
321 LegacyReceive
= WorkItem
->LegacyReceive
;
323 ExFreePoolWithTag(WorkItem
, WQ_CONTEXT_TAG
);
325 Interface
= Adapter
->Context
;
327 IPInitializePacket(&IPPacket
, 0);
329 IPPacket
.NdisPacket
= Packet
;
330 IPPacket
.ReturnPacket
= !LegacyReceive
;
334 /* Packet type is precomputed */
335 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
337 /* Data is at position 0 */
338 IPPacket
.Position
= 0;
340 /* Packet size is determined by bytes transferred */
341 IPPacket
.TotalSize
= BytesTransferred
;
345 /* Determine packet type from media header */
346 if (GetPacketTypeFromNdisPacket(Adapter
,
348 &PacketType
) != NDIS_STATUS_SUCCESS
)
351 IPPacket
.Free(&IPPacket
);
355 /* Data is at the end of the media header */
356 IPPacket
.Position
= Adapter
->HeaderSize
;
358 /* Calculate packet size (excluding media header) */
359 NdisQueryPacketLength(IPPacket
.NdisPacket
, &IPPacket
.TotalSize
);
364 ("Ether Type = %x Total = %d\n",
365 PacketType
, IPPacket
.TotalSize
));
367 /* Update interface stats */
368 Interface
->Stats
.InBytes
+= IPPacket
.TotalSize
+ Adapter
->HeaderSize
;
370 /* NDIS packet is freed in all of these cases */
371 switch (PacketType
) {
374 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
375 IPReceive(Adapter
->Context
, &IPPacket
);
378 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
379 ARPReceive(Adapter
->Context
, &IPPacket
);
382 IPPacket
.Free(&IPPacket
);
387 VOID
LanSubmitReceiveWork(
388 NDIS_HANDLE BindingContext
,
390 UINT BytesTransferred
,
391 BOOLEAN LegacyReceive
) {
392 PLAN_WQ_ITEM WQItem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_WQ_ITEM
),
394 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
396 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
400 WQItem
->Packet
= Packet
;
401 WQItem
->Adapter
= Adapter
;
402 WQItem
->BytesTransferred
= BytesTransferred
;
403 WQItem
->LegacyReceive
= LegacyReceive
;
405 if (!ChewCreate( LanReceiveWorker
, WQItem
))
406 ExFreePoolWithTag(WQItem
, WQ_CONTEXT_TAG
);
409 VOID NTAPI
ProtocolTransferDataComplete(
410 NDIS_HANDLE BindingContext
,
413 UINT BytesTransferred
)
415 * FUNCTION: Called by NDIS to complete reception of data
417 * BindingContext = Pointer to a device context (LAN_ADAPTER)
418 * Packet = Pointer to a packet descriptor
419 * Status = Status of the operation
420 * BytesTransferred = Number of bytes transferred
422 * If the packet was successfully received, determine the protocol
423 * type and pass it to the correct receive handler
426 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
428 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
430 TransferDataCompleteCalled
++;
431 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
433 if( Status
!= NDIS_STATUS_SUCCESS
) return;
435 LanSubmitReceiveWork(BindingContext
,
441 INT NTAPI
ProtocolReceivePacket(
442 NDIS_HANDLE BindingContext
,
443 PNDIS_PACKET NdisPacket
)
445 PLAN_ADAPTER Adapter
= BindingContext
;
447 if (Adapter
->State
!= LAN_STATE_STARTED
) {
448 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
452 LanSubmitReceiveWork(BindingContext
,
457 /* Hold 1 reference on this packet */
461 NDIS_STATUS NTAPI
ProtocolReceive(
462 NDIS_HANDLE BindingContext
,
463 NDIS_HANDLE MacReceiveContext
,
465 UINT HeaderBufferSize
,
466 PVOID LookaheadBuffer
,
467 UINT LookaheadBufferSize
,
470 * FUNCTION: Called by NDIS when a packet has been received on the physical link
472 * BindingContext = Pointer to a device context (LAN_ADAPTER)
473 * MacReceiveContext = Handle used by underlying NIC driver
474 * HeaderBuffer = Pointer to a buffer containing the packet header
475 * HeaderBufferSize = Number of bytes in HeaderBuffer
476 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
477 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
478 * PacketSize = Overall size of the packet (not including header)
480 * Status of operation
484 UINT BytesTransferred
;
486 NDIS_STATUS NdisStatus
;
487 PNDIS_PACKET NdisPacket
;
488 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
490 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
492 if (Adapter
->State
!= LAN_STATE_STARTED
) {
493 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
494 return NDIS_STATUS_NOT_ACCEPTED
;
497 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
498 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
499 return NDIS_STATUS_NOT_ACCEPTED
;
502 NdisStatus
= GetPacketTypeFromHeaderBuffer(Adapter
,
506 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
507 return NDIS_STATUS_NOT_ACCEPTED
;
509 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
510 Adapter
, Adapter
->MTU
));
512 /* Get a transfer data packet */
513 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
515 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
516 return NDIS_STATUS_NOT_ACCEPTED
;
519 PC(NdisPacket
)->PacketType
= PacketType
;
521 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
523 GetDataPtr( NdisPacket
, 0, &BufferData
, &PacketSize
);
525 TransferDataCalled
++;
527 if (LookaheadBufferSize
== PacketSize
)
529 /* Optimized code path for packets that are fully contained in
530 * the lookahead buffer. */
531 NdisCopyLookaheadData(BufferData
,
534 Adapter
->MacOptions
);
538 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
539 MacReceiveContext
, 0, PacketSize
,
540 NdisPacket
, &BytesTransferred
);
542 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
544 if (NdisStatus
!= NDIS_STATUS_PENDING
)
545 ProtocolTransferDataComplete(BindingContext
,
550 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
552 return NDIS_STATUS_SUCCESS
;
556 VOID NTAPI
ProtocolReceiveComplete(
557 NDIS_HANDLE BindingContext
)
559 * FUNCTION: Called by NDIS when we're done receiving data
561 * BindingContext = Pointer to a device context (LAN_ADAPTER)
564 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
567 BOOLEAN
ReadIpConfiguration(PIP_INTERFACE Interface
)
569 OBJECT_ATTRIBUTES ObjectAttributes
;
570 HANDLE ParameterHandle
;
571 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo
;
573 UNICODE_STRING IPAddress
= RTL_CONSTANT_STRING(L
"IPAddress");
574 UNICODE_STRING Netmask
= RTL_CONSTANT_STRING(L
"SubnetMask");
575 UNICODE_STRING Gateway
= RTL_CONSTANT_STRING(L
"DefaultGateway");
576 UNICODE_STRING EnableDhcp
= RTL_CONSTANT_STRING(L
"EnableDHCP");
577 UNICODE_STRING Prefix
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
578 UNICODE_STRING TcpipRegistryPath
;
579 UNICODE_STRING RegistryDataU
;
580 ANSI_STRING RegistryDataA
;
583 IP_ADDRESS DefaultMask
, Router
;
585 AddrInitIPv4(&DefaultMask
, 0);
587 TcpipRegistryPath
.MaximumLength
= sizeof(WCHAR
) * 150;
588 TcpipRegistryPath
.Length
= 0;
589 TcpipRegistryPath
.Buffer
= Buffer
;
591 /* Build the registry path */
592 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Prefix
);
593 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Interface
->Name
);
595 InitializeObjectAttributes(&ObjectAttributes
,
597 OBJ_CASE_INSENSITIVE
,
601 /* Open a handle to the adapter parameters */
602 Status
= ZwOpenKey(&ParameterHandle
, KEY_READ
, &ObjectAttributes
);
604 if (!NT_SUCCESS(Status
))
610 KeyValueInfo
= ExAllocatePool(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
));
613 ZwClose(ParameterHandle
);
617 /* Read the EnableDHCP entry */
618 Status
= ZwQueryValueKey(ParameterHandle
,
620 KeyValuePartialInformation
,
622 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
),
624 if (NT_SUCCESS(Status
) && KeyValueInfo
->DataLength
== sizeof(ULONG
) && (*(PULONG
)KeyValueInfo
->Data
) == 0)
626 RegistryDataU
.MaximumLength
= 16 + sizeof(WCHAR
);
627 RegistryDataU
.Buffer
= (PWCHAR
)KeyValueInfo
->Data
;
629 /* Read the IP address */
630 Status
= ZwQueryValueKey(ParameterHandle
,
632 KeyValuePartialInformation
,
634 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
636 if (NT_SUCCESS(Status
))
638 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
640 RtlUnicodeStringToAnsiString(&RegistryDataA
,
644 AddrInitIPv4(&Interface
->Unicast
, inet_addr(RegistryDataA
.Buffer
));
646 RtlFreeAnsiString(&RegistryDataA
);
649 Status
= ZwQueryValueKey(ParameterHandle
,
651 KeyValuePartialInformation
,
653 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
655 if (NT_SUCCESS(Status
))
657 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
659 RtlUnicodeStringToAnsiString(&RegistryDataA
,
663 AddrInitIPv4(&Interface
->Netmask
, inet_addr(RegistryDataA
.Buffer
));
665 RtlFreeAnsiString(&RegistryDataA
);
668 /* We have to wait until both IP address and subnet mask
669 * are read to add the interface route, but we must do it
670 * before we add the default gateway */
671 if (!AddrIsUnspecified(&Interface
->Unicast
) &&
672 !AddrIsUnspecified(&Interface
->Netmask
))
673 IPAddInterfaceRoute(Interface
);
675 /* Read default gateway info */
676 Status
= ZwQueryValueKey(ParameterHandle
,
678 KeyValuePartialInformation
,
680 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
682 if (NT_SUCCESS(Status
))
684 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
686 RtlUnicodeStringToAnsiString(&RegistryDataA
,
690 AddrInitIPv4(&Router
, inet_addr(RegistryDataA
.Buffer
));
692 if (!AddrIsUnspecified(&Router
))
693 RouterCreateRoute(&DefaultMask
, &DefaultMask
, &Router
, Interface
, 1);
695 RtlFreeAnsiString(&RegistryDataA
);
699 ZwClose(ParameterHandle
);
705 BOOLEAN
ReconfigureAdapter(PRECONFIGURE_CONTEXT Context
)
707 PLAN_ADAPTER Adapter
= Context
->Adapter
;
708 PIP_INTERFACE Interface
= Adapter
->Context
;
709 //NDIS_STATUS NdisStatus;
710 IP_ADDRESS DefaultMask
;
712 /* Initialize the default unspecified address (0.0.0.0) */
713 AddrInitIPv4(&DefaultMask
, 0);
714 if (Context
->State
== LAN_STATE_STARTED
&&
715 !Context
->Adapter
->CompletingReset
)
717 /* Read the IP configuration */
718 ReadIpConfiguration(Interface
);
720 /* Compute the broadcast address */
721 Interface
->Broadcast
.Type
= IP_ADDRESS_V4
;
722 Interface
->Broadcast
.Address
.IPv4Address
= Interface
->Unicast
.Address
.IPv4Address
|
723 ~Interface
->Netmask
.Address
.IPv4Address
;
725 else if (!Context
->Adapter
->CompletingReset
)
727 /* Clear IP configuration */
728 Interface
->Unicast
= DefaultMask
;
729 Interface
->Netmask
= DefaultMask
;
730 Interface
->Broadcast
= DefaultMask
;
732 /* Remove all interface routes */
733 RouterRemoveRoutesForInterface(Interface
);
735 /* Destroy all cached neighbors */
736 NBDestroyNeighborsForInterface(Interface
);
739 Context
->Adapter
->CompletingReset
= FALSE
;
741 /* Update the IP and link status information cached in TCP */
742 TCPUpdateInterfaceIPInformation(Interface
);
743 TCPUpdateInterfaceLinkStatus(Interface
);
745 /* We're done here if the adapter isn't connected */
746 if (Context
->State
!= LAN_STATE_STARTED
)
748 Adapter
->State
= Context
->State
;
754 /* Get maximum link speed */
755 NdisStatus
= NDISCall(Adapter
,
756 NdisRequestQueryInformation
,
761 if (!NT_SUCCESS(NdisStatus
))
762 Interface
->Speed
= IP_DEFAULT_LINK_SPEED
;
764 Adapter
->Speed
= Interface
->Speed
* 100L;
766 /* Get maximum frame size */
767 NdisStatus
= NDISCall(Adapter
,
768 NdisRequestQueryInformation
,
769 OID_GEN_MAXIMUM_FRAME_SIZE
,
772 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
775 Interface
->MTU
= Adapter
->MTU
;
777 /* Get maximum packet size */
778 NdisStatus
= NDISCall(Adapter
,
779 NdisRequestQueryInformation
,
780 OID_GEN_MAXIMUM_TOTAL_SIZE
,
781 &Adapter
->MaxPacketSize
,
783 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
787 Adapter
->State
= Context
->State
;
792 VOID
ReconfigureAdapterWorker(PVOID Context
)
794 PRECONFIGURE_CONTEXT ReconfigureContext
= Context
;
796 /* Complete the reconfiguration asynchronously */
797 ReconfigureAdapter(ReconfigureContext
);
799 /* Free the context */
800 ExFreePool(ReconfigureContext
);
803 VOID NTAPI
ProtocolStatus(
804 NDIS_HANDLE BindingContext
,
805 NDIS_STATUS GeneralStatus
,
807 UINT StatusBufferSize
)
809 * FUNCTION: Called by NDIS when the underlying driver has changed state
811 * BindingContext = Pointer to a device context (LAN_ADAPTER)
812 * GeneralStatus = A general status code
813 * StatusBuffer = Pointer to a buffer with medium-specific data
814 * StatusBufferSize = Number of bytes in StatusBuffer
817 PLAN_ADAPTER Adapter
= BindingContext
;
818 PRECONFIGURE_CONTEXT Context
;
820 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
822 /* Ignore the status indication if we have no context yet. We'll get another later */
823 if (!Adapter
->Context
)
826 Context
= ExAllocatePool(NonPagedPool
, sizeof(RECONFIGURE_CONTEXT
));
830 Context
->Adapter
= Adapter
;
832 switch(GeneralStatus
)
834 case NDIS_STATUS_MEDIA_CONNECT
:
835 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
837 if (Adapter
->State
== LAN_STATE_STARTED
)
843 Context
->State
= LAN_STATE_STARTED
;
846 case NDIS_STATUS_MEDIA_DISCONNECT
:
847 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
849 if (Adapter
->State
== LAN_STATE_STOPPED
)
855 Context
->State
= LAN_STATE_STOPPED
;
858 case NDIS_STATUS_RESET_START
:
859 Adapter
->OldState
= Adapter
->State
;
860 Adapter
->State
= LAN_STATE_RESETTING
;
861 /* Nothing else to do here */
865 case NDIS_STATUS_RESET_END
:
866 Adapter
->CompletingReset
= TRUE
;
867 Context
->State
= Adapter
->OldState
;
871 DbgPrint("Unhandled status: %x", GeneralStatus
);
876 /* Queue the work item */
877 if (!ChewCreate(ReconfigureAdapterWorker
, Context
))
881 VOID NTAPI
ProtocolStatusComplete(NDIS_HANDLE NdisBindingContext
)
883 * FUNCTION: Called by NDIS when a status-change has occurred
885 * BindingContext = Pointer to a device context (LAN_ADAPTER)
888 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
893 NDIS_HANDLE NdisBindingContext
,
894 PNET_PNP_EVENT PnPEvent
)
896 switch(PnPEvent
->NetEvent
)
898 case NetEventSetPower
:
899 DbgPrint("Device transitioned to power state %ld\n", PnPEvent
->Buffer
);
900 return NDIS_STATUS_SUCCESS
;
902 case NetEventQueryPower
:
903 DbgPrint("Device wants to go into power state %ld\n", PnPEvent
->Buffer
);
904 return NDIS_STATUS_SUCCESS
;
906 case NetEventQueryRemoveDevice
:
907 DbgPrint("Device is about to be removed\n");
908 return NDIS_STATUS_SUCCESS
;
910 case NetEventCancelRemoveDevice
:
911 DbgPrint("Device removal cancelled\n");
912 return NDIS_STATUS_SUCCESS
;
915 DbgPrint("Unhandled event type: %ld\n", PnPEvent
->NetEvent
);
916 return NDIS_STATUS_SUCCESS
;
920 VOID NTAPI
ProtocolBindAdapter(
921 OUT PNDIS_STATUS Status
,
922 IN NDIS_HANDLE BindContext
,
923 IN PNDIS_STRING DeviceName
,
924 IN PVOID SystemSpecific1
,
925 IN PVOID SystemSpecific2
)
927 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
928 * bindings, and periodically thereafer as new adapters come online
930 * Status: Return value to NDIS
931 * BindContext: Handle provided by NDIS to track pending binding operations
932 * DeviceName: Name of the miniport device to bind to
933 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
934 * SystemSpecific2: Unused & must not be touched
937 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
938 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
944 PNDIS_PACKET NdisPacket
,
949 * FUNCTION: Transmits a packet
951 * Context = Pointer to context information (LAN_ADAPTER)
952 * NdisPacket = Pointer to NDIS packet to send
953 * Offset = Offset in packet where data starts
954 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
955 * Type = LAN protocol type (LAN_PROTO_*)
958 NDIS_STATUS NdisStatus
;
962 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
964 PNDIS_PACKET XmitPacket
;
965 PIP_INTERFACE Interface
= Adapter
->Context
;
967 TI_DbgPrint(DEBUG_DATALINK
,
968 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
969 NdisPacket
, Offset
, Adapter
));
971 if (Adapter
->State
!= LAN_STATE_STARTED
) {
972 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
976 TI_DbgPrint(DEBUG_DATALINK
,
977 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
978 Adapter
->HWAddress
[0] & 0xff,
979 Adapter
->HWAddress
[1] & 0xff,
980 Adapter
->HWAddress
[2] & 0xff,
981 Adapter
->HWAddress
[3] & 0xff,
982 Adapter
->HWAddress
[4] & 0xff,
983 Adapter
->HWAddress
[5] & 0xff));
985 GetDataPtr( NdisPacket
, 0, &OldData
, &OldSize
);
987 NdisStatus
= AllocatePacketWithBuffer(&XmitPacket
, NULL
, OldSize
+ Adapter
->HeaderSize
);
988 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
989 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_RESOURCES
);
993 GetDataPtr(XmitPacket
, 0, &Data
, &Size
);
995 RtlCopyMemory(Data
+ Adapter
->HeaderSize
, OldData
, OldSize
);
997 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_SUCCESS
);
999 switch (Adapter
->Media
) {
1000 case NdisMedium802_3
:
1001 EHeader
= (PETH_HEADER
)Data
;
1004 /* Unicast address */
1005 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
1007 /* Broadcast address */
1008 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
1011 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
1014 case LAN_PROTO_IPv4
:
1015 EHeader
->EType
= ETYPE_IPv4
;
1018 EHeader
->EType
= ETYPE_ARP
;
1020 case LAN_PROTO_IPv6
:
1021 EHeader
->EType
= ETYPE_IPv6
;
1030 /* FIXME: Support other medias */
1034 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
1038 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
1039 ((PCHAR
)LinkAddress
)[0] & 0xff,
1040 ((PCHAR
)LinkAddress
)[1] & 0xff,
1041 ((PCHAR
)LinkAddress
)[2] & 0xff,
1042 ((PCHAR
)LinkAddress
)[3] & 0xff,
1043 ((PCHAR
)LinkAddress
)[4] & 0xff,
1044 ((PCHAR
)LinkAddress
)[5] & 0xff));
1047 if (Adapter
->MTU
< Size
) {
1048 /* This is NOT a pointer. MSDN explicitly says so. */
1049 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket
,
1050 TcpLargeSendPacketInfo
) = (PVOID
)((ULONG_PTR
)Adapter
->MTU
);
1053 /* Update interface stats */
1054 Interface
->Stats
.OutBytes
+= Size
;
1056 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
1057 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
1058 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, XmitPacket
);
1059 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
1060 NdisStatus
== NDIS_STATUS_PENDING
?
1061 "Pending" : "Complete"));
1062 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
1064 /* I had a talk with vizzini: these really ought to be here.
1065 * we're supposed to see these completed by ndis *only* when
1066 * status_pending is returned. Note that this is different from
1067 * the situation with IRPs. */
1068 if (NdisStatus
!= NDIS_STATUS_PENDING
)
1069 ProtocolSendComplete((NDIS_HANDLE
)Context
, XmitPacket
, NdisStatus
);
1073 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
1074 OBJECT_ATTRIBUTES Attributes
;
1077 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, 0, 0);
1078 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
1082 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
1083 PWCHAR RegistryValue
,
1084 PUNICODE_STRING String
) {
1085 UNICODE_STRING ValueName
;
1086 UNICODE_STRING UnicodeString
;
1090 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
1092 RtlInitUnicodeString(&ValueName
, RegistryValue
);
1094 ZwQueryValueKey(RegHandle
,
1096 KeyValuePartialInformation
,
1101 if (!NT_SUCCESS(Status
))
1103 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
1104 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
1106 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
1107 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
1108 UnicodeString
.MaximumLength
= Information
->DataLength
;
1111 (PWCHAR
)ExAllocatePool( NonPagedPool
,
1112 UnicodeString
.MaximumLength
+ sizeof(WCHAR
) );
1114 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
1116 String
->MaximumLength
= UnicodeString
.MaximumLength
;
1117 RtlCopyUnicodeString( String
, &UnicodeString
);
1119 return STATUS_SUCCESS
;
1123 * Utility to copy and append two unicode strings.
1125 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
1126 * IN PUNICODE_STRING Second -> Second string to append
1127 * IN BOOL Deallocate -> TRUE: Deallocate First string before
1133 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
1134 PUNICODE_STRING Second
,
1135 BOOLEAN Deallocate
) {
1137 UNICODE_STRING Ustr
= *ResultFirst
;
1138 PWSTR new_string
= ExAllocatePool
1140 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)));
1142 return STATUS_NO_MEMORY
;
1144 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
1145 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
1146 Second
->Buffer
, Second
->Length
);
1147 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
1148 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
1149 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
1150 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
1151 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
1152 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
1153 ExFreePool(new_string
);
1157 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
1158 PUNICODE_STRING TargetKeyName
,
1159 PUNICODE_STRING Name
,
1160 PUNICODE_STRING DeviceDesc
) {
1161 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
1162 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
1163 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
1164 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
1167 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
1169 RtlInitUnicodeString(&BackSlash
, L
"\\");
1170 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
1172 RtlInitUnicodeString(&DescKeyName
, L
"");
1173 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
1174 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
1175 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
1177 RtlInitUnicodeString(&LinkageKeyName
, L
"");
1178 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
1179 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
1181 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
1182 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1184 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
1185 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1187 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
1188 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
1189 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1191 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
1192 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1194 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
1195 } else Status
= STATUS_UNSUCCESSFUL
;
1198 RtlFreeUnicodeString( &RootDevice
);
1199 RtlFreeUnicodeString( &LinkageKeyName
);
1200 RtlFreeUnicodeString( &DescKeyName
);
1201 if( LinkageKey
) NtClose( LinkageKey
);
1202 if( DescKey
) NtClose( DescKey
);
1204 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
1209 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
1210 PUNICODE_STRING DeviceDesc
) {
1211 UNICODE_STRING EnumKeyName
, TargetKeyName
;
1215 KEY_BASIC_INFORMATION
*Kbio
=
1216 ExAllocatePool(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
));
1217 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
1219 RtlInitUnicodeString( DeviceDesc
, NULL
);
1221 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
1223 RtlInitUnicodeString
1224 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
1226 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
1228 if( !NT_SUCCESS(Status
) ) {
1229 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
1230 &EnumKeyName
, Status
));
1235 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
1236 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1237 Kbio
, KbioLength
, &ResultLength
);
1239 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
1241 KbioLength
= ResultLength
;
1242 Kbio
= ExAllocatePool( NonPagedPool
, KbioLength
);
1244 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
1246 return STATUS_NO_MEMORY
;
1249 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1250 Kbio
, KbioLength
, &ResultLength
);
1252 if( !NT_SUCCESS(Status
) ) {
1253 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
1260 if( NT_SUCCESS(Status
) ) {
1261 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
1263 TargetKeyName
.Buffer
= Kbio
->Name
;
1265 Status
= CheckForDeviceDesc
1266 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
1267 if( NT_SUCCESS(Status
) ) {
1271 } else Status
= STATUS_SUCCESS
;
1277 return STATUS_UNSUCCESSFUL
;
1280 VOID
GetName( PUNICODE_STRING RegistryKey
,
1281 PUNICODE_STRING OutName
) {
1283 UNICODE_STRING PartialRegistryKey
;
1285 PartialRegistryKey
.Buffer
=
1286 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
1287 Ptr
= PartialRegistryKey
.Buffer
;
1289 while( *Ptr
!= L
'\\' &&
1290 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
1293 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
1294 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
1296 RtlInitUnicodeString( OutName
, L
"" );
1297 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
1300 BOOLEAN
BindAdapter(
1301 PLAN_ADAPTER Adapter
,
1302 PNDIS_STRING RegistryPath
)
1304 * FUNCTION: Binds a LAN adapter to IP layer
1306 * Adapter = Pointer to LAN_ADAPTER structure
1308 * We set the lookahead buffer size, set the packet filter and
1309 * bind the adapter to IP layer
1313 NDIS_STATUS NdisStatus
;
1314 LLIP_BIND_INFO BindInfo
;
1315 ULONG Lookahead
= LOOKAHEAD_SIZE
;
1317 NDIS_MEDIA_STATE MediaState
;
1319 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1321 Adapter
->State
= LAN_STATE_OPENING
;
1323 NdisStatus
= NDISCall(Adapter
,
1324 NdisRequestSetInformation
,
1325 OID_GEN_CURRENT_LOOKAHEAD
,
1328 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1329 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
1333 /* Bind the adapter to IP layer */
1334 BindInfo
.Context
= Adapter
;
1335 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
1336 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
1337 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
1338 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
1339 BindInfo
.Transmit
= LANTransmit
;
1341 IF
= IPCreateInterface(&BindInfo
);
1344 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1349 * Query per-adapter configuration from the registry
1350 * In case anyone is curious: there *is* an Ndis configuration api
1351 * for this sort of thing, but it doesn't really support things like
1352 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
1353 * protocol drivers developed for win2k and above just use the native
1354 * services (ZwOpenKey, etc).
1357 GetName( RegistryPath
, &IF
->Name
);
1359 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
1360 if (!NT_SUCCESS(Status
)) {
1361 TI_DbgPrint(MIN_TRACE
, ("Failed to get device description.\n"));
1362 IPDestroyInterface(IF
);
1366 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
1369 /* Get maximum link speed */
1370 NdisStatus
= NDISCall(Adapter
,
1371 NdisRequestQueryInformation
,
1376 if (!NT_SUCCESS(NdisStatus
))
1377 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
1379 Adapter
->Speed
= IF
->Speed
* 100L;
1381 /* Get maximum frame size */
1382 NdisStatus
= NDISCall(Adapter
,
1383 NdisRequestQueryInformation
,
1384 OID_GEN_MAXIMUM_FRAME_SIZE
,
1387 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1390 IF
->MTU
= Adapter
->MTU
;
1392 /* Get maximum packet size */
1393 NdisStatus
= NDISCall(Adapter
,
1394 NdisRequestQueryInformation
,
1395 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1396 &Adapter
->MaxPacketSize
,
1398 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1401 /* Register interface with IP layer */
1402 IPRegisterInterface(IF
);
1404 /* Store adapter context */
1405 Adapter
->Context
= IF
;
1407 /* Get the media state */
1408 NdisStatus
= NDISCall(Adapter
,
1409 NdisRequestQueryInformation
,
1410 OID_GEN_MEDIA_CONNECT_STATUS
,
1412 sizeof(MediaState
));
1413 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1414 TI_DbgPrint(DEBUG_DATALINK
, ("Could not query media status (0x%X).\n", NdisStatus
));
1415 IPUnregisterInterface(IF
);
1416 IPDestroyInterface(IF
);
1420 /* Indicate the current media state */
1421 ProtocolStatus(Adapter
,
1422 (MediaState
== NdisMediaStateConnected
) ? NDIS_STATUS_MEDIA_CONNECT
: NDIS_STATUS_MEDIA_DISCONNECT
,
1425 /* Set packet filter so we can send and receive packets */
1426 NdisStatus
= NDISCall(Adapter
,
1427 NdisRequestSetInformation
,
1428 OID_GEN_CURRENT_PACKET_FILTER
,
1429 &Adapter
->PacketFilter
,
1432 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1433 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
1434 IPUnregisterInterface(IF
);
1435 IPDestroyInterface(IF
);
1444 PLAN_ADAPTER Adapter
)
1446 * FUNCTION: Unbinds a LAN adapter from IP layer
1448 * Adapter = Pointer to LAN_ADAPTER structure
1451 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1453 if (Adapter
->State
== LAN_STATE_STARTED
) {
1454 PIP_INTERFACE IF
= Adapter
->Context
;
1456 IPUnregisterInterface(IF
);
1458 IPDestroyInterface(IF
);
1463 NDIS_STATUS
LANRegisterAdapter(
1464 PNDIS_STRING AdapterName
,
1465 PNDIS_STRING RegistryPath
)
1467 * FUNCTION: Registers protocol with an NDIS adapter
1469 * AdapterName = Pointer to string with name of adapter to register
1470 * Adapter = Address of pointer to a LAN_ADAPTER structure
1472 * Status of operation
1476 NDIS_STATUS NdisStatus
;
1477 NDIS_STATUS OpenStatus
;
1479 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1482 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1484 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_ADAPTER
), LAN_ADAPTER_TAG
);
1486 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1487 return NDIS_STATUS_RESOURCES
;
1490 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1492 /* Put adapter in stopped state */
1493 IF
->State
= LAN_STATE_STOPPED
;
1495 /* Initialize protecting spin lock */
1496 KeInitializeSpinLock(&IF
->Lock
);
1498 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1500 /* Initialize array with media IDs we support */
1501 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1503 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1504 /* Open the adapter. */
1505 NdisOpenAdapter(&NdisStatus
,
1517 /* Wait until the adapter is opened */
1518 if (NdisStatus
== NDIS_STATUS_PENDING
)
1519 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1520 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1521 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1522 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1526 IF
->Media
= MediaArray
[MediaIndex
];
1528 /* Fill LAN_ADAPTER structure with some adapter specific information */
1529 switch (IF
->Media
) {
1530 case NdisMedium802_3
:
1531 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1532 IF
->BCastMask
= BCAST_ETH_MASK
;
1533 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1534 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1535 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1536 IF
->MinFrameSize
= 60;
1537 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1539 NDIS_PACKET_TYPE_BROADCAST
|
1540 NDIS_PACKET_TYPE_DIRECTED
|
1541 NDIS_PACKET_TYPE_MULTICAST
;
1545 /* Unsupported media */
1546 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1547 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1548 return NDIS_STATUS_NOT_SUPPORTED
;
1551 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1552 NdisStatus
= NDISCall(IF
,
1553 NdisRequestQueryInformation
,
1554 OID_GEN_MAXIMUM_SEND_PACKETS
,
1555 &IF
->MaxSendPackets
,
1557 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1558 /* Legacy NIC drivers may not support this query, if it fails we
1559 assume it can send at least one packet per call to NdisSend(Packets) */
1560 IF
->MaxSendPackets
= 1;
1562 /* Get current hardware address */
1563 NdisStatus
= NDISCall(IF
,
1564 NdisRequestQueryInformation
,
1567 IF
->HWAddressLength
);
1568 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1569 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1570 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1574 /* Bind adapter to IP layer */
1575 if( !BindAdapter(IF
, RegistryPath
) ) {
1576 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1577 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1578 return NDIS_STATUS_NOT_ACCEPTED
;
1581 /* Add adapter to the adapter list */
1582 ExInterlockedInsertTailList(&AdapterListHead
,
1586 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1588 return NDIS_STATUS_SUCCESS
;
1592 NDIS_STATUS
LANUnregisterAdapter(
1593 PLAN_ADAPTER Adapter
)
1595 * FUNCTION: Unregisters protocol with NDIS adapter
1597 * Adapter = Pointer to a LAN_ADAPTER structure
1599 * Status of operation
1603 NDIS_HANDLE NdisHandle
;
1604 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1606 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1608 /* Unlink the adapter from the list */
1609 RemoveEntryList(&Adapter
->ListEntry
);
1611 /* Unbind adapter from IP layer */
1612 UnbindAdapter(Adapter
);
1614 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1615 NdisHandle
= Adapter
->NdisHandle
;
1617 Adapter
->NdisHandle
= NULL
;
1618 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1620 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1621 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1622 TcpipWaitForSingleObject(&Adapter
->Event
,
1627 NdisStatus
= Adapter
->NdisStatus
;
1630 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1632 FreeAdapter(Adapter
);
1639 LANUnregisterProtocol(VOID
)
1641 * FUNCTION: Unregisters this protocol driver with NDIS
1642 * NOTES: Does not care wether we are already registered
1645 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1647 if (ProtocolRegistered
) {
1648 NDIS_STATUS NdisStatus
;
1649 PLIST_ENTRY CurrentEntry
;
1650 PLIST_ENTRY NextEntry
;
1651 PLAN_ADAPTER Current
;
1654 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1656 /* Search the list and remove every adapter we find */
1657 CurrentEntry
= AdapterListHead
.Flink
;
1658 while (CurrentEntry
!= &AdapterListHead
) {
1659 NextEntry
= CurrentEntry
->Flink
;
1660 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1662 LANUnregisterAdapter(Current
);
1663 CurrentEntry
= NextEntry
;
1666 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1668 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1669 ProtocolRegistered
= FALSE
;
1675 ProtocolUnbindAdapter(
1676 PNDIS_STATUS Status
,
1677 NDIS_HANDLE ProtocolBindingContext
,
1678 NDIS_HANDLE UnbindContext
)
1680 /* We don't pend any unbinding so we can just ignore UnbindContext */
1681 *Status
= LANUnregisterAdapter((PLAN_ADAPTER
)ProtocolBindingContext
);
1684 NTSTATUS
LANRegisterProtocol(
1687 * FUNCTION: Registers this protocol driver with NDIS
1689 * Name = Name of this protocol driver
1691 * Status of operation
1694 NDIS_STATUS NdisStatus
;
1695 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1697 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1699 InitializeListHead(&AdapterListHead
);
1700 KeInitializeSpinLock(&AdapterListLock
);
1702 /* Set up protocol characteristics */
1703 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1704 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1705 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1706 ProtChars
.Name
.Length
= Name
->Length
;
1707 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1708 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1709 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1710 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1711 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1712 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1713 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1714 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1715 ProtChars
.ReceivePacketHandler
= ProtocolReceivePacket
;
1716 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1717 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1718 ProtChars
.StatusHandler
= ProtocolStatus
;
1719 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1720 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1721 ProtChars
.PnPEventHandler
= ProtocolPnPEvent
;
1722 ProtChars
.UnbindAdapterHandler
= ProtocolUnbindAdapter
;
1723 ProtChars
.UnloadHandler
= LANUnregisterProtocol
;
1725 /* Try to register protocol */
1726 NdisRegisterProtocol(&NdisStatus
,
1727 &NdisProtocolHandle
,
1729 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1730 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1732 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1733 return (NTSTATUS
)NdisStatus
;
1736 ProtocolRegistered
= TRUE
;
1738 return STATUS_SUCCESS
;