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 typedef struct _RECONFIGURE_CONTEXT
{
30 } RECONFIGURE_CONTEXT
, *PRECONFIGURE_CONTEXT
;
32 NDIS_HANDLE NdisProtocolHandle
= (NDIS_HANDLE
)NULL
;
33 BOOLEAN ProtocolRegistered
= FALSE
;
34 LIST_ENTRY AdapterListHead
;
35 KSPIN_LOCK AdapterListLock
;
39 NDIS_REQUEST_TYPE Type
,
44 * FUNCTION: Send a request to NDIS
46 * Adapter = Pointer to a LAN_ADAPTER structure
47 * Type = Type of request (Set or Query)
48 * OID = Value to be set/queried for
49 * Buffer = Pointer to a buffer to use
50 * Length = Number of bytes in Buffer
56 NDIS_STATUS NdisStatus
;
58 Request
.RequestType
= Type
;
59 if (Type
== NdisRequestSetInformation
) {
60 Request
.DATA
.SET_INFORMATION
.Oid
= OID
;
61 Request
.DATA
.SET_INFORMATION
.InformationBuffer
= Buffer
;
62 Request
.DATA
.SET_INFORMATION
.InformationBufferLength
= Length
;
64 Request
.DATA
.QUERY_INFORMATION
.Oid
= OID
;
65 Request
.DATA
.QUERY_INFORMATION
.InformationBuffer
= Buffer
;
66 Request
.DATA
.QUERY_INFORMATION
.InformationBufferLength
= Length
;
69 if (Adapter
->State
!= LAN_STATE_RESETTING
) {
70 NdisRequest(&NdisStatus
, Adapter
->NdisHandle
, &Request
);
72 NdisStatus
= NDIS_STATUS_NOT_ACCEPTED
;
75 /* Wait for NDIS to complete the request */
76 if (NdisStatus
== NDIS_STATUS_PENDING
) {
77 KeWaitForSingleObject(&Adapter
->Event
,
82 NdisStatus
= Adapter
->NdisStatus
;
88 /* Used by legacy ProtocolReceive for packet type */
90 GetPacketTypeFromHeaderBuffer(PLAN_ADAPTER Adapter
,
92 ULONG HeaderBufferSize
,
95 PETH_HEADER EthHeader
= HeaderBuffer
;
97 if (HeaderBufferSize
< Adapter
->HeaderSize
)
99 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", HeaderBufferSize
));
100 return NDIS_STATUS_NOT_ACCEPTED
;
103 switch (Adapter
->Media
)
105 case NdisMedium802_3
:
106 /* Ethernet and IEEE 802.3 frames can be destinguished by
107 looking at the IEEE 802.3 length field. This field is
108 less than or equal to 1500 for a valid IEEE 802.3 frame
109 and larger than 1500 is it's a valid EtherType value.
110 See RFC 1122, section 2.3.3 for more information */
112 *PacketType
= EthHeader
->EType
;
116 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
118 /* FIXME: Support other medias */
119 return NDIS_STATUS_NOT_ACCEPTED
;
122 TI_DbgPrint(DEBUG_DATALINK
, ("EtherType (0x%X).\n", *PacketType
));
124 return NDIS_STATUS_SUCCESS
;
127 /* Used by ProtocolReceivePacket for packet type */
129 GetPacketTypeFromNdisPacket(PLAN_ADAPTER Adapter
,
130 PNDIS_PACKET NdisPacket
,
137 HeaderBuffer
= ExAllocatePool(NonPagedPool
,
138 Adapter
->HeaderSize
);
140 return NDIS_STATUS_RESOURCES
;
142 /* Copy the media header */
143 BytesCopied
= CopyPacketToBuffer(HeaderBuffer
,
146 Adapter
->HeaderSize
);
147 if (BytesCopied
!= Adapter
->HeaderSize
)
150 ExFreePool(HeaderBuffer
);
151 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame (size %d).\n", BytesCopied
));
152 return NDIS_STATUS_NOT_ACCEPTED
;
155 Status
= GetPacketTypeFromHeaderBuffer(Adapter
,
160 ExFreePool(HeaderBuffer
);
167 PLAN_ADAPTER Adapter
)
169 * FUNCTION: Frees memory for a LAN_ADAPTER structure
171 * Adapter = Pointer to LAN_ADAPTER structure to free
174 ExFreePoolWithTag(Adapter
, LAN_ADAPTER_TAG
);
178 NTSTATUS TcpipLanGetDwordOid
179 ( PIP_INTERFACE Interface
,
182 /* Get maximum frame size */
183 if( Interface
->Context
) {
184 return NDISCall((PLAN_ADAPTER
)Interface
->Context
,
185 NdisRequestQueryInformation
,
189 } else switch( Oid
) { /* Loopback Case */
190 case OID_GEN_HARDWARE_STATUS
:
191 *Result
= NdisHardwareStatusReady
;
192 return STATUS_SUCCESS
;
193 case OID_GEN_MEDIA_CONNECT_STATUS
:
194 *Result
= NdisMediaStateConnected
;
195 return STATUS_SUCCESS
;
197 return STATUS_INVALID_PARAMETER
;
202 VOID NTAPI
ProtocolOpenAdapterComplete(
203 NDIS_HANDLE BindingContext
,
205 NDIS_STATUS OpenErrorStatus
)
207 * FUNCTION: Called by NDIS to complete opening of an adapter
209 * BindingContext = Pointer to a device context (LAN_ADAPTER)
210 * Status = Status of the operation
211 * OpenErrorStatus = Additional status information
214 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
216 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
218 Adapter
->NdisStatus
= Status
;
220 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
224 VOID NTAPI
ProtocolCloseAdapterComplete(
225 NDIS_HANDLE BindingContext
,
228 * FUNCTION: Called by NDIS to complete closing an adapter
230 * BindingContext = Pointer to a device context (LAN_ADAPTER)
231 * Status = Status of the operation
234 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
236 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
238 Adapter
->NdisStatus
= Status
;
240 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
244 VOID NTAPI
ProtocolResetComplete(
245 NDIS_HANDLE BindingContext
,
248 * FUNCTION: Called by NDIS to complete resetting an adapter
250 * BindingContext = Pointer to a device context (LAN_ADAPTER)
251 * Status = Status of the operation
254 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
256 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
258 Adapter
->NdisStatus
= Status
;
260 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
264 VOID NTAPI
ProtocolRequestComplete(
265 NDIS_HANDLE BindingContext
,
266 PNDIS_REQUEST NdisRequest
,
269 * FUNCTION: Called by NDIS to complete a request
271 * BindingContext = Pointer to a device context (LAN_ADAPTER)
272 * NdisRequest = Pointer to an object describing the request
273 * Status = Status of the operation
276 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
278 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
280 /* Save status of request and signal an event */
281 Adapter
->NdisStatus
= Status
;
283 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
287 VOID NTAPI
ProtocolSendComplete(
288 NDIS_HANDLE BindingContext
,
292 * FUNCTION: Called by NDIS to complete sending process
294 * BindingContext = Pointer to a device context (LAN_ADAPTER)
295 * Packet = Pointer to a packet descriptor
296 * Status = Status of the operation
299 FreeNdisPacket(Packet
);
302 VOID
LanReceiveWorker( PVOID Context
) {
304 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
306 PLAN_ADAPTER Adapter
;
307 UINT BytesTransferred
;
309 BOOLEAN LegacyReceive
;
310 PIP_INTERFACE Interface
;
312 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
314 Packet
= WorkItem
->Packet
;
315 Adapter
= WorkItem
->Adapter
;
316 BytesTransferred
= WorkItem
->BytesTransferred
;
317 LegacyReceive
= WorkItem
->LegacyReceive
;
319 ExFreePoolWithTag(WorkItem
, WQ_CONTEXT_TAG
);
321 Interface
= Adapter
->Context
;
323 IPInitializePacket(&IPPacket
, 0);
325 IPPacket
.NdisPacket
= Packet
;
326 IPPacket
.ReturnPacket
= !LegacyReceive
;
330 /* Packet type is precomputed */
331 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
333 /* Data is at position 0 */
334 IPPacket
.Position
= 0;
336 /* Packet size is determined by bytes transferred */
337 IPPacket
.TotalSize
= BytesTransferred
;
341 /* Determine packet type from media header */
342 if (GetPacketTypeFromNdisPacket(Adapter
,
344 &PacketType
) != NDIS_STATUS_SUCCESS
)
347 IPPacket
.Free(&IPPacket
);
351 /* Data is at the end of the media header */
352 IPPacket
.Position
= Adapter
->HeaderSize
;
354 /* Calculate packet size (excluding media header) */
355 NdisQueryPacketLength(IPPacket
.NdisPacket
, &IPPacket
.TotalSize
);
360 ("Ether Type = %x Total = %d\n",
361 PacketType
, IPPacket
.TotalSize
));
363 /* Update interface stats */
364 Interface
->Stats
.InBytes
+= IPPacket
.TotalSize
+ Adapter
->HeaderSize
;
366 /* NDIS packet is freed in all of these cases */
367 switch (PacketType
) {
370 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
371 IPReceive(Adapter
->Context
, &IPPacket
);
374 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
375 ARPReceive(Adapter
->Context
, &IPPacket
);
378 IPPacket
.Free(&IPPacket
);
383 VOID
LanSubmitReceiveWork(
384 NDIS_HANDLE BindingContext
,
386 UINT BytesTransferred
,
387 BOOLEAN LegacyReceive
) {
388 PLAN_WQ_ITEM WQItem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_WQ_ITEM
),
390 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
392 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
396 WQItem
->Packet
= Packet
;
397 WQItem
->Adapter
= Adapter
;
398 WQItem
->BytesTransferred
= BytesTransferred
;
399 WQItem
->LegacyReceive
= LegacyReceive
;
401 if (!ChewCreate( LanReceiveWorker
, WQItem
))
402 ExFreePoolWithTag(WQItem
, WQ_CONTEXT_TAG
);
405 VOID NTAPI
ProtocolTransferDataComplete(
406 NDIS_HANDLE BindingContext
,
409 UINT BytesTransferred
)
411 * FUNCTION: Called by NDIS to complete reception of data
413 * BindingContext = Pointer to a device context (LAN_ADAPTER)
414 * Packet = Pointer to a packet descriptor
415 * Status = Status of the operation
416 * BytesTransferred = Number of bytes transferred
418 * If the packet was successfully received, determine the protocol
419 * type and pass it to the correct receive handler
422 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
424 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
426 TransferDataCompleteCalled
++;
427 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
429 if( Status
!= NDIS_STATUS_SUCCESS
) return;
431 LanSubmitReceiveWork(BindingContext
,
437 INT NTAPI
ProtocolReceivePacket(
438 NDIS_HANDLE BindingContext
,
439 PNDIS_PACKET NdisPacket
)
441 PLAN_ADAPTER Adapter
= BindingContext
;
443 if (Adapter
->State
!= LAN_STATE_STARTED
) {
444 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
448 LanSubmitReceiveWork(BindingContext
,
453 /* Hold 1 reference on this packet */
457 NDIS_STATUS NTAPI
ProtocolReceive(
458 NDIS_HANDLE BindingContext
,
459 NDIS_HANDLE MacReceiveContext
,
461 UINT HeaderBufferSize
,
462 PVOID LookaheadBuffer
,
463 UINT LookaheadBufferSize
,
466 * FUNCTION: Called by NDIS when a packet has been received on the physical link
468 * BindingContext = Pointer to a device context (LAN_ADAPTER)
469 * MacReceiveContext = Handle used by underlying NIC driver
470 * HeaderBuffer = Pointer to a buffer containing the packet header
471 * HeaderBufferSize = Number of bytes in HeaderBuffer
472 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
473 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
474 * PacketSize = Overall size of the packet (not including header)
476 * Status of operation
480 UINT BytesTransferred
;
482 NDIS_STATUS NdisStatus
;
483 PNDIS_PACKET NdisPacket
;
484 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
486 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
488 if (Adapter
->State
!= LAN_STATE_STARTED
) {
489 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
490 return NDIS_STATUS_NOT_ACCEPTED
;
493 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
494 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
495 return NDIS_STATUS_NOT_ACCEPTED
;
498 NdisStatus
= GetPacketTypeFromHeaderBuffer(Adapter
,
502 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
503 return NDIS_STATUS_NOT_ACCEPTED
;
505 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
506 Adapter
, Adapter
->MTU
));
508 /* Get a transfer data packet */
509 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
511 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
512 return NDIS_STATUS_NOT_ACCEPTED
;
515 PC(NdisPacket
)->PacketType
= PacketType
;
517 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
519 GetDataPtr( NdisPacket
, 0, &BufferData
, &PacketSize
);
521 TransferDataCalled
++;
523 if (LookaheadBufferSize
== PacketSize
)
525 /* Optimized code path for packets that are fully contained in
526 * the lookahead buffer. */
527 NdisCopyLookaheadData(BufferData
,
530 Adapter
->MacOptions
);
534 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
535 MacReceiveContext
, 0, PacketSize
,
536 NdisPacket
, &BytesTransferred
);
538 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
540 if (NdisStatus
!= NDIS_STATUS_PENDING
)
541 ProtocolTransferDataComplete(BindingContext
,
546 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
548 return NDIS_STATUS_SUCCESS
;
552 VOID NTAPI
ProtocolReceiveComplete(
553 NDIS_HANDLE BindingContext
)
555 * FUNCTION: Called by NDIS when we're done receiving data
557 * BindingContext = Pointer to a device context (LAN_ADAPTER)
560 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
563 BOOLEAN
ReadIpConfiguration(PIP_INTERFACE Interface
)
565 OBJECT_ATTRIBUTES ObjectAttributes
;
566 HANDLE ParameterHandle
;
567 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo
;
569 UNICODE_STRING IPAddress
= RTL_CONSTANT_STRING(L
"IPAddress");
570 UNICODE_STRING Netmask
= RTL_CONSTANT_STRING(L
"SubnetMask");
571 UNICODE_STRING Gateway
= RTL_CONSTANT_STRING(L
"DefaultGateway");
572 UNICODE_STRING EnableDhcp
= RTL_CONSTANT_STRING(L
"EnableDHCP");
573 UNICODE_STRING Prefix
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
574 UNICODE_STRING TcpipRegistryPath
;
575 UNICODE_STRING RegistryDataU
;
576 ANSI_STRING RegistryDataA
;
579 IP_ADDRESS DefaultMask
, Router
;
581 AddrInitIPv4(&DefaultMask
, 0);
583 TcpipRegistryPath
.MaximumLength
= sizeof(WCHAR
) * 150;
584 TcpipRegistryPath
.Length
= 0;
585 TcpipRegistryPath
.Buffer
= Buffer
;
587 /* Build the registry path */
588 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Prefix
);
589 RtlAppendUnicodeStringToString(&TcpipRegistryPath
, &Interface
->Name
);
591 InitializeObjectAttributes(&ObjectAttributes
,
593 OBJ_CASE_INSENSITIVE
,
597 /* Open a handle to the adapter parameters */
598 Status
= ZwOpenKey(&ParameterHandle
, KEY_READ
, &ObjectAttributes
);
600 if (!NT_SUCCESS(Status
))
606 KeyValueInfo
= ExAllocatePool(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
));
609 ZwClose(ParameterHandle
);
613 /* Read the EnableDHCP entry */
614 Status
= ZwQueryValueKey(ParameterHandle
,
616 KeyValuePartialInformation
,
618 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
),
620 if (NT_SUCCESS(Status
) && KeyValueInfo
->DataLength
== sizeof(ULONG
) && (*(PULONG
)KeyValueInfo
->Data
) == 0)
622 RegistryDataU
.MaximumLength
= 16 + sizeof(WCHAR
);
623 RegistryDataU
.Buffer
= (PWCHAR
)KeyValueInfo
->Data
;
625 /* Read the IP address */
626 Status
= ZwQueryValueKey(ParameterHandle
,
628 KeyValuePartialInformation
,
630 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
632 if (NT_SUCCESS(Status
))
634 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
636 RtlUnicodeStringToAnsiString(&RegistryDataA
,
640 AddrInitIPv4(&Interface
->Unicast
, inet_addr(RegistryDataA
.Buffer
));
642 RtlFreeAnsiString(&RegistryDataA
);
645 Status
= ZwQueryValueKey(ParameterHandle
,
647 KeyValuePartialInformation
,
649 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
651 if (NT_SUCCESS(Status
))
653 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
655 RtlUnicodeStringToAnsiString(&RegistryDataA
,
659 AddrInitIPv4(&Interface
->Netmask
, inet_addr(RegistryDataA
.Buffer
));
661 RtlFreeAnsiString(&RegistryDataA
);
664 /* We have to wait until both IP address and subnet mask
665 * are read to add the interface route, but we must do it
666 * before we add the default gateway */
667 if (!AddrIsUnspecified(&Interface
->Unicast
) &&
668 !AddrIsUnspecified(&Interface
->Netmask
))
669 IPAddInterfaceRoute(Interface
);
671 /* Read default gateway info */
672 Status
= ZwQueryValueKey(ParameterHandle
,
674 KeyValuePartialInformation
,
676 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
678 if (NT_SUCCESS(Status
))
680 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
682 RtlUnicodeStringToAnsiString(&RegistryDataA
,
686 AddrInitIPv4(&Router
, inet_addr(RegistryDataA
.Buffer
));
688 if (!AddrIsUnspecified(&Router
))
689 RouterCreateRoute(&DefaultMask
, &DefaultMask
, &Router
, Interface
, 1);
691 RtlFreeAnsiString(&RegistryDataA
);
695 ZwClose(ParameterHandle
);
701 BOOLEAN
ReconfigureAdapter(PRECONFIGURE_CONTEXT Context
)
703 PLAN_ADAPTER Adapter
= Context
->Adapter
;
704 PIP_INTERFACE Interface
= Adapter
->Context
;
705 //NDIS_STATUS NdisStatus;
706 IP_ADDRESS DefaultMask
;
708 /* Initalize the default unspecified address (0.0.0.0) */
709 AddrInitIPv4(&DefaultMask
, 0);
710 if (Context
->State
== LAN_STATE_STARTED
&&
711 !Context
->Adapter
->CompletingReset
)
713 /* Read the IP configuration */
714 ReadIpConfiguration(Interface
);
716 /* Compute the broadcast address */
717 Interface
->Broadcast
.Type
= IP_ADDRESS_V4
;
718 Interface
->Broadcast
.Address
.IPv4Address
= Interface
->Unicast
.Address
.IPv4Address
|
719 ~Interface
->Netmask
.Address
.IPv4Address
;
721 else if (!Context
->Adapter
->CompletingReset
)
723 /* Clear IP configuration */
724 Interface
->Unicast
= DefaultMask
;
725 Interface
->Netmask
= DefaultMask
;
726 Interface
->Broadcast
= DefaultMask
;
728 /* Remove all interface routes */
729 RouterRemoveRoutesForInterface(Interface
);
731 /* Destroy all cached neighbors */
732 NBDestroyNeighborsForInterface(Interface
);
735 Context
->Adapter
->CompletingReset
= FALSE
;
737 /* We're done here if the adapter isn't connected */
738 if (Context
->State
!= LAN_STATE_STARTED
)
740 Adapter
->State
= Context
->State
;
746 /* Get maximum link speed */
747 NdisStatus
= NDISCall(Adapter
,
748 NdisRequestQueryInformation
,
753 if (!NT_SUCCESS(NdisStatus
))
754 Interface
->Speed
= IP_DEFAULT_LINK_SPEED
;
756 Adapter
->Speed
= Interface
->Speed
* 100L;
758 /* Get maximum frame size */
759 NdisStatus
= NDISCall(Adapter
,
760 NdisRequestQueryInformation
,
761 OID_GEN_MAXIMUM_FRAME_SIZE
,
764 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
767 Interface
->MTU
= Adapter
->MTU
;
769 /* Get maximum packet size */
770 NdisStatus
= NDISCall(Adapter
,
771 NdisRequestQueryInformation
,
772 OID_GEN_MAXIMUM_TOTAL_SIZE
,
773 &Adapter
->MaxPacketSize
,
775 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
779 Adapter
->State
= Context
->State
;
784 VOID
ReconfigureAdapterWorker(PVOID Context
)
786 PRECONFIGURE_CONTEXT ReconfigureContext
= Context
;
788 /* Complete the reconfiguration asynchronously */
789 ReconfigureAdapter(ReconfigureContext
);
791 /* Free the context */
792 ExFreePool(ReconfigureContext
);
795 VOID NTAPI
ProtocolStatus(
796 NDIS_HANDLE BindingContext
,
797 NDIS_STATUS GeneralStatus
,
799 UINT StatusBufferSize
)
801 * FUNCTION: Called by NDIS when the underlying driver has changed state
803 * BindingContext = Pointer to a device context (LAN_ADAPTER)
804 * GeneralStatus = A general status code
805 * StatusBuffer = Pointer to a buffer with medium-specific data
806 * StatusBufferSize = Number of bytes in StatusBuffer
809 PLAN_ADAPTER Adapter
= BindingContext
;
810 PRECONFIGURE_CONTEXT Context
;
812 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
814 Context
= ExAllocatePool(NonPagedPool
, sizeof(RECONFIGURE_CONTEXT
));
818 Context
->Adapter
= Adapter
;
820 switch(GeneralStatus
)
822 case NDIS_STATUS_MEDIA_CONNECT
:
823 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
825 if (Adapter
->State
== LAN_STATE_STARTED
)
831 Context
->State
= LAN_STATE_STARTED
;
834 case NDIS_STATUS_MEDIA_DISCONNECT
:
835 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
837 if (Adapter
->State
== LAN_STATE_STOPPED
)
843 Context
->State
= LAN_STATE_STOPPED
;
846 case NDIS_STATUS_RESET_START
:
847 Adapter
->OldState
= Adapter
->State
;
848 Adapter
->State
= LAN_STATE_RESETTING
;
849 /* Nothing else to do here */
853 case NDIS_STATUS_RESET_END
:
854 Adapter
->CompletingReset
= TRUE
;
855 Context
->State
= Adapter
->OldState
;
859 DbgPrint("Unhandled status: %x", GeneralStatus
);
864 /* Queue the work item */
865 if (!ChewCreate(ReconfigureAdapterWorker
, Context
))
869 VOID NTAPI
ProtocolStatusComplete(NDIS_HANDLE NdisBindingContext
)
871 * FUNCTION: Called by NDIS when a status-change has occurred
873 * BindingContext = Pointer to a device context (LAN_ADAPTER)
876 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
881 NDIS_HANDLE NdisBindingContext
,
882 PNET_PNP_EVENT PnPEvent
)
884 switch(PnPEvent
->NetEvent
)
886 case NetEventSetPower
:
887 DbgPrint("Device transitioned to power state %ld\n", PnPEvent
->Buffer
);
888 return NDIS_STATUS_SUCCESS
;
890 case NetEventQueryPower
:
891 DbgPrint("Device wants to go into power state %ld\n", PnPEvent
->Buffer
);
892 return NDIS_STATUS_SUCCESS
;
894 case NetEventQueryRemoveDevice
:
895 DbgPrint("Device is about to be removed\n");
896 return NDIS_STATUS_SUCCESS
;
898 case NetEventCancelRemoveDevice
:
899 DbgPrint("Device removal cancelled\n");
900 return NDIS_STATUS_SUCCESS
;
903 DbgPrint("Unhandled event type: %ld\n", PnPEvent
->NetEvent
);
904 return NDIS_STATUS_SUCCESS
;
908 VOID NTAPI
ProtocolBindAdapter(
909 OUT PNDIS_STATUS Status
,
910 IN NDIS_HANDLE BindContext
,
911 IN PNDIS_STRING DeviceName
,
912 IN PVOID SystemSpecific1
,
913 IN PVOID SystemSpecific2
)
915 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
916 * bindings, and periodically thereafer as new adapters come online
918 * Status: Return value to NDIS
919 * BindContext: Handle provided by NDIS to track pending binding operations
920 * DeviceName: Name of the miniport device to bind to
921 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
922 * SystemSpecific2: Unused & must not be touched
925 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
926 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
932 PNDIS_PACKET NdisPacket
,
937 * FUNCTION: Transmits a packet
939 * Context = Pointer to context information (LAN_ADAPTER)
940 * NdisPacket = Pointer to NDIS packet to send
941 * Offset = Offset in packet where data starts
942 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
943 * Type = LAN protocol type (LAN_PROTO_*)
946 NDIS_STATUS NdisStatus
;
950 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
952 PNDIS_PACKET XmitPacket
;
953 PIP_INTERFACE Interface
= Adapter
->Context
;
955 TI_DbgPrint(DEBUG_DATALINK
,
956 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
957 NdisPacket
, Offset
, Adapter
));
959 if (Adapter
->State
!= LAN_STATE_STARTED
) {
960 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
964 TI_DbgPrint(DEBUG_DATALINK
,
965 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
966 Adapter
->HWAddress
[0] & 0xff,
967 Adapter
->HWAddress
[1] & 0xff,
968 Adapter
->HWAddress
[2] & 0xff,
969 Adapter
->HWAddress
[3] & 0xff,
970 Adapter
->HWAddress
[4] & 0xff,
971 Adapter
->HWAddress
[5] & 0xff));
973 GetDataPtr( NdisPacket
, 0, &OldData
, &OldSize
);
975 NdisStatus
= AllocatePacketWithBuffer(&XmitPacket
, NULL
, OldSize
+ Adapter
->HeaderSize
);
976 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
977 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_RESOURCES
);
981 GetDataPtr(XmitPacket
, 0, &Data
, &Size
);
983 RtlCopyMemory(Data
+ Adapter
->HeaderSize
, OldData
, OldSize
);
985 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_SUCCESS
);
987 switch (Adapter
->Media
) {
988 case NdisMedium802_3
:
989 EHeader
= (PETH_HEADER
)Data
;
992 /* Unicast address */
993 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
995 /* Broadcast address */
996 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
999 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
1002 case LAN_PROTO_IPv4
:
1003 EHeader
->EType
= ETYPE_IPv4
;
1006 EHeader
->EType
= ETYPE_ARP
;
1008 case LAN_PROTO_IPv6
:
1009 EHeader
->EType
= ETYPE_IPv6
;
1018 /* FIXME: Support other medias */
1022 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
1026 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
1027 ((PCHAR
)LinkAddress
)[0] & 0xff,
1028 ((PCHAR
)LinkAddress
)[1] & 0xff,
1029 ((PCHAR
)LinkAddress
)[2] & 0xff,
1030 ((PCHAR
)LinkAddress
)[3] & 0xff,
1031 ((PCHAR
)LinkAddress
)[4] & 0xff,
1032 ((PCHAR
)LinkAddress
)[5] & 0xff));
1035 if (Adapter
->MTU
< Size
) {
1036 /* This is NOT a pointer. MSDN explicitly says so. */
1037 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket
,
1038 TcpLargeSendPacketInfo
) = (PVOID
)((ULONG_PTR
)Adapter
->MTU
);
1041 /* Update interface stats */
1042 Interface
->Stats
.OutBytes
+= Size
;
1044 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
1045 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
1046 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, XmitPacket
);
1047 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
1048 NdisStatus
== NDIS_STATUS_PENDING
?
1049 "Pending" : "Complete"));
1050 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
1052 /* I had a talk with vizzini: these really ought to be here.
1053 * we're supposed to see these completed by ndis *only* when
1054 * status_pending is returned. Note that this is different from
1055 * the situation with IRPs. */
1056 if (NdisStatus
!= NDIS_STATUS_PENDING
)
1057 ProtocolSendComplete((NDIS_HANDLE
)Context
, XmitPacket
, NdisStatus
);
1061 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
1062 OBJECT_ATTRIBUTES Attributes
;
1065 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, 0, 0);
1066 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
1070 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
1071 PWCHAR RegistryValue
,
1072 PUNICODE_STRING String
) {
1073 UNICODE_STRING ValueName
;
1074 UNICODE_STRING UnicodeString
;
1078 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
1080 RtlInitUnicodeString(&ValueName
, RegistryValue
);
1082 ZwQueryValueKey(RegHandle
,
1084 KeyValuePartialInformation
,
1089 if (!NT_SUCCESS(Status
))
1091 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
1092 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
1094 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
1095 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
1096 UnicodeString
.MaximumLength
= Information
->DataLength
;
1099 (PWCHAR
)ExAllocatePool( NonPagedPool
,
1100 UnicodeString
.MaximumLength
+ sizeof(WCHAR
) );
1102 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
1104 String
->MaximumLength
= UnicodeString
.MaximumLength
;
1105 RtlCopyUnicodeString( String
, &UnicodeString
);
1107 return STATUS_SUCCESS
;
1111 * Utility to copy and append two unicode strings.
1113 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
1114 * IN PUNICODE_STRING Second -> Second string to append
1115 * IN BOOL Deallocate -> TRUE: Deallocate First string before
1121 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
1122 PUNICODE_STRING Second
,
1123 BOOLEAN Deallocate
) {
1125 UNICODE_STRING Ustr
= *ResultFirst
;
1126 PWSTR new_string
= ExAllocatePool
1128 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)));
1130 return STATUS_NO_MEMORY
;
1132 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
1133 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
1134 Second
->Buffer
, Second
->Length
);
1135 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
1136 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
1137 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
1138 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
1139 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
1140 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
1141 ExFreePool(new_string
);
1145 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
1146 PUNICODE_STRING TargetKeyName
,
1147 PUNICODE_STRING Name
,
1148 PUNICODE_STRING DeviceDesc
) {
1149 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
1150 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
1151 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
1152 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
1155 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
1157 RtlInitUnicodeString(&BackSlash
, L
"\\");
1158 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
1160 RtlInitUnicodeString(&DescKeyName
, L
"");
1161 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
1162 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
1163 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
1165 RtlInitUnicodeString(&LinkageKeyName
, L
"");
1166 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
1167 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
1169 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
1170 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1172 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
1173 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1175 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
1176 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
1177 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1179 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
1180 if( !NT_SUCCESS(Status
) ) goto cleanup
;
1182 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
1183 } else Status
= STATUS_UNSUCCESSFUL
;
1186 RtlFreeUnicodeString( &RootDevice
);
1187 RtlFreeUnicodeString( &LinkageKeyName
);
1188 RtlFreeUnicodeString( &DescKeyName
);
1189 if( LinkageKey
) NtClose( LinkageKey
);
1190 if( DescKey
) NtClose( DescKey
);
1192 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
1197 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
1198 PUNICODE_STRING DeviceDesc
) {
1199 UNICODE_STRING EnumKeyName
, TargetKeyName
;
1203 KEY_BASIC_INFORMATION
*Kbio
=
1204 ExAllocatePool(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
));
1205 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
1207 RtlInitUnicodeString( DeviceDesc
, NULL
);
1209 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
1211 RtlInitUnicodeString
1212 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
1214 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
1216 if( !NT_SUCCESS(Status
) ) {
1217 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
1218 &EnumKeyName
, Status
));
1223 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
1224 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1225 Kbio
, KbioLength
, &ResultLength
);
1227 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
1229 KbioLength
= ResultLength
;
1230 Kbio
= ExAllocatePool( NonPagedPool
, KbioLength
);
1232 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
1234 return STATUS_NO_MEMORY
;
1237 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
1238 Kbio
, KbioLength
, &ResultLength
);
1240 if( !NT_SUCCESS(Status
) ) {
1241 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
1248 if( NT_SUCCESS(Status
) ) {
1249 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
1251 TargetKeyName
.Buffer
= Kbio
->Name
;
1253 Status
= CheckForDeviceDesc
1254 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
1255 if( NT_SUCCESS(Status
) ) {
1259 } else Status
= STATUS_SUCCESS
;
1265 return STATUS_UNSUCCESSFUL
;
1268 VOID
GetName( PUNICODE_STRING RegistryKey
,
1269 PUNICODE_STRING OutName
) {
1271 UNICODE_STRING PartialRegistryKey
;
1273 PartialRegistryKey
.Buffer
=
1274 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
1275 Ptr
= PartialRegistryKey
.Buffer
;
1277 while( *Ptr
!= L
'\\' &&
1278 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
1281 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
1282 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
1284 RtlInitUnicodeString( OutName
, L
"" );
1285 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
1288 BOOLEAN
BindAdapter(
1289 PLAN_ADAPTER Adapter
,
1290 PNDIS_STRING RegistryPath
)
1292 * FUNCTION: Binds a LAN adapter to IP layer
1294 * Adapter = Pointer to LAN_ADAPTER structure
1296 * We set the lookahead buffer size, set the packet filter and
1297 * bind the adapter to IP layer
1301 NDIS_STATUS NdisStatus
;
1302 LLIP_BIND_INFO BindInfo
;
1303 ULONG Lookahead
= LOOKAHEAD_SIZE
;
1306 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1308 Adapter
->State
= LAN_STATE_OPENING
;
1310 NdisStatus
= NDISCall(Adapter
,
1311 NdisRequestSetInformation
,
1312 OID_GEN_CURRENT_LOOKAHEAD
,
1315 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1316 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
1320 /* Bind the adapter to IP layer */
1321 BindInfo
.Context
= Adapter
;
1322 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
1323 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
1324 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
1325 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
1326 BindInfo
.Transmit
= LANTransmit
;
1328 IF
= IPCreateInterface(&BindInfo
);
1331 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1336 * Query per-adapter configuration from the registry
1337 * In case anyone is curious: there *is* an Ndis configuration api
1338 * for this sort of thing, but it doesn't really support things like
1339 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
1340 * protocol drivers developed for win2k and above just use the native
1341 * services (ZwOpenKey, etc).
1344 GetName( RegistryPath
, &IF
->Name
);
1346 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
1347 if (!NT_SUCCESS(Status
)) {
1348 TI_DbgPrint(MIN_TRACE
, ("Failed to get device description.\n"));
1349 IPDestroyInterface(IF
);
1353 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
1356 /* Get maximum link speed */
1357 NdisStatus
= NDISCall(Adapter
,
1358 NdisRequestQueryInformation
,
1363 if (!NT_SUCCESS(NdisStatus
))
1364 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
1366 Adapter
->Speed
= IF
->Speed
* 100L;
1368 /* Get maximum frame size */
1369 NdisStatus
= NDISCall(Adapter
,
1370 NdisRequestQueryInformation
,
1371 OID_GEN_MAXIMUM_FRAME_SIZE
,
1374 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1377 IF
->MTU
= Adapter
->MTU
;
1379 /* Get maximum packet size */
1380 NdisStatus
= NDISCall(Adapter
,
1381 NdisRequestQueryInformation
,
1382 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1383 &Adapter
->MaxPacketSize
,
1385 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1388 /* Register interface with IP layer */
1389 IPRegisterInterface(IF
);
1391 /* Set adapter state */
1392 Adapter
->Context
= IF
;
1394 /* Set packet filter so we can send and receive packets */
1395 NdisStatus
= NDISCall(Adapter
,
1396 NdisRequestSetInformation
,
1397 OID_GEN_CURRENT_PACKET_FILTER
,
1398 &Adapter
->PacketFilter
,
1401 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1402 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
1403 IPUnregisterInterface(IF
);
1404 IPDestroyInterface(IF
);
1408 /* Indicate media connect (our drivers are broken and don't do this) */
1409 ProtocolStatus(Adapter
, NDIS_STATUS_MEDIA_CONNECT
, NULL
, 0);
1416 PLAN_ADAPTER Adapter
)
1418 * FUNCTION: Unbinds a LAN adapter from IP layer
1420 * Adapter = Pointer to LAN_ADAPTER structure
1423 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1425 if (Adapter
->State
== LAN_STATE_STARTED
) {
1426 PIP_INTERFACE IF
= Adapter
->Context
;
1428 IPUnregisterInterface(IF
);
1430 IPDestroyInterface(IF
);
1435 NDIS_STATUS
LANRegisterAdapter(
1436 PNDIS_STRING AdapterName
,
1437 PNDIS_STRING RegistryPath
)
1439 * FUNCTION: Registers protocol with an NDIS adapter
1441 * AdapterName = Pointer to string with name of adapter to register
1442 * Adapter = Address of pointer to a LAN_ADAPTER structure
1444 * Status of operation
1448 NDIS_STATUS NdisStatus
;
1449 NDIS_STATUS OpenStatus
;
1451 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1454 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1456 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_ADAPTER
), LAN_ADAPTER_TAG
);
1458 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1459 return NDIS_STATUS_RESOURCES
;
1462 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1464 /* Put adapter in stopped state */
1465 IF
->State
= LAN_STATE_STOPPED
;
1467 /* Initialize protecting spin lock */
1468 KeInitializeSpinLock(&IF
->Lock
);
1470 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1472 /* Initialize array with media IDs we support */
1473 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1475 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1476 /* Open the adapter. */
1477 NdisOpenAdapter(&NdisStatus
,
1489 /* Wait until the adapter is opened */
1490 if (NdisStatus
== NDIS_STATUS_PENDING
)
1491 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1492 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1493 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1494 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1498 IF
->Media
= MediaArray
[MediaIndex
];
1500 /* Fill LAN_ADAPTER structure with some adapter specific information */
1501 switch (IF
->Media
) {
1502 case NdisMedium802_3
:
1503 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1504 IF
->BCastMask
= BCAST_ETH_MASK
;
1505 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1506 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1507 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1508 IF
->MinFrameSize
= 60;
1509 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1511 NDIS_PACKET_TYPE_BROADCAST
|
1512 NDIS_PACKET_TYPE_DIRECTED
|
1513 NDIS_PACKET_TYPE_MULTICAST
;
1517 /* Unsupported media */
1518 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1519 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1520 return NDIS_STATUS_NOT_SUPPORTED
;
1523 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1524 NdisStatus
= NDISCall(IF
,
1525 NdisRequestQueryInformation
,
1526 OID_GEN_MAXIMUM_SEND_PACKETS
,
1527 &IF
->MaxSendPackets
,
1529 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1530 /* Legacy NIC drivers may not support this query, if it fails we
1531 assume it can send at least one packet per call to NdisSend(Packets) */
1532 IF
->MaxSendPackets
= 1;
1534 /* Get current hardware address */
1535 NdisStatus
= NDISCall(IF
,
1536 NdisRequestQueryInformation
,
1539 IF
->HWAddressLength
);
1540 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1541 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1542 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1546 /* Bind adapter to IP layer */
1547 if( !BindAdapter(IF
, RegistryPath
) ) {
1548 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1549 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1550 return NDIS_STATUS_NOT_ACCEPTED
;
1553 /* Add adapter to the adapter list */
1554 ExInterlockedInsertTailList(&AdapterListHead
,
1558 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1560 return NDIS_STATUS_SUCCESS
;
1564 NDIS_STATUS
LANUnregisterAdapter(
1565 PLAN_ADAPTER Adapter
)
1567 * FUNCTION: Unregisters protocol with NDIS adapter
1569 * Adapter = Pointer to a LAN_ADAPTER structure
1571 * Status of operation
1575 NDIS_HANDLE NdisHandle
;
1576 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1578 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1580 /* Unlink the adapter from the list */
1581 RemoveEntryList(&Adapter
->ListEntry
);
1583 /* Unbind adapter from IP layer */
1584 UnbindAdapter(Adapter
);
1586 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1587 NdisHandle
= Adapter
->NdisHandle
;
1589 Adapter
->NdisHandle
= NULL
;
1590 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1592 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1593 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1594 TcpipWaitForSingleObject(&Adapter
->Event
,
1599 NdisStatus
= Adapter
->NdisStatus
;
1602 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1604 FreeAdapter(Adapter
);
1611 LANUnregisterProtocol(VOID
)
1613 * FUNCTION: Unregisters this protocol driver with NDIS
1614 * NOTES: Does not care wether we are already registered
1617 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1619 if (ProtocolRegistered
) {
1620 NDIS_STATUS NdisStatus
;
1621 PLIST_ENTRY CurrentEntry
;
1622 PLIST_ENTRY NextEntry
;
1623 PLAN_ADAPTER Current
;
1626 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1628 /* Search the list and remove every adapter we find */
1629 CurrentEntry
= AdapterListHead
.Flink
;
1630 while (CurrentEntry
!= &AdapterListHead
) {
1631 NextEntry
= CurrentEntry
->Flink
;
1632 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1634 LANUnregisterAdapter(Current
);
1635 CurrentEntry
= NextEntry
;
1638 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1640 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1641 ProtocolRegistered
= FALSE
;
1647 ProtocolUnbindAdapter(
1648 PNDIS_STATUS Status
,
1649 NDIS_HANDLE ProtocolBindingContext
,
1650 NDIS_HANDLE UnbindContext
)
1652 /* We don't pend any unbinding so we can just ignore UnbindContext */
1653 *Status
= LANUnregisterAdapter((PLAN_ADAPTER
)ProtocolBindingContext
);
1656 NTSTATUS
LANRegisterProtocol(
1659 * FUNCTION: Registers this protocol driver with NDIS
1661 * Name = Name of this protocol driver
1663 * Status of operation
1666 NDIS_STATUS NdisStatus
;
1667 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1669 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1671 InitializeListHead(&AdapterListHead
);
1672 KeInitializeSpinLock(&AdapterListLock
);
1674 /* Set up protocol characteristics */
1675 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1676 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1677 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1678 ProtChars
.Name
.Length
= Name
->Length
;
1679 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1680 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1681 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1682 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1683 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1684 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1685 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1686 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1687 ProtChars
.ReceivePacketHandler
= ProtocolReceivePacket
;
1688 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1689 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1690 ProtChars
.StatusHandler
= ProtocolStatus
;
1691 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1692 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1693 ProtChars
.PnPEventHandler
= ProtocolPnPEvent
;
1694 ProtChars
.UnbindAdapterHandler
= ProtocolUnbindAdapter
;
1695 ProtChars
.UnloadHandler
= LANUnregisterProtocol
;
1697 /* Try to register protocol */
1698 NdisRegisterProtocol(&NdisStatus
,
1699 &NdisProtocolHandle
,
1701 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1702 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1704 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1705 return (NTSTATUS
)NdisStatus
;
1708 ProtocolRegistered
= TRUE
;
1710 return STATUS_SUCCESS
;