Sync with trunk (48237)
[reactos.git] / 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 ExFreePoolWithTag(Adapter, LAN_ADAPTER_TAG);
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 FreeNdisPacket(Packet);
235 }
236
237 VOID LanReceiveWorker( PVOID Context ) {
238 UINT PacketType;
239 PLAN_WQ_ITEM WorkItem = (PLAN_WQ_ITEM)Context;
240 PNDIS_PACKET Packet;
241 PLAN_ADAPTER Adapter;
242 UINT BytesTransferred;
243 PNDIS_BUFFER NdisBuffer;
244 IP_PACKET IPPacket;
245
246 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
247
248 Packet = WorkItem->Packet;
249 Adapter = WorkItem->Adapter;
250 BytesTransferred = WorkItem->BytesTransferred;
251
252 ExFreePoolWithTag(WorkItem, WQ_CONTEXT_TAG);
253
254 IPInitializePacket(&IPPacket, 0);
255
256 IPPacket.NdisPacket = Packet;
257
258 NdisGetFirstBufferFromPacket(Packet,
259 &NdisBuffer,
260 &IPPacket.Header,
261 &IPPacket.ContigSize,
262 &IPPacket.TotalSize);
263
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 */
267
268 TI_DbgPrint(MID_TRACE,
269 ("ContigSize: %d, TotalSize: %d, BytesTransferred: %d\n",
270 IPPacket.ContigSize, IPPacket.TotalSize,
271 BytesTransferred));
272
273 PacketType = PC(IPPacket.NdisPacket)->PacketType;
274 IPPacket.Position = 0;
275
276 TI_DbgPrint
277 (DEBUG_DATALINK,
278 ("Ether Type = %x ContigSize = %d Total = %d\n",
279 PacketType, IPPacket.ContigSize, IPPacket.TotalSize));
280
281 switch (PacketType) {
282 case ETYPE_IPv4:
283 case ETYPE_IPv6:
284 TI_DbgPrint(MID_TRACE,("Received IP Packet\n"));
285 IPReceive(Adapter->Context, &IPPacket);
286 break;
287 case ETYPE_ARP:
288 TI_DbgPrint(MID_TRACE,("Received ARP Packet\n"));
289 ARPReceive(Adapter->Context, &IPPacket);
290 default:
291 IPPacket.Free(&IPPacket);
292 break;
293 }
294
295 FreeNdisPacket( Packet );
296 }
297
298 VOID LanSubmitReceiveWork(
299 NDIS_HANDLE BindingContext,
300 PNDIS_PACKET Packet,
301 NDIS_STATUS Status,
302 UINT BytesTransferred) {
303 PLAN_WQ_ITEM WQItem = ExAllocatePoolWithTag(NonPagedPool, sizeof(LAN_WQ_ITEM),
304 WQ_CONTEXT_TAG);
305 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
306
307 TI_DbgPrint(DEBUG_DATALINK,("called\n"));
308
309 if (!WQItem) return;
310
311 WQItem->Packet = Packet;
312 WQItem->Adapter = Adapter;
313 WQItem->BytesTransferred = BytesTransferred;
314
315 if (!ChewCreate( LanReceiveWorker, WQItem ))
316 ExFreePoolWithTag(WQItem, WQ_CONTEXT_TAG);
317 }
318
319 VOID NTAPI ProtocolTransferDataComplete(
320 NDIS_HANDLE BindingContext,
321 PNDIS_PACKET Packet,
322 NDIS_STATUS Status,
323 UINT BytesTransferred)
324 /*
325 * FUNCTION: Called by NDIS to complete reception of data
326 * ARGUMENTS:
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
331 * NOTES:
332 * If the packet was successfully received, determine the protocol
333 * type and pass it to the correct receive handler
334 */
335 {
336 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
337
338 TI_DbgPrint(DEBUG_DATALINK,("called\n"));
339
340 TransferDataCompleteCalled++;
341 ASSERT(TransferDataCompleteCalled <= TransferDataCalled);
342
343 if( Status != NDIS_STATUS_SUCCESS ) return;
344
345 LanSubmitReceiveWork( BindingContext, Packet, Status, BytesTransferred );
346 }
347
348 NDIS_STATUS NTAPI ProtocolReceive(
349 NDIS_HANDLE BindingContext,
350 NDIS_HANDLE MacReceiveContext,
351 PVOID HeaderBuffer,
352 UINT HeaderBufferSize,
353 PVOID LookaheadBuffer,
354 UINT LookaheadBufferSize,
355 UINT PacketSize)
356 /*
357 * FUNCTION: Called by NDIS when a packet has been received on the physical link
358 * ARGUMENTS:
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)
366 * RETURNS:
367 * Status of operation
368 */
369 {
370 USHORT EType;
371 UINT PacketType, BytesTransferred;
372 UINT temp;
373 IP_PACKET IPPacket;
374 PCHAR BufferData;
375 NDIS_STATUS NdisStatus;
376 PNDIS_PACKET NdisPacket;
377 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
378 PETH_HEADER EHeader = (PETH_HEADER)HeaderBuffer;
379
380 TI_DbgPrint(DEBUG_DATALINK, ("Called. (packetsize %d)\n",PacketSize));
381
382 if (Adapter->State != LAN_STATE_STARTED) {
383 TI_DbgPrint(DEBUG_DATALINK, ("Adapter is stopped.\n"));
384 return NDIS_STATUS_NOT_ACCEPTED;
385 }
386
387 if (HeaderBufferSize < Adapter->HeaderSize) {
388 TI_DbgPrint(DEBUG_DATALINK, ("Runt frame received.\n"));
389 return NDIS_STATUS_NOT_ACCEPTED;
390 }
391
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;
402 }
403 /* We use EtherType constants to destinguish packet types */
404 PacketType = EType;
405 } else {
406 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
407 /* FIXME: Support other medias */
408 return NDIS_STATUS_NOT_ACCEPTED;
409 }
410
411 /* Get a transfer data packet */
412
413 TI_DbgPrint(DEBUG_DATALINK, ("Adapter: %x (MTU %d)\n",
414 Adapter, Adapter->MTU));
415
416 NdisStatus = AllocatePacketWithBuffer( &NdisPacket, NULL,
417 PacketSize );
418 if( NdisStatus != NDIS_STATUS_SUCCESS ) {
419 return NDIS_STATUS_NOT_ACCEPTED;
420 }
421
422 PC(NdisPacket)->PacketType = PacketType;
423
424 TI_DbgPrint(DEBUG_DATALINK, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize,PacketSize));
425
426 GetDataPtr( NdisPacket, 0, &BufferData, &temp );
427
428 IPPacket.NdisPacket = NdisPacket;
429 IPPacket.Position = 0;
430
431 TransferDataCalled++;
432
433 if (LookaheadBufferSize == PacketSize)
434 {
435 /* Optimized code path for packets that are fully contained in
436 * the lookahead buffer. */
437 NdisCopyLookaheadData(BufferData,
438 LookaheadBuffer,
439 LookaheadBufferSize,
440 Adapter->MacOptions);
441 }
442 else
443 {
444 NdisTransferData(&NdisStatus, Adapter->NdisHandle,
445 MacReceiveContext, 0, PacketSize,
446 NdisPacket, &BytesTransferred);
447 }
448 TI_DbgPrint(DEBUG_DATALINK, ("Calling complete\n"));
449
450 if (NdisStatus != NDIS_STATUS_PENDING)
451 ProtocolTransferDataComplete(BindingContext,
452 NdisPacket,
453 NdisStatus,
454 PacketSize);
455
456 TI_DbgPrint(DEBUG_DATALINK, ("leaving\n"));
457
458 return NDIS_STATUS_SUCCESS;
459 }
460
461
462 VOID NTAPI ProtocolReceiveComplete(
463 NDIS_HANDLE BindingContext)
464 /*
465 * FUNCTION: Called by NDIS when we're done receiving data
466 * ARGUMENTS:
467 * BindingContext = Pointer to a device context (LAN_ADAPTER)
468 */
469 {
470 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
471 }
472
473
474 VOID NTAPI ProtocolStatus(
475 NDIS_HANDLE BindingContext,
476 NDIS_STATUS GeneralStatus,
477 PVOID StatusBuffer,
478 UINT StatusBufferSize)
479 /*
480 * FUNCTION: Called by NDIS when the underlying driver has changed state
481 * ARGUMENTS:
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
486 */
487 {
488 PLAN_ADAPTER Adapter = BindingContext;
489
490 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
491
492 switch(GeneralStatus)
493 {
494 case NDIS_STATUS_MEDIA_CONNECT:
495 DbgPrint("NDIS_STATUS_MEDIA_CONNECT\n");
496 break;
497
498 case NDIS_STATUS_MEDIA_DISCONNECT:
499 DbgPrint("NDIS_STATUS_MEDIA_DISCONNECT\n");
500 break;
501
502 case NDIS_STATUS_RESET_START:
503 Adapter->State = LAN_STATE_RESETTING;
504 break;
505
506 case NDIS_STATUS_RESET_END:
507 Adapter->State = LAN_STATE_STARTED;
508 break;
509
510 default:
511 DbgPrint("Unhandled status: %x", GeneralStatus);
512 break;
513 }
514 }
515
516 NDIS_STATUS NTAPI
517 ProtocolPnPEvent(
518 NDIS_HANDLE NdisBindingContext,
519 PNET_PNP_EVENT PnPEvent)
520 {
521 switch(PnPEvent->NetEvent)
522 {
523 case NetEventSetPower:
524 DbgPrint("Device transitioned to power state %ld\n", PnPEvent->Buffer);
525 return NDIS_STATUS_SUCCESS;
526
527 case NetEventQueryPower:
528 DbgPrint("Device wants to go into power state %ld\n", PnPEvent->Buffer);
529 return NDIS_STATUS_SUCCESS;
530
531 case NetEventQueryRemoveDevice:
532 DbgPrint("Device is about to be removed\n");
533 return NDIS_STATUS_SUCCESS;
534
535 case NetEventCancelRemoveDevice:
536 DbgPrint("Device removal cancelled\n");
537 return NDIS_STATUS_SUCCESS;
538
539 default:
540 DbgPrint("Unhandled event type: %ld\n", PnPEvent->NetEvent);
541 return NDIS_STATUS_SUCCESS;
542 }
543 }
544
545 VOID NTAPI ProtocolStatusComplete(
546 NDIS_HANDLE NdisBindingContext)
547 /*
548 * FUNCTION: Called by NDIS when a status-change has occurred
549 * ARGUMENTS:
550 * BindingContext = Pointer to a device context (LAN_ADAPTER)
551 */
552 {
553 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
554 }
555
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)
562 /*
563 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
564 * bindings, and periodically thereafer as new adapters come online
565 * ARGUMENTS:
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
571 */
572 {
573 /* XXX confirm that this is still true, or re-word the following comment */
574 /* we get to ignore BindContext because we will never pend an operation with NDIS */
575 TI_DbgPrint(DEBUG_DATALINK, ("Called with registry path %wZ for %wZ\n", SystemSpecific1, DeviceName));
576 *Status = LANRegisterAdapter(DeviceName, SystemSpecific1);
577 }
578
579
580 VOID LANTransmit(
581 PVOID Context,
582 PNDIS_PACKET NdisPacket,
583 UINT Offset,
584 PVOID LinkAddress,
585 USHORT Type)
586 /*
587 * FUNCTION: Transmits a packet
588 * ARGUMENTS:
589 * Context = Pointer to context information (LAN_ADAPTER)
590 * NdisPacket = Pointer to NDIS packet to send
591 * Offset = Offset in packet where data starts
592 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
593 * Type = LAN protocol type (LAN_PROTO_*)
594 */
595 {
596 NDIS_STATUS NdisStatus;
597 PETH_HEADER EHeader;
598 PCHAR Data, OldData;
599 UINT Size, OldSize;
600 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)Context;
601 KIRQL OldIrql;
602 PNDIS_PACKET XmitPacket;
603
604 TI_DbgPrint(DEBUG_DATALINK,
605 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
606 NdisPacket, Offset, Adapter));
607
608 if (Adapter->State != LAN_STATE_STARTED) {
609 (*PC(NdisPacket)->DLComplete)(PC(NdisPacket)->Context, NdisPacket, NDIS_STATUS_NOT_ACCEPTED);
610 return;
611 }
612
613 TI_DbgPrint(DEBUG_DATALINK,
614 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
615 Adapter->HWAddress[0] & 0xff,
616 Adapter->HWAddress[1] & 0xff,
617 Adapter->HWAddress[2] & 0xff,
618 Adapter->HWAddress[3] & 0xff,
619 Adapter->HWAddress[4] & 0xff,
620 Adapter->HWAddress[5] & 0xff));
621
622 GetDataPtr( NdisPacket, 0, &OldData, &OldSize );
623
624 NdisStatus = AllocatePacketWithBuffer(&XmitPacket, NULL, OldSize + Adapter->HeaderSize);
625 if (NdisStatus != NDIS_STATUS_SUCCESS) {
626 (*PC(NdisPacket)->DLComplete)(PC(NdisPacket)->Context, NdisPacket, NDIS_STATUS_RESOURCES);
627 return;
628 }
629
630 GetDataPtr(XmitPacket, 0, &Data, &Size);
631
632 RtlCopyMemory(Data + Adapter->HeaderSize, OldData, OldSize);
633
634 (*PC(NdisPacket)->DLComplete)(PC(NdisPacket)->Context, NdisPacket, NDIS_STATUS_SUCCESS);
635
636 switch (Adapter->Media) {
637 case NdisMedium802_3:
638 EHeader = (PETH_HEADER)Data;
639
640 if (LinkAddress) {
641 /* Unicast address */
642 RtlCopyMemory(EHeader->DstAddr, LinkAddress, IEEE_802_ADDR_LENGTH);
643 } else {
644 /* Broadcast address */
645 RtlFillMemory(EHeader->DstAddr, IEEE_802_ADDR_LENGTH, 0xFF);
646 }
647
648 RtlCopyMemory(EHeader->SrcAddr, Adapter->HWAddress, IEEE_802_ADDR_LENGTH);
649
650 switch (Type) {
651 case LAN_PROTO_IPv4:
652 EHeader->EType = ETYPE_IPv4;
653 break;
654 case LAN_PROTO_ARP:
655 EHeader->EType = ETYPE_ARP;
656 break;
657 case LAN_PROTO_IPv6:
658 EHeader->EType = ETYPE_IPv6;
659 break;
660 default:
661 ASSERT(FALSE);
662 return;
663 }
664 break;
665
666 default:
667 /* FIXME: Support other medias */
668 break;
669 }
670
671 TI_DbgPrint( MID_TRACE, ("LinkAddress: %x\n", LinkAddress));
672 if( LinkAddress ) {
673 TI_DbgPrint
674 ( MID_TRACE,
675 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
676 ((PCHAR)LinkAddress)[0] & 0xff,
677 ((PCHAR)LinkAddress)[1] & 0xff,
678 ((PCHAR)LinkAddress)[2] & 0xff,
679 ((PCHAR)LinkAddress)[3] & 0xff,
680 ((PCHAR)LinkAddress)[4] & 0xff,
681 ((PCHAR)LinkAddress)[5] & 0xff));
682 }
683
684 if (Adapter->MTU < Size) {
685 /* This is NOT a pointer. MSDN explicitly says so. */
686 NDIS_PER_PACKET_INFO_FROM_PACKET(NdisPacket,
687 TcpLargeSendPacketInfo) = (PVOID)((ULONG_PTR)Adapter->MTU);
688 }
689
690 TcpipAcquireSpinLock( &Adapter->Lock, &OldIrql );
691 TI_DbgPrint(MID_TRACE, ("NdisSend\n"));
692 NdisSend(&NdisStatus, Adapter->NdisHandle, XmitPacket);
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, XmitPacket, 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 = ExAllocatePool
773 (PagedPool,
774 (ResultFirst->Length + Second->Length + sizeof(WCHAR)));
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 RtlInitUnicodeString( DeviceDesc, NULL );
854
855 if( !Kbio ) return STATUS_INSUFFICIENT_RESOURCES;
856
857 RtlInitUnicodeString
858 (&EnumKeyName, CCS_ROOT L"\\Control\\Class\\" TCPIP_GUID);
859
860 Status = OpenRegistryKey( &EnumKeyName, &EnumKey );
861
862 if( !NT_SUCCESS(Status) ) {
863 TI_DbgPrint(DEBUG_DATALINK,("Couldn't open Enum key %wZ: %x\n",
864 &EnumKeyName, Status));
865 ExFreePool( Kbio );
866 return Status;
867 }
868
869 for( i = 0; NT_SUCCESS(Status); i++ ) {
870 Status = ZwEnumerateKey( EnumKey, i, KeyBasicInformation,
871 Kbio, KbioLength, &ResultLength );
872
873 if( Status == STATUS_BUFFER_TOO_SMALL || Status == STATUS_BUFFER_OVERFLOW ) {
874 ExFreePool( Kbio );
875 KbioLength = ResultLength;
876 Kbio = ExAllocatePool( NonPagedPool, KbioLength );
877 if( !Kbio ) {
878 TI_DbgPrint(DEBUG_DATALINK,("Failed to allocate memory\n"));
879 NtClose( EnumKey );
880 return STATUS_NO_MEMORY;
881 }
882
883 Status = ZwEnumerateKey( EnumKey, i, KeyBasicInformation,
884 Kbio, KbioLength, &ResultLength );
885
886 if( !NT_SUCCESS(Status) ) {
887 TI_DbgPrint(DEBUG_DATALINK,("Couldn't enum key child %d\n", i));
888 NtClose( EnumKey );
889 ExFreePool( Kbio );
890 return Status;
891 }
892 }
893
894 if( NT_SUCCESS(Status) ) {
895 TargetKeyName.Length = TargetKeyName.MaximumLength =
896 Kbio->NameLength;
897 TargetKeyName.Buffer = Kbio->Name;
898
899 Status = CheckForDeviceDesc
900 ( &EnumKeyName, &TargetKeyName, Name, DeviceDesc );
901 if( NT_SUCCESS(Status) ) {
902 NtClose( EnumKey );
903 ExFreePool( Kbio );
904 return Status;
905 } else Status = STATUS_SUCCESS;
906 }
907 }
908
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, Router;
950 ULONG Lookahead = LOOKAHEAD_SIZE, Unused;
951 NTSTATUS Status;
952 OBJECT_ATTRIBUTES ObjectAttributes;
953 HANDLE ParameterHandle;
954 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInfo;
955 UNICODE_STRING IPAddress = RTL_CONSTANT_STRING(L"IPAddress");
956 UNICODE_STRING Netmask = RTL_CONSTANT_STRING(L"SubnetMask");
957 UNICODE_STRING Gateway = RTL_CONSTANT_STRING(L"DefaultGateway");
958 UNICODE_STRING EnableDhcp = RTL_CONSTANT_STRING(L"EnableDHCP");
959 UNICODE_STRING RegistryDataU;
960 ANSI_STRING RegistryDataA;
961
962 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
963
964 Adapter->State = LAN_STATE_OPENING;
965
966 NdisStatus = NDISCall(Adapter,
967 NdisRequestSetInformation,
968 OID_GEN_CURRENT_LOOKAHEAD,
969 &Lookahead,
970 sizeof(ULONG));
971 if (NdisStatus != NDIS_STATUS_SUCCESS) {
972 TI_DbgPrint(DEBUG_DATALINK, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus));
973 return FALSE;
974 }
975
976 /* Bind the adapter to IP layer */
977 BindInfo.Context = Adapter;
978 BindInfo.HeaderSize = Adapter->HeaderSize;
979 BindInfo.MinFrameSize = Adapter->MinFrameSize;
980 BindInfo.MTU = Adapter->MTU;
981 BindInfo.Address = (PUCHAR)&Adapter->HWAddress;
982 BindInfo.AddressLength = Adapter->HWAddressLength;
983 BindInfo.Transmit = LANTransmit;
984
985 IF = IPCreateInterface(&BindInfo);
986
987 if (!IF) {
988 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
989 return FALSE;
990 }
991
992 /*
993 * Query per-adapter configuration from the registry
994 * In case anyone is curious: there *is* an Ndis configuration api
995 * for this sort of thing, but it doesn't really support things like
996 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
997 * protocol drivers developed for win2k and above just use the native
998 * services (ZwOpenKey, etc).
999 */
1000
1001 GetName( RegistryPath, &IF->Name );
1002
1003 Status = FindDeviceDescForAdapter( &IF->Name, &IF->Description );
1004 if (!NT_SUCCESS(Status)) {
1005 TI_DbgPrint(MIN_TRACE, ("Failed to get device description.\n"));
1006 IPDestroyInterface(IF);
1007 return FALSE;
1008 }
1009
1010 TI_DbgPrint(DEBUG_DATALINK,("Adapter Description: %wZ\n",
1011 &IF->Description));
1012
1013 InitializeObjectAttributes(&ObjectAttributes,
1014 RegistryPath,
1015 OBJ_CASE_INSENSITIVE,
1016 0,
1017 NULL);
1018
1019 AddrInitIPv4(&DefaultMask, 0);
1020
1021 Status = ZwOpenKey(&ParameterHandle, KEY_READ, &ObjectAttributes);
1022 if (!NT_SUCCESS(Status))
1023 {
1024 IF->Unicast = DefaultMask;
1025 IF->Netmask = DefaultMask;
1026 }
1027 else
1028 {
1029 KeyValueInfo = ExAllocatePool(PagedPool, sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR));
1030 if (!KeyValueInfo)
1031 {
1032 ZwClose(ParameterHandle);
1033 IPDestroyInterface(IF);
1034 return FALSE;
1035 }
1036
1037 Status = ZwQueryValueKey(ParameterHandle,
1038 &EnableDhcp,
1039 KeyValuePartialInformation,
1040 KeyValueInfo,
1041 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG),
1042 &Unused);
1043 if (NT_SUCCESS(Status) && KeyValueInfo->DataLength == sizeof(ULONG) && (*(PULONG)KeyValueInfo->Data) != 0)
1044 {
1045 RegistryDataU.MaximumLength = 16 + sizeof(WCHAR);
1046 RegistryDataU.Buffer = (PWCHAR)KeyValueInfo->Data;
1047
1048 Status = ZwQueryValueKey(ParameterHandle,
1049 &IPAddress,
1050 KeyValuePartialInformation,
1051 KeyValueInfo,
1052 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
1053 &Unused);
1054 if (NT_SUCCESS(Status))
1055 {
1056 RegistryDataU.Length = KeyValueInfo->DataLength;
1057
1058 RtlUnicodeStringToAnsiString(&RegistryDataA,
1059 &RegistryDataU,
1060 TRUE);
1061
1062 AddrInitIPv4(&IF->Unicast, inet_addr(RegistryDataA.Buffer));
1063
1064 RtlFreeAnsiString(&RegistryDataA);
1065
1066 }
1067 else
1068 {
1069 IF->Unicast = DefaultMask;
1070 }
1071
1072 Status = ZwQueryValueKey(ParameterHandle,
1073 &Netmask,
1074 KeyValuePartialInformation,
1075 KeyValueInfo,
1076 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
1077 &Unused);
1078 if (NT_SUCCESS(Status))
1079 {
1080 RegistryDataU.Length = KeyValueInfo->DataLength;
1081
1082 RtlUnicodeStringToAnsiString(&RegistryDataA,
1083 &RegistryDataU,
1084 TRUE);
1085
1086 AddrInitIPv4(&IF->Netmask, inet_addr(RegistryDataA.Buffer));
1087
1088 RtlFreeAnsiString(&RegistryDataA);
1089 }
1090 else
1091 {
1092 IF->Netmask = DefaultMask;
1093 }
1094
1095 Status = ZwQueryValueKey(ParameterHandle,
1096 &Gateway,
1097 KeyValuePartialInformation,
1098 KeyValueInfo,
1099 sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 16 * sizeof(WCHAR),
1100 &Unused);
1101 if (NT_SUCCESS(Status))
1102 {
1103 RegistryDataU.Length = KeyValueInfo->DataLength;
1104
1105 RtlUnicodeStringToAnsiString(&RegistryDataA,
1106 &RegistryDataU,
1107 TRUE);
1108
1109 AddrInitIPv4(&Router, inet_addr(RegistryDataA.Buffer));
1110
1111 RtlFreeAnsiString(&RegistryDataA);
1112
1113 if (!AddrIsUnspecified(&Router)) RouterCreateRoute(&DefaultMask, &DefaultMask, &Router, IF, 1);
1114 }
1115 }
1116 else
1117 {
1118 IF->Unicast = DefaultMask;
1119 IF->Netmask = DefaultMask;
1120 }
1121
1122 ZwClose(ParameterHandle);
1123 }
1124
1125 IF->Broadcast.Type = IP_ADDRESS_V4;
1126 IF->Broadcast.Address.IPv4Address =
1127 IF->Unicast.Address.IPv4Address |
1128 ~IF->Netmask.Address.IPv4Address;
1129
1130 TI_DbgPrint(DEBUG_DATALINK,("BCAST(IF) %s\n", A2S(&IF->Broadcast)));
1131
1132 /* Get maximum link speed */
1133 NdisStatus = NDISCall(Adapter,
1134 NdisRequestQueryInformation,
1135 OID_GEN_LINK_SPEED,
1136 &IF->Speed,
1137 sizeof(UINT));
1138
1139 if( !NT_SUCCESS(NdisStatus) )
1140 IF->Speed = IP_DEFAULT_LINK_SPEED;
1141
1142 /* Register interface with IP layer */
1143 IPRegisterInterface(IF);
1144
1145 /* Set packet filter so we can send and receive packets */
1146 NdisStatus = NDISCall(Adapter,
1147 NdisRequestSetInformation,
1148 OID_GEN_CURRENT_PACKET_FILTER,
1149 &Adapter->PacketFilter,
1150 sizeof(UINT));
1151
1152 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1153 TI_DbgPrint(DEBUG_DATALINK, ("Could not set packet filter (0x%X).\n", NdisStatus));
1154 IPUnregisterInterface(IF);
1155 IPDestroyInterface(IF);
1156 return FALSE;
1157 }
1158
1159 Adapter->Context = IF;
1160 Adapter->State = LAN_STATE_STARTED;
1161 return TRUE;
1162 }
1163
1164
1165 VOID UnbindAdapter(
1166 PLAN_ADAPTER Adapter)
1167 /*
1168 * FUNCTION: Unbinds a LAN adapter from IP layer
1169 * ARGUMENTS:
1170 * Adapter = Pointer to LAN_ADAPTER structure
1171 */
1172 {
1173 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1174
1175 if (Adapter->State == LAN_STATE_STARTED) {
1176 PIP_INTERFACE IF = Adapter->Context;
1177
1178 IPUnregisterInterface(IF);
1179
1180 IPDestroyInterface(IF);
1181 }
1182 }
1183
1184
1185 NDIS_STATUS LANRegisterAdapter(
1186 PNDIS_STRING AdapterName,
1187 PNDIS_STRING RegistryPath)
1188 /*
1189 * FUNCTION: Registers protocol with an NDIS adapter
1190 * ARGUMENTS:
1191 * AdapterName = Pointer to string with name of adapter to register
1192 * Adapter = Address of pointer to a LAN_ADAPTER structure
1193 * RETURNS:
1194 * Status of operation
1195 */
1196 {
1197 PLAN_ADAPTER IF;
1198 NDIS_STATUS NdisStatus;
1199 NDIS_STATUS OpenStatus;
1200 UINT MediaIndex;
1201 NDIS_MEDIUM MediaArray[MAX_MEDIA];
1202 UINT AddressOID;
1203 UINT Speed;
1204
1205 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1206
1207 IF = ExAllocatePoolWithTag(NonPagedPool, sizeof(LAN_ADAPTER), LAN_ADAPTER_TAG);
1208 if (!IF) {
1209 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1210 return NDIS_STATUS_RESOURCES;
1211 }
1212
1213 RtlZeroMemory(IF, sizeof(LAN_ADAPTER));
1214
1215 /* Put adapter in stopped state */
1216 IF->State = LAN_STATE_STOPPED;
1217
1218 /* Initialize protecting spin lock */
1219 KeInitializeSpinLock(&IF->Lock);
1220
1221 KeInitializeEvent(&IF->Event, SynchronizationEvent, FALSE);
1222
1223 /* Initialize array with media IDs we support */
1224 MediaArray[MEDIA_ETH] = NdisMedium802_3;
1225
1226 TI_DbgPrint(DEBUG_DATALINK,("opening adapter %wZ\n", AdapterName));
1227 /* Open the adapter. */
1228 NdisOpenAdapter(&NdisStatus,
1229 &OpenStatus,
1230 &IF->NdisHandle,
1231 &MediaIndex,
1232 MediaArray,
1233 MAX_MEDIA,
1234 NdisProtocolHandle,
1235 IF,
1236 AdapterName,
1237 0,
1238 NULL);
1239
1240 /* Wait until the adapter is opened */
1241 if (NdisStatus == NDIS_STATUS_PENDING)
1242 KeWaitForSingleObject(&IF->Event, UserRequest, KernelMode, FALSE, NULL);
1243 else if (NdisStatus != NDIS_STATUS_SUCCESS) {
1244 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ\n", AdapterName));
1245 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1246 return NdisStatus;
1247 }
1248
1249 IF->Media = MediaArray[MediaIndex];
1250
1251 /* Fill LAN_ADAPTER structure with some adapter specific information */
1252 switch (IF->Media) {
1253 case NdisMedium802_3:
1254 IF->HWAddressLength = IEEE_802_ADDR_LENGTH;
1255 IF->BCastMask = BCAST_ETH_MASK;
1256 IF->BCastCheck = BCAST_ETH_CHECK;
1257 IF->BCastOffset = BCAST_ETH_OFFSET;
1258 IF->HeaderSize = sizeof(ETH_HEADER);
1259 IF->MinFrameSize = 60;
1260 AddressOID = OID_802_3_CURRENT_ADDRESS;
1261 IF->PacketFilter =
1262 NDIS_PACKET_TYPE_BROADCAST |
1263 NDIS_PACKET_TYPE_DIRECTED |
1264 NDIS_PACKET_TYPE_MULTICAST;
1265 break;
1266
1267 default:
1268 /* Unsupported media */
1269 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
1270 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1271 return NDIS_STATUS_NOT_SUPPORTED;
1272 }
1273
1274 /* Get maximum frame size */
1275 NdisStatus = NDISCall(IF,
1276 NdisRequestQueryInformation,
1277 OID_GEN_MAXIMUM_FRAME_SIZE,
1278 &IF->MTU,
1279 sizeof(UINT));
1280 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1281 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ (NDISCall)\n", AdapterName));
1282 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1283 return NdisStatus;
1284 }
1285
1286 /* Get maximum packet size */
1287 NdisStatus = NDISCall(IF,
1288 NdisRequestQueryInformation,
1289 OID_GEN_MAXIMUM_TOTAL_SIZE,
1290 &IF->MaxPacketSize,
1291 sizeof(UINT));
1292 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1293 TI_DbgPrint(MIN_TRACE, ("Query for maximum packet size failed.\n"));
1294 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1295 return NdisStatus;
1296 }
1297
1298 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
1299 NdisStatus = NDISCall(IF,
1300 NdisRequestQueryInformation,
1301 OID_GEN_MAXIMUM_SEND_PACKETS,
1302 &IF->MaxSendPackets,
1303 sizeof(UINT));
1304 if (NdisStatus != NDIS_STATUS_SUCCESS)
1305 /* Legacy NIC drivers may not support this query, if it fails we
1306 assume it can send at least one packet per call to NdisSend(Packets) */
1307 IF->MaxSendPackets = 1;
1308
1309 /* Get current hardware address */
1310 NdisStatus = NDISCall(IF,
1311 NdisRequestQueryInformation,
1312 AddressOID,
1313 &IF->HWAddress,
1314 IF->HWAddressLength);
1315 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1316 TI_DbgPrint(MIN_TRACE, ("Query for current hardware address failed.\n"));
1317 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1318 return NdisStatus;
1319 }
1320
1321 /* Get maximum link speed */
1322 NdisStatus = NDISCall(IF,
1323 NdisRequestQueryInformation,
1324 OID_GEN_LINK_SPEED,
1325 &Speed,
1326 sizeof(UINT));
1327 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1328 TI_DbgPrint(MIN_TRACE, ("Query for maximum link speed failed.\n"));
1329 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1330 return NdisStatus;
1331 }
1332
1333 /* Convert returned link speed to bps (it is in 100bps increments) */
1334 IF->Speed = Speed * 100L;
1335
1336 /* Bind adapter to IP layer */
1337 if( !BindAdapter(IF, RegistryPath) ) {
1338 TI_DbgPrint(DEBUG_DATALINK,("denying adapter %wZ (BindAdapter)\n", AdapterName));
1339 ExFreePoolWithTag(IF, LAN_ADAPTER_TAG);
1340 return NDIS_STATUS_NOT_ACCEPTED;
1341 }
1342
1343 /* Add adapter to the adapter list */
1344 ExInterlockedInsertTailList(&AdapterListHead,
1345 &IF->ListEntry,
1346 &AdapterListLock);
1347
1348 TI_DbgPrint(DEBUG_DATALINK, ("Leaving.\n"));
1349
1350 return NDIS_STATUS_SUCCESS;
1351 }
1352
1353
1354 NDIS_STATUS LANUnregisterAdapter(
1355 PLAN_ADAPTER Adapter)
1356 /*
1357 * FUNCTION: Unregisters protocol with NDIS adapter
1358 * ARGUMENTS:
1359 * Adapter = Pointer to a LAN_ADAPTER structure
1360 * RETURNS:
1361 * Status of operation
1362 */
1363 {
1364 KIRQL OldIrql;
1365 NDIS_HANDLE NdisHandle;
1366 NDIS_STATUS NdisStatus = NDIS_STATUS_SUCCESS;
1367
1368 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1369
1370 /* Unlink the adapter from the list */
1371 RemoveEntryList(&Adapter->ListEntry);
1372
1373 /* Unbind adapter from IP layer */
1374 UnbindAdapter(Adapter);
1375
1376 TcpipAcquireSpinLock(&Adapter->Lock, &OldIrql);
1377 NdisHandle = Adapter->NdisHandle;
1378 if (NdisHandle) {
1379 Adapter->NdisHandle = NULL;
1380 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1381
1382 NdisCloseAdapter(&NdisStatus, NdisHandle);
1383 if (NdisStatus == NDIS_STATUS_PENDING) {
1384 TcpipWaitForSingleObject(&Adapter->Event,
1385 UserRequest,
1386 KernelMode,
1387 FALSE,
1388 NULL);
1389 NdisStatus = Adapter->NdisStatus;
1390 }
1391 } else
1392 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1393
1394 FreeAdapter(Adapter);
1395
1396 return NdisStatus;
1397 }
1398
1399 VOID
1400 NTAPI
1401 LANUnregisterProtocol(VOID)
1402 /*
1403 * FUNCTION: Unregisters this protocol driver with NDIS
1404 * NOTES: Does not care wether we are already registered
1405 */
1406 {
1407 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1408
1409 if (ProtocolRegistered) {
1410 NDIS_STATUS NdisStatus;
1411 PLIST_ENTRY CurrentEntry;
1412 PLIST_ENTRY NextEntry;
1413 PLAN_ADAPTER Current;
1414 KIRQL OldIrql;
1415
1416 TcpipAcquireSpinLock(&AdapterListLock, &OldIrql);
1417
1418 /* Search the list and remove every adapter we find */
1419 CurrentEntry = AdapterListHead.Flink;
1420 while (CurrentEntry != &AdapterListHead) {
1421 NextEntry = CurrentEntry->Flink;
1422 Current = CONTAINING_RECORD(CurrentEntry, LAN_ADAPTER, ListEntry);
1423 /* Unregister it */
1424 LANUnregisterAdapter(Current);
1425 CurrentEntry = NextEntry;
1426 }
1427
1428 TcpipReleaseSpinLock(&AdapterListLock, OldIrql);
1429
1430 NdisDeregisterProtocol(&NdisStatus, NdisProtocolHandle);
1431 ProtocolRegistered = FALSE;
1432 }
1433 }
1434
1435 VOID
1436 NTAPI
1437 ProtocolUnbindAdapter(
1438 PNDIS_STATUS Status,
1439 NDIS_HANDLE ProtocolBindingContext,
1440 NDIS_HANDLE UnbindContext)
1441 {
1442 /* We don't pend any unbinding so we can just ignore UnbindContext */
1443 *Status = LANUnregisterAdapter((PLAN_ADAPTER)ProtocolBindingContext);
1444 }
1445
1446 NTSTATUS LANRegisterProtocol(
1447 PNDIS_STRING Name)
1448 /*
1449 * FUNCTION: Registers this protocol driver with NDIS
1450 * ARGUMENTS:
1451 * Name = Name of this protocol driver
1452 * RETURNS:
1453 * Status of operation
1454 */
1455 {
1456 NDIS_STATUS NdisStatus;
1457 NDIS_PROTOCOL_CHARACTERISTICS ProtChars;
1458
1459 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1460
1461 InitializeListHead(&AdapterListHead);
1462 KeInitializeSpinLock(&AdapterListLock);
1463
1464 /* Set up protocol characteristics */
1465 RtlZeroMemory(&ProtChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1466 ProtChars.MajorNdisVersion = NDIS_VERSION_MAJOR;
1467 ProtChars.MinorNdisVersion = NDIS_VERSION_MINOR;
1468 ProtChars.Name.Length = Name->Length;
1469 ProtChars.Name.Buffer = Name->Buffer;
1470 ProtChars.Name.MaximumLength = Name->MaximumLength;
1471 ProtChars.OpenAdapterCompleteHandler = ProtocolOpenAdapterComplete;
1472 ProtChars.CloseAdapterCompleteHandler = ProtocolCloseAdapterComplete;
1473 ProtChars.ResetCompleteHandler = ProtocolResetComplete;
1474 ProtChars.RequestCompleteHandler = ProtocolRequestComplete;
1475 ProtChars.SendCompleteHandler = ProtocolSendComplete;
1476 ProtChars.TransferDataCompleteHandler = ProtocolTransferDataComplete;
1477 ProtChars.ReceiveHandler = ProtocolReceive;
1478 ProtChars.ReceiveCompleteHandler = ProtocolReceiveComplete;
1479 ProtChars.StatusHandler = ProtocolStatus;
1480 ProtChars.StatusCompleteHandler = ProtocolStatusComplete;
1481 ProtChars.BindAdapterHandler = ProtocolBindAdapter;
1482 ProtChars.PnPEventHandler = ProtocolPnPEvent;
1483 ProtChars.UnbindAdapterHandler = ProtocolUnbindAdapter;
1484 ProtChars.UnloadHandler = LANUnregisterProtocol;
1485
1486 /* Try to register protocol */
1487 NdisRegisterProtocol(&NdisStatus,
1488 &NdisProtocolHandle,
1489 &ProtChars,
1490 sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1491 if (NdisStatus != NDIS_STATUS_SUCCESS)
1492 {
1493 TI_DbgPrint(DEBUG_DATALINK, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus));
1494 return (NTSTATUS)NdisStatus;
1495 }
1496
1497 ProtocolRegistered = TRUE;
1498
1499 return STATUS_SUCCESS;
1500 }
1501
1502 /* EOF */