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;
15 UINT LanReceiveWorkerCalled
= 0;
16 BOOLEAN LanReceiveWorkerBusy
= FALSE
;
18 #define CCS_ROOT L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet"
19 #define TCPIP_GUID L"{4D36E972-E325-11CE-BFC1-08002BE10318}"
21 #define NGFP(_Packet) \
24 ULONG _ContigSize, _TotalSize; \
25 PNDIS_BUFFER _NdisBuffer; \
27 TI_DbgPrint(MID_TRACE,("Checking Packet %x\n", _Packet)); \
28 NdisGetFirstBufferFromPacket(_Packet, \
33 TI_DbgPrint(MID_TRACE,("NdisBuffer: %x\n", _NdisBuffer)); \
34 TI_DbgPrint(MID_TRACE,("Header : %x\n", _Header)); \
35 TI_DbgPrint(MID_TRACE,("ContigSize: %x\n", _ContigSize)); \
36 TI_DbgPrint(MID_TRACE,("TotalSize : %x\n", _TotalSize)); \
39 typedef struct _LAN_WQ_ITEM
{
43 UINT BytesTransferred
;
44 } LAN_WQ_ITEM
, *PLAN_WQ_ITEM
;
46 NDIS_HANDLE NdisProtocolHandle
= (NDIS_HANDLE
)NULL
;
47 BOOLEAN ProtocolRegistered
= FALSE
;
48 LIST_ENTRY AdapterListHead
;
49 KSPIN_LOCK AdapterListLock
;
53 NDIS_REQUEST_TYPE Type
,
58 * FUNCTION: Send a request to NDIS
60 * Adapter = Pointer to a LAN_ADAPTER structure
61 * Type = Type of request (Set or Query)
62 * OID = Value to be set/queried for
63 * Buffer = Pointer to a buffer to use
64 * Length = Number of bytes in Buffer
70 NDIS_STATUS NdisStatus
;
72 Request
.RequestType
= Type
;
73 if (Type
== NdisRequestSetInformation
) {
74 Request
.DATA
.SET_INFORMATION
.Oid
= OID
;
75 Request
.DATA
.SET_INFORMATION
.InformationBuffer
= Buffer
;
76 Request
.DATA
.SET_INFORMATION
.InformationBufferLength
= Length
;
78 Request
.DATA
.QUERY_INFORMATION
.Oid
= OID
;
79 Request
.DATA
.QUERY_INFORMATION
.InformationBuffer
= Buffer
;
80 Request
.DATA
.QUERY_INFORMATION
.InformationBufferLength
= Length
;
83 if (Adapter
->State
!= LAN_STATE_RESETTING
) {
84 NdisRequest(&NdisStatus
, Adapter
->NdisHandle
, &Request
);
86 NdisStatus
= NDIS_STATUS_NOT_ACCEPTED
;
89 /* Wait for NDIS to complete the request */
90 if (NdisStatus
== NDIS_STATUS_PENDING
) {
91 KeWaitForSingleObject(&Adapter
->Event
,
96 NdisStatus
= Adapter
->NdisStatus
;
104 PLAN_ADAPTER Adapter
)
106 * FUNCTION: Frees memory for a LAN_ADAPTER structure
108 * Adapter = Pointer to LAN_ADAPTER structure to free
111 ExFreePoolWithTag(Adapter
, LAN_ADAPTER_TAG
);
115 NTSTATUS TcpipLanGetDwordOid
116 ( PIP_INTERFACE Interface
,
119 /* Get maximum frame size */
120 if( Interface
->Context
) {
121 return NDISCall((PLAN_ADAPTER
)Interface
->Context
,
122 NdisRequestQueryInformation
,
126 } else switch( Oid
) { /* Loopback Case */
127 case OID_GEN_HARDWARE_STATUS
:
128 *Result
= NdisHardwareStatusReady
;
129 return STATUS_SUCCESS
;
132 return STATUS_INVALID_PARAMETER
;
137 VOID NTAPI
ProtocolOpenAdapterComplete(
138 NDIS_HANDLE BindingContext
,
140 NDIS_STATUS OpenErrorStatus
)
142 * FUNCTION: Called by NDIS to complete opening of an adapter
144 * BindingContext = Pointer to a device context (LAN_ADAPTER)
145 * Status = Status of the operation
146 * OpenErrorStatus = Additional status information
149 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
151 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
153 Adapter
->NdisStatus
= Status
;
155 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
159 VOID NTAPI
ProtocolCloseAdapterComplete(
160 NDIS_HANDLE BindingContext
,
163 * FUNCTION: Called by NDIS to complete closing an adapter
165 * BindingContext = Pointer to a device context (LAN_ADAPTER)
166 * Status = Status of the operation
169 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
171 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
173 Adapter
->NdisStatus
= Status
;
175 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
179 VOID NTAPI
ProtocolResetComplete(
180 NDIS_HANDLE BindingContext
,
183 * FUNCTION: Called by NDIS to complete resetting an adapter
185 * BindingContext = Pointer to a device context (LAN_ADAPTER)
186 * Status = Status of the operation
189 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
191 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
193 Adapter
->NdisStatus
= Status
;
195 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
199 VOID NTAPI
ProtocolRequestComplete(
200 NDIS_HANDLE BindingContext
,
201 PNDIS_REQUEST NdisRequest
,
204 * FUNCTION: Called by NDIS to complete a request
206 * BindingContext = Pointer to a device context (LAN_ADAPTER)
207 * NdisRequest = Pointer to an object describing the request
208 * Status = Status of the operation
211 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
213 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
215 /* Save status of request and signal an event */
216 Adapter
->NdisStatus
= Status
;
218 KeSetEvent(&Adapter
->Event
, 0, FALSE
);
222 VOID NTAPI
ProtocolSendComplete(
223 NDIS_HANDLE BindingContext
,
227 * FUNCTION: Called by NDIS to complete sending process
229 * BindingContext = Pointer to a device context (LAN_ADAPTER)
230 * Packet = Pointer to a packet descriptor
231 * Status = Status of the operation
234 FreeNdisPacket(Packet
);
237 VOID
LanReceiveWorker( PVOID Context
) {
239 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
241 PLAN_ADAPTER Adapter
;
242 UINT BytesTransferred
;
243 PNDIS_BUFFER NdisBuffer
;
246 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
248 Packet
= WorkItem
->Packet
;
249 Adapter
= WorkItem
->Adapter
;
250 BytesTransferred
= WorkItem
->BytesTransferred
;
252 ExFreePoolWithTag(WorkItem
, WQ_CONTEXT_TAG
);
254 IPInitializePacket(&IPPacket
, 0);
256 IPPacket
.NdisPacket
= Packet
;
258 NdisGetFirstBufferFromPacket(Packet
,
261 &IPPacket
.ContigSize
,
262 &IPPacket
.TotalSize
);
264 IPPacket
.ContigSize
= IPPacket
.TotalSize
= BytesTransferred
;
265 /* Determine which upper layer protocol that should receive
266 this packet and pass it to the correct receive handler */
268 TI_DbgPrint(MID_TRACE
,
269 ("ContigSize: %d, TotalSize: %d, BytesTransferred: %d\n",
270 IPPacket
.ContigSize
, IPPacket
.TotalSize
,
273 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
274 IPPacket
.Position
= 0;
278 ("Ether Type = %x ContigSize = %d Total = %d\n",
279 PacketType
, IPPacket
.ContigSize
, IPPacket
.TotalSize
));
281 switch (PacketType
) {
284 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
285 IPReceive(Adapter
->Context
, &IPPacket
);
288 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
289 ARPReceive(Adapter
->Context
, &IPPacket
);
291 IPPacket
.Free(&IPPacket
);
295 FreeNdisPacket( Packet
);
298 VOID
LanSubmitReceiveWork(
299 NDIS_HANDLE BindingContext
,
302 UINT BytesTransferred
) {
303 PLAN_WQ_ITEM WQItem
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_WQ_ITEM
),
305 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
307 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
311 WQItem
->Packet
= Packet
;
312 WQItem
->Adapter
= Adapter
;
313 WQItem
->BytesTransferred
= BytesTransferred
;
315 if (!ChewCreate( LanReceiveWorker
, WQItem
))
316 ExFreePoolWithTag(WQItem
, WQ_CONTEXT_TAG
);
319 VOID NTAPI
ProtocolTransferDataComplete(
320 NDIS_HANDLE BindingContext
,
323 UINT BytesTransferred
)
325 * FUNCTION: Called by NDIS to complete reception of data
327 * BindingContext = Pointer to a device context (LAN_ADAPTER)
328 * Packet = Pointer to a packet descriptor
329 * Status = Status of the operation
330 * BytesTransferred = Number of bytes transferred
332 * If the packet was successfully received, determine the protocol
333 * type and pass it to the correct receive handler
336 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
338 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
340 TransferDataCompleteCalled
++;
341 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
343 if( Status
!= NDIS_STATUS_SUCCESS
) return;
345 LanSubmitReceiveWork( BindingContext
, Packet
, Status
, BytesTransferred
);
348 NDIS_STATUS NTAPI
ProtocolReceive(
349 NDIS_HANDLE BindingContext
,
350 NDIS_HANDLE MacReceiveContext
,
352 UINT HeaderBufferSize
,
353 PVOID LookaheadBuffer
,
354 UINT LookaheadBufferSize
,
357 * FUNCTION: Called by NDIS when a packet has been received on the physical link
359 * BindingContext = Pointer to a device context (LAN_ADAPTER)
360 * MacReceiveContext = Handle used by underlying NIC driver
361 * HeaderBuffer = Pointer to a buffer containing the packet header
362 * HeaderBufferSize = Number of bytes in HeaderBuffer
363 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
364 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
365 * PacketSize = Overall size of the packet (not including header)
367 * Status of operation
371 UINT PacketType
, BytesTransferred
;
375 NDIS_STATUS NdisStatus
;
376 PNDIS_PACKET NdisPacket
;
377 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
378 PETH_HEADER EHeader
= (PETH_HEADER
)HeaderBuffer
;
380 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
382 if (Adapter
->State
!= LAN_STATE_STARTED
) {
383 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
384 return NDIS_STATUS_NOT_ACCEPTED
;
387 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
388 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
389 return NDIS_STATUS_NOT_ACCEPTED
;
392 if (Adapter
->Media
== NdisMedium802_3
) {
393 /* Ethernet and IEEE 802.3 frames can be destinguished by
394 looking at the IEEE 802.3 length field. This field is
395 less than or equal to 1500 for a valid IEEE 802.3 frame
396 and larger than 1500 is it's a valid EtherType value.
397 See RFC 1122, section 2.3.3 for more information */
398 /* FIXME: Test for Ethernet and IEEE 802.3 frame */
399 if (((EType
= EHeader
->EType
) != ETYPE_IPv4
) && (EType
!= ETYPE_ARP
)) {
400 TI_DbgPrint(DEBUG_DATALINK
, ("Not IP or ARP frame. EtherType (0x%X).\n", EType
));
401 return NDIS_STATUS_NOT_ACCEPTED
;
403 /* We use EtherType constants to destinguish packet types */
406 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
407 /* FIXME: Support other medias */
408 return NDIS_STATUS_NOT_ACCEPTED
;
411 /* Get a transfer data packet */
413 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
414 Adapter
, Adapter
->MTU
));
416 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
418 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
419 return NDIS_STATUS_NOT_ACCEPTED
;
422 PC(NdisPacket
)->PacketType
= PacketType
;
424 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
426 GetDataPtr( NdisPacket
, 0, &BufferData
, &temp
);
428 IPPacket
.NdisPacket
= NdisPacket
;
429 IPPacket
.Position
= 0;
431 TransferDataCalled
++;
433 if (LookaheadBufferSize
== PacketSize
)
435 /* Optimized code path for packets that are fully contained in
436 * the lookahead buffer. */
437 NdisCopyLookaheadData(BufferData
,
440 Adapter
->MacOptions
);
444 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
445 MacReceiveContext
, 0, PacketSize
,
446 NdisPacket
, &BytesTransferred
);
448 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
450 if (NdisStatus
!= NDIS_STATUS_PENDING
)
451 ProtocolTransferDataComplete(BindingContext
,
456 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
458 return NDIS_STATUS_SUCCESS
;
462 VOID NTAPI
ProtocolReceiveComplete(
463 NDIS_HANDLE BindingContext
)
465 * FUNCTION: Called by NDIS when we're done receiving data
467 * BindingContext = Pointer to a device context (LAN_ADAPTER)
470 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
474 VOID NTAPI
ProtocolStatus(
475 NDIS_HANDLE BindingContext
,
476 NDIS_STATUS GeneralStatus
,
478 UINT StatusBufferSize
)
480 * FUNCTION: Called by NDIS when the underlying driver has changed state
482 * BindingContext = Pointer to a device context (LAN_ADAPTER)
483 * GeneralStatus = A general status code
484 * StatusBuffer = Pointer to a buffer with medium-specific data
485 * StatusBufferSize = Number of bytes in StatusBuffer
488 PLAN_ADAPTER Adapter
= BindingContext
;
490 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
492 switch(GeneralStatus
)
494 case NDIS_STATUS_MEDIA_CONNECT
:
495 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
498 case NDIS_STATUS_MEDIA_DISCONNECT
:
499 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
502 case NDIS_STATUS_RESET_START
:
503 Adapter
->State
= LAN_STATE_RESETTING
;
506 case NDIS_STATUS_RESET_END
:
507 Adapter
->State
= LAN_STATE_STARTED
;
511 DbgPrint("Unhandled status: %x", GeneralStatus
);
518 NDIS_HANDLE NdisBindingContext
,
519 PNET_PNP_EVENT PnPEvent
)
521 switch(PnPEvent
->NetEvent
)
523 case NetEventSetPower
:
524 DbgPrint("Device transitioned to power state %ld\n", PnPEvent
->Buffer
);
525 return NDIS_STATUS_SUCCESS
;
527 case NetEventQueryPower
:
528 DbgPrint("Device wants to go into power state %ld\n", PnPEvent
->Buffer
);
529 return NDIS_STATUS_SUCCESS
;
531 case NetEventQueryRemoveDevice
:
532 DbgPrint("Device is about to be removed\n");
533 return NDIS_STATUS_SUCCESS
;
535 case NetEventCancelRemoveDevice
:
536 DbgPrint("Device removal cancelled\n");
537 return NDIS_STATUS_SUCCESS
;
540 DbgPrint("Unhandled event type: %ld\n", PnPEvent
->NetEvent
);
541 return NDIS_STATUS_SUCCESS
;
545 VOID NTAPI
ProtocolStatusComplete(
546 NDIS_HANDLE NdisBindingContext
)
548 * FUNCTION: Called by NDIS when a status-change has occurred
550 * BindingContext = Pointer to a device context (LAN_ADAPTER)
553 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
556 VOID NTAPI
ProtocolBindAdapter(
557 OUT PNDIS_STATUS Status
,
558 IN NDIS_HANDLE BindContext
,
559 IN PNDIS_STRING DeviceName
,
560 IN PVOID SystemSpecific1
,
561 IN PVOID SystemSpecific2
)
563 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
564 * bindings, and periodically thereafer as new adapters come online
566 * Status: Return value to NDIS
567 * BindContext: Handle provided by NDIS to track pending binding operations
568 * DeviceName: Name of the miniport device to bind to
569 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
570 * SystemSpecific2: Unused & must not be touched
573 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
574 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
580 PNDIS_PACKET NdisPacket
,
585 * FUNCTION: Transmits a packet
587 * Context = Pointer to context information (LAN_ADAPTER)
588 * NdisPacket = Pointer to NDIS packet to send
589 * Offset = Offset in packet where data starts
590 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
591 * Type = LAN protocol type (LAN_PROTO_*)
594 NDIS_STATUS NdisStatus
;
598 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
600 PNDIS_PACKET XmitPacket
;
602 TI_DbgPrint(DEBUG_DATALINK
,
603 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
604 NdisPacket
, Offset
, Adapter
));
606 if (Adapter
->State
!= LAN_STATE_STARTED
) {
607 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
611 TI_DbgPrint(DEBUG_DATALINK
,
612 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
613 Adapter
->HWAddress
[0] & 0xff,
614 Adapter
->HWAddress
[1] & 0xff,
615 Adapter
->HWAddress
[2] & 0xff,
616 Adapter
->HWAddress
[3] & 0xff,
617 Adapter
->HWAddress
[4] & 0xff,
618 Adapter
->HWAddress
[5] & 0xff));
620 GetDataPtr( NdisPacket
, 0, &OldData
, &OldSize
);
622 NdisStatus
= AllocatePacketWithBuffer(&XmitPacket
, NULL
, OldSize
+ Adapter
->HeaderSize
);
623 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
624 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_RESOURCES
);
628 GetDataPtr(XmitPacket
, 0, &Data
, &Size
);
630 RtlCopyMemory(Data
+ Adapter
->HeaderSize
, OldData
, OldSize
);
632 (*PC(NdisPacket
)->DLComplete
)(PC(NdisPacket
)->Context
, NdisPacket
, NDIS_STATUS_SUCCESS
);
634 switch (Adapter
->Media
) {
635 case NdisMedium802_3
:
636 EHeader
= (PETH_HEADER
)Data
;
639 /* Unicast address */
640 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
642 /* Broadcast address */
643 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
646 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
650 EHeader
->EType
= ETYPE_IPv4
;
653 EHeader
->EType
= ETYPE_ARP
;
656 EHeader
->EType
= ETYPE_IPv6
;
665 /* FIXME: Support other medias */
669 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
673 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
674 ((PCHAR
)LinkAddress
)[0] & 0xff,
675 ((PCHAR
)LinkAddress
)[1] & 0xff,
676 ((PCHAR
)LinkAddress
)[2] & 0xff,
677 ((PCHAR
)LinkAddress
)[3] & 0xff,
678 ((PCHAR
)LinkAddress
)[4] & 0xff,
679 ((PCHAR
)LinkAddress
)[5] & 0xff));
682 if (Adapter
->MTU
< Size
) {
683 /* This is NOT a pointer. MSDN explicitly says so. */
684 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket
,
685 TcpLargeSendPacketInfo
) = (PVOID
)((ULONG_PTR
)Adapter
->MTU
);
688 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
689 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
690 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, XmitPacket
);
691 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
692 NdisStatus
== NDIS_STATUS_PENDING
?
693 "Pending" : "Complete"));
694 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
696 /* I had a talk with vizzini: these really ought to be here.
697 * we're supposed to see these completed by ndis *only* when
698 * status_pending is returned. Note that this is different from
699 * the situation with IRPs. */
700 if (NdisStatus
!= NDIS_STATUS_PENDING
)
701 ProtocolSendComplete((NDIS_HANDLE
)Context
, XmitPacket
, NdisStatus
);
705 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
706 OBJECT_ATTRIBUTES Attributes
;
709 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, 0, 0);
710 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
714 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
715 PWCHAR RegistryValue
,
716 PUNICODE_STRING String
) {
717 UNICODE_STRING ValueName
;
718 UNICODE_STRING UnicodeString
;
722 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
724 RtlInitUnicodeString(&ValueName
, RegistryValue
);
726 ZwQueryValueKey(RegHandle
,
728 KeyValuePartialInformation
,
733 if (!NT_SUCCESS(Status
))
735 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
736 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
738 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
739 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
740 UnicodeString
.MaximumLength
= Information
->DataLength
;
743 (PWCHAR
)ExAllocatePool( NonPagedPool
,
744 UnicodeString
.MaximumLength
+ sizeof(WCHAR
) );
746 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
748 String
->MaximumLength
= UnicodeString
.MaximumLength
;
749 RtlCopyUnicodeString( String
, &UnicodeString
);
751 return STATUS_SUCCESS
;
755 * Utility to copy and append two unicode strings.
757 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
758 * IN PUNICODE_STRING Second -> Second string to append
759 * IN BOOL Deallocate -> TRUE: Deallocate First string before
765 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
766 PUNICODE_STRING Second
,
767 BOOLEAN Deallocate
) {
769 UNICODE_STRING Ustr
= *ResultFirst
;
770 PWSTR new_string
= ExAllocatePool
772 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)));
774 return STATUS_NO_MEMORY
;
776 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
777 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
778 Second
->Buffer
, Second
->Length
);
779 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
780 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
781 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
782 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
783 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
784 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
785 ExFreePool(new_string
);
789 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
790 PUNICODE_STRING TargetKeyName
,
791 PUNICODE_STRING Name
,
792 PUNICODE_STRING DeviceDesc
) {
793 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
794 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
795 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
796 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
799 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
801 RtlInitUnicodeString(&BackSlash
, L
"\\");
802 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
804 RtlInitUnicodeString(&DescKeyName
, L
"");
805 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
806 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
807 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
809 RtlInitUnicodeString(&LinkageKeyName
, L
"");
810 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
811 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
813 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
814 if( !NT_SUCCESS(Status
) ) goto cleanup
;
816 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
817 if( !NT_SUCCESS(Status
) ) goto cleanup
;
819 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
820 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
821 if( !NT_SUCCESS(Status
) ) goto cleanup
;
823 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
824 if( !NT_SUCCESS(Status
) ) goto cleanup
;
826 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
827 } else Status
= STATUS_UNSUCCESSFUL
;
830 RtlFreeUnicodeString( &RootDevice
);
831 RtlFreeUnicodeString( &LinkageKeyName
);
832 RtlFreeUnicodeString( &DescKeyName
);
833 if( LinkageKey
) NtClose( LinkageKey
);
834 if( DescKey
) NtClose( DescKey
);
836 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
841 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
842 PUNICODE_STRING DeviceDesc
) {
843 UNICODE_STRING EnumKeyName
, TargetKeyName
;
847 KEY_BASIC_INFORMATION
*Kbio
=
848 ExAllocatePool(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
));
849 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
851 RtlInitUnicodeString( DeviceDesc
, NULL
);
853 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
856 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
858 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
860 if( !NT_SUCCESS(Status
) ) {
861 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
862 &EnumKeyName
, Status
));
867 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
868 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
869 Kbio
, KbioLength
, &ResultLength
);
871 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
873 KbioLength
= ResultLength
;
874 Kbio
= ExAllocatePool( NonPagedPool
, KbioLength
);
876 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
878 return STATUS_NO_MEMORY
;
881 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
882 Kbio
, KbioLength
, &ResultLength
);
884 if( !NT_SUCCESS(Status
) ) {
885 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
892 if( NT_SUCCESS(Status
) ) {
893 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
895 TargetKeyName
.Buffer
= Kbio
->Name
;
897 Status
= CheckForDeviceDesc
898 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
899 if( NT_SUCCESS(Status
) ) {
903 } else Status
= STATUS_SUCCESS
;
909 return STATUS_UNSUCCESSFUL
;
912 VOID
GetName( PUNICODE_STRING RegistryKey
,
913 PUNICODE_STRING OutName
) {
915 UNICODE_STRING PartialRegistryKey
;
917 PartialRegistryKey
.Buffer
=
918 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
919 Ptr
= PartialRegistryKey
.Buffer
;
921 while( *Ptr
!= L
'\\' &&
922 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
925 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
926 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
928 RtlInitUnicodeString( OutName
, L
"" );
929 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
933 PLAN_ADAPTER Adapter
,
934 PNDIS_STRING RegistryPath
)
936 * FUNCTION: Binds a LAN adapter to IP layer
938 * Adapter = Pointer to LAN_ADAPTER structure
940 * We set the lookahead buffer size, set the packet filter and
941 * bind the adapter to IP layer
945 NDIS_STATUS NdisStatus
;
946 LLIP_BIND_INFO BindInfo
;
947 IP_ADDRESS DefaultMask
, Router
;
948 ULONG Lookahead
= LOOKAHEAD_SIZE
, Unused
;
950 OBJECT_ATTRIBUTES ObjectAttributes
;
951 HANDLE ParameterHandle
;
952 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo
;
954 UNICODE_STRING IPAddress
= RTL_CONSTANT_STRING(L
"IPAddress");
955 UNICODE_STRING Netmask
= RTL_CONSTANT_STRING(L
"SubnetMask");
956 UNICODE_STRING Gateway
= RTL_CONSTANT_STRING(L
"DefaultGateway");
957 UNICODE_STRING EnableDhcp
= RTL_CONSTANT_STRING(L
"EnableDHCP");
958 UNICODE_STRING Prefix
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\");
959 UNICODE_STRING TcpipRegistryPath
;
960 UNICODE_STRING RegistryDataU
;
961 ANSI_STRING RegistryDataA
;
963 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
965 Adapter
->State
= LAN_STATE_OPENING
;
967 NdisStatus
= NDISCall(Adapter
,
968 NdisRequestSetInformation
,
969 OID_GEN_CURRENT_LOOKAHEAD
,
972 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
973 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
977 /* Bind the adapter to IP layer */
978 BindInfo
.Context
= Adapter
;
979 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
980 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
981 BindInfo
.MTU
= Adapter
->MTU
;
982 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
983 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
984 BindInfo
.Transmit
= LANTransmit
;
986 IF
= IPCreateInterface(&BindInfo
);
989 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
994 * Query per-adapter configuration from the registry
995 * In case anyone is curious: there *is* an Ndis configuration api
996 * for this sort of thing, but it doesn't really support things like
997 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
998 * protocol drivers developed for win2k and above just use the native
999 * services (ZwOpenKey, etc).
1002 GetName( RegistryPath
, &IF
->Name
);
1004 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
1005 if (!NT_SUCCESS(Status
)) {
1006 TI_DbgPrint(MIN_TRACE
, ("Failed to get device description.\n"));
1007 IPDestroyInterface(IF
);
1011 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
1014 TcpipRegistryPath
.MaximumLength
= sizeof(WCHAR
) * 150;
1015 TcpipRegistryPath
.Length
= 0;
1016 TcpipRegistryPath
.Buffer
= Buffer
;
1018 RtlAppendUnicodeStringToString(&TcpipRegistryPath
,
1021 RtlAppendUnicodeStringToString(&TcpipRegistryPath
,
1024 InitializeObjectAttributes(&ObjectAttributes
,
1026 OBJ_CASE_INSENSITIVE
,
1030 AddrInitIPv4(&DefaultMask
, 0);
1032 Status
= ZwOpenKey(&ParameterHandle
, KEY_READ
, &ObjectAttributes
);
1034 if (!NT_SUCCESS(Status
))
1036 IF
->Unicast
= DefaultMask
;
1037 IF
->Netmask
= DefaultMask
;
1041 KeyValueInfo
= ExAllocatePool(PagedPool
, sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
));
1044 ZwClose(ParameterHandle
);
1045 IPDestroyInterface(IF
);
1049 Status
= ZwQueryValueKey(ParameterHandle
,
1051 KeyValuePartialInformation
,
1053 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(ULONG
),
1055 if (NT_SUCCESS(Status
) && KeyValueInfo
->DataLength
== sizeof(ULONG
) && (*(PULONG
)KeyValueInfo
->Data
) == 0)
1057 RegistryDataU
.MaximumLength
= 16 + sizeof(WCHAR
);
1058 RegistryDataU
.Buffer
= (PWCHAR
)KeyValueInfo
->Data
;
1060 Status
= ZwQueryValueKey(ParameterHandle
,
1062 KeyValuePartialInformation
,
1064 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1066 if (NT_SUCCESS(Status
))
1068 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1070 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1074 AddrInitIPv4(&IF
->Unicast
, inet_addr(RegistryDataA
.Buffer
));
1076 RtlFreeAnsiString(&RegistryDataA
);
1081 IF
->Unicast
= DefaultMask
;
1084 Status
= ZwQueryValueKey(ParameterHandle
,
1086 KeyValuePartialInformation
,
1088 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1090 if (NT_SUCCESS(Status
))
1092 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1094 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1098 AddrInitIPv4(&IF
->Netmask
, inet_addr(RegistryDataA
.Buffer
));
1100 RtlFreeAnsiString(&RegistryDataA
);
1104 IF
->Netmask
= DefaultMask
;
1107 Status
= ZwQueryValueKey(ParameterHandle
,
1109 KeyValuePartialInformation
,
1111 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 16 * sizeof(WCHAR
),
1113 if (NT_SUCCESS(Status
))
1115 RegistryDataU
.Length
= KeyValueInfo
->DataLength
;
1117 RtlUnicodeStringToAnsiString(&RegistryDataA
,
1121 AddrInitIPv4(&Router
, inet_addr(RegistryDataA
.Buffer
));
1123 RtlFreeAnsiString(&RegistryDataA
);
1125 if (!AddrIsUnspecified(&Router
)) RouterCreateRoute(&DefaultMask
, &DefaultMask
, &Router
, IF
, 1);
1130 IF
->Unicast
= DefaultMask
;
1131 IF
->Netmask
= DefaultMask
;
1134 ZwClose(ParameterHandle
);
1137 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
1138 IF
->Broadcast
.Address
.IPv4Address
=
1139 IF
->Unicast
.Address
.IPv4Address
|
1140 ~IF
->Netmask
.Address
.IPv4Address
;
1142 TI_DbgPrint(DEBUG_DATALINK
,("BCAST(IF) %s\n", A2S(&IF
->Broadcast
)));
1144 /* Get maximum link speed */
1145 NdisStatus
= NDISCall(Adapter
,
1146 NdisRequestQueryInformation
,
1151 if( !NT_SUCCESS(NdisStatus
) )
1152 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
1154 /* Register interface with IP layer */
1155 IPRegisterInterface(IF
);
1157 /* Set packet filter so we can send and receive packets */
1158 NdisStatus
= NDISCall(Adapter
,
1159 NdisRequestSetInformation
,
1160 OID_GEN_CURRENT_PACKET_FILTER
,
1161 &Adapter
->PacketFilter
,
1164 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1165 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
1166 IPUnregisterInterface(IF
);
1167 IPDestroyInterface(IF
);
1171 Adapter
->Context
= IF
;
1172 Adapter
->State
= LAN_STATE_STARTED
;
1178 PLAN_ADAPTER Adapter
)
1180 * FUNCTION: Unbinds a LAN adapter from IP layer
1182 * Adapter = Pointer to LAN_ADAPTER structure
1185 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1187 if (Adapter
->State
== LAN_STATE_STARTED
) {
1188 PIP_INTERFACE IF
= Adapter
->Context
;
1190 IPUnregisterInterface(IF
);
1192 IPDestroyInterface(IF
);
1197 NDIS_STATUS
LANRegisterAdapter(
1198 PNDIS_STRING AdapterName
,
1199 PNDIS_STRING RegistryPath
)
1201 * FUNCTION: Registers protocol with an NDIS adapter
1203 * AdapterName = Pointer to string with name of adapter to register
1204 * Adapter = Address of pointer to a LAN_ADAPTER structure
1206 * Status of operation
1210 NDIS_STATUS NdisStatus
;
1211 NDIS_STATUS OpenStatus
;
1213 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1217 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1219 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(LAN_ADAPTER
), LAN_ADAPTER_TAG
);
1221 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1222 return NDIS_STATUS_RESOURCES
;
1225 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1227 /* Put adapter in stopped state */
1228 IF
->State
= LAN_STATE_STOPPED
;
1230 /* Initialize protecting spin lock */
1231 KeInitializeSpinLock(&IF
->Lock
);
1233 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1235 /* Initialize array with media IDs we support */
1236 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1238 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1239 /* Open the adapter. */
1240 NdisOpenAdapter(&NdisStatus
,
1252 /* Wait until the adapter is opened */
1253 if (NdisStatus
== NDIS_STATUS_PENDING
)
1254 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1255 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1256 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1257 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1261 IF
->Media
= MediaArray
[MediaIndex
];
1263 /* Fill LAN_ADAPTER structure with some adapter specific information */
1264 switch (IF
->Media
) {
1265 case NdisMedium802_3
:
1266 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1267 IF
->BCastMask
= BCAST_ETH_MASK
;
1268 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1269 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1270 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1271 IF
->MinFrameSize
= 60;
1272 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1274 NDIS_PACKET_TYPE_BROADCAST
|
1275 NDIS_PACKET_TYPE_DIRECTED
|
1276 NDIS_PACKET_TYPE_MULTICAST
;
1280 /* Unsupported media */
1281 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1282 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1283 return NDIS_STATUS_NOT_SUPPORTED
;
1286 /* Get maximum frame size */
1287 NdisStatus
= NDISCall(IF
,
1288 NdisRequestQueryInformation
,
1289 OID_GEN_MAXIMUM_FRAME_SIZE
,
1292 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1293 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (NDISCall)\n", AdapterName
));
1294 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1298 /* Get maximum packet size */
1299 NdisStatus
= NDISCall(IF
,
1300 NdisRequestQueryInformation
,
1301 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1304 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1305 TI_DbgPrint(MIN_TRACE
, ("Query for maximum packet size failed.\n"));
1306 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1310 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1311 NdisStatus
= NDISCall(IF
,
1312 NdisRequestQueryInformation
,
1313 OID_GEN_MAXIMUM_SEND_PACKETS
,
1314 &IF
->MaxSendPackets
,
1316 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1317 /* Legacy NIC drivers may not support this query, if it fails we
1318 assume it can send at least one packet per call to NdisSend(Packets) */
1319 IF
->MaxSendPackets
= 1;
1321 /* Get current hardware address */
1322 NdisStatus
= NDISCall(IF
,
1323 NdisRequestQueryInformation
,
1326 IF
->HWAddressLength
);
1327 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1328 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1329 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1333 /* Get maximum link speed */
1334 NdisStatus
= NDISCall(IF
,
1335 NdisRequestQueryInformation
,
1339 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1340 TI_DbgPrint(MIN_TRACE
, ("Query for maximum link speed failed.\n"));
1341 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1345 /* Convert returned link speed to bps (it is in 100bps increments) */
1346 IF
->Speed
= Speed
* 100L;
1348 /* Bind adapter to IP layer */
1349 if( !BindAdapter(IF
, RegistryPath
) ) {
1350 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1351 ExFreePoolWithTag(IF
, LAN_ADAPTER_TAG
);
1352 return NDIS_STATUS_NOT_ACCEPTED
;
1355 /* Add adapter to the adapter list */
1356 ExInterlockedInsertTailList(&AdapterListHead
,
1360 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1362 return NDIS_STATUS_SUCCESS
;
1366 NDIS_STATUS
LANUnregisterAdapter(
1367 PLAN_ADAPTER Adapter
)
1369 * FUNCTION: Unregisters protocol with NDIS adapter
1371 * Adapter = Pointer to a LAN_ADAPTER structure
1373 * Status of operation
1377 NDIS_HANDLE NdisHandle
;
1378 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1380 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1382 /* Unlink the adapter from the list */
1383 RemoveEntryList(&Adapter
->ListEntry
);
1385 /* Unbind adapter from IP layer */
1386 UnbindAdapter(Adapter
);
1388 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1389 NdisHandle
= Adapter
->NdisHandle
;
1391 Adapter
->NdisHandle
= NULL
;
1392 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1394 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1395 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1396 TcpipWaitForSingleObject(&Adapter
->Event
,
1401 NdisStatus
= Adapter
->NdisStatus
;
1404 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1406 FreeAdapter(Adapter
);
1413 LANUnregisterProtocol(VOID
)
1415 * FUNCTION: Unregisters this protocol driver with NDIS
1416 * NOTES: Does not care wether we are already registered
1419 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1421 if (ProtocolRegistered
) {
1422 NDIS_STATUS NdisStatus
;
1423 PLIST_ENTRY CurrentEntry
;
1424 PLIST_ENTRY NextEntry
;
1425 PLAN_ADAPTER Current
;
1428 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1430 /* Search the list and remove every adapter we find */
1431 CurrentEntry
= AdapterListHead
.Flink
;
1432 while (CurrentEntry
!= &AdapterListHead
) {
1433 NextEntry
= CurrentEntry
->Flink
;
1434 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1436 LANUnregisterAdapter(Current
);
1437 CurrentEntry
= NextEntry
;
1440 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1442 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1443 ProtocolRegistered
= FALSE
;
1449 ProtocolUnbindAdapter(
1450 PNDIS_STATUS Status
,
1451 NDIS_HANDLE ProtocolBindingContext
,
1452 NDIS_HANDLE UnbindContext
)
1454 /* We don't pend any unbinding so we can just ignore UnbindContext */
1455 *Status
= LANUnregisterAdapter((PLAN_ADAPTER
)ProtocolBindingContext
);
1458 NTSTATUS
LANRegisterProtocol(
1461 * FUNCTION: Registers this protocol driver with NDIS
1463 * Name = Name of this protocol driver
1465 * Status of operation
1468 NDIS_STATUS NdisStatus
;
1469 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1471 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1473 InitializeListHead(&AdapterListHead
);
1474 KeInitializeSpinLock(&AdapterListLock
);
1476 /* Set up protocol characteristics */
1477 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1478 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1479 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1480 ProtChars
.Name
.Length
= Name
->Length
;
1481 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1482 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1483 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1484 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1485 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1486 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1487 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1488 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1489 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1490 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1491 ProtChars
.StatusHandler
= ProtocolStatus
;
1492 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1493 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1494 ProtChars
.PnPEventHandler
= ProtocolPnPEvent
;
1495 ProtChars
.UnbindAdapterHandler
= ProtocolUnbindAdapter
;
1496 ProtChars
.UnloadHandler
= LANUnregisterProtocol
;
1498 /* Try to register protocol */
1499 NdisRegisterProtocol(&NdisStatus
,
1500 &NdisProtocolHandle
,
1502 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1503 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1505 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1506 return (NTSTATUS
)NdisStatus
;
1509 ProtocolRegistered
= TRUE
;
1511 return STATUS_SUCCESS
;