- In Win32 DBG is defined to 0 for a non-debug build and to 1 for a debug build....
[reactos.git] / reactos / drivers / network / tcpip / datalink / lan.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: datalink/lan.c
5 * PURPOSE: Local Area Network media routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 UINT TransferDataCalled = 0;
14 UINT TransferDataCompleteCalled = 0;
15 UINT LanReceiveWorkerCalled = 0;
16 BOOLEAN LanReceiveWorkerBusy = FALSE;
17
18 #define CCS_ROOT L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet"
19 #define TCPIP_GUID L"{4D36E972-E325-11CE-BFC1-08002BE10318}"
20
21 #define NGFP(_Packet) \
22 { \
23 PVOID _Header; \
24 ULONG _ContigSize, _TotalSize; \
25 PNDIS_BUFFER _NdisBuffer; \
26 \
27 TI_DbgPrint(MID_TRACE,("Checking Packet %x\n", _Packet)); \
28 NdisGetFirstBufferFromPacket(_Packet, \
29 &_NdisBuffer, \
30 &_Header, \
31 &_ContigSize, \
32 &_TotalSize); \
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)); \
37 }
38
39 typedef struct _LAN_WQ_ITEM {
40 LIST_ENTRY ListEntry;
41 PNDIS_PACKET Packet;
42 PLAN_ADAPTER Adapter;
43 UINT BytesTransferred;
44 } LAN_WQ_ITEM, *PLAN_WQ_ITEM;
45
46 NDIS_HANDLE NdisProtocolHandle = (NDIS_HANDLE)NULL;
47 BOOLEAN ProtocolRegistered = FALSE;
48 LIST_ENTRY AdapterListHead;
49 KSPIN_LOCK AdapterListLock;
50
51 NDIS_STATUS NDISCall(
52 PLAN_ADAPTER Adapter,
53 NDIS_REQUEST_TYPE Type,
54 NDIS_OID OID,
55 PVOID Buffer,
56 UINT Length)
57 /*
58 * FUNCTION: Send a request to NDIS
59 * ARGUMENTS:
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
65 * RETURNS:
66 * Status of operation
67 */
68 {
69 NDIS_REQUEST Request;
70 NDIS_STATUS NdisStatus;
71
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;
77 } else {
78 Request.DATA.QUERY_INFORMATION.Oid = OID;
79 Request.DATA.QUERY_INFORMATION.InformationBuffer = Buffer;
80 Request.DATA.QUERY_INFORMATION.InformationBufferLength = Length;
81 }
82
83 if (Adapter->State != LAN_STATE_RESETTING) {
84 NdisRequest(&NdisStatus, Adapter->NdisHandle, &Request);
85 } else {
86 NdisStatus = NDIS_STATUS_NOT_ACCEPTED;
87 }
88
89 /* Wait for NDIS to complete the request */
90 if (NdisStatus == NDIS_STATUS_PENDING) {
91 KeWaitForSingleObject(&Adapter->Event,
92 UserRequest,
93 KernelMode,
94 FALSE,
95 NULL);
96 NdisStatus = Adapter->NdisStatus;
97 }
98
99 return NdisStatus;
100 }
101
102
103 VOID FreeAdapter(
104 PLAN_ADAPTER Adapter)
105 /*
106 * FUNCTION: Frees memory for a LAN_ADAPTER structure
107 * ARGUMENTS:
108 * Adapter = Pointer to LAN_ADAPTER structure to free
109 */
110 {
111 exFreePool(Adapter);
112 }
113
114
115 NTSTATUS TcpipLanGetDwordOid
116 ( PIP_INTERFACE Interface,
117 NDIS_OID Oid,
118 PULONG Result ) {
119 /* Get maximum frame size */
120 if( Interface->Context ) {
121 return NDISCall((PLAN_ADAPTER)Interface->Context,
122 NdisRequestQueryInformation,
123 Oid,
124 Result,
125 sizeof(ULONG));
126 } else switch( Oid ) { /* Loopback Case */
127 case OID_GEN_HARDWARE_STATUS:
128 *Result = NdisHardwareStatusReady;
129 return STATUS_SUCCESS;
130
131 default:
132 return STATUS_INVALID_PARAMETER;
133 }
134 }
135
136
137 VOID NTAPI ProtocolOpenAdapterComplete(
138 NDIS_HANDLE BindingContext,
139 NDIS_STATUS Status,
140 NDIS_STATUS OpenErrorStatus)
141 /*
142 * FUNCTION: Called by NDIS to complete opening of an adapter
143 * ARGUMENTS:
144 * BindingContext = Pointer to a device context (LAN_ADAPTER)
145 * Status = Status of the operation
146 * OpenErrorStatus = Additional status information
147 */
148 {
149 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
150
151 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
152
153 Adapter->NdisStatus = Status;
154
155 KeSetEvent(&Adapter->Event, 0, FALSE);
156 }
157
158
159 VOID NTAPI ProtocolCloseAdapterComplete(
160 NDIS_HANDLE BindingContext,
161 NDIS_STATUS Status)
162 /*
163 * FUNCTION: Called by NDIS to complete closing an adapter
164 * ARGUMENTS:
165 * BindingContext = Pointer to a device context (LAN_ADAPTER)
166 * Status = Status of the operation
167 */
168 {
169 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
170
171 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
172
173 Adapter->NdisStatus = Status;
174
175 KeSetEvent(&Adapter->Event, 0, FALSE);
176 }
177
178
179 VOID NTAPI ProtocolResetComplete(
180 NDIS_HANDLE BindingContext,
181 NDIS_STATUS Status)
182 /*
183 * FUNCTION: Called by NDIS to complete resetting an adapter
184 * ARGUMENTS:
185 * BindingContext = Pointer to a device context (LAN_ADAPTER)
186 * Status = Status of the operation
187 */
188 {
189 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
190
191 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
192
193 Adapter->NdisStatus = Status;
194
195 KeSetEvent(&Adapter->Event, 0, FALSE);
196 }
197
198
199 VOID NTAPI ProtocolRequestComplete(
200 NDIS_HANDLE BindingContext,
201 PNDIS_REQUEST NdisRequest,
202 NDIS_STATUS Status)
203 /*
204 * FUNCTION: Called by NDIS to complete a request
205 * ARGUMENTS:
206 * BindingContext = Pointer to a device context (LAN_ADAPTER)
207 * NdisRequest = Pointer to an object describing the request
208 * Status = Status of the operation
209 */
210 {
211 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
212
213 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
214
215 /* Save status of request and signal an event */
216 Adapter->NdisStatus = Status;
217
218 KeSetEvent(&Adapter->Event, 0, FALSE);
219 }
220
221
222 VOID NTAPI ProtocolSendComplete(
223 NDIS_HANDLE BindingContext,
224 PNDIS_PACKET Packet,
225 NDIS_STATUS Status)
226 /*
227 * FUNCTION: Called by NDIS to complete sending process
228 * ARGUMENTS:
229 * BindingContext = Pointer to a device context (LAN_ADAPTER)
230 * Packet = Pointer to a packet descriptor
231 * Status = Status of the operation
232 */
233 {
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"));
240 }
241
242 VOID LanReceiveWorker( PVOID Context ) {
243 UINT PacketType;
244 PLAN_WQ_ITEM WorkItem = (PLAN_WQ_ITEM)Context;
245 PNDIS_PACKET Packet;
246 PLAN_ADAPTER Adapter;
247 UINT BytesTransferred;
248 PNDIS_BUFFER NdisBuffer;
249 IP_PACKET IPPacket;
250
251 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
252
253 Packet = WorkItem->Packet;
254 Adapter = WorkItem->Adapter;
255 BytesTransferred = WorkItem->BytesTransferred;
256
257 IPInitializePacket(&IPPacket, 0);
258
259 IPPacket.NdisPacket = Packet;
260
261 NdisGetFirstBufferFromPacket(Packet,
262 &NdisBuffer,
263 &IPPacket.Header,
264 &IPPacket.ContigSize,
265 &IPPacket.TotalSize);
266
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 */
270
271 TI_DbgPrint(MID_TRACE,
272 ("ContigSize: %d, TotalSize: %d, BytesTransferred: %d\n",
273 IPPacket.ContigSize, IPPacket.TotalSize,
274 BytesTransferred));
275
276 PacketType = PC(IPPacket.NdisPacket)->PacketType;
277 IPPacket.Position = 0;
278
279 TI_DbgPrint
280 (DEBUG_DATALINK,
281 ("Ether Type = %x ContigSize = %d Total = %d\n",
282 PacketType, IPPacket.ContigSize, IPPacket.TotalSize));
283
284 switch (PacketType) {
285 case ETYPE_IPv4:
286 case ETYPE_IPv6:
287 TI_DbgPrint(MID_TRACE,("Received IP Packet\n"));
288 IPReceive(Adapter->Context, &IPPacket);
289 break;
290 case ETYPE_ARP:
291 TI_DbgPrint(MID_TRACE,("Received ARP Packet\n"));
292 ARPReceive(Adapter->Context, &IPPacket);
293 default:
294 IPPacket.Free(&IPPacket);
295 break;
296 }
297
298 FreeNdisPacket( Packet );
299 }
300
301 VOID LanSubmitReceiveWork(
302 NDIS_HANDLE BindingContext,
303 PNDIS_PACKET Packet,
304 NDIS_STATUS Status,
305 UINT BytesTransferred) {
306 LAN_WQ_ITEM WQItem;
307 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
308
309 TI_DbgPrint(DEBUG_DATALINK,("called\n"));
310
311 WQItem.Packet = Packet;
312 WQItem.Adapter = Adapter;
313 WQItem.BytesTransferred = BytesTransferred;
314
315 if( !ChewCreate
316 ( NULL, sizeof(LAN_WQ_ITEM), LanReceiveWorker, &WQItem ) )
317 ASSERT(0);
318 }
319
320 VOID NTAPI ProtocolTransferDataComplete(
321 NDIS_HANDLE BindingContext,
322 PNDIS_PACKET Packet,
323 NDIS_STATUS Status,
324 UINT BytesTransferred)
325 /*
326 * FUNCTION: Called by NDIS to complete reception of data
327 * ARGUMENTS:
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
332 * NOTES:
333 * If the packet was successfully received, determine the protocol
334 * type and pass it to the correct receive handler
335 */
336 {
337 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
338
339 TI_DbgPrint(DEBUG_DATALINK,("called\n"));
340
341 TransferDataCompleteCalled++;
342 ASSERT(TransferDataCompleteCalled <= TransferDataCalled);
343
344 if( Status != NDIS_STATUS_SUCCESS ) return;
345
346 LanSubmitReceiveWork( BindingContext, Packet, Status, BytesTransferred );
347 }
348
349 NDIS_STATUS NTAPI ProtocolReceive(
350 NDIS_HANDLE BindingContext,
351 NDIS_HANDLE MacReceiveContext,
352 PVOID HeaderBuffer,
353 UINT HeaderBufferSize,
354 PVOID LookaheadBuffer,
355 UINT LookaheadBufferSize,
356 UINT PacketSize)
357 /*
358 * FUNCTION: Called by NDIS when a packet has been received on the physical link
359 * ARGUMENTS:
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)
367 * RETURNS:
368 * Status of operation
369 */
370 {
371 USHORT EType;
372 UINT PacketType, BytesTransferred;
373 UINT temp;
374 IP_PACKET IPPacket;
375 PCHAR BufferData;
376 NDIS_STATUS NdisStatus;
377 PNDIS_PACKET NdisPacket;
378 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
379 PETH_HEADER EHeader = (PETH_HEADER)HeaderBuffer;
380
381 TI_DbgPrint(DEBUG_DATALINK, ("Called. (packetsize %d)\n",PacketSize));
382
383 if (Adapter->State != LAN_STATE_STARTED) {
384 TI_DbgPrint(DEBUG_DATALINK, ("Adapter is stopped.\n"));
385 return NDIS_STATUS_NOT_ACCEPTED;
386 }
387
388 if (HeaderBufferSize < Adapter->HeaderSize) {
389 TI_DbgPrint(DEBUG_DATALINK, ("Runt frame received.\n"));
390 return NDIS_STATUS_NOT_ACCEPTED;
391 }
392
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;
403 }
404 /* We use EtherType constants to destinguish packet types */
405 PacketType = EType;
406 } else {
407 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
408 /* FIXME: Support other medias */
409 return NDIS_STATUS_NOT_ACCEPTED;
410 }
411
412 /* Get a transfer data packet */
413
414 TI_DbgPrint(DEBUG_DATALINK, ("Adapter: %x (MTU %d)\n",
415 Adapter, Adapter->MTU));
416
417 NdisStatus = AllocatePacketWithBuffer( &NdisPacket, NULL,
418 PacketSize + HeaderBufferSize );
419 if( NdisStatus != NDIS_STATUS_SUCCESS ) {
420 return NDIS_STATUS_NOT_ACCEPTED;
421 }
422
423 PC(NdisPacket)->PacketType = PacketType;
424
425 TI_DbgPrint(DEBUG_DATALINK, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize,PacketSize));
426
427 GetDataPtr( NdisPacket, 0, &BufferData, &temp );
428
429 IPPacket.NdisPacket = NdisPacket;
430 IPPacket.Position = 0;
431
432 TransferDataCalled++;
433
434 if (LookaheadBufferSize == PacketSize)
435 {
436 /* Optimized code path for packets that are fully contained in
437 * the lookahead buffer. */
438 NdisCopyLookaheadData(BufferData,
439 LookaheadBuffer,
440 LookaheadBufferSize,
441 Adapter->MacOptions);
442 }
443 else
444 {
445 NdisTransferData(&NdisStatus, Adapter->NdisHandle,
446 MacReceiveContext, 0, PacketSize,
447 NdisPacket, &BytesTransferred);
448 }
449 TI_DbgPrint(DEBUG_DATALINK, ("Calling complete\n"));
450
451 if (NdisStatus != NDIS_STATUS_PENDING)
452 ProtocolTransferDataComplete(BindingContext,
453 NdisPacket,
454 NdisStatus,
455 PacketSize);
456
457 TI_DbgPrint(DEBUG_DATALINK, ("leaving\n"));
458
459 return NDIS_STATUS_SUCCESS;
460 }
461
462
463 VOID NTAPI ProtocolReceiveComplete(
464 NDIS_HANDLE BindingContext)
465 /*
466 * FUNCTION: Called by NDIS when we're done receiving data
467 * ARGUMENTS:
468 * BindingContext = Pointer to a device context (LAN_ADAPTER)
469 */
470 {
471 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
472 }
473
474
475 VOID NTAPI ProtocolStatus(
476 NDIS_HANDLE BindingContext,
477 NDIS_STATUS GeneralStatus,
478 PVOID StatusBuffer,
479 UINT StatusBufferSize)
480 /*
481 * FUNCTION: Called by NDIS when the underlying driver has changed state
482 * ARGUMENTS:
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
487 */
488 {
489 PLAN_ADAPTER Adapter = BindingContext;
490
491 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
492
493 switch(GeneralStatus)
494 {
495 case NDIS_STATUS_MEDIA_CONNECT:
496 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
497 break;
498
499 case NDIS_STATUS_MEDIA_DISCONNECT:
500 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
501 break;
502
503 case NDIS_STATUS_RESET_START:
504 Adapter->State = LAN_STATE_RESETTING;
505 break;
506
507 case NDIS_STATUS_RESET_END:
508 Adapter->State = LAN_STATE_STARTED;
509 break;
510
511 default:
512 DbgPrint("Unhandled status: %x", GeneralStatus);
513 break;
514 }
515 }
516
517 NDIS_STATUS NTAPI
518 ProtocolPnPEvent(
519 NDIS_HANDLE NdisBindingContext,
520 PNET_PNP_EVENT PnPEvent)
521 {
522 switch(PnPEvent->NetEvent)
523 {
524 case NetEventSetPower:
525 DbgPrint("Device transitioned to power state %ld\n", PnPEvent->Buffer);
526 return NDIS_STATUS_SUCCESS;
527
528 case NetEventQueryPower:
529 DbgPrint("Device wants to go into power state %ld\n", PnPEvent->Buffer);
530 return NDIS_STATUS_SUCCESS;
531
532 case NetEventQueryRemoveDevice:
533 DbgPrint("Device is about to be removed\n");
534 return NDIS_STATUS_SUCCESS;
535
536 case NetEventCancelRemoveDevice:
537 DbgPrint("Device removal cancelled\n");
538 return NDIS_STATUS_SUCCESS;
539
540 default:
541 DbgPrint("Unhandled event type: %ld\n", PnPEvent->NetEvent);
542 return NDIS_STATUS_SUCCESS;
543 }
544 }
545
546 VOID NTAPI ProtocolStatusComplete(
547 NDIS_HANDLE NdisBindingContext)
548 /*
549 * FUNCTION: Called by NDIS when a status-change has occurred
550 * ARGUMENTS:
551 * BindingContext = Pointer to a device context (LAN_ADAPTER)
552 */
553 {
554 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
555 }
556
557 VOID NTAPI ProtocolBindAdapter(
558 OUT PNDIS_STATUS Status,
559 IN NDIS_HANDLE BindContext,
560 IN PNDIS_STRING DeviceName,
561 IN PVOID SystemSpecific1,
562 IN PVOID SystemSpecific2)
563 /*
564 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
565 * bindings, and periodically thereafer as new adapters come online
566 * ARGUMENTS:
567 * Status: Return value to NDIS
568 * BindContext: Handle provided by NDIS to track pending binding operations
569 * DeviceName: Name of the miniport device to bind to
570 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
571 * SystemSpecific2: Unused & must not be touched
572 */
573 {
574 /* XXX confirm that this is still true, or re-word the following comment */
575 /* we get to ignore BindContext because we will never pend an operation with NDIS */
576 TI_DbgPrint(DEBUG_DATALINK, ("Called with registry path %wZ for %wZ\n", SystemSpecific1, DeviceName));
577 *Status = LANRegisterAdapter(DeviceName, SystemSpecific1);
578 }
579
580
581 VOID LANTransmit(
582 PVOID Context,
583 PNDIS_PACKET NdisPacket,
584 UINT Offset,
585 PVOID LinkAddress,
586 USHORT Type)
587 /*
588 * FUNCTION: Transmits a packet
589 * ARGUMENTS:
590 * Context = Pointer to context information (LAN_ADAPTER)
591 * NdisPacket = Pointer to NDIS packet to send
592 * Offset = Offset in packet where data starts
593 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
594 * Type = LAN protocol type (LAN_PROTO_*)
595 */
596 {
597 NDIS_STATUS NdisStatus;
598 PETH_HEADER EHeader;
599 PCHAR Data;
600 UINT Size;
601 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)Context;
602 KIRQL OldIrql;
603 UINT PacketLength;
604
605 TI_DbgPrint(DEBUG_DATALINK,
606 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
607 NdisPacket, Offset, Adapter));
608
609 if (Adapter->State != LAN_STATE_STARTED) {
610 ProtocolSendComplete(Context, NdisPacket, NDIS_STATUS_NOT_ACCEPTED);
611 return;
612 }
613
614 TI_DbgPrint(DEBUG_DATALINK,
615 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
616 Adapter->HWAddress[0] & 0xff,
617 Adapter->HWAddress[1] & 0xff,
618 Adapter->HWAddress[2] & 0xff,
619 Adapter->HWAddress[3] & 0xff,
620 Adapter->HWAddress[4] & 0xff,
621 Adapter->HWAddress[5] & 0xff));
622
623 /* XXX arty -- Handled adjustment in a saner way than before ...
624 * not needed immediately */
625 GetDataPtr( NdisPacket, 0, &Data, &Size );
626
627 switch (Adapter->Media) {
628 case NdisMedium802_3:
629 EHeader = (PETH_HEADER)Data;
630
631 if (LinkAddress) {
632 /* Unicast address */
633 RtlCopyMemory(EHeader->DstAddr, LinkAddress, IEEE_802_ADDR_LENGTH);
634 } else {
635 /* Broadcast address */
636 RtlFillMemory(EHeader->DstAddr, IEEE_802_ADDR_LENGTH, 0xFF);
637 }
638
639 RtlCopyMemory(EHeader->SrcAddr, Adapter->HWAddress, IEEE_802_ADDR_LENGTH);
640
641 switch (Type) {
642 case LAN_PROTO_IPv4:
643 EHeader->EType = ETYPE_IPv4;
644 break;
645 case LAN_PROTO_ARP:
646 EHeader->EType = ETYPE_ARP;
647 break;
648 case LAN_PROTO_IPv6:
649 EHeader->EType = ETYPE_IPv6;
650 break;
651 default:
652 #if DBG
653 /* Should not happen */
654 TI_DbgPrint(MIN_TRACE, ("Unknown LAN protocol.\n"));
655
656 ProtocolSendComplete((NDIS_HANDLE)Context,
657 NdisPacket,
658 NDIS_STATUS_FAILURE);
659 #endif
660 return;
661 }
662 break;
663
664 default:
665 /* FIXME: Support other medias */
666 break;
667 }
668
669 TI_DbgPrint( MID_TRACE, ("LinkAddress: %x\n", LinkAddress));
670 if( LinkAddress ) {
671 TI_DbgPrint
672 ( MID_TRACE,
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));
680 }
681
682 NdisQueryPacketLength(NdisPacket, &PacketLength);
683
684 if (Adapter->MTU < PacketLength) {
685 /* This is NOT a pointer. MSDN explicitly says so. */
686 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket,
687 TcpLargeSendPacketInfo) = (PVOID)((ULONG)Adapter->MTU);
688 }
689
690 TcpipAcquireSpinLock( &Adapter->Lock, &OldIrql );
691 TI_DbgPrint(MID_TRACE, ("NdisSend\n"));
692 NdisSend(&NdisStatus, Adapter->NdisHandle, NdisPacket);
693 TI_DbgPrint(MID_TRACE, ("NdisSend %s\n",
694 NdisStatus == NDIS_STATUS_PENDING ?
695 "Pending" : "Complete"));
696 TcpipReleaseSpinLock( &Adapter->Lock, OldIrql );
697
698 /* I had a talk with vizzini: these really ought to be here.
699 * we're supposed to see these completed by ndis *only* when
700 * status_pending is returned. Note that this is different from
701 * the situation with IRPs. */
702 if (NdisStatus != NDIS_STATUS_PENDING)
703 ProtocolSendComplete((NDIS_HANDLE)Context, NdisPacket, NdisStatus);
704 }
705
706 static NTSTATUS
707 OpenRegistryKey( PNDIS_STRING RegistryPath, PHANDLE RegHandle ) {
708 OBJECT_ATTRIBUTES Attributes;
709 NTSTATUS Status;
710
711 InitializeObjectAttributes(&Attributes, RegistryPath, OBJ_CASE_INSENSITIVE, 0, 0);
712 Status = ZwOpenKey(RegHandle, KEY_ALL_ACCESS, &Attributes);
713 return Status;
714 }
715
716 static NTSTATUS ReadStringFromRegistry( HANDLE RegHandle,
717 PWCHAR RegistryValue,
718 PUNICODE_STRING String ) {
719 UNICODE_STRING ValueName;
720 UNICODE_STRING UnicodeString;
721 NTSTATUS Status;
722 ULONG ResultLength;
723 UCHAR buf[1024];
724 PKEY_VALUE_PARTIAL_INFORMATION Information = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
725
726 RtlInitUnicodeString(&ValueName, RegistryValue);
727 Status =
728 ZwQueryValueKey(RegHandle,
729 &ValueName,
730 KeyValuePartialInformation,
731 Information,
732 sizeof(buf),
733 &ResultLength);
734
735 if (!NT_SUCCESS(Status))
736 return Status;
737 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
738 TI_DbgPrint(MIN_TRACE, ("Information DataLength: 0x%x\n", Information->DataLength));
739
740 UnicodeString.Buffer = (PWCHAR)&Information->Data;
741 UnicodeString.Length = Information->DataLength - sizeof(WCHAR);
742 UnicodeString.MaximumLength = Information->DataLength;
743
744 String->Buffer =
745 (PWCHAR)ExAllocatePool( NonPagedPool,
746 UnicodeString.MaximumLength + sizeof(WCHAR) );
747
748 if( !String->Buffer ) return STATUS_NO_MEMORY;
749
750 String->MaximumLength = UnicodeString.MaximumLength;
751 RtlCopyUnicodeString( String, &UnicodeString );
752
753 return STATUS_SUCCESS;
754 }
755
756 /*
757 * Utility to copy and append two unicode strings.
758 *
759 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
760 * IN PUNICODE_STRING Second -> Second string to append
761 * IN BOOL Deallocate -> TRUE: Deallocate First string before
762 * overwriting.
763 *
764 * Returns NTSTATUS.
765 */
766
767 NTSTATUS NTAPI AppendUnicodeString(PUNICODE_STRING ResultFirst,
768 PUNICODE_STRING Second,
769 BOOLEAN Deallocate) {
770 NTSTATUS Status;
771 UNICODE_STRING Ustr = *ResultFirst;
772 PWSTR new_string = ExAllocatePoolWithTag
773 (PagedPool,
774 (ResultFirst->Length + Second->Length + sizeof(WCHAR)), TAG_STRING);
775 if( !new_string ) {
776 return STATUS_NO_MEMORY;
777 }
778 memcpy( new_string, ResultFirst->Buffer, ResultFirst->Length );
779 memcpy( new_string + ResultFirst->Length / sizeof(WCHAR),
780 Second->Buffer, Second->Length );
781 if( Deallocate ) RtlFreeUnicodeString(ResultFirst);
782 ResultFirst->Length = Ustr.Length + Second->Length;
783 ResultFirst->MaximumLength = ResultFirst->Length;
784 new_string[ResultFirst->Length / sizeof(WCHAR)] = 0;
785 Status = RtlCreateUnicodeString(ResultFirst,new_string) ?
786 STATUS_SUCCESS : STATUS_NO_MEMORY;
787 ExFreePool(new_string);
788 return Status;
789 }
790
791 static NTSTATUS CheckForDeviceDesc( PUNICODE_STRING EnumKeyName,
792 PUNICODE_STRING TargetKeyName,
793 PUNICODE_STRING Name,
794 PUNICODE_STRING DeviceDesc ) {
795 UNICODE_STRING RootDevice = { 0, 0, NULL }, LinkageKeyName = { 0, 0, NULL };
796 UNICODE_STRING DescKeyName = { 0, 0, NULL }, Linkage = { 0, 0, NULL };
797 UNICODE_STRING BackSlash = { 0, 0, NULL };
798 HANDLE DescKey = NULL, LinkageKey = NULL;
799 NTSTATUS Status;
800
801 TI_DbgPrint(DEBUG_DATALINK,("EnumKeyName %wZ\n", EnumKeyName));
802
803 RtlInitUnicodeString(&BackSlash, L"\\");
804 RtlInitUnicodeString(&Linkage, L"\\Linkage");
805
806 RtlInitUnicodeString(&DescKeyName, L"");
807 AppendUnicodeString( &DescKeyName, EnumKeyName, FALSE );
808 AppendUnicodeString( &DescKeyName, &BackSlash, TRUE );
809 AppendUnicodeString( &DescKeyName, TargetKeyName, TRUE );
810
811 RtlInitUnicodeString(&LinkageKeyName, L"");
812 AppendUnicodeString( &LinkageKeyName, &DescKeyName, FALSE );
813 AppendUnicodeString( &LinkageKeyName, &Linkage, TRUE );
814
815 Status = OpenRegistryKey( &LinkageKeyName, &LinkageKey );
816 if( !NT_SUCCESS(Status) ) goto cleanup;
817
818 Status = ReadStringFromRegistry( LinkageKey, L"RootDevice", &RootDevice );
819 if( !NT_SUCCESS(Status) ) goto cleanup;
820
821 if( RtlCompareUnicodeString( &RootDevice, Name, TRUE ) == 0 ) {
822 Status = OpenRegistryKey( &DescKeyName, &DescKey );
823 if( !NT_SUCCESS(Status) ) goto cleanup;
824
825 Status = ReadStringFromRegistry( DescKey, L"DriverDesc", DeviceDesc );
826 if( !NT_SUCCESS(Status) ) goto cleanup;
827
828 TI_DbgPrint(DEBUG_DATALINK,("ADAPTER DESC: %wZ\n", DeviceDesc));
829 } else Status = STATUS_UNSUCCESSFUL;
830
831 cleanup:
832 RtlFreeUnicodeString( &RootDevice );
833 RtlFreeUnicodeString( &LinkageKeyName );
834 RtlFreeUnicodeString( &DescKeyName );
835 if( LinkageKey ) NtClose( LinkageKey );
836 if( DescKey ) NtClose( DescKey );
837
838 TI_DbgPrint(DEBUG_DATALINK,("Returning %x\n", Status));
839
840 return Status;
841 }
842
843 static NTSTATUS FindDeviceDescForAdapter( PUNICODE_STRING Name,
844 PUNICODE_STRING DeviceDesc ) {
845 UNICODE_STRING EnumKeyName, TargetKeyName;
846 HANDLE EnumKey;
847 NTSTATUS Status;
848 ULONG i;
849 KEY_BASIC_INFORMATION *Kbio =
850 ExAllocatePool(NonPagedPool, sizeof(KEY_BASIC_INFORMATION));
851 ULONG KbioLength = sizeof(KEY_BASIC_INFORMATION), ResultLength;
852
853 if( !Kbio ) return STATUS_INSUFFICIENT_RESOURCES;
854
855 RtlInitUnicodeString
856 (&EnumKeyName, CCS_ROOT L"\\Control\\Class\\" TCPIP_GUID);
857
858 Status = OpenRegistryKey( &EnumKeyName, &EnumKey );
859
860 if( !NT_SUCCESS(Status) ) {
861 TI_DbgPrint(DEBUG_DATALINK,("Couldn't open Enum key %wZ: %x\n",
862 &EnumKeyName, Status));
863 ExFreePool( Kbio );
864 return Status;
865 }
866
867 for( i = 0; NT_SUCCESS(Status); i++ ) {
868 Status = ZwEnumerateKey( EnumKey, i, KeyBasicInformation,
869 Kbio, KbioLength, &ResultLength );
870
871 if( Status == STATUS_BUFFER_TOO_SMALL || Status == STATUS_BUFFER_OVERFLOW ) {
872 ExFreePool( Kbio );
873 KbioLength = ResultLength;
874 Kbio = ExAllocatePool( NonPagedPool, KbioLength );
875 if( !Kbio ) {
876 TI_DbgPrint(DEBUG_DATALINK,("Failed to allocate memory\n"));
877 NtClose( EnumKey );
878 return STATUS_NO_MEMORY;
879 }
880
881 Status = ZwEnumerateKey( EnumKey, i, KeyBasicInformation,
882 Kbio, KbioLength, &ResultLength );
883
884 if( !NT_SUCCESS(Status) ) {
885 TI_DbgPrint(DEBUG_DATALINK,("Couldn't enum key child %d\n", i));
886 NtClose( EnumKey );
887 ExFreePool( Kbio );
888 return Status;
889 }
890 }
891
892 if( NT_SUCCESS(Status) ) {
893 TargetKeyName.Length = TargetKeyName.MaximumLength =
894 Kbio->NameLength;
895 TargetKeyName.Buffer = Kbio->Name;
896
897 Status = CheckForDeviceDesc
898 ( &EnumKeyName, &TargetKeyName, Name, DeviceDesc );
899 if( NT_SUCCESS(Status) ) {
900 NtClose( EnumKey );
901 ExFreePool( Kbio );
902 return Status;
903 } else Status = STATUS_SUCCESS;
904 }
905 }
906
907 RtlInitUnicodeString( DeviceDesc, L"" );
908 AppendUnicodeString( DeviceDesc, &TargetKeyName, FALSE );
909 NtClose( EnumKey );
910 ExFreePool( Kbio );
911 return STATUS_UNSUCCESSFUL;
912 }
913
914 VOID GetName( PUNICODE_STRING RegistryKey,
915 PUNICODE_STRING OutName ) {
916 PWCHAR Ptr;
917 UNICODE_STRING PartialRegistryKey;
918
919 PartialRegistryKey.Buffer =
920 RegistryKey->Buffer + wcslen(CCS_ROOT L"\\Services\\");
921 Ptr = PartialRegistryKey.Buffer;
922
923 while( *Ptr != L'\\' &&
924 ((PCHAR)Ptr) < ((PCHAR)RegistryKey->Buffer) + RegistryKey->Length )
925 Ptr++;
926
927 PartialRegistryKey.Length = PartialRegistryKey.MaximumLength =
928 (Ptr - PartialRegistryKey.Buffer) * sizeof(WCHAR);
929
930 RtlInitUnicodeString( OutName, L"" );
931 AppendUnicodeString( OutName, &PartialRegistryKey, FALSE );
932 }
933
934 BOOLEAN BindAdapter(
935 PLAN_ADAPTER Adapter,
936 PNDIS_STRING RegistryPath)
937 /*
938 * FUNCTION: Binds a LAN adapter to IP layer
939 * ARGUMENTS:
940 * Adapter = Pointer to LAN_ADAPTER structure
941 * NOTES:
942 * We set the lookahead buffer size, set the packet filter and
943 * bind the adapter to IP layer
944 */
945 {
946 PIP_INTERFACE IF;
947 NDIS_STATUS NdisStatus;
948 LLIP_BIND_INFO BindInfo;
949 IP_ADDRESS DefaultMask;
950 ULONG Lookahead = LOOKAHEAD_SIZE;
951 NTSTATUS Status;
952
953 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
954
955 Adapter->State = LAN_STATE_OPENING;
956
957 NdisStatus = NDISCall(Adapter,
958 NdisRequestSetInformation,
959 OID_GEN_CURRENT_LOOKAHEAD,
960 &Lookahead,
961 sizeof(ULONG));
962 if (NdisStatus != NDIS_STATUS_SUCCESS) {
963 TI_DbgPrint(DEBUG_DATALINK, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus));
964 return FALSE;
965 }
966
967 /* Bind the adapter to IP layer */
968 BindInfo.Context = Adapter;
969 BindInfo.HeaderSize = Adapter->HeaderSize;
970 BindInfo.MinFrameSize = Adapter->MinFrameSize;
971 BindInfo.MTU = Adapter->MTU;
972 BindInfo.Address = (PUCHAR)&Adapter->HWAddress;
973 BindInfo.AddressLength = Adapter->HWAddressLength;
974 BindInfo.Transmit = LANTransmit;
975
976 IF = IPCreateInterface(&BindInfo);
977
978 if (!IF) {
979 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
980 return FALSE;
981 }
982
983 /*
984 * Query per-adapter configuration from the registry
985 * In case anyone is curious: there *is* an Ndis configuration api
986 * for this sort of thing, but it doesn't really support things like
987 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
988 * protocol drivers developed for win2k and above just use the native
989 * services (ZwOpenKey, etc).
990 */
991
992 GetName( RegistryPath, &IF->Name );
993
994 Status = FindDeviceDescForAdapter( &IF->Name, &IF->Description );
995
996 TI_DbgPrint(DEBUG_DATALINK,("Adapter Description: %wZ\n",
997 &IF->Description));
998
999 AddrInitIPv4(&DefaultMask, 0);
1000
1001 IF->Unicast = DefaultMask;
1002 IF->Netmask = DefaultMask;
1003
1004 IF->Broadcast.Type = IP_ADDRESS_V4;
1005 IF->Broadcast.Address.IPv4Address =
1006 IF->Unicast.Address.IPv4Address |
1007 ~IF->Netmask.Address.IPv4Address;
1008
1009 TI_DbgPrint(DEBUG_DATALINK,("BCAST(IF) %s\n", A2S(&IF->Broadcast)));
1010
1011 /* Get maximum link speed */
1012 NdisStatus = NDISCall(Adapter,
1013 NdisRequestQueryInformation,
1014 OID_GEN_LINK_SPEED,
1015 &IF->Speed,
1016 sizeof(UINT));
1017
1018 if( !NT_SUCCESS(NdisStatus) )
1019 IF->Speed = IP_DEFAULT_LINK_SPEED;
1020
1021 /* Register interface with IP layer */
1022 IPRegisterInterface(IF);
1023
1024 /* Set packet filter so we can send and receive packets */
1025 NdisStatus = NDISCall(Adapter,
1026 NdisRequestSetInformation,
1027 OID_GEN_CURRENT_PACKET_FILTER,
1028 &Adapter->PacketFilter,
1029 sizeof(UINT));
1030
1031 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1032 TI_DbgPrint(DEBUG_DATALINK, ("Could not set packet filter (0x%X).\n", NdisStatus));
1033 IPUnregisterInterface(IF);
1034 IPDestroyInterface(IF);
1035 return FALSE;
1036 }
1037
1038 Adapter->Context = IF;
1039 Adapter->State = LAN_STATE_STARTED;
1040 return TRUE;
1041 }
1042
1043
1044 VOID UnbindAdapter(
1045 PLAN_ADAPTER Adapter)
1046 /*
1047 * FUNCTION: Unbinds a LAN adapter from IP layer
1048 * ARGUMENTS:
1049 * Adapter = Pointer to LAN_ADAPTER structure
1050 */
1051 {
1052 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1053
1054 if (Adapter->State == LAN_STATE_STARTED) {
1055 PIP_INTERFACE IF = Adapter->Context;
1056
1057 IPUnregisterInterface(IF);
1058
1059 IPDestroyInterface(IF);
1060 }
1061 }
1062
1063
1064 NDIS_STATUS LANRegisterAdapter(
1065 PNDIS_STRING AdapterName,
1066 PNDIS_STRING RegistryPath)
1067 /*
1068 * FUNCTION: Registers protocol with an NDIS adapter
1069 * ARGUMENTS:
1070 * AdapterName = Pointer to string with name of adapter to register
1071 * Adapter = Address of pointer to a LAN_ADAPTER structure
1072 * RETURNS:
1073 * Status of operation
1074 */
1075 {
1076 PLAN_ADAPTER IF;
1077 NDIS_STATUS NdisStatus;
1078 NDIS_STATUS OpenStatus;
1079 UINT MediaIndex;
1080 NDIS_MEDIUM MediaArray[MAX_MEDIA];
1081 UINT AddressOID;
1082 UINT Speed;
1083
1084 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1085
1086 IF = exAllocatePool(NonPagedPool, sizeof(LAN_ADAPTER));
1087 if (!IF) {
1088 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1089 return NDIS_STATUS_RESOURCES;
1090 }
1091
1092 RtlZeroMemory(IF, sizeof(LAN_ADAPTER));
1093
1094 /* Put adapter in stopped state */
1095 IF->State = LAN_STATE_STOPPED;
1096
1097 /* Initialize protecting spin lock */
1098 KeInitializeSpinLock(&IF->Lock);
1099
1100 KeInitializeEvent(&IF->Event, SynchronizationEvent, FALSE);
1101
1102 /* Initialize array with media IDs we support */
1103 MediaArray[MEDIA_ETH] = NdisMedium802_3;
1104
1105 TI_DbgPrint(DEBUG_DATALINK,("opening adapter %wZ\n", AdapterName));
1106 /* Open the adapter. */
1107 NdisOpenAdapter(&NdisStatus,
1108 &OpenStatus,
1109 &IF->NdisHandle,
1110 &MediaIndex,
1111 MediaArray,
1112 MAX_MEDIA,
1113 NdisProtocolHandle,
1114 IF,
1115 AdapterName,
1116 0,
1117 NULL);
1118
1119 /* Wait until the adapter is opened */
1120 if (NdisStatus == NDIS_STATUS_PENDING)
1121 KeWaitForSingleObject(&IF->Event, UserRequest, KernelMode, FALSE, NULL);
1122 else if (NdisStatus != NDIS_STATUS_SUCCESS) {
1123 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ\n", AdapterName));
1124 exFreePool(IF);
1125 return NdisStatus;
1126 }
1127
1128 IF->Media = MediaArray[MediaIndex];
1129
1130 /* Fill LAN_ADAPTER structure with some adapter specific information */
1131 switch (IF->Media) {
1132 case NdisMedium802_3:
1133 IF->HWAddressLength = IEEE_802_ADDR_LENGTH;
1134 IF->BCastMask = BCAST_ETH_MASK;
1135 IF->BCastCheck = BCAST_ETH_CHECK;
1136 IF->BCastOffset = BCAST_ETH_OFFSET;
1137 IF->HeaderSize = sizeof(ETH_HEADER);
1138 IF->MinFrameSize = 60;
1139 AddressOID = OID_802_3_CURRENT_ADDRESS;
1140 IF->PacketFilter =
1141 NDIS_PACKET_TYPE_BROADCAST |
1142 NDIS_PACKET_TYPE_DIRECTED |
1143 NDIS_PACKET_TYPE_MULTICAST;
1144 break;
1145
1146 default:
1147 /* Unsupported media */
1148 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
1149 exFreePool(IF);
1150 return NDIS_STATUS_NOT_SUPPORTED;
1151 }
1152
1153 /* Get maximum frame size */
1154 NdisStatus = NDISCall(IF,
1155 NdisRequestQueryInformation,
1156 OID_GEN_MAXIMUM_FRAME_SIZE,
1157 &IF->MTU,
1158 sizeof(UINT));
1159 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1160 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ (NDISCall)\n", AdapterName));
1161 exFreePool(IF);
1162 return NdisStatus;
1163 }
1164
1165 /* Get maximum packet size */
1166 NdisStatus = NDISCall(IF,
1167 NdisRequestQueryInformation,
1168 OID_GEN_MAXIMUM_TOTAL_SIZE,
1169 &IF->MaxPacketSize,
1170 sizeof(UINT));
1171 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1172 TI_DbgPrint(MIN_TRACE, ("Query for maximum packet size failed.\n"));
1173 exFreePool(IF);
1174 return NdisStatus;
1175 }
1176
1177 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1178 NdisStatus = NDISCall(IF,
1179 NdisRequestQueryInformation,
1180 OID_GEN_MAXIMUM_SEND_PACKETS,
1181 &IF->MaxSendPackets,
1182 sizeof(UINT));
1183 if (NdisStatus != NDIS_STATUS_SUCCESS)
1184 /* Legacy NIC drivers may not support this query, if it fails we
1185 assume it can send at least one packet per call to NdisSend(Packets) */
1186 IF->MaxSendPackets = 1;
1187
1188 /* Get current hardware address */
1189 NdisStatus = NDISCall(IF,
1190 NdisRequestQueryInformation,
1191 AddressOID,
1192 &IF->HWAddress,
1193 IF->HWAddressLength);
1194 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1195 TI_DbgPrint(MIN_TRACE, ("Query for current hardware address failed.\n"));
1196 exFreePool(IF);
1197 return NdisStatus;
1198 }
1199
1200 /* Get maximum link speed */
1201 NdisStatus = NDISCall(IF,
1202 NdisRequestQueryInformation,
1203 OID_GEN_LINK_SPEED,
1204 &Speed,
1205 sizeof(UINT));
1206 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1207 TI_DbgPrint(MIN_TRACE, ("Query for maximum link speed failed.\n"));
1208 exFreePool(IF);
1209 return NdisStatus;
1210 }
1211
1212 /* Convert returned link speed to bps (it is in 100bps increments) */
1213 IF->Speed = Speed * 100L;
1214
1215 /* Bind adapter to IP layer */
1216 if( !BindAdapter(IF, RegistryPath) ) {
1217 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ (BindAdapter)\n", AdapterName));
1218 exFreePool(IF);
1219 return NDIS_STATUS_NOT_ACCEPTED;
1220 }
1221
1222 /* Add adapter to the adapter list */
1223 ExInterlockedInsertTailList(&AdapterListHead,
1224 &IF->ListEntry,
1225 &AdapterListLock);
1226
1227 TI_DbgPrint(DEBUG_DATALINK, ("Leaving.\n"));
1228
1229 return NDIS_STATUS_SUCCESS;
1230 }
1231
1232
1233 NDIS_STATUS LANUnregisterAdapter(
1234 PLAN_ADAPTER Adapter)
1235 /*
1236 * FUNCTION: Unregisters protocol with NDIS adapter
1237 * ARGUMENTS:
1238 * Adapter = Pointer to a LAN_ADAPTER structure
1239 * RETURNS:
1240 * Status of operation
1241 */
1242 {
1243 KIRQL OldIrql;
1244 NDIS_HANDLE NdisHandle;
1245 NDIS_STATUS NdisStatus = NDIS_STATUS_SUCCESS;
1246
1247 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1248
1249 /* Unlink the adapter from the list */
1250 RemoveEntryList(&Adapter->ListEntry);
1251
1252 /* Unbind adapter from IP layer */
1253 UnbindAdapter(Adapter);
1254
1255 TcpipAcquireSpinLock(&Adapter->Lock, &OldIrql);
1256 NdisHandle = Adapter->NdisHandle;
1257 if (NdisHandle) {
1258 Adapter->NdisHandle = NULL;
1259 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1260
1261 NdisCloseAdapter(&NdisStatus, NdisHandle);
1262 if (NdisStatus == NDIS_STATUS_PENDING) {
1263 TcpipWaitForSingleObject(&Adapter->Event,
1264 UserRequest,
1265 KernelMode,
1266 FALSE,
1267 NULL);
1268 NdisStatus = Adapter->NdisStatus;
1269 }
1270 } else
1271 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1272
1273 FreeAdapter(Adapter);
1274
1275 return NdisStatus;
1276 }
1277
1278 VOID
1279 NTAPI
1280 LANUnregisterProtocol(VOID)
1281 /*
1282 * FUNCTION: Unregisters this protocol driver with NDIS
1283 * NOTES: Does not care wether we are already registered
1284 */
1285 {
1286 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1287
1288 if (ProtocolRegistered) {
1289 NDIS_STATUS NdisStatus;
1290 PLIST_ENTRY CurrentEntry;
1291 PLIST_ENTRY NextEntry;
1292 PLAN_ADAPTER Current;
1293 KIRQL OldIrql;
1294
1295 TcpipAcquireSpinLock(&AdapterListLock, &OldIrql);
1296
1297 /* Search the list and remove every adapter we find */
1298 CurrentEntry = AdapterListHead.Flink;
1299 while (CurrentEntry != &AdapterListHead) {
1300 NextEntry = CurrentEntry->Flink;
1301 Current = CONTAINING_RECORD(CurrentEntry, LAN_ADAPTER, ListEntry);
1302 /* Unregister it */
1303 LANUnregisterAdapter(Current);
1304 CurrentEntry = NextEntry;
1305 }
1306
1307 TcpipReleaseSpinLock(&AdapterListLock, OldIrql);
1308
1309 NdisDeregisterProtocol(&NdisStatus, NdisProtocolHandle);
1310 ProtocolRegistered = FALSE;
1311 }
1312 }
1313
1314 VOID
1315 NTAPI
1316 ProtocolUnbindAdapter(
1317 PNDIS_STATUS Status,
1318 NDIS_HANDLE ProtocolBindingContext,
1319 NDIS_HANDLE UnbindContext)
1320 {
1321 /* We don't pend any unbinding so we can just ignore UnbindContext */
1322 *Status = LANUnregisterAdapter((PLAN_ADAPTER)ProtocolBindingContext);
1323 }
1324
1325 NTSTATUS LANRegisterProtocol(
1326 PNDIS_STRING Name)
1327 /*
1328 * FUNCTION: Registers this protocol driver with NDIS
1329 * ARGUMENTS:
1330 * Name = Name of this protocol driver
1331 * RETURNS:
1332 * Status of operation
1333 */
1334 {
1335 NDIS_STATUS NdisStatus;
1336 NDIS_PROTOCOL_CHARACTERISTICS ProtChars;
1337
1338 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1339
1340 InitializeListHead(&AdapterListHead);
1341 KeInitializeSpinLock(&AdapterListLock);
1342
1343 /* Set up protocol characteristics */
1344 RtlZeroMemory(&ProtChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1345 ProtChars.MajorNdisVersion = NDIS_VERSION_MAJOR;
1346 ProtChars.MinorNdisVersion = NDIS_VERSION_MINOR;
1347 ProtChars.Name.Length = Name->Length;
1348 ProtChars.Name.Buffer = Name->Buffer;
1349 ProtChars.Name.MaximumLength = Name->MaximumLength;
1350 ProtChars.OpenAdapterCompleteHandler = ProtocolOpenAdapterComplete;
1351 ProtChars.CloseAdapterCompleteHandler = ProtocolCloseAdapterComplete;
1352 ProtChars.ResetCompleteHandler = ProtocolResetComplete;
1353 ProtChars.RequestCompleteHandler = ProtocolRequestComplete;
1354 ProtChars.SendCompleteHandler = ProtocolSendComplete;
1355 ProtChars.TransferDataCompleteHandler = ProtocolTransferDataComplete;
1356 ProtChars.ReceiveHandler = ProtocolReceive;
1357 ProtChars.ReceiveCompleteHandler = ProtocolReceiveComplete;
1358 ProtChars.StatusHandler = ProtocolStatus;
1359 ProtChars.StatusCompleteHandler = ProtocolStatusComplete;
1360 ProtChars.BindAdapterHandler = ProtocolBindAdapter;
1361 ProtChars.PnPEventHandler = ProtocolPnPEvent;
1362 ProtChars.UnbindAdapterHandler = ProtocolUnbindAdapter;
1363 ProtChars.UnloadHandler = LANUnregisterProtocol;
1364
1365 /* Try to register protocol */
1366 NdisRegisterProtocol(&NdisStatus,
1367 &NdisProtocolHandle,
1368 &ProtChars,
1369 sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1370 if (NdisStatus != NDIS_STATUS_SUCCESS)
1371 {
1372 TI_DbgPrint(DEBUG_DATALINK, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus));
1373 return (NTSTATUS)NdisStatus;
1374 }
1375
1376 ProtocolRegistered = TRUE;
1377
1378 return STATUS_SUCCESS;
1379 }
1380
1381 /* EOF */