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
= ExAllocatePoolWithTag(NonPagedPool
,
145 return NDIS_STATUS_RESOURCES
;
147 /* Copy the media header */
148 BytesCopied
= CopyPacketToBuffer(HeaderBuffer
,
151 Adapter
->HeaderSize
);
152 if (BytesCopied
!= Adapter
->HeaderSize
)
155 ExFreePoolWithTag(HeaderBuffer
, HEADER_TAG
);
156 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", BytesCopied
));
157 return NDIS_STATUS_NOT_ACCEPTED
;
160 Status
= GetPacketTypeFromHeaderBuffer(Adapter
,
165 ExFreePoolWithTag(HeaderBuffer
, HEADER_TAG
);
172 PLAN_ADAPTER Adapter
)
174 * FUNCTION: Frees memory for a LAN_ADAPTER structure
176 * Adapter = Pointer to LAN_ADAPTER structure to free
179 ExFreePoolWithTag(Adapter
, LAN_ADAPTER_TAG
);
183 NTSTATUS TcpipLanGetDwordOid
184 ( PIP_INTERFACE Interface
,
187 /* Get maximum frame size */
188 if( Interface
->Context
) {
189 return NDISCall((PLAN_ADAPTER
)Interface
->Context
,
190 NdisRequestQueryInformation
,
194 } else switch( Oid
) { /* Loopback Case */
195 case OID_GEN_HARDWARE_STATUS
:
196 *Result
= NdisHardwareStatusReady
;
197 return STATUS_SUCCESS
;
198 case OID_GEN_MEDIA_CONNECT_STATUS
:
199 *Result
= NdisMediaStateConnected
;
200 return STATUS_SUCCESS
;
202 return STATUS_INVALID_PARAMETER
;
207 VOID NTAPI
ProtocolOpenAdapterComplete(
208 NDIS_HANDLE BindingContext
,
210 NDIS_STATUS OpenErrorStatus
)
212 * FUNCTION: Called by NDIS to complete opening of an adapter
214 * BindingContext = Pointer to a device context (LAN_ADAPTER)
215 * Status = Status of the operation
216 * OpenErrorStatus = Additional status information
219 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
221 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
223 Adapter
->NdisStatus
= Status
;
225 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
229 VOID NTAPI
ProtocolCloseAdapterComplete(
230 NDIS_HANDLE BindingContext
,
233 * FUNCTION: Called by NDIS to complete closing an adapter
235 * BindingContext = Pointer to a device context (LAN_ADAPTER)
236 * Status = Status of the operation
239 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
241 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
243 Adapter
->NdisStatus
= Status
;
245 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
249 VOID NTAPI
ProtocolResetComplete(
250 NDIS_HANDLE BindingContext
,
253 * FUNCTION: Called by NDIS to complete resetting an adapter
255 * BindingContext = Pointer to a device context (LAN_ADAPTER)
256 * Status = Status of the operation
259 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
261 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
263 Adapter
->NdisStatus
= Status
;
265 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
269 VOID NTAPI
ProtocolRequestComplete(
270 NDIS_HANDLE BindingContext
,
271 PNDIS_REQUEST NdisRequest
,
274 * FUNCTION: Called by NDIS to complete a request
276 * BindingContext = Pointer to a device context (LAN_ADAPTER)
277 * NdisRequest = Pointer to an object describing the request
278 * Status = Status of the operation
281 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
283 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
285 /* Save status of request and signal an event */
286 Adapter
->NdisStatus
= Status
;
288 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
292 VOID NTAPI
ProtocolSendComplete(
293 NDIS_HANDLE BindingContext
,
297 * FUNCTION: Called by NDIS to complete sending process
299 * BindingContext = Pointer to a device context (LAN_ADAPTER)
300 * Packet = Pointer to a packet descriptor
301 * Status = Status of the operation
304 FreeNdisPacket(Packet
);
307 VOID
LanReceiveWorker( PVOID Context
) {
309 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
311 PLAN_ADAPTER Adapter
;
312 UINT BytesTransferred
;
314 BOOLEAN LegacyReceive
;
315 PIP_INTERFACE Interface
;
317 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
319 Packet
= WorkItem
->Packet
;
320 Adapter
= WorkItem
->Adapter
;
321 BytesTransferred
= WorkItem
->BytesTransferred
;
322 LegacyReceive
= WorkItem
->LegacyReceive
;
324 ExFreePoolWithTag(WorkItem
, WQ_CONTEXT_TAG
);
326 Interface
= Adapter
->Context
;
328 IPInitializePacket(&IPPacket
, 0);
330 IPPacket
.NdisPacket
= Packet
;
331 IPPacket
.ReturnPacket
= !LegacyReceive
;
335 /* Packet type is precomputed */
336 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
338 /* Data is at position 0 */
339 IPPacket
.Position
= 0;
341 /* Packet size is determined by bytes transferred */
342 IPPacket
.TotalSize
= BytesTransferred
;
346 /* Determine packet type from media header */
347 if (GetPacketTypeFromNdisPacket(Adapter
,
349 &PacketType
) != NDIS_STATUS_SUCCESS
)
352 IPPacket
.Free(&IPPacket
);
356 /* Data is at the end of the media header */
357 IPPacket
.Position
= Adapter
->HeaderSize
;
359 /* Calculate packet size (excluding media header) */
360 NdisQueryPacketLength(IPPacket
.NdisPacket
, &IPPacket
.TotalSize
);
365 ("Ether Type = %x Total = %d\n",
366 PacketType
, IPPacket
.TotalSize
));
368 /* Update interface stats */
369 Interface
->Stats
.InBytes
+= IPPacket
.TotalSize
+ Adapter
->HeaderSize
;
371 /* NDIS packet is freed in all of these cases */
372 switch (PacketType
) {
375 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
376 IPReceive(Adapter
->Context
, &IPPacket
);
379 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
380 ARPReceive(Adapter
->Context
, &IPPacket
);
383 IPPacket
.Free(&IPPacket
);
388 VOID
LanSubmitReceiveWork(
389 NDIS_HANDLE BindingContext
,
391 UINT BytesTransferred
,
392 BOOLEAN LegacyReceive
) {
393 PLAN_WQ_ITEM WQItem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_WQ_ITEM
),
395 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
397 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
401 WQItem
->Packet
= Packet
;
402 WQItem
->Adapter
= Adapter
;
403 WQItem
->BytesTransferred
= BytesTransferred
;
404 WQItem
->LegacyReceive
= LegacyReceive
;
406 if (!ChewCreate( LanReceiveWorker
, WQItem
))
407 ExFreePoolWithTag(WQItem
, WQ_CONTEXT_TAG
);
410 VOID NTAPI
ProtocolTransferDataComplete(
411 NDIS_HANDLE BindingContext
,
414 UINT BytesTransferred
)
416 * FUNCTION: Called by NDIS to complete reception of data
418 * BindingContext = Pointer to a device context (LAN_ADAPTER)
419 * Packet = Pointer to a packet descriptor
420 * Status = Status of the operation
421 * BytesTransferred = Number of bytes transferred
423 * If the packet was successfully received, determine the protocol
424 * type and pass it to the correct receive handler
427 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
429 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
431 TransferDataCompleteCalled
++;
432 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
434 if( Status
!= NDIS_STATUS_SUCCESS
) return;
436 LanSubmitReceiveWork(BindingContext
,
442 INT NTAPI
ProtocolReceivePacket(
443 NDIS_HANDLE BindingContext
,
444 PNDIS_PACKET NdisPacket
)
446 PLAN_ADAPTER Adapter
= BindingContext
;
448 if (Adapter
->State
!= LAN_STATE_STARTED
) {
449 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
453 LanSubmitReceiveWork(BindingContext
,
458 /* Hold 1 reference on this packet */
462 NDIS_STATUS NTAPI
ProtocolReceive(
463 NDIS_HANDLE BindingContext
,
464 NDIS_HANDLE MacReceiveContext
,
466 UINT HeaderBufferSize
,
467 PVOID LookaheadBuffer
,
468 UINT LookaheadBufferSize
,
471 * FUNCTION: Called by NDIS when a packet has been received on the physical link
473 * BindingContext = Pointer to a device context (LAN_ADAPTER)
474 * MacReceiveContext = Handle used by underlying NIC driver
475 * HeaderBuffer = Pointer to a buffer containing the packet header
476 * HeaderBufferSize = Number of bytes in HeaderBuffer
477 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
478 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
479 * PacketSize = Overall size of the packet (not including header)
481 * Status of operation
485 UINT BytesTransferred
;
487 NDIS_STATUS NdisStatus
;
488 PNDIS_PACKET NdisPacket
;
489 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
491 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
493 if (Adapter
->State
!= LAN_STATE_STARTED
) {
494 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
495 return NDIS_STATUS_NOT_ACCEPTED
;
498 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
499 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
500 return NDIS_STATUS_NOT_ACCEPTED
;
503 NdisStatus
= GetPacketTypeFromHeaderBuffer(Adapter
,
507 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
508 return NDIS_STATUS_NOT_ACCEPTED
;
510 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
511 Adapter
, Adapter
->MTU
));
513 /* Get a transfer data packet */
514 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
516 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
517 return NDIS_STATUS_NOT_ACCEPTED
;
520 PC(NdisPacket
)->PacketType
= PacketType
;
522 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
524 GetDataPtr( NdisPacket
, 0, &BufferData
, &PacketSize
);
526 TransferDataCalled
++;
528 if (LookaheadBufferSize
== PacketSize
)
530 /* Optimized code path for packets that are fully contained in
531 * the lookahead buffer. */
532 NdisCopyLookaheadData(BufferData
,
535 Adapter
->MacOptions
);
539 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
540 MacReceiveContext
, 0, PacketSize
,
541 NdisPacket
, &BytesTransferred
);
543 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
545 if (NdisStatus
!= NDIS_STATUS_PENDING
)
546 ProtocolTransferDataComplete(BindingContext
,
551 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
553 return NDIS_STATUS_SUCCESS
;
557 VOID NTAPI
ProtocolReceiveComplete(
558 NDIS_HANDLE BindingContext
)
560 * FUNCTION: Called by NDIS when we're done receiving data
562 * BindingContext = Pointer to a device context (LAN_ADAPTER)
565 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
568 BOOLEAN
ReadIpConfiguration(PIP_INTERFACE Interface
)
570 OBJECT_ATTRIBUTES ObjectAttributes
;
571 HANDLE ParameterHandle
;
572 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo
;
574 UNICODE_STRING IPAddress
= RTL_CONSTANT_STRING(L
"IPAddress");
575 UNICODE_STRING Netmask
= RTL_CONSTANT_STRING(L
"SubnetMask");
576 UNICODE_STRING Gateway
= RTL_CONSTANT_STRING(L
"DefaultGateway");
577 UNICODE_STRING EnableDhcp
= RTL_CONSTANT_STRING(L
"EnableDHCP");
578 UNICODE_STRING Prefix
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
579 UNICODE_STRING TcpipRegistryPath
;
580 UNICODE_STRING RegistryDataU
;
581 ANSI_STRING RegistryDataA
;
584 IP_ADDRESS DefaultMask
, Router
;
586 AddrInitIPv4(&DefaultMask
, 0);
588 TcpipRegistryPath
.MaximumLength
= sizeof(WCHAR
) * 150;
589 TcpipRegistryPath
.Length
= 0;
590 TcpipRegistryPath
.Buffer
= Buffer
;
592 /* Build the registry path */
593 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Prefix
);
594 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Interface
->Name
);
596 InitializeObjectAttributes(&ObjectAttributes
,
598 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
602 /* Open a handle to the adapter parameters */
603 Status
= ZwOpenKey(&ParameterHandle
, KEY_READ
, &ObjectAttributes
);
605 if (!NT_SUCCESS(Status
))
611 KeyValueInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
), KEY_VALUE_TAG
);
614 ZwClose(ParameterHandle
);
618 /* Read the EnableDHCP entry */
619 Status
= ZwQueryValueKey(ParameterHandle
,
621 KeyValuePartialInformation
,
623 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
),
625 if (NT_SUCCESS(Status
) && KeyValueInfo
->DataLength
== sizeof(ULONG
) && (*(PULONG
)KeyValueInfo
->Data
) == 0)
627 RegistryDataU
.MaximumLength
= 16 + sizeof(WCHAR
);
628 RegistryDataU
.Buffer
= (PWCHAR
)KeyValueInfo
->Data
;
630 /* Read the IP address */
631 Status
= ZwQueryValueKey(ParameterHandle
,
633 KeyValuePartialInformation
,
635 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
637 if (NT_SUCCESS(Status
))
639 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
641 RtlUnicodeStringToAnsiString(&RegistryDataA
,
645 AddrInitIPv4(&Interface
->Unicast
, inet_addr(RegistryDataA
.Buffer
));
647 RtlFreeAnsiString(&RegistryDataA
);
650 Status
= ZwQueryValueKey(ParameterHandle
,
652 KeyValuePartialInformation
,
654 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
656 if (NT_SUCCESS(Status
))
658 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
660 RtlUnicodeStringToAnsiString(&RegistryDataA
,
664 AddrInitIPv4(&Interface
->Netmask
, inet_addr(RegistryDataA
.Buffer
));
666 RtlFreeAnsiString(&RegistryDataA
);
669 /* We have to wait until both IP address and subnet mask
670 * are read to add the interface route, but we must do it
671 * before we add the default gateway */
672 if (!AddrIsUnspecified(&Interface
->Unicast
) &&
673 !AddrIsUnspecified(&Interface
->Netmask
))
674 IPAddInterfaceRoute(Interface
);
676 /* Read default gateway info */
677 Status
= ZwQueryValueKey(ParameterHandle
,
679 KeyValuePartialInformation
,
681 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
683 if (NT_SUCCESS(Status
))
685 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
687 RtlUnicodeStringToAnsiString(&RegistryDataA
,
691 AddrInitIPv4(&Router
, inet_addr(RegistryDataA
.Buffer
));
693 if (!AddrIsUnspecified(&Router
))
694 RouterCreateRoute(&DefaultMask
, &DefaultMask
, &Router
, Interface
, 1);
696 RtlFreeAnsiString(&RegistryDataA
);
700 ExFreePoolWithTag(KeyValueInfo
, KEY_VALUE_TAG
);
701 ZwClose(ParameterHandle
);
707 BOOLEAN
ReconfigureAdapter(PRECONFIGURE_CONTEXT Context
)
709 PLAN_ADAPTER Adapter
= Context
->Adapter
;
710 PIP_INTERFACE Interface
= Adapter
->Context
;
711 //NDIS_STATUS NdisStatus;
712 IP_ADDRESS DefaultMask
;
714 /* Initialize the default unspecified address (0.0.0.0) */
715 AddrInitIPv4(&DefaultMask
, 0);
716 if (Context
->State
== LAN_STATE_STARTED
&&
717 !Context
->Adapter
->CompletingReset
)
719 /* Read the IP configuration */
720 ReadIpConfiguration(Interface
);
722 /* Compute the broadcast address */
723 Interface
->Broadcast
.Type
= IP_ADDRESS_V4
;
724 Interface
->Broadcast
.Address
.IPv4Address
= Interface
->Unicast
.Address
.IPv4Address
|
725 ~Interface
->Netmask
.Address
.IPv4Address
;
727 else if (!Context
->Adapter
->CompletingReset
)
729 /* Clear IP configuration */
730 Interface
->Unicast
= DefaultMask
;
731 Interface
->Netmask
= DefaultMask
;
732 Interface
->Broadcast
= DefaultMask
;
734 /* Remove all interface routes */
735 RouterRemoveRoutesForInterface(Interface
);
737 /* Destroy all cached neighbors */
738 NBDestroyNeighborsForInterface(Interface
);
741 Context
->Adapter
->CompletingReset
= FALSE
;
743 /* Update the IP and link status information cached in TCP */
744 TCPUpdateInterfaceIPInformation(Interface
);
745 TCPUpdateInterfaceLinkStatus(Interface
);
747 /* We're done here if the adapter isn't connected */
748 if (Context
->State
!= LAN_STATE_STARTED
)
750 Adapter
->State
= Context
->State
;
756 /* Get maximum link speed */
757 NdisStatus
= NDISCall(Adapter
,
758 NdisRequestQueryInformation
,
763 if (!NT_SUCCESS(NdisStatus
))
764 Interface
->Speed
= IP_DEFAULT_LINK_SPEED
;
766 Adapter
->Speed
= Interface
->Speed
* 100L;
768 /* Get maximum frame size */
769 NdisStatus
= NDISCall(Adapter
,
770 NdisRequestQueryInformation
,
771 OID_GEN_MAXIMUM_FRAME_SIZE
,
774 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
777 Interface
->MTU
= Adapter
->MTU
;
779 /* Get maximum packet size */
780 NdisStatus
= NDISCall(Adapter
,
781 NdisRequestQueryInformation
,
782 OID_GEN_MAXIMUM_TOTAL_SIZE
,
783 &Adapter
->MaxPacketSize
,
785 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
789 Adapter
->State
= Context
->State
;
794 VOID
ReconfigureAdapterWorker(PVOID Context
)
796 PRECONFIGURE_CONTEXT ReconfigureContext
= Context
;
798 /* Complete the reconfiguration asynchronously */
799 ReconfigureAdapter(ReconfigureContext
);
801 /* Free the context */
802 ExFreePool(ReconfigureContext
);
805 VOID NTAPI
ProtocolStatus(
806 NDIS_HANDLE BindingContext
,
807 NDIS_STATUS GeneralStatus
,
809 UINT StatusBufferSize
)
811 * FUNCTION: Called by NDIS when the underlying driver has changed state
813 * BindingContext = Pointer to a device context (LAN_ADAPTER)
814 * GeneralStatus = A general status code
815 * StatusBuffer = Pointer to a buffer with medium-specific data
816 * StatusBufferSize = Number of bytes in StatusBuffer
819 PLAN_ADAPTER Adapter
= BindingContext
;
820 PRECONFIGURE_CONTEXT Context
;
822 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
824 /* Ignore the status indication if we have no context yet. We'll get another later */
825 if (!Adapter
->Context
)
828 Context
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(RECONFIGURE_CONTEXT
), CONTEXT_TAG
);
832 Context
->Adapter
= Adapter
;
834 switch(GeneralStatus
)
836 case NDIS_STATUS_MEDIA_CONNECT
:
837 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
839 if (Adapter
->State
== LAN_STATE_STARTED
)
841 ExFreePoolWithTag(Context
, CONTEXT_TAG
);
845 Context
->State
= LAN_STATE_STARTED
;
848 case NDIS_STATUS_MEDIA_DISCONNECT
:
849 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
851 if (Adapter
->State
== LAN_STATE_STOPPED
)
853 ExFreePoolWithTag(Context
, CONTEXT_TAG
);
857 Context
->State
= LAN_STATE_STOPPED
;
860 case NDIS_STATUS_RESET_START
:
861 Adapter
->OldState
= Adapter
->State
;
862 Adapter
->State
= LAN_STATE_RESETTING
;
863 /* Nothing else to do here */
864 ExFreePoolWithTag(Context
, CONTEXT_TAG
);
867 case NDIS_STATUS_RESET_END
:
868 Adapter
->CompletingReset
= TRUE
;
869 Context
->State
= Adapter
->OldState
;
873 DbgPrint("Unhandled status: %x", GeneralStatus
);
874 ExFreePoolWithTag(Context
, CONTEXT_TAG
);
878 /* Queue the work item */
879 if (!ChewCreate(ReconfigureAdapterWorker
, Context
))
880 ExFreePoolWithTag(Context
, CONTEXT_TAG
);
883 VOID NTAPI
ProtocolStatusComplete(NDIS_HANDLE NdisBindingContext
)
885 * FUNCTION: Called by NDIS when a status-change has occurred
887 * BindingContext = Pointer to a device context (LAN_ADAPTER)
890 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
895 NDIS_HANDLE NdisBindingContext
,
896 PNET_PNP_EVENT PnPEvent
)
898 switch(PnPEvent
->NetEvent
)
900 case NetEventSetPower
:
901 DbgPrint("Device transitioned to power state %ld\n", PnPEvent
->Buffer
);
902 return NDIS_STATUS_SUCCESS
;
904 case NetEventQueryPower
:
905 DbgPrint("Device wants to go into power state %ld\n", PnPEvent
->Buffer
);
906 return NDIS_STATUS_SUCCESS
;
908 case NetEventQueryRemoveDevice
:
909 DbgPrint("Device is about to be removed\n");
910 return NDIS_STATUS_SUCCESS
;
912 case NetEventCancelRemoveDevice
:
913 DbgPrint("Device removal cancelled\n");
914 return NDIS_STATUS_SUCCESS
;
917 DbgPrint("Unhandled event type: %ld\n", PnPEvent
->NetEvent
);
918 return NDIS_STATUS_SUCCESS
;
922 VOID NTAPI
ProtocolBindAdapter(
923 OUT PNDIS_STATUS Status
,
924 IN NDIS_HANDLE BindContext
,
925 IN PNDIS_STRING DeviceName
,
926 IN PVOID SystemSpecific1
,
927 IN PVOID SystemSpecific2
)
929 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
930 * bindings, and periodically thereafer as new adapters come online
932 * Status: Return value to NDIS
933 * BindContext: Handle provided by NDIS to track pending binding operations
934 * DeviceName: Name of the miniport device to bind to
935 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
936 * SystemSpecific2: Unused & must not be touched
939 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
940 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
946 PNDIS_PACKET NdisPacket
,
951 * FUNCTION: Transmits a packet
953 * Context = Pointer to context information (LAN_ADAPTER)
954 * NdisPacket = Pointer to NDIS packet to send
955 * Offset = Offset in packet where data starts
956 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
957 * Type = LAN protocol type (LAN_PROTO_*)
960 NDIS_STATUS NdisStatus
;
964 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
966 PNDIS_PACKET XmitPacket
;
967 PIP_INTERFACE Interface
= Adapter
->Context
;
969 TI_DbgPrint(DEBUG_DATALINK
,
970 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
971 NdisPacket
, Offset
, Adapter
));
973 if (Adapter
->State
!= LAN_STATE_STARTED
) {
974 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
978 TI_DbgPrint(DEBUG_DATALINK
,
979 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
980 Adapter
->HWAddress
[0] & 0xff,
981 Adapter
->HWAddress
[1] & 0xff,
982 Adapter
->HWAddress
[2] & 0xff,
983 Adapter
->HWAddress
[3] & 0xff,
984 Adapter
->HWAddress
[4] & 0xff,
985 Adapter
->HWAddress
[5] & 0xff));
987 GetDataPtr( NdisPacket
, 0, &OldData
, &OldSize
);
989 NdisStatus
= AllocatePacketWithBuffer(&XmitPacket
, NULL
, OldSize
+ Adapter
->HeaderSize
);
990 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
991 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_RESOURCES
);
995 GetDataPtr(XmitPacket
, 0, &Data
, &Size
);
997 RtlCopyMemory(Data
+ Adapter
->HeaderSize
, OldData
, OldSize
);
999 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_SUCCESS
);
1001 switch (Adapter
->Media
) {
1002 case NdisMedium802_3
:
1003 EHeader
= (PETH_HEADER
)Data
;
1006 /* Unicast address */
1007 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
1009 /* Broadcast address */
1010 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
1013 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
1016 case LAN_PROTO_IPv4
:
1017 EHeader
->EType
= ETYPE_IPv4
;
1020 EHeader
->EType
= ETYPE_ARP
;
1022 case LAN_PROTO_IPv6
:
1023 EHeader
->EType
= ETYPE_IPv6
;
1032 /* FIXME: Support other medias */
1036 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
1040 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
1041 ((PCHAR
)LinkAddress
)[0] & 0xff,
1042 ((PCHAR
)LinkAddress
)[1] & 0xff,
1043 ((PCHAR
)LinkAddress
)[2] & 0xff,
1044 ((PCHAR
)LinkAddress
)[3] & 0xff,
1045 ((PCHAR
)LinkAddress
)[4] & 0xff,
1046 ((PCHAR
)LinkAddress
)[5] & 0xff));
1049 if (Adapter
->MTU
< Size
) {
1050 /* This is NOT a pointer. MSDN explicitly says so. */
1051 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket
,
1052 TcpLargeSendPacketInfo
) = (PVOID
)((ULONG_PTR
)Adapter
->MTU
);
1055 /* Update interface stats */
1056 Interface
->Stats
.OutBytes
+= Size
;
1058 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
1059 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
1060 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, XmitPacket
);
1061 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
1062 NdisStatus
== NDIS_STATUS_PENDING
?
1063 "Pending" : "Complete"));
1064 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
1066 /* I had a talk with vizzini: these really ought to be here.
1067 * we're supposed to see these completed by ndis *only* when
1068 * status_pending is returned. Note that this is different from
1069 * the situation with IRPs. */
1070 if (NdisStatus
!= NDIS_STATUS_PENDING
)
1071 ProtocolSendComplete((NDIS_HANDLE
)Context
, XmitPacket
, NdisStatus
);
1075 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
1076 OBJECT_ATTRIBUTES Attributes
;
1079 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
, 0, 0);
1080 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
1084 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
1085 PWCHAR RegistryValue
,
1086 PUNICODE_STRING String
) {
1087 UNICODE_STRING ValueName
;
1088 UNICODE_STRING UnicodeString
;
1092 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
1094 RtlInitUnicodeString(&ValueName
, RegistryValue
);
1096 ZwQueryValueKey(RegHandle
,
1098 KeyValuePartialInformation
,
1103 if (!NT_SUCCESS(Status
))
1105 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
1106 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
1108 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
1109 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
1110 UnicodeString
.MaximumLength
= Information
->DataLength
;
1113 (PWCHAR
)ExAllocatePoolWithTag( NonPagedPool
,
1114 UnicodeString
.MaximumLength
+ sizeof(WCHAR
), REG_STR_TAG
);
1116 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
1118 String
->MaximumLength
= UnicodeString
.MaximumLength
;
1119 RtlCopyUnicodeString( String
, &UnicodeString
);
1121 return STATUS_SUCCESS
;
1125 * Utility to copy and append two unicode strings.
1127 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
1128 * IN PUNICODE_STRING Second -> Second string to append
1129 * IN BOOL Deallocate -> TRUE: Deallocate First string before
1135 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
1136 PUNICODE_STRING Second
,
1137 BOOLEAN Deallocate
) {
1139 UNICODE_STRING Ustr
= *ResultFirst
;
1140 PWSTR new_string
= ExAllocatePoolWithTag
1142 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)), TEMP_STRING_TAG
);
1144 return STATUS_NO_MEMORY
;
1146 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
1147 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
1148 Second
->Buffer
, Second
->Length
);
1149 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
1150 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
1151 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
1152 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
1153 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
1154 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
1155 ExFreePoolWithTag(new_string
, TEMP_STRING_TAG
);
1159 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
1160 PUNICODE_STRING TargetKeyName
,
1161 PUNICODE_STRING Name
,
1162 PUNICODE_STRING DeviceDesc
) {
1163 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
1164 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
1165 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
1166 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
1169 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
1171 RtlInitUnicodeString(&BackSlash
, L
"\\");
1172 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
1174 RtlInitUnicodeString(&DescKeyName
, L
"");
1175 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
1176 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
1177 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
1179 RtlInitUnicodeString(&LinkageKeyName
, L
"");
1180 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
1181 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
1183 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
1184 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1186 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
1187 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1189 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
1190 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
1191 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1193 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
1194 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1196 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
1197 } else Status
= STATUS_UNSUCCESSFUL
;
1200 RtlFreeUnicodeString( &RootDevice
);
1201 RtlFreeUnicodeString( &LinkageKeyName
);
1202 RtlFreeUnicodeString( &DescKeyName
);
1203 if( LinkageKey
) ZwClose( LinkageKey
);
1204 if( DescKey
) ZwClose( DescKey
);
1206 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
1211 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
1212 PUNICODE_STRING DeviceDesc
) {
1213 UNICODE_STRING EnumKeyName
, TargetKeyName
;
1217 KEY_BASIC_INFORMATION
*Kbio
=
1218 ExAllocatePoolWithTag(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
), KBIO_TAG
);
1219 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
1221 RtlInitUnicodeString( DeviceDesc
, NULL
);
1223 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
1225 RtlInitUnicodeString
1226 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
1228 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
1230 if( !NT_SUCCESS(Status
) ) {
1231 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
1232 &EnumKeyName
, Status
));
1233 ExFreePoolWithTag( Kbio
, KBIO_TAG
);
1237 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
1238 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1239 Kbio
, KbioLength
, &ResultLength
);
1241 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
1242 ExFreePoolWithTag( Kbio
, KBIO_TAG
);
1243 KbioLength
= ResultLength
;
1244 Kbio
= ExAllocatePoolWithTag( NonPagedPool
, KbioLength
, KBIO_TAG
);
1246 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
1248 return STATUS_NO_MEMORY
;
1251 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1252 Kbio
, KbioLength
, &ResultLength
);
1254 if( !NT_SUCCESS(Status
) ) {
1255 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
1257 ExFreePoolWithTag( Kbio
, KBIO_TAG
);
1262 if( NT_SUCCESS(Status
) ) {
1263 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
1265 TargetKeyName
.Buffer
= Kbio
->Name
;
1267 Status
= CheckForDeviceDesc
1268 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
1269 if( NT_SUCCESS(Status
) ) {
1271 ExFreePoolWithTag( Kbio
, KBIO_TAG
);
1273 } else Status
= STATUS_SUCCESS
;
1278 ExFreePoolWithTag( Kbio
, KBIO_TAG
);
1279 return STATUS_UNSUCCESSFUL
;
1282 VOID
GetName( PUNICODE_STRING RegistryKey
,
1283 PUNICODE_STRING OutName
) {
1285 UNICODE_STRING PartialRegistryKey
;
1287 PartialRegistryKey
.Buffer
=
1288 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
1289 Ptr
= PartialRegistryKey
.Buffer
;
1291 while( *Ptr
!= L
'\\' &&
1292 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
1295 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
1296 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
1298 RtlInitUnicodeString( OutName
, L
"" );
1299 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
1302 BOOLEAN
BindAdapter(
1303 PLAN_ADAPTER Adapter
,
1304 PNDIS_STRING RegistryPath
)
1306 * FUNCTION: Binds a LAN adapter to IP layer
1308 * Adapter = Pointer to LAN_ADAPTER structure
1310 * We set the lookahead buffer size, set the packet filter and
1311 * bind the adapter to IP layer
1315 NDIS_STATUS NdisStatus
;
1316 LLIP_BIND_INFO BindInfo
;
1317 ULONG Lookahead
= LOOKAHEAD_SIZE
;
1319 NDIS_MEDIA_STATE MediaState
;
1321 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1323 Adapter
->State
= LAN_STATE_OPENING
;
1325 NdisStatus
= NDISCall(Adapter
,
1326 NdisRequestSetInformation
,
1327 OID_GEN_CURRENT_LOOKAHEAD
,
1330 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1331 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
1335 /* Bind the adapter to IP layer */
1336 BindInfo
.Context
= Adapter
;
1337 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
1338 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
1339 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
1340 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
1341 BindInfo
.Transmit
= LANTransmit
;
1343 IF
= IPCreateInterface(&BindInfo
);
1346 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1351 * Query per-adapter configuration from the registry
1352 * In case anyone is curious: there *is* an Ndis configuration api
1353 * for this sort of thing, but it doesn't really support things like
1354 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
1355 * protocol drivers developed for win2k and above just use the native
1356 * services (ZwOpenKey, etc).
1359 GetName( RegistryPath
, &IF
->Name
);
1361 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
1362 if (!NT_SUCCESS(Status
)) {
1363 TI_DbgPrint(MIN_TRACE
, ("Failed to get device description.\n"));
1364 IPDestroyInterface(IF
);
1368 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
1371 /* Get maximum link speed */
1372 NdisStatus
= NDISCall(Adapter
,
1373 NdisRequestQueryInformation
,
1378 if (!NT_SUCCESS(NdisStatus
))
1379 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
1381 Adapter
->Speed
= IF
->Speed
* 100L;
1383 /* Get maximum frame size */
1384 NdisStatus
= NDISCall(Adapter
,
1385 NdisRequestQueryInformation
,
1386 OID_GEN_MAXIMUM_FRAME_SIZE
,
1389 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1392 IF
->MTU
= Adapter
->MTU
;
1394 /* Get maximum packet size */
1395 NdisStatus
= NDISCall(Adapter
,
1396 NdisRequestQueryInformation
,
1397 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1398 &Adapter
->MaxPacketSize
,
1400 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1403 /* Register interface with IP layer */
1404 IPRegisterInterface(IF
);
1406 /* Store adapter context */
1407 Adapter
->Context
= IF
;
1409 /* Get the media state */
1410 NdisStatus
= NDISCall(Adapter
,
1411 NdisRequestQueryInformation
,
1412 OID_GEN_MEDIA_CONNECT_STATUS
,
1414 sizeof(MediaState
));
1415 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1416 TI_DbgPrint(DEBUG_DATALINK
, ("Could not query media status (0x%X).\n", NdisStatus
));
1417 IPUnregisterInterface(IF
);
1418 IPDestroyInterface(IF
);
1422 /* Indicate the current media state */
1423 ProtocolStatus(Adapter
,
1424 (MediaState
== NdisMediaStateConnected
) ? NDIS_STATUS_MEDIA_CONNECT
: NDIS_STATUS_MEDIA_DISCONNECT
,
1427 /* Set packet filter so we can send and receive packets */
1428 NdisStatus
= NDISCall(Adapter
,
1429 NdisRequestSetInformation
,
1430 OID_GEN_CURRENT_PACKET_FILTER
,
1431 &Adapter
->PacketFilter
,
1434 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1435 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
1436 IPUnregisterInterface(IF
);
1437 IPDestroyInterface(IF
);
1446 PLAN_ADAPTER Adapter
)
1448 * FUNCTION: Unbinds a LAN adapter from IP layer
1450 * Adapter = Pointer to LAN_ADAPTER structure
1453 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1455 if (Adapter
->State
== LAN_STATE_STARTED
) {
1456 PIP_INTERFACE IF
= Adapter
->Context
;
1458 IPUnregisterInterface(IF
);
1460 IPDestroyInterface(IF
);
1465 NDIS_STATUS
LANRegisterAdapter(
1466 PNDIS_STRING AdapterName
,
1467 PNDIS_STRING RegistryPath
)
1469 * FUNCTION: Registers protocol with an NDIS adapter
1471 * AdapterName = Pointer to string with name of adapter to register
1472 * Adapter = Address of pointer to a LAN_ADAPTER structure
1474 * Status of operation
1478 NDIS_STATUS NdisStatus
;
1479 NDIS_STATUS OpenStatus
;
1481 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1484 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1486 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_ADAPTER
), LAN_ADAPTER_TAG
);
1488 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1489 return NDIS_STATUS_RESOURCES
;
1492 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1494 /* Put adapter in stopped state */
1495 IF
->State
= LAN_STATE_STOPPED
;
1497 /* Initialize protecting spin lock */
1498 KeInitializeSpinLock(&IF
->Lock
);
1500 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1502 /* Initialize array with media IDs we support */
1503 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1505 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1506 /* Open the adapter. */
1507 NdisOpenAdapter(&NdisStatus
,
1519 /* Wait until the adapter is opened */
1520 if (NdisStatus
== NDIS_STATUS_PENDING
)
1521 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1522 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1523 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1524 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1528 IF
->Media
= MediaArray
[MediaIndex
];
1530 /* Fill LAN_ADAPTER structure with some adapter specific information */
1531 switch (IF
->Media
) {
1532 case NdisMedium802_3
:
1533 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1534 IF
->BCastMask
= BCAST_ETH_MASK
;
1535 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1536 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1537 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1538 IF
->MinFrameSize
= 60;
1539 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1541 NDIS_PACKET_TYPE_BROADCAST
|
1542 NDIS_PACKET_TYPE_DIRECTED
|
1543 NDIS_PACKET_TYPE_MULTICAST
;
1547 /* Unsupported media */
1548 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1549 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1550 return NDIS_STATUS_NOT_SUPPORTED
;
1553 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1554 NdisStatus
= NDISCall(IF
,
1555 NdisRequestQueryInformation
,
1556 OID_GEN_MAXIMUM_SEND_PACKETS
,
1557 &IF
->MaxSendPackets
,
1559 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1560 /* Legacy NIC drivers may not support this query, if it fails we
1561 assume it can send at least one packet per call to NdisSend(Packets) */
1562 IF
->MaxSendPackets
= 1;
1564 /* Get current hardware address */
1565 NdisStatus
= NDISCall(IF
,
1566 NdisRequestQueryInformation
,
1569 IF
->HWAddressLength
);
1570 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1571 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1572 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1576 /* Bind adapter to IP layer */
1577 if( !BindAdapter(IF
, RegistryPath
) ) {
1578 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1579 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1580 return NDIS_STATUS_NOT_ACCEPTED
;
1583 /* Add adapter to the adapter list */
1584 ExInterlockedInsertTailList(&AdapterListHead
,
1588 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1590 return NDIS_STATUS_SUCCESS
;
1594 NDIS_STATUS
LANUnregisterAdapter(
1595 PLAN_ADAPTER Adapter
)
1597 * FUNCTION: Unregisters protocol with NDIS adapter
1599 * Adapter = Pointer to a LAN_ADAPTER structure
1601 * Status of operation
1605 NDIS_HANDLE NdisHandle
;
1606 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1608 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1610 /* Unlink the adapter from the list */
1611 RemoveEntryList(&Adapter
->ListEntry
);
1613 /* Unbind adapter from IP layer */
1614 UnbindAdapter(Adapter
);
1616 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1617 NdisHandle
= Adapter
->NdisHandle
;
1619 Adapter
->NdisHandle
= NULL
;
1620 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1622 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1623 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1624 TcpipWaitForSingleObject(&Adapter
->Event
,
1629 NdisStatus
= Adapter
->NdisStatus
;
1632 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1634 FreeAdapter(Adapter
);
1641 LANUnregisterProtocol(VOID
)
1643 * FUNCTION: Unregisters this protocol driver with NDIS
1644 * NOTES: Does not care wether we are already registered
1647 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1649 if (ProtocolRegistered
) {
1650 NDIS_STATUS NdisStatus
;
1651 PLIST_ENTRY CurrentEntry
;
1652 PLIST_ENTRY NextEntry
;
1653 PLAN_ADAPTER Current
;
1656 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1658 /* Search the list and remove every adapter we find */
1659 CurrentEntry
= AdapterListHead
.Flink
;
1660 while (CurrentEntry
!= &AdapterListHead
) {
1661 NextEntry
= CurrentEntry
->Flink
;
1662 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1664 LANUnregisterAdapter(Current
);
1665 CurrentEntry
= NextEntry
;
1668 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1670 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1671 ProtocolRegistered
= FALSE
;
1677 ProtocolUnbindAdapter(
1678 PNDIS_STATUS Status
,
1679 NDIS_HANDLE ProtocolBindingContext
,
1680 NDIS_HANDLE UnbindContext
)
1682 /* We don't pend any unbinding so we can just ignore UnbindContext */
1683 *Status
= LANUnregisterAdapter((PLAN_ADAPTER
)ProtocolBindingContext
);
1686 NTSTATUS
LANRegisterProtocol(
1689 * FUNCTION: Registers this protocol driver with NDIS
1691 * Name = Name of this protocol driver
1693 * Status of operation
1696 NDIS_STATUS NdisStatus
;
1697 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1699 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1701 InitializeListHead(&AdapterListHead
);
1702 KeInitializeSpinLock(&AdapterListLock
);
1704 /* Set up protocol characteristics */
1705 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1706 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1707 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1708 ProtChars
.Name
.Length
= Name
->Length
;
1709 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1710 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1711 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1712 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1713 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1714 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1715 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1716 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1717 ProtChars
.ReceivePacketHandler
= ProtocolReceivePacket
;
1718 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1719 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1720 ProtChars
.StatusHandler
= ProtocolStatus
;
1721 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1722 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1723 ProtChars
.PnPEventHandler
= ProtocolPnPEvent
;
1724 ProtChars
.UnbindAdapterHandler
= ProtocolUnbindAdapter
;
1725 ProtChars
.UnloadHandler
= LANUnregisterProtocol
;
1727 /* Try to register protocol */
1728 NdisRegisterProtocol(&NdisStatus
,
1729 &NdisProtocolHandle
,
1731 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1732 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1734 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1735 return (NTSTATUS
)NdisStatus
;
1738 ProtocolRegistered
= TRUE
;
1740 return STATUS_SUCCESS
;