66baca4c59ce6bbe269a721f0672e0dfbb23a52e
[reactos.git] / reactos / drivers / net / 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 typedef struct _LAN_WQ_ITEM {
14 LIST_ENTRY ListEntry;
15 PNDIS_PACKET Packet;
16 PLAN_ADAPTER Adapter;
17 UINT BytesTransferred;
18 } LAN_WQ_ITEM, *PLAN_WQ_ITEM;
19
20 NDIS_HANDLE NdisProtocolHandle = (NDIS_HANDLE)NULL;
21 BOOLEAN ProtocolRegistered = FALSE;
22 LIST_ENTRY AdapterListHead;
23 KSPIN_LOCK AdapterListLock;
24
25 /* Work around being called back into afd at Dpc level */
26 KSPIN_LOCK LanWorkLock;
27 LIST_ENTRY LanWorkList;
28 WORK_QUEUE_ITEM LanWorkItem;
29
30 NDIS_STATUS NDISCall(
31 PLAN_ADAPTER Adapter,
32 NDIS_REQUEST_TYPE Type,
33 NDIS_OID OID,
34 PVOID Buffer,
35 UINT Length)
36 /*
37 * FUNCTION: Send a request to NDIS
38 * ARGUMENTS:
39 * Adapter = Pointer to a LAN_ADAPTER structure
40 * Type = Type of request (Set or Query)
41 * OID = Value to be set/queried for
42 * Buffer = Pointer to a buffer to use
43 * Length = Number of bytes in Buffer
44 * RETURNS:
45 * Status of operation
46 */
47 {
48 NDIS_REQUEST Request;
49 NDIS_STATUS NdisStatus;
50
51 Request.RequestType = Type;
52 if (Type == NdisRequestSetInformation) {
53 Request.DATA.SET_INFORMATION.Oid = OID;
54 Request.DATA.SET_INFORMATION.InformationBuffer = Buffer;
55 Request.DATA.SET_INFORMATION.InformationBufferLength = Length;
56 } else {
57 Request.DATA.QUERY_INFORMATION.Oid = OID;
58 Request.DATA.QUERY_INFORMATION.InformationBuffer = Buffer;
59 Request.DATA.QUERY_INFORMATION.InformationBufferLength = Length;
60 }
61
62 if (Adapter->State != LAN_STATE_RESETTING) {
63 NdisRequest(&NdisStatus, Adapter->NdisHandle, &Request);
64 } else {
65 NdisStatus = NDIS_STATUS_NOT_ACCEPTED;
66 }
67
68 /* Wait for NDIS to complete the request */
69 if (NdisStatus == NDIS_STATUS_PENDING) {
70 KeWaitForSingleObject(&Adapter->Event,
71 UserRequest,
72 KernelMode,
73 FALSE,
74 NULL);
75 NdisStatus = Adapter->NdisStatus;
76 }
77
78 return NdisStatus;
79 }
80
81
82 VOID FreeAdapter(
83 PLAN_ADAPTER Adapter)
84 /*
85 * FUNCTION: Frees memory for a LAN_ADAPTER structure
86 * ARGUMENTS:
87 * Adapter = Pointer to LAN_ADAPTER structure to free
88 */
89 {
90 exFreePool(Adapter);
91 }
92
93
94 VOID STDCALL ProtocolOpenAdapterComplete(
95 NDIS_HANDLE BindingContext,
96 NDIS_STATUS Status,
97 NDIS_STATUS OpenErrorStatus)
98 /*
99 * FUNCTION: Called by NDIS to complete opening of an adapter
100 * ARGUMENTS:
101 * BindingContext = Pointer to a device context (LAN_ADAPTER)
102 * Status = Status of the operation
103 * OpenErrorStatus = Additional status information
104 */
105 {
106 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
107
108 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
109
110 KeSetEvent(&Adapter->Event, 0, FALSE);
111 }
112
113
114 VOID STDCALL ProtocolCloseAdapterComplete(
115 NDIS_HANDLE BindingContext,
116 NDIS_STATUS Status)
117 /*
118 * FUNCTION: Called by NDIS to complete closing an adapter
119 * ARGUMENTS:
120 * BindingContext = Pointer to a device context (LAN_ADAPTER)
121 * Status = Status of the operation
122 */
123 {
124 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
125
126 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
127
128 Adapter->NdisStatus = Status;
129
130 KeSetEvent(&Adapter->Event, 0, FALSE);
131 }
132
133
134 VOID STDCALL ProtocolResetComplete(
135 NDIS_HANDLE BindingContext,
136 NDIS_STATUS Status)
137 /*
138 * FUNCTION: Called by NDIS to complete resetting an adapter
139 * ARGUMENTS:
140 * BindingContext = Pointer to a device context (LAN_ADAPTER)
141 * Status = Status of the operation
142 */
143 {
144 TI_DbgPrint(MID_TRACE, ("Called.\n"));
145 }
146
147
148 VOID STDCALL ProtocolRequestComplete(
149 NDIS_HANDLE BindingContext,
150 PNDIS_REQUEST NdisRequest,
151 NDIS_STATUS Status)
152 /*
153 * FUNCTION: Called by NDIS to complete a request
154 * ARGUMENTS:
155 * BindingContext = Pointer to a device context (LAN_ADAPTER)
156 * NdisRequest = Pointer to an object describing the request
157 * Status = Status of the operation
158 */
159 {
160 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
161
162 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
163
164 /* Save status of request and signal an event */
165 Adapter->NdisStatus = Status;
166
167 KeSetEvent(&Adapter->Event, 0, FALSE);
168 }
169
170
171 VOID STDCALL ProtocolSendComplete(
172 NDIS_HANDLE BindingContext,
173 PNDIS_PACKET Packet,
174 NDIS_STATUS Status)
175 /*
176 * FUNCTION: Called by NDIS to complete sending process
177 * ARGUMENTS:
178 * BindingContext = Pointer to a device context (LAN_ADAPTER)
179 * Packet = Pointer to a packet descriptor
180 * Status = Status of the operation
181 */
182 {
183 TI_DbgPrint(DEBUG_DATALINK, ("Calling completion routine\n"));
184 ASSERT_KM_POINTER(Packet);
185 ASSERT_KM_POINTER(PC(Packet));
186 ASSERT_KM_POINTER(PC(Packet)->DLComplete);
187 (*PC(Packet)->DLComplete)( PC(Packet)->Context, Packet, Status);
188 TI_DbgPrint(DEBUG_DATALINK, ("Finished\n"));
189 }
190
191 VOID STDCALL LanReceiveWorker( PVOID Context ) {
192 UINT PacketType;
193 PLIST_ENTRY ListEntry;
194 PLAN_WQ_ITEM WorkItem;
195 PNDIS_PACKET Packet;
196 PLAN_ADAPTER Adapter;
197 UINT BytesTransferred;
198 PNDIS_BUFFER NdisBuffer;
199 IP_PACKET IPPacket;
200
201
202 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
203
204 while( (ListEntry =
205 ExInterlockedRemoveHeadList( &LanWorkList, &LanWorkLock )) ) {
206 WorkItem = CONTAINING_RECORD(ListEntry, LAN_WQ_ITEM, ListEntry);
207
208 Packet = WorkItem->Packet;
209 Adapter = WorkItem->Adapter;
210 BytesTransferred = WorkItem->BytesTransferred;
211
212 ExFreePool( WorkItem );
213
214 IPPacket.NdisPacket = Packet;
215
216 NdisGetFirstBufferFromPacket(Packet,
217 &NdisBuffer,
218 &IPPacket.Header,
219 &IPPacket.ContigSize,
220 &IPPacket.TotalSize);
221
222 IPPacket.ContigSize = IPPacket.TotalSize = BytesTransferred;
223 /* Determine which upper layer protocol that should receive
224 this packet and pass it to the correct receive handler */
225
226 TI_DbgPrint(MID_TRACE,
227 ("ContigSize: %d, TotalSize: %d, BytesTransferred: %d\n",
228 IPPacket.ContigSize, IPPacket.TotalSize,
229 BytesTransferred));
230
231 /*OskitDumpBuffer( IPPacket.Header, IPPacket.TotalSize );*/
232
233 PacketType = PC(IPPacket.NdisPacket)->PacketType;
234 IPPacket.Position = 0;
235
236 TI_DbgPrint
237 (DEBUG_DATALINK,
238 ("Ether Type = %x ContigSize = %d Total = %d\n",
239 PacketType, IPPacket.ContigSize, IPPacket.TotalSize));
240
241 switch (PacketType) {
242 case ETYPE_IPv4:
243 case ETYPE_IPv6:
244 TI_DbgPrint(MID_TRACE,("Received IP Packet\n"));
245 IPReceive(Adapter->Context, &IPPacket);
246 break;
247 case ETYPE_ARP:
248 TI_DbgPrint(MID_TRACE,("Received ARP Packet\n"));
249 ARPReceive(Adapter->Context, &IPPacket);
250 default:
251 break;
252 }
253
254 FreeNdisPacket( Packet );
255 }
256 }
257
258 VOID STDCALL ProtocolTransferDataComplete(
259 NDIS_HANDLE BindingContext,
260 PNDIS_PACKET Packet,
261 NDIS_STATUS Status,
262 UINT BytesTransferred)
263 /*
264 * FUNCTION: Called by NDIS to complete reception of data
265 * ARGUMENTS:
266 * BindingContext = Pointer to a device context (LAN_ADAPTER)
267 * Packet = Pointer to a packet descriptor
268 * Status = Status of the operation
269 * BytesTransferred = Number of bytes transferred
270 * NOTES:
271 * If the packet was successfully received, determine the protocol
272 * type and pass it to the correct receive handler
273 */
274 {
275 BOOLEAN WorkStart;
276 PLAN_WQ_ITEM WQItem;
277 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
278
279 if( Status != NDIS_STATUS_SUCCESS ) return;
280 WQItem = ExAllocatePool( NonPagedPool, sizeof(LAN_WQ_ITEM) );
281 if( !WQItem ) return;
282
283 TcpipAcquireSpinLockAtDpcLevel( &LanWorkLock );
284 WorkStart = IsListEmpty( &LanWorkList );
285 WQItem->Packet = Packet;
286 WQItem->Adapter = Adapter;
287 WQItem->BytesTransferred = BytesTransferred;
288 InsertTailList( &LanWorkList, &WQItem->ListEntry );
289 if( WorkStart )
290 ExQueueWorkItem( &LanWorkItem, CriticalWorkQueue );
291 TcpipReleaseSpinLockFromDpcLevel( &LanWorkLock );
292 }
293
294 NDIS_STATUS STDCALL ProtocolReceive(
295 NDIS_HANDLE BindingContext,
296 NDIS_HANDLE MacReceiveContext,
297 PVOID HeaderBuffer,
298 UINT HeaderBufferSize,
299 PVOID LookaheadBuffer,
300 UINT LookaheadBufferSize,
301 UINT PacketSize)
302 /*
303 * FUNCTION: Called by NDIS when a packet has been received on the physical link
304 * ARGUMENTS:
305 * BindingContext = Pointer to a device context (LAN_ADAPTER)
306 * MacReceiveContext = Handle used by underlying NIC driver
307 * HeaderBuffer = Pointer to a buffer containing the packet header
308 * HeaderBufferSize = Number of bytes in HeaderBuffer
309 * LookaheadBuffer = Pointer to a buffer containing buffered packet data
310 * LookaheadBufferSize = Size of LookaheadBuffer. May be less than asked for
311 * PacketSize = Overall size of the packet (not including header)
312 * RETURNS:
313 * Status of operation
314 */
315 {
316 USHORT EType;
317 UINT PacketType, BytesTransferred;
318 UINT temp;
319 IP_PACKET IPPacket;
320 PCHAR BufferData;
321 NDIS_STATUS NdisStatus;
322 PNDIS_PACKET NdisPacket;
323 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)BindingContext;
324 PETH_HEADER EHeader = (PETH_HEADER)HeaderBuffer;
325
326 TI_DbgPrint(DEBUG_DATALINK, ("Called. (packetsize %d)\n",PacketSize));
327
328 if (Adapter->State != LAN_STATE_STARTED) {
329 TI_DbgPrint(DEBUG_DATALINK, ("Adapter is stopped.\n"));
330 return NDIS_STATUS_NOT_ACCEPTED;
331 }
332
333 if (HeaderBufferSize < Adapter->HeaderSize) {
334 TI_DbgPrint(DEBUG_DATALINK, ("Runt frame received.\n"));
335 return NDIS_STATUS_NOT_ACCEPTED;
336 }
337
338 if (Adapter->Media == NdisMedium802_3) {
339 /* Ethernet and IEEE 802.3 frames can be destinguished by
340 looking at the IEEE 802.3 length field. This field is
341 less than or equal to 1500 for a valid IEEE 802.3 frame
342 and larger than 1500 is it's a valid EtherType value.
343 See RFC 1122, section 2.3.3 for more information */
344 /* FIXME: Test for Ethernet and IEEE 802.3 frame */
345 if (((EType = EHeader->EType) != ETYPE_IPv4) && (EType != ETYPE_ARP)) {
346 TI_DbgPrint(DEBUG_DATALINK, ("Not IP or ARP frame. EtherType (0x%X).\n", EType));
347 return NDIS_STATUS_NOT_ACCEPTED;
348 }
349 /* We use EtherType constants to destinguish packet types */
350 PacketType = EType;
351 } else {
352 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
353 /* FIXME: Support other medias */
354 return NDIS_STATUS_NOT_ACCEPTED;
355 }
356
357 /* Get a transfer data packet */
358
359 TI_DbgPrint(DEBUG_DATALINK, ("Adapter: %x (MTU %d)\n",
360 Adapter, Adapter->MTU));
361
362 NdisStatus = AllocatePacketWithBuffer( &NdisPacket, NULL,
363 PacketSize + HeaderBufferSize );
364 if( NdisStatus != NDIS_STATUS_SUCCESS ) {
365 return NDIS_STATUS_NOT_ACCEPTED;
366 }
367
368 PC(NdisPacket)->PacketType = PacketType;
369
370 TI_DbgPrint(DEBUG_DATALINK, ("pretransfer LookaheadBufferSize %d packsize %d\n",LookaheadBufferSize,PacketSize));
371
372 GetDataPtr( NdisPacket, 0, &BufferData, &temp );
373
374 IPPacket.NdisPacket = NdisPacket;
375 IPPacket.Position = 0;
376
377 if (LookaheadBufferSize == PacketSize)
378 {
379 /* Optimized code path for packets that are fully contained in
380 * the lookahead buffer. */
381 NdisCopyLookaheadData(BufferData,
382 LookaheadBuffer,
383 LookaheadBufferSize,
384 Adapter->MacOptions);
385 }
386 else
387 {
388 if (NdisStatus == NDIS_STATUS_SUCCESS)
389 {
390 ASSERT(PacketSize <= Adapter->MTU);
391
392 NdisTransferData(&NdisStatus, Adapter->NdisHandle,
393 MacReceiveContext, 0, PacketSize,
394 NdisPacket, &BytesTransferred);
395 }
396 else
397 {
398 BytesTransferred = 0;
399 }
400 }
401 TI_DbgPrint(DEBUG_DATALINK, ("Calling complete\n"));
402
403 if (NdisStatus != NDIS_STATUS_PENDING)
404 ProtocolTransferDataComplete(BindingContext,
405 NdisPacket,
406 NdisStatus,
407 PacketSize);
408
409 TI_DbgPrint(DEBUG_DATALINK, ("leaving\n"));
410
411 return NDIS_STATUS_SUCCESS;
412 }
413
414
415 VOID STDCALL ProtocolReceiveComplete(
416 NDIS_HANDLE BindingContext)
417 /*
418 * FUNCTION: Called by NDIS when we're done receiving data
419 * ARGUMENTS:
420 * BindingContext = Pointer to a device context (LAN_ADAPTER)
421 */
422 {
423 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
424 }
425
426
427 VOID STDCALL ProtocolStatus(
428 NDIS_HANDLE BindingContext,
429 NDIS_STATUS GenerelStatus,
430 PVOID StatusBuffer,
431 UINT StatusBufferSize)
432 /*
433 * FUNCTION: Called by NDIS when the underlying driver has changed state
434 * ARGUMENTS:
435 * BindingContext = Pointer to a device context (LAN_ADAPTER)
436 * GenerelStatus = A generel status code
437 * StatusBuffer = Pointer to a buffer with medium-specific data
438 * StatusBufferSize = Number of bytes in StatusBuffer
439 */
440 {
441 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
442 }
443
444
445 VOID STDCALL ProtocolStatusComplete(
446 NDIS_HANDLE NdisBindingContext)
447 /*
448 * FUNCTION: Called by NDIS when a status-change has occurred
449 * ARGUMENTS:
450 * BindingContext = Pointer to a device context (LAN_ADAPTER)
451 */
452 {
453 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
454 }
455
456 VOID STDCALL ProtocolBindAdapter(
457 OUT PNDIS_STATUS Status,
458 IN NDIS_HANDLE BindContext,
459 IN PNDIS_STRING DeviceName,
460 IN PVOID SystemSpecific1,
461 IN PVOID SystemSpecific2)
462 /*
463 * FUNCTION: Called by NDIS during NdisRegisterProtocol to set up initial
464 * bindings, and periodically thereafer as new adapters come online
465 * ARGUMENTS:
466 * Status: Return value to NDIS
467 * BindContext: Handle provided by NDIS to track pending binding operations
468 * DeviceName: Name of the miniport device to bind to
469 * SystemSpecific1: Pointer to a registry path with protocol-specific configuration information
470 * SystemSpecific2: Unused & must not be touched
471 */
472 {
473 /* XXX confirm that this is still true, or re-word the following comment */
474 /* we get to ignore BindContext because we will never pend an operation with NDIS */
475 TI_DbgPrint(DEBUG_DATALINK, ("Called with registry path %wZ for %wZ\n", SystemSpecific1, DeviceName));
476 *Status = LANRegisterAdapter(DeviceName, SystemSpecific1);
477 }
478
479
480 VOID LANTransmit(
481 PVOID Context,
482 PNDIS_PACKET NdisPacket,
483 UINT Offset,
484 PVOID LinkAddress,
485 USHORT Type)
486 /*
487 * FUNCTION: Transmits a packet
488 * ARGUMENTS:
489 * Context = Pointer to context information (LAN_ADAPTER)
490 * NdisPacket = Pointer to NDIS packet to send
491 * Offset = Offset in packet where data starts
492 * LinkAddress = Pointer to link address of destination (NULL = broadcast)
493 * Type = LAN protocol type (LAN_PROTO_*)
494 */
495 {
496 NDIS_STATUS NdisStatus;
497 PETH_HEADER EHeader;
498 PCHAR Data;
499 UINT Size;
500 KIRQL OldIrql;
501 PLAN_ADAPTER Adapter = (PLAN_ADAPTER)Context;
502
503 TI_DbgPrint(DEBUG_DATALINK,
504 ("Called( NdisPacket %x, Offset %d, Adapter %x )\n",
505 NdisPacket, Offset, Adapter));
506
507 TI_DbgPrint(DEBUG_DATALINK,
508 ("Adapter Address [%02x %02x %02x %02x %02x %02x]\n",
509 Adapter->HWAddress[0] & 0xff,
510 Adapter->HWAddress[1] & 0xff,
511 Adapter->HWAddress[2] & 0xff,
512 Adapter->HWAddress[3] & 0xff,
513 Adapter->HWAddress[4] & 0xff,
514 Adapter->HWAddress[5] & 0xff));
515
516 /* XXX arty -- Handled adjustment in a saner way than before ...
517 * not needed immediately */
518 GetDataPtr( NdisPacket, 0, &Data, &Size );
519
520 if (Adapter->State == LAN_STATE_STARTED) {
521 switch (Adapter->Media) {
522 case NdisMedium802_3:
523 EHeader = (PETH_HEADER)Data;
524
525 if (LinkAddress) {
526 /* Unicast address */
527 RtlCopyMemory(EHeader->DstAddr, LinkAddress, IEEE_802_ADDR_LENGTH);
528 } else {
529 /* Broadcast address */
530 RtlFillMemory(EHeader->DstAddr, IEEE_802_ADDR_LENGTH, 0xFF);
531 }
532
533 RtlCopyMemory(EHeader->SrcAddr, Adapter->HWAddress, IEEE_802_ADDR_LENGTH);
534
535 switch (Type) {
536 case LAN_PROTO_IPv4:
537 EHeader->EType = ETYPE_IPv4;
538 break;
539 case LAN_PROTO_ARP:
540 EHeader->EType = ETYPE_ARP;
541 break;
542 case LAN_PROTO_IPv6:
543 EHeader->EType = ETYPE_IPv6;
544 break;
545 default:
546 #ifdef DBG
547 /* Should not happen */
548 TI_DbgPrint(MIN_TRACE, ("Unknown LAN protocol.\n"));
549
550 ProtocolSendComplete((NDIS_HANDLE)Context,
551 NdisPacket,
552 NDIS_STATUS_FAILURE);
553 #endif
554 return;
555 }
556 break;
557
558 default:
559 /* FIXME: Support other medias */
560 break;
561 }
562
563 TI_DbgPrint( MID_TRACE, ("LinkAddress: %x\n", LinkAddress));
564 if( LinkAddress ) {
565 TI_DbgPrint
566 ( MID_TRACE,
567 ("Link Address [%02x %02x %02x %02x %02x %02x]\n",
568 ((PCHAR)LinkAddress)[0] & 0xff,
569 ((PCHAR)LinkAddress)[1] & 0xff,
570 ((PCHAR)LinkAddress)[2] & 0xff,
571 ((PCHAR)LinkAddress)[3] & 0xff,
572 ((PCHAR)LinkAddress)[4] & 0xff,
573 ((PCHAR)LinkAddress)[5] & 0xff));
574 }
575
576 /*OskitDumpBuffer( Data, Size );*/
577
578 TcpipAcquireSpinLock( &Adapter->Lock, &OldIrql );
579 TI_DbgPrint(MID_TRACE, ("NdisSend\n"));
580 NdisSend(&NdisStatus, Adapter->NdisHandle, NdisPacket);
581 TI_DbgPrint(MID_TRACE, ("NdisSend Done\n"));
582 TcpipReleaseSpinLock( &Adapter->Lock, OldIrql );
583
584 if (NdisStatus != NDIS_STATUS_PENDING)
585 ProtocolSendComplete((NDIS_HANDLE)Context, NdisPacket, NdisStatus);
586 } else {
587 ProtocolSendComplete((NDIS_HANDLE)Context, NdisPacket, NDIS_STATUS_CLOSED);
588 }
589 }
590
591 static NTSTATUS
592 OpenRegistryKey( PNDIS_STRING RegistryPath, PHANDLE RegHandle ) {
593 OBJECT_ATTRIBUTES Attributes;
594 NTSTATUS Status;
595
596 InitializeObjectAttributes(&Attributes, RegistryPath, OBJ_CASE_INSENSITIVE, 0, 0);
597 Status = ZwOpenKey(RegHandle, GENERIC_READ, &Attributes);
598 return Status;
599 }
600
601 static NTSTATUS ReadIPAddressFromRegistry( HANDLE RegHandle,
602 PWCHAR RegistryValue,
603 PIP_ADDRESS *Address ) {
604 UNICODE_STRING ValueName;
605 UNICODE_STRING UnicodeAddress;
606 NTSTATUS Status;
607 ULONG ResultLength;
608 UCHAR buf[1024];
609 PKEY_VALUE_PARTIAL_INFORMATION Information = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
610 ANSI_STRING AnsiAddress;
611 ULONG AnsiLen;
612
613 RtlInitUnicodeString(&ValueName, RegistryValue);
614 Status =
615 ZwQueryValueKey(RegHandle,
616 &ValueName,
617 KeyValuePartialInformation,
618 Information,
619 sizeof(buf),
620 &ResultLength);
621
622 if (!NT_SUCCESS(Status))
623 return Status;
624 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
625 TI_DbgPrint(MIN_TRACE, ("Information DataLength: 0x%x\n", Information->DataLength));
626
627 UnicodeAddress.Buffer = (PWCHAR)&Information->Data;
628 UnicodeAddress.Length = Information->DataLength;
629 UnicodeAddress.MaximumLength = Information->DataLength;
630
631 AnsiLen = RtlUnicodeStringToAnsiSize(&UnicodeAddress);
632 if(!AnsiLen)
633 return STATUS_NO_MEMORY;
634
635 AnsiAddress.Buffer = exAllocatePoolWithTag(PagedPool, AnsiLen, 0x01020304);
636 if(!AnsiAddress.Buffer)
637 return STATUS_NO_MEMORY;
638
639 AnsiAddress.Length = AnsiLen;
640 AnsiAddress.MaximumLength = AnsiLen;
641
642 Status = RtlUnicodeStringToAnsiString(&AnsiAddress, &UnicodeAddress, FALSE);
643 if (!NT_SUCCESS(Status)) {
644 exFreePool(AnsiAddress.Buffer);
645 return STATUS_UNSUCCESSFUL;
646 }
647
648 AnsiAddress.Buffer[AnsiAddress.Length] = 0;
649 *Address = AddrBuildIPv4(inet_addr(AnsiAddress.Buffer));
650 if (!Address) {
651 exFreePool(AnsiAddress.Buffer);
652 return STATUS_UNSUCCESSFUL;
653 }
654
655 return *Address ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
656 }
657
658 VOID BindAdapter(
659 PLAN_ADAPTER Adapter,
660 PNDIS_STRING RegistryPath)
661 /*
662 * FUNCTION: Binds a LAN adapter to IP layer
663 * ARGUMENTS:
664 * Adapter = Pointer to LAN_ADAPTER structure
665 * NOTES:
666 * We set the lookahead buffer size, set the packet filter and
667 * bind the adapter to IP layer
668 */
669 {
670 PIP_INTERFACE IF;
671 PIP_ADDRESS Address = 0;
672 PIP_ADDRESS Netmask = 0;
673 NDIS_STATUS NdisStatus;
674 LLIP_BIND_INFO BindInfo;
675 ULONG Lookahead = LOOKAHEAD_SIZE;
676 NTSTATUS Status;
677 HANDLE RegHandle = 0;
678
679 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
680
681 Adapter->State = LAN_STATE_OPENING;
682
683 NdisStatus = NDISCall(Adapter,
684 NdisRequestSetInformation,
685 OID_GEN_CURRENT_LOOKAHEAD,
686 &Lookahead,
687 sizeof(ULONG));
688 if (NdisStatus != NDIS_STATUS_SUCCESS) {
689 TI_DbgPrint(MID_TRACE, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus));
690 return;
691 }
692
693 /* Bind the adapter to IP layer */
694 BindInfo.Context = Adapter;
695 BindInfo.HeaderSize = Adapter->HeaderSize;
696 BindInfo.MinFrameSize = Adapter->MinFrameSize;
697 BindInfo.MTU = Adapter->MTU;
698 BindInfo.Address = (PUCHAR)&Adapter->HWAddress;
699 BindInfo.AddressLength = Adapter->HWAddressLength;
700 BindInfo.Transmit = LANTransmit;
701
702 IF = IPCreateInterface(&BindInfo);
703
704 if (!IF) {
705 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
706 return;
707 }
708
709 /*
710 * Query per-adapter configuration from the registry
711 * In case anyone is curious: there *is* an Ndis configuration api
712 * for this sort of thing, but it doesn't really support things like
713 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
714 * protocol drivers developed for win2k and above just use the native
715 * services (ZwOpenKey, etc).
716 */
717
718 Status = OpenRegistryKey( RegistryPath, &RegHandle );
719
720 if(NT_SUCCESS(Status))
721 Status = ReadIPAddressFromRegistry( RegHandle, L"IPAddress",
722 &Address );
723 if(NT_SUCCESS(Status))
724 Status = ReadIPAddressFromRegistry( RegHandle, L"SubnetMask",
725 &Netmask );
726
727 if(!NT_SUCCESS(Status) || !Address || !Netmask)
728 {
729 TI_DbgPrint(MIN_TRACE, ("Unable to open protocol-specific registry key: 0x%x\n", Status));
730
731 /* XXX how do we proceed? No ip address, no parameters... do we guess? */
732 if(RegHandle)
733 ZwClose(RegHandle);
734 IPDestroyInterface(IF);
735 return;
736 }
737
738 TI_DbgPrint
739 (MID_TRACE,
740 ("--> Our IP address on this interface: '%s'\n",
741 A2S(Address)));
742
743 TI_DbgPrint
744 (MID_TRACE,
745 ("--> Our net mask on this interface: '%s'\n",
746 A2S(Netmask)));
747
748 /* Create a net table entry for this interface */
749 if (!IPCreateNTE(IF, Address, AddrCountPrefixBits(Netmask))) {
750 TI_DbgPrint(MIN_TRACE, ("IPCreateNTE() failed.\n"));
751 IPDestroyInterface(IF);
752 return;
753 }
754
755 /* Register interface with IP layer */
756 IPRegisterInterface(IF);
757
758 /* Set packet filter so we can send and receive packets */
759 NdisStatus = NDISCall(Adapter,
760 NdisRequestSetInformation,
761 OID_GEN_CURRENT_PACKET_FILTER,
762 &Adapter->PacketFilter,
763 sizeof(UINT));
764 if (NdisStatus != NDIS_STATUS_SUCCESS) {
765 TI_DbgPrint(MID_TRACE, ("Could not set packet filter (0x%X).\n", NdisStatus));
766 IPDestroyInterface(IF);
767 return;
768 }
769
770 Adapter->Context = IF;
771 Adapter->State = LAN_STATE_STARTED;
772 }
773
774
775 VOID UnbindAdapter(
776 PLAN_ADAPTER Adapter)
777 /*
778 * FUNCTION: Unbinds a LAN adapter from IP layer
779 * ARGUMENTS:
780 * Adapter = Pointer to LAN_ADAPTER structure
781 */
782 {
783 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
784
785 if (Adapter->State == LAN_STATE_STARTED) {
786 PIP_INTERFACE IF = Adapter->Context;
787
788 IPUnregisterInterface(IF);
789
790 IPDestroyInterface(IF);
791 }
792 }
793
794
795 NDIS_STATUS LANRegisterAdapter(
796 PNDIS_STRING AdapterName,
797 PNDIS_STRING RegistryPath)
798 /*
799 * FUNCTION: Registers protocol with an NDIS adapter
800 * ARGUMENTS:
801 * AdapterName = Pointer to string with name of adapter to register
802 * Adapter = Address of pointer to a LAN_ADAPTER structure
803 * RETURNS:
804 * Status of operation
805 */
806 {
807 PLAN_ADAPTER IF;
808 NDIS_STATUS NdisStatus;
809 NDIS_STATUS OpenStatus;
810 UINT MediaIndex;
811 NDIS_MEDIUM MediaArray[MAX_MEDIA];
812 UINT AddressOID;
813 UINT Speed;
814
815 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
816
817 IF = exAllocatePool(NonPagedPool, sizeof(LAN_ADAPTER));
818 if (!IF) {
819 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
820 return NDIS_STATUS_RESOURCES;
821 }
822
823 RtlZeroMemory(IF, sizeof(LAN_ADAPTER));
824
825 /* Put adapter in stopped state */
826 IF->State = LAN_STATE_STOPPED;
827
828 /* Initialize protecting spin lock */
829 KeInitializeSpinLock(&IF->Lock);
830
831 KeInitializeEvent(&IF->Event, SynchronizationEvent, FALSE);
832
833 /* Initialize array with media IDs we support */
834 MediaArray[MEDIA_ETH] = NdisMedium802_3;
835
836 TI_DbgPrint(DEBUG_DATALINK,("opening adapter %wZ\n", AdapterName));
837 /* Open the adapter. */
838 NdisOpenAdapter(&NdisStatus,
839 &OpenStatus,
840 &IF->NdisHandle,
841 &MediaIndex,
842 MediaArray,
843 MAX_MEDIA,
844 NdisProtocolHandle,
845 IF,
846 AdapterName,
847 0,
848 NULL);
849
850 /* Wait until the adapter is opened */
851 if (NdisStatus == NDIS_STATUS_PENDING)
852 KeWaitForSingleObject(&IF->Event, UserRequest, KernelMode, FALSE, NULL);
853 else if (NdisStatus != NDIS_STATUS_SUCCESS) {
854 exFreePool(IF);
855 return NdisStatus;
856 }
857
858 IF->Media = MediaArray[MediaIndex];
859
860 /* Fill LAN_ADAPTER structure with some adapter specific information */
861 switch (IF->Media) {
862 case NdisMedium802_3:
863 IF->HWAddressLength = IEEE_802_ADDR_LENGTH;
864 IF->BCastMask = BCAST_ETH_MASK;
865 IF->BCastCheck = BCAST_ETH_CHECK;
866 IF->BCastOffset = BCAST_ETH_OFFSET;
867 IF->HeaderSize = sizeof(ETH_HEADER);
868 IF->MinFrameSize = 60;
869 AddressOID = OID_802_3_CURRENT_ADDRESS;
870 IF->PacketFilter =
871 NDIS_PACKET_TYPE_BROADCAST |
872 NDIS_PACKET_TYPE_DIRECTED |
873 NDIS_PACKET_TYPE_MULTICAST;
874 break;
875
876 default:
877 /* Unsupported media */
878 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
879 exFreePool(IF);
880 return NDIS_STATUS_NOT_SUPPORTED;
881 }
882
883 /* Get maximum frame size */
884 NdisStatus = NDISCall(IF,
885 NdisRequestQueryInformation,
886 OID_GEN_MAXIMUM_FRAME_SIZE,
887 &IF->MTU,
888 sizeof(UINT));
889 if (NdisStatus != NDIS_STATUS_SUCCESS) {
890 exFreePool(IF);
891 return NdisStatus;
892 }
893
894 /* Get maximum packet size */
895 NdisStatus = NDISCall(IF,
896 NdisRequestQueryInformation,
897 OID_GEN_MAXIMUM_TOTAL_SIZE,
898 &IF->MaxPacketSize,
899 sizeof(UINT));
900 if (NdisStatus != NDIS_STATUS_SUCCESS) {
901 TI_DbgPrint(MIN_TRACE, ("Query for maximum packet size failed.\n"));
902 exFreePool(IF);
903 return NdisStatus;
904 }
905
906 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
907 NdisStatus = NDISCall(IF,
908 NdisRequestQueryInformation,
909 OID_GEN_MAXIMUM_SEND_PACKETS,
910 &IF->MaxSendPackets,
911 sizeof(UINT));
912 if (NdisStatus != NDIS_STATUS_SUCCESS)
913 /* Legacy NIC drivers may not support this query, if it fails we
914 assume it can send at least one packet per call to NdisSend(Packets) */
915 IF->MaxSendPackets = 1;
916
917 /* Get current hardware address */
918 NdisStatus = NDISCall(IF,
919 NdisRequestQueryInformation,
920 AddressOID,
921 &IF->HWAddress,
922 IF->HWAddressLength);
923 if (NdisStatus != NDIS_STATUS_SUCCESS) {
924 TI_DbgPrint(MIN_TRACE, ("Query for current hardware address failed.\n"));
925 exFreePool(IF);
926 return NdisStatus;
927 }
928
929 /* Get maximum link speed */
930 NdisStatus = NDISCall(IF,
931 NdisRequestQueryInformation,
932 OID_GEN_LINK_SPEED,
933 &Speed,
934 sizeof(UINT));
935 if (NdisStatus != NDIS_STATUS_SUCCESS) {
936 TI_DbgPrint(MIN_TRACE, ("Query for maximum link speed failed.\n"));
937 exFreePool(IF);
938 return NdisStatus;
939 }
940
941 /* Convert returned link speed to bps (it is in 100bps increments) */
942 IF->Speed = Speed * 100L;
943
944 /* Add adapter to the adapter list */
945 ExInterlockedInsertTailList(&AdapterListHead,
946 &IF->ListEntry,
947 &AdapterListLock);
948
949 /* Bind adapter to IP layer */
950 BindAdapter(IF, RegistryPath);
951
952 TI_DbgPrint(DEBUG_DATALINK, ("Leaving.\n"));
953
954 return NDIS_STATUS_SUCCESS;
955 }
956
957
958 NDIS_STATUS LANUnregisterAdapter(
959 PLAN_ADAPTER Adapter)
960 /*
961 * FUNCTION: Unregisters protocol with NDIS adapter
962 * ARGUMENTS:
963 * Adapter = Pointer to a LAN_ADAPTER structure
964 * RETURNS:
965 * Status of operation
966 */
967 {
968 KIRQL OldIrql;
969 NDIS_HANDLE NdisHandle;
970 NDIS_STATUS NdisStatus = NDIS_STATUS_SUCCESS;
971
972 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
973
974 /* Unlink the adapter from the list */
975 RemoveEntryList(&Adapter->ListEntry);
976
977 /* Unbind adapter from IP layer */
978 UnbindAdapter(Adapter);
979
980 TcpipAcquireSpinLock(&Adapter->Lock, &OldIrql);
981 NdisHandle = Adapter->NdisHandle;
982 if (NdisHandle) {
983 Adapter->NdisHandle = NULL;
984 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
985
986 NdisCloseAdapter(&NdisStatus, NdisHandle);
987 if (NdisStatus == NDIS_STATUS_PENDING) {
988 TcpipWaitForSingleObject(&Adapter->Event,
989 UserRequest,
990 KernelMode,
991 FALSE,
992 NULL);
993 NdisStatus = Adapter->NdisStatus;
994 }
995 } else
996 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
997
998 FreeAdapter(Adapter);
999
1000 return NDIS_STATUS_SUCCESS;
1001 }
1002
1003
1004 NTSTATUS LANRegisterProtocol(
1005 PNDIS_STRING Name)
1006 /*
1007 * FUNCTION: Registers this protocol driver with NDIS
1008 * ARGUMENTS:
1009 * Name = Name of this protocol driver
1010 * RETURNS:
1011 * Status of operation
1012 */
1013 {
1014 NDIS_STATUS NdisStatus;
1015 NDIS_PROTOCOL_CHARACTERISTICS ProtChars;
1016
1017 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1018
1019 InitializeListHead(&AdapterListHead);
1020 KeInitializeSpinLock(&AdapterListLock);
1021
1022 /* Set up protocol characteristics */
1023 RtlZeroMemory(&ProtChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1024 ProtChars.MajorNdisVersion = NDIS_VERSION_MAJOR;
1025 ProtChars.MinorNdisVersion = NDIS_VERSION_MINOR;
1026 ProtChars.Name.Length = Name->Length;
1027 ProtChars.Name.Buffer = Name->Buffer;
1028 ProtChars.Name.MaximumLength = Name->MaximumLength;
1029 ProtChars.OpenAdapterCompleteHandler = ProtocolOpenAdapterComplete;
1030 ProtChars.CloseAdapterCompleteHandler = ProtocolCloseAdapterComplete;
1031 ProtChars.ResetCompleteHandler = ProtocolResetComplete;
1032 ProtChars.RequestCompleteHandler = ProtocolRequestComplete;
1033 ProtChars.SendCompleteHandler = ProtocolSendComplete;
1034 ProtChars.TransferDataCompleteHandler = ProtocolTransferDataComplete;
1035 ProtChars.ReceiveHandler = ProtocolReceive;
1036 ProtChars.ReceiveCompleteHandler = ProtocolReceiveComplete;
1037 ProtChars.StatusHandler = ProtocolStatus;
1038 ProtChars.StatusCompleteHandler = ProtocolStatusComplete;
1039 ProtChars.BindAdapterHandler = ProtocolBindAdapter;
1040
1041 /* Try to register protocol */
1042 NdisRegisterProtocol(&NdisStatus,
1043 &NdisProtocolHandle,
1044 &ProtChars,
1045 sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1046 if (NdisStatus != NDIS_STATUS_SUCCESS)
1047 {
1048 TI_DbgPrint(MID_TRACE, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus));
1049 return (NTSTATUS)NdisStatus;
1050 }
1051
1052 ProtocolRegistered = TRUE;
1053
1054 return STATUS_SUCCESS;
1055 }
1056
1057
1058 VOID LANUnregisterProtocol(
1059 VOID)
1060 /*
1061 * FUNCTION: Unregisters this protocol driver with NDIS
1062 * NOTES: Does not care wether we are already registered
1063 */
1064 {
1065 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1066
1067 if (ProtocolRegistered) {
1068 NDIS_STATUS NdisStatus;
1069 PLIST_ENTRY CurrentEntry;
1070 PLIST_ENTRY NextEntry;
1071 PLAN_ADAPTER Current;
1072 KIRQL OldIrql;
1073
1074 TcpipAcquireSpinLock(&AdapterListLock, &OldIrql);
1075
1076 /* Search the list and remove every adapter we find */
1077 CurrentEntry = AdapterListHead.Flink;
1078 while (CurrentEntry != &AdapterListHead) {
1079 NextEntry = CurrentEntry->Flink;
1080 Current = CONTAINING_RECORD(CurrentEntry, LAN_ADAPTER, ListEntry);
1081 /* Unregister it */
1082 LANUnregisterAdapter(Current);
1083 CurrentEntry = NextEntry;
1084 }
1085
1086 TcpipReleaseSpinLock(&AdapterListLock, OldIrql);
1087
1088 NdisDeregisterProtocol(&NdisStatus, NdisProtocolHandle);
1089 ProtocolRegistered = FALSE;
1090 }
1091 }
1092
1093 VOID LANStartup() {
1094 InitializeListHead( &LanWorkList );
1095 ExInitializeWorkItem( &LanWorkItem, LanReceiveWorker, NULL );
1096 }
1097
1098 VOID LANShutdown() {
1099 KIRQL OldIrql;
1100 PLAN_WQ_ITEM WorkItem;
1101 PLIST_ENTRY ListEntry;
1102
1103 TcpipAcquireSpinLock( &LanWorkLock, &OldIrql );
1104 while( !IsListEmpty( &LanWorkList ) ) {
1105 ListEntry = RemoveHeadList( &LanWorkList );
1106 WorkItem = CONTAINING_RECORD(ListEntry, LAN_WQ_ITEM, ListEntry);
1107 FreeNdisPacket( WorkItem->Packet );
1108 ExFreePool( WorkItem );
1109 }
1110 TcpipReleaseSpinLock( &LanWorkLock, OldIrql );
1111 }
1112
1113 /* EOF */