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
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 TI_DbgPrint(DEBUG_DATALINK
, ("Calling completion routine\n"));
235 ASSERT_KM_POINTER(Packet
);
236 ASSERT_KM_POINTER(PC(Packet
));
237 ASSERT_KM_POINTER(PC(Packet
)->DLComplete
);
238 (*PC(Packet
)->DLComplete
)( PC(Packet
)->Context
, Packet
, Status
);
239 TI_DbgPrint(DEBUG_DATALINK
, ("Finished\n"));
242 VOID
LanReceiveWorker( PVOID Context
) {
244 PLAN_WQ_ITEM WorkItem
= (PLAN_WQ_ITEM
)Context
;
246 PLAN_ADAPTER Adapter
;
247 UINT BytesTransferred
;
248 PNDIS_BUFFER NdisBuffer
;
251 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
253 Packet
= WorkItem
->Packet
;
254 Adapter
= WorkItem
->Adapter
;
255 BytesTransferred
= WorkItem
->BytesTransferred
;
257 IPInitializePacket(&IPPacket
, 0);
259 IPPacket
.NdisPacket
= Packet
;
261 NdisGetFirstBufferFromPacket(Packet
,
264 &IPPacket
.ContigSize
,
265 &IPPacket
.TotalSize
);
267 IPPacket
.ContigSize
= IPPacket
.TotalSize
= BytesTransferred
;
268 /* Determine which upper layer protocol that should receive
269 this packet and pass it to the correct receive handler */
271 TI_DbgPrint(MID_TRACE
,
272 ("ContigSize: %d, TotalSize: %d, BytesTransferred: %d\n",
273 IPPacket
.ContigSize
, IPPacket
.TotalSize
,
276 PacketType
= PC(IPPacket
.NdisPacket
)->PacketType
;
277 IPPacket
.Position
= 0;
281 ("Ether Type = %x ContigSize = %d Total = %d\n",
282 PacketType
, IPPacket
.ContigSize
, IPPacket
.TotalSize
));
284 switch (PacketType
) {
287 TI_DbgPrint(MID_TRACE
,("Received IP Packet\n"));
288 IPReceive(Adapter
->Context
, &IPPacket
);
291 TI_DbgPrint(MID_TRACE
,("Received ARP Packet\n"));
292 ARPReceive(Adapter
->Context
, &IPPacket
);
294 IPPacket
.Free(&IPPacket
);
298 FreeNdisPacket( Packet
);
301 VOID
LanSubmitReceiveWork(
302 NDIS_HANDLE BindingContext
,
305 UINT BytesTransferred
) {
307 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
309 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
311 WQItem
.Packet
= Packet
;
312 WQItem
.Adapter
= Adapter
;
313 WQItem
.BytesTransferred
= BytesTransferred
;
316 ( NULL
, sizeof(LAN_WQ_ITEM
), LanReceiveWorker
, &WQItem
) )
320 VOID NTAPI
ProtocolTransferDataComplete(
321 NDIS_HANDLE BindingContext
,
324 UINT BytesTransferred
)
326 * FUNCTION: Called by NDIS to complete reception of data
328 * BindingContext = Pointer to a device context (LAN_ADAPTER)
329 * Packet = Pointer to a packet descriptor
330 * Status = Status of the operation
331 * BytesTransferred = Number of bytes transferred
333 * If the packet was successfully received, determine the protocol
334 * type and pass it to the correct receive handler
337 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
339 TI_DbgPrint(DEBUG_DATALINK
,("called\n"));
341 TransferDataCompleteCalled
++;
342 ASSERT(TransferDataCompleteCalled
<= TransferDataCalled
);
344 if( Status
!= NDIS_STATUS_SUCCESS
) return;
346 LanSubmitReceiveWork( BindingContext
, Packet
, Status
, BytesTransferred
);
349 NDIS_STATUS NTAPI
ProtocolReceive(
350 NDIS_HANDLE BindingContext
,
351 NDIS_HANDLE MacReceiveContext
,
353 UINT HeaderBufferSize
,
354 PVOID LookaheadBuffer
,
355 UINT LookaheadBufferSize
,
358 * FUNCTION: Called by NDIS when a packet has been received on the physical link
360 * BindingContext = Pointer to a device context (LAN_ADAPTER)
361 * MacReceiveContext = Handle used by underlying NIC driver
362 * HeaderBuffer = Pointer to a buffer containing the packet header
363 * HeaderBufferSize = Number of bytes in HeaderBuffer
364 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
365 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
366 * PacketSize = Overall size of the packet (not including header)
368 * Status of operation
372 UINT PacketType
, BytesTransferred
;
376 NDIS_STATUS NdisStatus
;
377 PNDIS_PACKET NdisPacket
;
378 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)BindingContext
;
379 PETH_HEADER EHeader
= (PETH_HEADER
)HeaderBuffer
;
381 TI_DbgPrint(DEBUG_DATALINK
, ("Called. (packetsize %d)\n",PacketSize
));
383 if (Adapter
->State
!= LAN_STATE_STARTED
) {
384 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter is stopped.\n"));
385 return NDIS_STATUS_NOT_ACCEPTED
;
388 if (HeaderBufferSize
< Adapter
->HeaderSize
) {
389 TI_DbgPrint(DEBUG_DATALINK
, ("Runt frame received.\n"));
390 return NDIS_STATUS_NOT_ACCEPTED
;
393 if (Adapter
->Media
== NdisMedium802_3
) {
394 /* Ethernet and IEEE 802.3 frames can be destinguished by
395 looking at the IEEE 802.3 length field. This field is
396 less than or equal to 1500 for a valid IEEE 802.3 frame
397 and larger than 1500 is it's a valid EtherType value.
398 See RFC 1122, section 2.3.3 for more information */
399 /* FIXME: Test for Ethernet and IEEE 802.3 frame */
400 if (((EType
= EHeader
->EType
) != ETYPE_IPv4
) && (EType
!= ETYPE_ARP
)) {
401 TI_DbgPrint(DEBUG_DATALINK
, ("Not IP or ARP frame. EtherType (0x%X).\n", EType
));
402 return NDIS_STATUS_NOT_ACCEPTED
;
404 /* We use EtherType constants to destinguish packet types */
407 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
408 /* FIXME: Support other medias */
409 return NDIS_STATUS_NOT_ACCEPTED
;
412 /* Get a transfer data packet */
414 TI_DbgPrint(DEBUG_DATALINK
, ("Adapter: %x (MTU %d)\n",
415 Adapter
, Adapter
->MTU
));
417 NdisStatus
= AllocatePacketWithBuffer( &NdisPacket
, NULL
,
418 PacketSize
+ HeaderBufferSize
);
419 if( NdisStatus
!= NDIS_STATUS_SUCCESS
) {
420 return NDIS_STATUS_NOT_ACCEPTED
;
423 PC(NdisPacket
)->PacketType
= PacketType
;
425 TI_DbgPrint(DEBUG_DATALINK
, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize
,PacketSize
));
427 GetDataPtr( NdisPacket
, 0, &BufferData
, &temp
);
429 IPPacket
.NdisPacket
= NdisPacket
;
430 IPPacket
.Position
= 0;
432 TransferDataCalled
++;
434 if (LookaheadBufferSize
== PacketSize
)
436 /* Optimized code path for packets that are fully contained in
437 * the lookahead buffer. */
438 NdisCopyLookaheadData(BufferData
,
441 Adapter
->MacOptions
);
445 NdisTransferData(&NdisStatus
, Adapter
->NdisHandle
,
446 MacReceiveContext
, 0, PacketSize
,
447 NdisPacket
, &BytesTransferred
);
449 TI_DbgPrint(DEBUG_DATALINK
, ("Calling complete\n"));
451 if (NdisStatus
!= NDIS_STATUS_PENDING
)
452 ProtocolTransferDataComplete(BindingContext
,
457 TI_DbgPrint(DEBUG_DATALINK
, ("leaving\n"));
459 return NDIS_STATUS_SUCCESS
;
463 VOID NTAPI
ProtocolReceiveComplete(
464 NDIS_HANDLE BindingContext
)
466 * FUNCTION: Called by NDIS when we're done receiving data
468 * BindingContext = Pointer to a device context (LAN_ADAPTER)
471 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
475 VOID NTAPI
ProtocolStatus(
476 NDIS_HANDLE BindingContext
,
477 NDIS_STATUS GeneralStatus
,
479 UINT StatusBufferSize
)
481 * FUNCTION: Called by NDIS when the underlying driver has changed state
483 * BindingContext = Pointer to a device context (LAN_ADAPTER)
484 * GeneralStatus = A general status code
485 * StatusBuffer = Pointer to a buffer with medium-specific data
486 * StatusBufferSize = Number of bytes in StatusBuffer
489 PLAN_ADAPTER Adapter
= BindingContext
;
491 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
493 switch(GeneralStatus
)
495 case NDIS_STATUS_MEDIA_CONNECT
:
496 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
499 case NDIS_STATUS_MEDIA_DISCONNECT
:
500 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
503 case NDIS_STATUS_RESET_START
:
504 Adapter
->State
= LAN_STATE_RESETTING
;
507 case NDIS_STATUS_RESET_END
:
508 Adapter
->State
= LAN_STATE_STARTED
;
512 DbgPrint("Unhandled status: %x", GeneralStatus
);
518 VOID NTAPI
ProtocolStatusComplete(
519 NDIS_HANDLE NdisBindingContext
)
521 * FUNCTION: Called by NDIS when a status-change has occurred
523 * BindingContext = Pointer to a device context (LAN_ADAPTER)
526 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
529 VOID NTAPI
ProtocolBindAdapter(
530 OUT PNDIS_STATUS Status
,
531 IN NDIS_HANDLE BindContext
,
532 IN PNDIS_STRING DeviceName
,
533 IN PVOID SystemSpecific1
,
534 IN PVOID SystemSpecific2
)
536 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
537 * bindings, and periodically thereafer as new adapters come online
539 * Status: Return value to NDIS
540 * BindContext: Handle provided by NDIS to track pending binding operations
541 * DeviceName: Name of the miniport device to bind to
542 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
543 * SystemSpecific2: Unused & must not be touched
546 /* XXX confirm that this is still true, or re-word the following comment */
547 /* we get to ignore BindContext because we will never pend an operation with NDIS */
548 TI_DbgPrint(DEBUG_DATALINK
, ("Called with registry path %wZ for %wZ\n", SystemSpecific1
, DeviceName
));
549 *Status
= LANRegisterAdapter(DeviceName
, SystemSpecific1
);
555 PNDIS_PACKET NdisPacket
,
560 * FUNCTION: Transmits a packet
562 * Context = Pointer to context information (LAN_ADAPTER)
563 * NdisPacket = Pointer to NDIS packet to send
564 * Offset = Offset in packet where data starts
565 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
566 * Type = LAN protocol type (LAN_PROTO_*)
569 NDIS_STATUS NdisStatus
;
573 PLAN_ADAPTER Adapter
= (PLAN_ADAPTER
)Context
;
576 TI_DbgPrint(DEBUG_DATALINK
,
577 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
578 NdisPacket
, Offset
, Adapter
));
580 if (Adapter
->State
!= LAN_STATE_STARTED
) {
581 ProtocolSendComplete(Context
, NdisPacket
, NDIS_STATUS_NOT_ACCEPTED
);
585 TI_DbgPrint(DEBUG_DATALINK
,
586 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
587 Adapter
->HWAddress
[0] & 0xff,
588 Adapter
->HWAddress
[1] & 0xff,
589 Adapter
->HWAddress
[2] & 0xff,
590 Adapter
->HWAddress
[3] & 0xff,
591 Adapter
->HWAddress
[4] & 0xff,
592 Adapter
->HWAddress
[5] & 0xff));
594 /* XXX arty -- Handled adjustment in a saner way than before ...
595 * not needed immediately */
596 GetDataPtr( NdisPacket
, 0, &Data
, &Size
);
598 switch (Adapter
->Media
) {
599 case NdisMedium802_3
:
600 EHeader
= (PETH_HEADER
)Data
;
603 /* Unicast address */
604 RtlCopyMemory(EHeader
->DstAddr
, LinkAddress
, IEEE_802_ADDR_LENGTH
);
606 /* Broadcast address */
607 RtlFillMemory(EHeader
->DstAddr
, IEEE_802_ADDR_LENGTH
, 0xFF);
610 RtlCopyMemory(EHeader
->SrcAddr
, Adapter
->HWAddress
, IEEE_802_ADDR_LENGTH
);
614 EHeader
->EType
= ETYPE_IPv4
;
617 EHeader
->EType
= ETYPE_ARP
;
620 EHeader
->EType
= ETYPE_IPv6
;
624 /* Should not happen */
625 TI_DbgPrint(MIN_TRACE
, ("Unknown LAN protocol.\n"));
627 ProtocolSendComplete((NDIS_HANDLE
)Context
,
629 NDIS_STATUS_FAILURE
);
636 /* FIXME: Support other medias */
640 TI_DbgPrint( MID_TRACE
, ("LinkAddress: %x\n", LinkAddress
));
644 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
645 ((PCHAR
)LinkAddress
)[0] & 0xff,
646 ((PCHAR
)LinkAddress
)[1] & 0xff,
647 ((PCHAR
)LinkAddress
)[2] & 0xff,
648 ((PCHAR
)LinkAddress
)[3] & 0xff,
649 ((PCHAR
)LinkAddress
)[4] & 0xff,
650 ((PCHAR
)LinkAddress
)[5] & 0xff));
653 TcpipAcquireSpinLock( &Adapter
->Lock
, &OldIrql
);
654 TI_DbgPrint(MID_TRACE
, ("NdisSend\n"));
655 NdisSend(&NdisStatus
, Adapter
->NdisHandle
, NdisPacket
);
656 TI_DbgPrint(MID_TRACE
, ("NdisSend %s\n",
657 NdisStatus
== NDIS_STATUS_PENDING
?
658 "Pending" : "Complete"));
659 TcpipReleaseSpinLock( &Adapter
->Lock
, OldIrql
);
661 /* I had a talk with vizzini: these really ought to be here.
662 * we're supposed to see these completed by ndis *only* when
663 * status_pending is returned. Note that this is different from
664 * the situation with IRPs. */
665 if (NdisStatus
!= NDIS_STATUS_PENDING
)
666 ProtocolSendComplete((NDIS_HANDLE
)Context
, NdisPacket
, NdisStatus
);
670 OpenRegistryKey( PNDIS_STRING RegistryPath
, PHANDLE RegHandle
) {
671 OBJECT_ATTRIBUTES Attributes
;
674 InitializeObjectAttributes(&Attributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, 0, 0);
675 Status
= ZwOpenKey(RegHandle
, KEY_ALL_ACCESS
, &Attributes
);
679 static NTSTATUS
ReadStringFromRegistry( HANDLE RegHandle
,
680 PWCHAR RegistryValue
,
681 PUNICODE_STRING String
) {
682 UNICODE_STRING ValueName
;
683 UNICODE_STRING UnicodeString
;
687 PKEY_VALUE_PARTIAL_INFORMATION Information
= (PKEY_VALUE_PARTIAL_INFORMATION
)buf
;
689 RtlInitUnicodeString(&ValueName
, RegistryValue
);
691 ZwQueryValueKey(RegHandle
,
693 KeyValuePartialInformation
,
698 if (!NT_SUCCESS(Status
))
700 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
701 TI_DbgPrint(MIN_TRACE
, ("Information DataLength: 0x%x\n", Information
->DataLength
));
703 UnicodeString
.Buffer
= (PWCHAR
)&Information
->Data
;
704 UnicodeString
.Length
= Information
->DataLength
- sizeof(WCHAR
);
705 UnicodeString
.MaximumLength
= Information
->DataLength
;
708 (PWCHAR
)ExAllocatePool( NonPagedPool
,
709 UnicodeString
.MaximumLength
+ sizeof(WCHAR
) );
711 if( !String
->Buffer
) return STATUS_NO_MEMORY
;
713 String
->MaximumLength
= UnicodeString
.MaximumLength
;
714 RtlCopyUnicodeString( String
, &UnicodeString
);
716 return STATUS_SUCCESS
;
720 * Utility to copy and append two unicode strings.
722 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
723 * IN PUNICODE_STRING Second -> Second string to append
724 * IN BOOL Deallocate -> TRUE: Deallocate First string before
730 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
731 PUNICODE_STRING Second
,
732 BOOLEAN Deallocate
) {
734 UNICODE_STRING Ustr
= *ResultFirst
;
735 PWSTR new_string
= ExAllocatePoolWithTag
737 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)), TAG_STRING
);
739 return STATUS_NO_MEMORY
;
741 memcpy( new_string
, ResultFirst
->Buffer
, ResultFirst
->Length
);
742 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
743 Second
->Buffer
, Second
->Length
);
744 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
745 ResultFirst
->Length
= Ustr
.Length
+ Second
->Length
;
746 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
747 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
748 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
749 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
750 ExFreePool(new_string
);
754 static NTSTATUS
CheckForDeviceDesc( PUNICODE_STRING EnumKeyName
,
755 PUNICODE_STRING TargetKeyName
,
756 PUNICODE_STRING Name
,
757 PUNICODE_STRING DeviceDesc
) {
758 UNICODE_STRING RootDevice
= { 0, 0, NULL
}, LinkageKeyName
= { 0, 0, NULL
};
759 UNICODE_STRING DescKeyName
= { 0, 0, NULL
}, Linkage
= { 0, 0, NULL
};
760 UNICODE_STRING BackSlash
= { 0, 0, NULL
};
761 HANDLE DescKey
= NULL
, LinkageKey
= NULL
;
764 TI_DbgPrint(DEBUG_DATALINK
,("EnumKeyName %wZ\n", EnumKeyName
));
766 RtlInitUnicodeString(&BackSlash
, L
"\\");
767 RtlInitUnicodeString(&Linkage
, L
"\\Linkage");
769 RtlInitUnicodeString(&DescKeyName
, L
"");
770 AppendUnicodeString( &DescKeyName
, EnumKeyName
, FALSE
);
771 AppendUnicodeString( &DescKeyName
, &BackSlash
, TRUE
);
772 AppendUnicodeString( &DescKeyName
, TargetKeyName
, TRUE
);
774 RtlInitUnicodeString(&LinkageKeyName
, L
"");
775 AppendUnicodeString( &LinkageKeyName
, &DescKeyName
, FALSE
);
776 AppendUnicodeString( &LinkageKeyName
, &Linkage
, TRUE
);
778 Status
= OpenRegistryKey( &LinkageKeyName
, &LinkageKey
);
779 if( !NT_SUCCESS(Status
) ) goto cleanup
;
781 Status
= ReadStringFromRegistry( LinkageKey
, L
"RootDevice", &RootDevice
);
782 if( !NT_SUCCESS(Status
) ) goto cleanup
;
784 if( RtlCompareUnicodeString( &RootDevice
, Name
, TRUE
) == 0 ) {
785 Status
= OpenRegistryKey( &DescKeyName
, &DescKey
);
786 if( !NT_SUCCESS(Status
) ) goto cleanup
;
788 Status
= ReadStringFromRegistry( DescKey
, L
"DriverDesc", DeviceDesc
);
789 if( !NT_SUCCESS(Status
) ) goto cleanup
;
791 TI_DbgPrint(DEBUG_DATALINK
,("ADAPTER DESC: %wZ\n", DeviceDesc
));
792 } else Status
= STATUS_UNSUCCESSFUL
;
795 RtlFreeUnicodeString( &RootDevice
);
796 RtlFreeUnicodeString( &LinkageKeyName
);
797 RtlFreeUnicodeString( &DescKeyName
);
798 if( LinkageKey
) NtClose( LinkageKey
);
799 if( DescKey
) NtClose( DescKey
);
801 TI_DbgPrint(DEBUG_DATALINK
,("Returning %x\n", Status
));
806 static NTSTATUS
FindDeviceDescForAdapter( PUNICODE_STRING Name
,
807 PUNICODE_STRING DeviceDesc
) {
808 UNICODE_STRING EnumKeyName
, TargetKeyName
;
812 KEY_BASIC_INFORMATION
*Kbio
=
813 ExAllocatePool(NonPagedPool
, sizeof(KEY_BASIC_INFORMATION
));
814 ULONG KbioLength
= sizeof(KEY_BASIC_INFORMATION
), ResultLength
;
816 if( !Kbio
) return STATUS_INSUFFICIENT_RESOURCES
;
819 (&EnumKeyName
, CCS_ROOT L
"\\Control\\Class\\" TCPIP_GUID
);
821 Status
= OpenRegistryKey( &EnumKeyName
, &EnumKey
);
823 if( !NT_SUCCESS(Status
) ) {
824 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't open Enum key %wZ: %x\n",
825 &EnumKeyName
, Status
));
830 for( i
= 0; NT_SUCCESS(Status
); i
++ ) {
831 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
832 Kbio
, KbioLength
, &ResultLength
);
834 if( Status
== STATUS_BUFFER_TOO_SMALL
|| Status
== STATUS_BUFFER_OVERFLOW
) {
836 KbioLength
= ResultLength
;
837 Kbio
= ExAllocatePool( NonPagedPool
, KbioLength
);
839 TI_DbgPrint(DEBUG_DATALINK
,("Failed to allocate memory\n"));
841 return STATUS_NO_MEMORY
;
844 Status
= ZwEnumerateKey( EnumKey
, i
, KeyBasicInformation
,
845 Kbio
, KbioLength
, &ResultLength
);
847 if( !NT_SUCCESS(Status
) ) {
848 TI_DbgPrint(DEBUG_DATALINK
,("Couldn't enum key child %d\n", i
));
855 if( NT_SUCCESS(Status
) ) {
856 TargetKeyName
.Length
= TargetKeyName
.MaximumLength
=
858 TargetKeyName
.Buffer
= Kbio
->Name
;
860 Status
= CheckForDeviceDesc
861 ( &EnumKeyName
, &TargetKeyName
, Name
, DeviceDesc
);
862 if( NT_SUCCESS(Status
) ) {
866 } else Status
= STATUS_SUCCESS
;
870 RtlInitUnicodeString( DeviceDesc
, L
"" );
871 AppendUnicodeString( DeviceDesc
, &TargetKeyName
, FALSE
);
874 return STATUS_UNSUCCESSFUL
;
877 VOID
GetName( PUNICODE_STRING RegistryKey
,
878 PUNICODE_STRING OutName
) {
880 UNICODE_STRING PartialRegistryKey
;
882 PartialRegistryKey
.Buffer
=
883 RegistryKey
->Buffer
+ wcslen(CCS_ROOT L
"\\Services\\");
884 Ptr
= PartialRegistryKey
.Buffer
;
886 while( *Ptr
!= L
'\\' &&
887 ((PCHAR
)Ptr
) < ((PCHAR
)RegistryKey
->Buffer
) + RegistryKey
->Length
)
890 PartialRegistryKey
.Length
= PartialRegistryKey
.MaximumLength
=
891 (Ptr
- PartialRegistryKey
.Buffer
) * sizeof(WCHAR
);
893 RtlInitUnicodeString( OutName
, L
"" );
894 AppendUnicodeString( OutName
, &PartialRegistryKey
, FALSE
);
898 PLAN_ADAPTER Adapter
,
899 PNDIS_STRING RegistryPath
)
901 * FUNCTION: Binds a LAN adapter to IP layer
903 * Adapter = Pointer to LAN_ADAPTER structure
905 * We set the lookahead buffer size, set the packet filter and
906 * bind the adapter to IP layer
910 NDIS_STATUS NdisStatus
;
911 LLIP_BIND_INFO BindInfo
;
912 IP_ADDRESS DefaultMask
;
913 ULONG Lookahead
= LOOKAHEAD_SIZE
;
916 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
918 Adapter
->State
= LAN_STATE_OPENING
;
920 NdisStatus
= NDISCall(Adapter
,
921 NdisRequestSetInformation
,
922 OID_GEN_CURRENT_LOOKAHEAD
,
925 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
926 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus
));
930 /* Bind the adapter to IP layer */
931 BindInfo
.Context
= Adapter
;
932 BindInfo
.HeaderSize
= Adapter
->HeaderSize
;
933 BindInfo
.MinFrameSize
= Adapter
->MinFrameSize
;
934 BindInfo
.MTU
= Adapter
->MTU
;
935 BindInfo
.Address
= (PUCHAR
)&Adapter
->HWAddress
;
936 BindInfo
.AddressLength
= Adapter
->HWAddressLength
;
937 BindInfo
.Transmit
= LANTransmit
;
939 IF
= IPCreateInterface(&BindInfo
);
942 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
947 * Query per-adapter configuration from the registry
948 * In case anyone is curious: there *is* an Ndis configuration api
949 * for this sort of thing, but it doesn't really support things like
950 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
951 * protocol drivers developed for win2k and above just use the native
952 * services (ZwOpenKey, etc).
955 GetName( RegistryPath
, &IF
->Name
);
957 Status
= FindDeviceDescForAdapter( &IF
->Name
, &IF
->Description
);
959 TI_DbgPrint(DEBUG_DATALINK
,("Adapter Description: %wZ\n",
962 AddrInitIPv4(&DefaultMask
, 0);
964 IF
->Unicast
= DefaultMask
;
965 IF
->Netmask
= DefaultMask
;
967 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
968 IF
->Broadcast
.Address
.IPv4Address
=
969 IF
->Unicast
.Address
.IPv4Address
|
970 ~IF
->Netmask
.Address
.IPv4Address
;
972 TI_DbgPrint(DEBUG_DATALINK
,("BCAST(IF) %s\n", A2S(&IF
->Broadcast
)));
974 /* Get maximum link speed */
975 NdisStatus
= NDISCall(Adapter
,
976 NdisRequestQueryInformation
,
981 if( !NT_SUCCESS(NdisStatus
) )
982 IF
->Speed
= IP_DEFAULT_LINK_SPEED
;
984 /* Register interface with IP layer */
985 IPRegisterInterface(IF
);
987 /* Set packet filter so we can send and receive packets */
988 NdisStatus
= NDISCall(Adapter
,
989 NdisRequestSetInformation
,
990 OID_GEN_CURRENT_PACKET_FILTER
,
991 &Adapter
->PacketFilter
,
994 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
995 TI_DbgPrint(DEBUG_DATALINK
, ("Could not set packet filter (0x%X).\n", NdisStatus
));
996 IPUnregisterInterface(IF
);
997 IPDestroyInterface(IF
);
1001 Adapter
->Context
= IF
;
1002 Adapter
->State
= LAN_STATE_STARTED
;
1008 PLAN_ADAPTER Adapter
)
1010 * FUNCTION: Unbinds a LAN adapter from IP layer
1012 * Adapter = Pointer to LAN_ADAPTER structure
1015 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1017 if (Adapter
->State
== LAN_STATE_STARTED
) {
1018 PIP_INTERFACE IF
= Adapter
->Context
;
1020 IPUnregisterInterface(IF
);
1022 IPDestroyInterface(IF
);
1027 NDIS_STATUS
LANRegisterAdapter(
1028 PNDIS_STRING AdapterName
,
1029 PNDIS_STRING RegistryPath
)
1031 * FUNCTION: Registers protocol with an NDIS adapter
1033 * AdapterName = Pointer to string with name of adapter to register
1034 * Adapter = Address of pointer to a LAN_ADAPTER structure
1036 * Status of operation
1040 NDIS_STATUS NdisStatus
;
1041 NDIS_STATUS OpenStatus
;
1043 NDIS_MEDIUM MediaArray
[MAX_MEDIA
];
1047 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1049 IF
= exAllocatePool(NonPagedPool
, sizeof(LAN_ADAPTER
));
1051 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
1052 return NDIS_STATUS_RESOURCES
;
1055 RtlZeroMemory(IF
, sizeof(LAN_ADAPTER
));
1057 /* Put adapter in stopped state */
1058 IF
->State
= LAN_STATE_STOPPED
;
1060 /* Initialize protecting spin lock */
1061 KeInitializeSpinLock(&IF
->Lock
);
1063 KeInitializeEvent(&IF
->Event
, SynchronizationEvent
, FALSE
);
1065 /* Initialize array with media IDs we support */
1066 MediaArray
[MEDIA_ETH
] = NdisMedium802_3
;
1068 TI_DbgPrint(DEBUG_DATALINK
,("opening adapter %wZ\n", AdapterName
));
1069 /* Open the adapter. */
1070 NdisOpenAdapter(&NdisStatus
,
1082 /* Wait until the adapter is opened */
1083 if (NdisStatus
== NDIS_STATUS_PENDING
)
1084 KeWaitForSingleObject(&IF
->Event
, UserRequest
, KernelMode
, FALSE
, NULL
);
1085 else if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1086 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ\n", AdapterName
));
1091 IF
->Media
= MediaArray
[MediaIndex
];
1093 /* Fill LAN_ADAPTER structure with some adapter specific information */
1094 switch (IF
->Media
) {
1095 case NdisMedium802_3
:
1096 IF
->HWAddressLength
= IEEE_802_ADDR_LENGTH
;
1097 IF
->BCastMask
= BCAST_ETH_MASK
;
1098 IF
->BCastCheck
= BCAST_ETH_CHECK
;
1099 IF
->BCastOffset
= BCAST_ETH_OFFSET
;
1100 IF
->HeaderSize
= sizeof(ETH_HEADER
);
1101 IF
->MinFrameSize
= 60;
1102 AddressOID
= OID_802_3_CURRENT_ADDRESS
;
1104 NDIS_PACKET_TYPE_BROADCAST
|
1105 NDIS_PACKET_TYPE_DIRECTED
|
1106 NDIS_PACKET_TYPE_MULTICAST
;
1110 /* Unsupported media */
1111 TI_DbgPrint(MIN_TRACE
, ("Unsupported media.\n"));
1113 return NDIS_STATUS_NOT_SUPPORTED
;
1116 /* Get maximum frame size */
1117 NdisStatus
= NDISCall(IF
,
1118 NdisRequestQueryInformation
,
1119 OID_GEN_MAXIMUM_FRAME_SIZE
,
1122 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1123 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (NDISCall)\n", AdapterName
));
1128 /* Get maximum packet size */
1129 NdisStatus
= NDISCall(IF
,
1130 NdisRequestQueryInformation
,
1131 OID_GEN_MAXIMUM_TOTAL_SIZE
,
1134 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1135 TI_DbgPrint(MIN_TRACE
, ("Query for maximum packet size failed.\n"));
1140 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1141 NdisStatus
= NDISCall(IF
,
1142 NdisRequestQueryInformation
,
1143 OID_GEN_MAXIMUM_SEND_PACKETS
,
1144 &IF
->MaxSendPackets
,
1146 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1147 /* Legacy NIC drivers may not support this query, if it fails we
1148 assume it can send at least one packet per call to NdisSend(Packets) */
1149 IF
->MaxSendPackets
= 1;
1151 /* Get current hardware address */
1152 NdisStatus
= NDISCall(IF
,
1153 NdisRequestQueryInformation
,
1156 IF
->HWAddressLength
);
1157 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1158 TI_DbgPrint(MIN_TRACE
, ("Query for current hardware address failed.\n"));
1163 /* Get maximum link speed */
1164 NdisStatus
= NDISCall(IF
,
1165 NdisRequestQueryInformation
,
1169 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
1170 TI_DbgPrint(MIN_TRACE
, ("Query for maximum link speed failed.\n"));
1175 /* Convert returned link speed to bps (it is in 100bps increments) */
1176 IF
->Speed
= Speed
* 100L;
1178 /* Bind adapter to IP layer */
1179 if( !BindAdapter(IF
, RegistryPath
) ) {
1180 TI_DbgPrint(DEBUG_DATALINK
,("denying adapter %wZ (BindAdapter)\n", AdapterName
));
1182 return NDIS_STATUS_NOT_ACCEPTED
;
1185 /* Add adapter to the adapter list */
1186 ExInterlockedInsertTailList(&AdapterListHead
,
1190 TI_DbgPrint(DEBUG_DATALINK
, ("Leaving.\n"));
1192 return NDIS_STATUS_SUCCESS
;
1196 NDIS_STATUS
LANUnregisterAdapter(
1197 PLAN_ADAPTER Adapter
)
1199 * FUNCTION: Unregisters protocol with NDIS adapter
1201 * Adapter = Pointer to a LAN_ADAPTER structure
1203 * Status of operation
1207 NDIS_HANDLE NdisHandle
;
1208 NDIS_STATUS NdisStatus
= NDIS_STATUS_SUCCESS
;
1210 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1212 /* Unlink the adapter from the list */
1213 RemoveEntryList(&Adapter
->ListEntry
);
1215 /* Unbind adapter from IP layer */
1216 UnbindAdapter(Adapter
);
1218 TcpipAcquireSpinLock(&Adapter
->Lock
, &OldIrql
);
1219 NdisHandle
= Adapter
->NdisHandle
;
1221 Adapter
->NdisHandle
= NULL
;
1222 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1224 NdisCloseAdapter(&NdisStatus
, NdisHandle
);
1225 if (NdisStatus
== NDIS_STATUS_PENDING
) {
1226 TcpipWaitForSingleObject(&Adapter
->Event
,
1231 NdisStatus
= Adapter
->NdisStatus
;
1234 TcpipReleaseSpinLock(&Adapter
->Lock
, OldIrql
);
1236 FreeAdapter(Adapter
);
1242 NTSTATUS
LANRegisterProtocol(
1245 * FUNCTION: Registers this protocol driver with NDIS
1247 * Name = Name of this protocol driver
1249 * Status of operation
1252 NDIS_STATUS NdisStatus
;
1253 NDIS_PROTOCOL_CHARACTERISTICS ProtChars
;
1255 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1257 InitializeListHead(&AdapterListHead
);
1258 KeInitializeSpinLock(&AdapterListLock
);
1260 /* Set up protocol characteristics */
1261 RtlZeroMemory(&ProtChars
, sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1262 ProtChars
.MajorNdisVersion
= NDIS_VERSION_MAJOR
;
1263 ProtChars
.MinorNdisVersion
= NDIS_VERSION_MINOR
;
1264 ProtChars
.Name
.Length
= Name
->Length
;
1265 ProtChars
.Name
.Buffer
= Name
->Buffer
;
1266 ProtChars
.Name
.MaximumLength
= Name
->MaximumLength
;
1267 ProtChars
.OpenAdapterCompleteHandler
= ProtocolOpenAdapterComplete
;
1268 ProtChars
.CloseAdapterCompleteHandler
= ProtocolCloseAdapterComplete
;
1269 ProtChars
.ResetCompleteHandler
= ProtocolResetComplete
;
1270 ProtChars
.RequestCompleteHandler
= ProtocolRequestComplete
;
1271 ProtChars
.SendCompleteHandler
= ProtocolSendComplete
;
1272 ProtChars
.TransferDataCompleteHandler
= ProtocolTransferDataComplete
;
1273 ProtChars
.ReceiveHandler
= ProtocolReceive
;
1274 ProtChars
.ReceiveCompleteHandler
= ProtocolReceiveComplete
;
1275 ProtChars
.StatusHandler
= ProtocolStatus
;
1276 ProtChars
.StatusCompleteHandler
= ProtocolStatusComplete
;
1277 ProtChars
.BindAdapterHandler
= ProtocolBindAdapter
;
1279 /* Try to register protocol */
1280 NdisRegisterProtocol(&NdisStatus
,
1281 &NdisProtocolHandle
,
1283 sizeof(NDIS_PROTOCOL_CHARACTERISTICS
));
1284 if (NdisStatus
!= NDIS_STATUS_SUCCESS
)
1286 TI_DbgPrint(DEBUG_DATALINK
, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus
));
1287 return (NTSTATUS
)NdisStatus
;
1290 ProtocolRegistered
= TRUE
;
1292 return STATUS_SUCCESS
;
1296 VOID
LANUnregisterProtocol(
1299 * FUNCTION: Unregisters this protocol driver with NDIS
1300 * NOTES: Does not care wether we are already registered
1303 TI_DbgPrint(DEBUG_DATALINK
, ("Called.\n"));
1305 if (ProtocolRegistered
) {
1306 NDIS_STATUS NdisStatus
;
1307 PLIST_ENTRY CurrentEntry
;
1308 PLIST_ENTRY NextEntry
;
1309 PLAN_ADAPTER Current
;
1312 TcpipAcquireSpinLock(&AdapterListLock
, &OldIrql
);
1314 /* Search the list and remove every adapter we find */
1315 CurrentEntry
= AdapterListHead
.Flink
;
1316 while (CurrentEntry
!= &AdapterListHead
) {
1317 NextEntry
= CurrentEntry
->Flink
;
1318 Current
= CONTAINING_RECORD(CurrentEntry
, LAN_ADAPTER
, ListEntry
);
1320 LANUnregisterAdapter(Current
);
1321 CurrentEntry
= NextEntry
;
1324 TcpipReleaseSpinLock(&AdapterListLock
, OldIrql
);
1326 NdisDeregisterProtocol(&NdisStatus
, NdisProtocolHandle
);
1327 ProtocolRegistered
= FALSE
;