- Removed prefix.c and the prefix list. Adapter and route netmasks are now
[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 AddrInitIPv4(Address, inet_addr(AnsiAddress.Buffer));
650
651 return STATUS_SUCCESS;
652 }
653
654 static NTSTATUS ReadStringFromRegistry( HANDLE RegHandle,
655 PWCHAR RegistryValue,
656 PUNICODE_STRING String ) {
657 UNICODE_STRING ValueName;
658 UNICODE_STRING UnicodeString;
659 NTSTATUS Status;
660 ULONG ResultLength;
661 UCHAR buf[1024];
662 PKEY_VALUE_PARTIAL_INFORMATION Information = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
663
664 RtlInitUnicodeString(&ValueName, RegistryValue);
665 Status =
666 ZwQueryValueKey(RegHandle,
667 &ValueName,
668 KeyValuePartialInformation,
669 Information,
670 sizeof(buf),
671 &ResultLength);
672
673 if (!NT_SUCCESS(Status))
674 return Status;
675 /* IP address is stored as a REG_MULTI_SZ - we only pay attention to the first one though */
676 TI_DbgPrint(MIN_TRACE, ("Information DataLength: 0x%x\n", Information->DataLength));
677
678 UnicodeString.Buffer = (PWCHAR)&Information->Data;
679 UnicodeString.Length = Information->DataLength;
680 UnicodeString.MaximumLength = Information->DataLength;
681
682 String->Buffer =
683 (PWCHAR)exAllocatePool( NonPagedPool,
684 UnicodeString.MaximumLength + sizeof(WCHAR) );
685
686 if( !String->Buffer ) return STATUS_NO_MEMORY;
687
688 String->MaximumLength = UnicodeString.MaximumLength;
689 RtlCopyUnicodeString( String, &UnicodeString );
690
691 return STATUS_SUCCESS;
692 }
693
694 static VOID GetSimpleAdapterNameFromRegistryPath
695 ( PUNICODE_STRING TargetString,
696 PUNICODE_STRING RegistryPath ) {
697 PWCHAR i, LastSlash = NULL;
698 UINT NewStringLength = 0;
699
700 for( i = RegistryPath->Buffer;
701 i < RegistryPath->Buffer +
702 (RegistryPath->Length / sizeof(WCHAR));
703 i++ ) if( *i == '\\' ) LastSlash = i;
704
705 if( LastSlash ) LastSlash++; else LastSlash = RegistryPath->Buffer;
706
707 NewStringLength = RegistryPath->MaximumLength -
708 ((LastSlash - RegistryPath->Buffer) * sizeof(WCHAR));
709
710 TargetString->Buffer =
711 (PWCHAR)exAllocatePool( NonPagedPool, NewStringLength );
712
713 if( !TargetString->Buffer ) {
714 TargetString->Length = TargetString->MaximumLength = 0;
715 return;
716 }
717
718 TargetString->Length = TargetString->MaximumLength = NewStringLength;
719 RtlCopyMemory( TargetString->Buffer, LastSlash, NewStringLength );
720 }
721
722 VOID BindAdapter(
723 PLAN_ADAPTER Adapter,
724 PNDIS_STRING RegistryPath)
725 /*
726 * FUNCTION: Binds a LAN adapter to IP layer
727 * ARGUMENTS:
728 * Adapter = Pointer to LAN_ADAPTER structure
729 * NOTES:
730 * We set the lookahead buffer size, set the packet filter and
731 * bind the adapter to IP layer
732 */
733 {
734 PIP_INTERFACE IF;
735 NDIS_STATUS NdisStatus;
736 LLIP_BIND_INFO BindInfo;
737 ULONG Lookahead = LOOKAHEAD_SIZE;
738 NTSTATUS Status;
739 HANDLE RegHandle = 0;
740
741 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
742
743 Adapter->State = LAN_STATE_OPENING;
744
745 NdisStatus = NDISCall(Adapter,
746 NdisRequestSetInformation,
747 OID_GEN_CURRENT_LOOKAHEAD,
748 &Lookahead,
749 sizeof(ULONG));
750 if (NdisStatus != NDIS_STATUS_SUCCESS) {
751 TI_DbgPrint(MID_TRACE, ("Could not set lookahead buffer size (0x%X).\n", NdisStatus));
752 return;
753 }
754
755 /* Bind the adapter to IP layer */
756 BindInfo.Context = Adapter;
757 BindInfo.HeaderSize = Adapter->HeaderSize;
758 BindInfo.MinFrameSize = Adapter->MinFrameSize;
759 BindInfo.MTU = Adapter->MTU;
760 BindInfo.Address = (PUCHAR)&Adapter->HWAddress;
761 BindInfo.AddressLength = Adapter->HWAddressLength;
762 BindInfo.Transmit = LANTransmit;
763
764 IF = IPCreateInterface(&BindInfo);
765
766 if (!IF) {
767 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
768 return;
769 }
770
771 /*
772 * Query per-adapter configuration from the registry
773 * In case anyone is curious: there *is* an Ndis configuration api
774 * for this sort of thing, but it doesn't really support things like
775 * REG_MULTI_SZ very well, and there is a note in the DDK that says that
776 * protocol drivers developed for win2k and above just use the native
777 * services (ZwOpenKey, etc).
778 */
779
780 Status = OpenRegistryKey( RegistryPath, &RegHandle );
781
782 if(NT_SUCCESS(Status))
783 Status = ReadIPAddressFromRegistry( RegHandle, L"IPAddress",
784 &IF->Unicast );
785 if(NT_SUCCESS(Status))
786 Status = ReadIPAddressFromRegistry( RegHandle, L"SubnetMask",
787 &IF->Netmask );
788 if(NT_SUCCESS(Status)) {
789 Status = ReadStringFromRegistry( RegHandle, L"DeviceDesc",
790 &IF->Name );
791
792 RtlZeroMemory( &IF->Name, sizeof( IF->Name ) );
793
794 /* I think that not getting a devicedesc is not a fatal error */
795 if( !NT_SUCCESS(Status) ) {
796 if( IF->Name.Buffer ) exFreePool( IF->Name.Buffer );
797 GetSimpleAdapterNameFromRegistryPath( &IF->Name, RegistryPath );
798 }
799 Status = STATUS_SUCCESS;
800 }
801
802 if(!NT_SUCCESS(Status))
803 {
804 TI_DbgPrint(MIN_TRACE, ("Unable to open protocol-specific registry key: 0x%x\n", Status));
805
806 /* XXX how do we proceed? No ip address, no parameters... do we guess? */
807 if(RegHandle)
808 ZwClose(RegHandle);
809 IPDestroyInterface(IF);
810 return;
811 }
812
813 TI_DbgPrint
814 (MID_TRACE,
815 ("--> Our IP address on this interface: '%s'\n",
816 A2S(&IF->Unicast)));
817
818 TI_DbgPrint
819 (MID_TRACE,
820 ("--> Our net mask on this interface: '%s'\n",
821 A2S(&IF->Netmask)));
822
823 /* Register interface with IP layer */
824 IPRegisterInterface(IF);
825
826 /* Set packet filter so we can send and receive packets */
827 NdisStatus = NDISCall(Adapter,
828 NdisRequestSetInformation,
829 OID_GEN_CURRENT_PACKET_FILTER,
830 &Adapter->PacketFilter,
831 sizeof(UINT));
832 if (NdisStatus != NDIS_STATUS_SUCCESS) {
833 TI_DbgPrint(MID_TRACE, ("Could not set packet filter (0x%X).\n", NdisStatus));
834 IPDestroyInterface(IF);
835 return;
836 }
837
838 Adapter->Context = IF;
839 Adapter->State = LAN_STATE_STARTED;
840 }
841
842
843 VOID UnbindAdapter(
844 PLAN_ADAPTER Adapter)
845 /*
846 * FUNCTION: Unbinds a LAN adapter from IP layer
847 * ARGUMENTS:
848 * Adapter = Pointer to LAN_ADAPTER structure
849 */
850 {
851 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
852
853 if (Adapter->State == LAN_STATE_STARTED) {
854 PIP_INTERFACE IF = Adapter->Context;
855
856 IPUnregisterInterface(IF);
857
858 IPDestroyInterface(IF);
859 }
860 }
861
862
863 NDIS_STATUS LANRegisterAdapter(
864 PNDIS_STRING AdapterName,
865 PNDIS_STRING RegistryPath)
866 /*
867 * FUNCTION: Registers protocol with an NDIS adapter
868 * ARGUMENTS:
869 * AdapterName = Pointer to string with name of adapter to register
870 * Adapter = Address of pointer to a LAN_ADAPTER structure
871 * RETURNS:
872 * Status of operation
873 */
874 {
875 PLAN_ADAPTER IF;
876 NDIS_STATUS NdisStatus;
877 NDIS_STATUS OpenStatus;
878 UINT MediaIndex;
879 NDIS_MEDIUM MediaArray[MAX_MEDIA];
880 UINT AddressOID;
881 UINT Speed;
882
883 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
884
885 IF = exAllocatePool(NonPagedPool, sizeof(LAN_ADAPTER));
886 if (!IF) {
887 TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
888 return NDIS_STATUS_RESOURCES;
889 }
890
891 RtlZeroMemory(IF, sizeof(LAN_ADAPTER));
892
893 /* Put adapter in stopped state */
894 IF->State = LAN_STATE_STOPPED;
895
896 /* Initialize protecting spin lock */
897 KeInitializeSpinLock(&IF->Lock);
898
899 KeInitializeEvent(&IF->Event, SynchronizationEvent, FALSE);
900
901 /* Initialize array with media IDs we support */
902 MediaArray[MEDIA_ETH] = NdisMedium802_3;
903
904 TI_DbgPrint(DEBUG_DATALINK,("opening adapter %wZ\n", AdapterName));
905 /* Open the adapter. */
906 NdisOpenAdapter(&NdisStatus,
907 &OpenStatus,
908 &IF->NdisHandle,
909 &MediaIndex,
910 MediaArray,
911 MAX_MEDIA,
912 NdisProtocolHandle,
913 IF,
914 AdapterName,
915 0,
916 NULL);
917
918 /* Wait until the adapter is opened */
919 if (NdisStatus == NDIS_STATUS_PENDING)
920 KeWaitForSingleObject(&IF->Event, UserRequest, KernelMode, FALSE, NULL);
921 else if (NdisStatus != NDIS_STATUS_SUCCESS) {
922 exFreePool(IF);
923 return NdisStatus;
924 }
925
926 IF->Media = MediaArray[MediaIndex];
927
928 /* Fill LAN_ADAPTER structure with some adapter specific information */
929 switch (IF->Media) {
930 case NdisMedium802_3:
931 IF->HWAddressLength = IEEE_802_ADDR_LENGTH;
932 IF->BCastMask = BCAST_ETH_MASK;
933 IF->BCastCheck = BCAST_ETH_CHECK;
934 IF->BCastOffset = BCAST_ETH_OFFSET;
935 IF->HeaderSize = sizeof(ETH_HEADER);
936 IF->MinFrameSize = 60;
937 AddressOID = OID_802_3_CURRENT_ADDRESS;
938 IF->PacketFilter =
939 NDIS_PACKET_TYPE_BROADCAST |
940 NDIS_PACKET_TYPE_DIRECTED |
941 NDIS_PACKET_TYPE_MULTICAST;
942 break;
943
944 default:
945 /* Unsupported media */
946 TI_DbgPrint(MIN_TRACE, ("Unsupported media.\n"));
947 exFreePool(IF);
948 return NDIS_STATUS_NOT_SUPPORTED;
949 }
950
951 /* Get maximum frame size */
952 NdisStatus = NDISCall(IF,
953 NdisRequestQueryInformation,
954 OID_GEN_MAXIMUM_FRAME_SIZE,
955 &IF->MTU,
956 sizeof(UINT));
957 if (NdisStatus != NDIS_STATUS_SUCCESS) {
958 exFreePool(IF);
959 return NdisStatus;
960 }
961
962 /* Get maximum packet size */
963 NdisStatus = NDISCall(IF,
964 NdisRequestQueryInformation,
965 OID_GEN_MAXIMUM_TOTAL_SIZE,
966 &IF->MaxPacketSize,
967 sizeof(UINT));
968 if (NdisStatus != NDIS_STATUS_SUCCESS) {
969 TI_DbgPrint(MIN_TRACE, ("Query for maximum packet size failed.\n"));
970 exFreePool(IF);
971 return NdisStatus;
972 }
973
974 /* Get maximum number of packets we can pass to NdisSend(Packets) at one time */
975 NdisStatus = NDISCall(IF,
976 NdisRequestQueryInformation,
977 OID_GEN_MAXIMUM_SEND_PACKETS,
978 &IF->MaxSendPackets,
979 sizeof(UINT));
980 if (NdisStatus != NDIS_STATUS_SUCCESS)
981 /* Legacy NIC drivers may not support this query, if it fails we
982 assume it can send at least one packet per call to NdisSend(Packets) */
983 IF->MaxSendPackets = 1;
984
985 /* Get current hardware address */
986 NdisStatus = NDISCall(IF,
987 NdisRequestQueryInformation,
988 AddressOID,
989 &IF->HWAddress,
990 IF->HWAddressLength);
991 if (NdisStatus != NDIS_STATUS_SUCCESS) {
992 TI_DbgPrint(MIN_TRACE, ("Query for current hardware address failed.\n"));
993 exFreePool(IF);
994 return NdisStatus;
995 }
996
997 /* Get maximum link speed */
998 NdisStatus = NDISCall(IF,
999 NdisRequestQueryInformation,
1000 OID_GEN_LINK_SPEED,
1001 &Speed,
1002 sizeof(UINT));
1003 if (NdisStatus != NDIS_STATUS_SUCCESS) {
1004 TI_DbgPrint(MIN_TRACE, ("Query for maximum link speed failed.\n"));
1005 exFreePool(IF);
1006 return NdisStatus;
1007 }
1008
1009 /* Convert returned link speed to bps (it is in 100bps increments) */
1010 IF->Speed = Speed * 100L;
1011
1012 /* Add adapter to the adapter list */
1013 ExInterlockedInsertTailList(&AdapterListHead,
1014 &IF->ListEntry,
1015 &AdapterListLock);
1016
1017 /* Bind adapter to IP layer */
1018 BindAdapter(IF, RegistryPath);
1019
1020 TI_DbgPrint(DEBUG_DATALINK, ("Leaving.\n"));
1021
1022 return NDIS_STATUS_SUCCESS;
1023 }
1024
1025
1026 NDIS_STATUS LANUnregisterAdapter(
1027 PLAN_ADAPTER Adapter)
1028 /*
1029 * FUNCTION: Unregisters protocol with NDIS adapter
1030 * ARGUMENTS:
1031 * Adapter = Pointer to a LAN_ADAPTER structure
1032 * RETURNS:
1033 * Status of operation
1034 */
1035 {
1036 KIRQL OldIrql;
1037 NDIS_HANDLE NdisHandle;
1038 NDIS_STATUS NdisStatus = NDIS_STATUS_SUCCESS;
1039
1040 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1041
1042 /* Unlink the adapter from the list */
1043 RemoveEntryList(&Adapter->ListEntry);
1044
1045 /* Unbind adapter from IP layer */
1046 UnbindAdapter(Adapter);
1047
1048 TcpipAcquireSpinLock(&Adapter->Lock, &OldIrql);
1049 NdisHandle = Adapter->NdisHandle;
1050 if (NdisHandle) {
1051 Adapter->NdisHandle = NULL;
1052 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1053
1054 NdisCloseAdapter(&NdisStatus, NdisHandle);
1055 if (NdisStatus == NDIS_STATUS_PENDING) {
1056 TcpipWaitForSingleObject(&Adapter->Event,
1057 UserRequest,
1058 KernelMode,
1059 FALSE,
1060 NULL);
1061 NdisStatus = Adapter->NdisStatus;
1062 }
1063 } else
1064 TcpipReleaseSpinLock(&Adapter->Lock, OldIrql);
1065
1066 FreeAdapter(Adapter);
1067
1068 return NDIS_STATUS_SUCCESS;
1069 }
1070
1071
1072 NTSTATUS LANRegisterProtocol(
1073 PNDIS_STRING Name)
1074 /*
1075 * FUNCTION: Registers this protocol driver with NDIS
1076 * ARGUMENTS:
1077 * Name = Name of this protocol driver
1078 * RETURNS:
1079 * Status of operation
1080 */
1081 {
1082 NDIS_STATUS NdisStatus;
1083 NDIS_PROTOCOL_CHARACTERISTICS ProtChars;
1084
1085 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1086
1087 InitializeListHead(&AdapterListHead);
1088 KeInitializeSpinLock(&AdapterListLock);
1089
1090 /* Set up protocol characteristics */
1091 RtlZeroMemory(&ProtChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1092 ProtChars.MajorNdisVersion = NDIS_VERSION_MAJOR;
1093 ProtChars.MinorNdisVersion = NDIS_VERSION_MINOR;
1094 ProtChars.Name.Length = Name->Length;
1095 ProtChars.Name.Buffer = Name->Buffer;
1096 ProtChars.Name.MaximumLength = Name->MaximumLength;
1097 ProtChars.OpenAdapterCompleteHandler = ProtocolOpenAdapterComplete;
1098 ProtChars.CloseAdapterCompleteHandler = ProtocolCloseAdapterComplete;
1099 ProtChars.ResetCompleteHandler = ProtocolResetComplete;
1100 ProtChars.RequestCompleteHandler = ProtocolRequestComplete;
1101 ProtChars.SendCompleteHandler = ProtocolSendComplete;
1102 ProtChars.TransferDataCompleteHandler = ProtocolTransferDataComplete;
1103 ProtChars.ReceiveHandler = ProtocolReceive;
1104 ProtChars.ReceiveCompleteHandler = ProtocolReceiveComplete;
1105 ProtChars.StatusHandler = ProtocolStatus;
1106 ProtChars.StatusCompleteHandler = ProtocolStatusComplete;
1107 ProtChars.BindAdapterHandler = ProtocolBindAdapter;
1108
1109 /* Try to register protocol */
1110 NdisRegisterProtocol(&NdisStatus,
1111 &NdisProtocolHandle,
1112 &ProtChars,
1113 sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
1114 if (NdisStatus != NDIS_STATUS_SUCCESS)
1115 {
1116 TI_DbgPrint(MID_TRACE, ("NdisRegisterProtocol failed, status 0x%x\n", NdisStatus));
1117 return (NTSTATUS)NdisStatus;
1118 }
1119
1120 ProtocolRegistered = TRUE;
1121
1122 return STATUS_SUCCESS;
1123 }
1124
1125
1126 VOID LANUnregisterProtocol(
1127 VOID)
1128 /*
1129 * FUNCTION: Unregisters this protocol driver with NDIS
1130 * NOTES: Does not care wether we are already registered
1131 */
1132 {
1133 TI_DbgPrint(DEBUG_DATALINK, ("Called.\n"));
1134
1135 if (ProtocolRegistered) {
1136 NDIS_STATUS NdisStatus;
1137 PLIST_ENTRY CurrentEntry;
1138 PLIST_ENTRY NextEntry;
1139 PLAN_ADAPTER Current;
1140 KIRQL OldIrql;
1141
1142 TcpipAcquireSpinLock(&AdapterListLock, &OldIrql);
1143
1144 /* Search the list and remove every adapter we find */
1145 CurrentEntry = AdapterListHead.Flink;
1146 while (CurrentEntry != &AdapterListHead) {
1147 NextEntry = CurrentEntry->Flink;
1148 Current = CONTAINING_RECORD(CurrentEntry, LAN_ADAPTER, ListEntry);
1149 /* Unregister it */
1150 LANUnregisterAdapter(Current);
1151 CurrentEntry = NextEntry;
1152 }
1153
1154 TcpipReleaseSpinLock(&AdapterListLock, OldIrql);
1155
1156 NdisDeregisterProtocol(&NdisStatus, NdisProtocolHandle);
1157 ProtocolRegistered = FALSE;
1158 }
1159 }
1160
1161 VOID LANStartup() {
1162 InitializeListHead( &LanWorkList );
1163 ExInitializeWorkItem( &LanWorkItem, LanReceiveWorker, NULL );
1164 }
1165
1166 VOID LANShutdown() {
1167 KIRQL OldIrql;
1168 PLAN_WQ_ITEM WorkItem;
1169 PLIST_ENTRY ListEntry;
1170
1171 TcpipAcquireSpinLock( &LanWorkLock, &OldIrql );
1172 while( !IsListEmpty( &LanWorkList ) ) {
1173 ListEntry = RemoveHeadList( &LanWorkList );
1174 WorkItem = CONTAINING_RECORD(ListEntry, LAN_WQ_ITEM, ListEntry);
1175 FreeNdisPacket( WorkItem->Packet );
1176 ExFreePool( WorkItem );
1177 }
1178 TcpipReleaseSpinLock( &LanWorkLock, OldIrql );
1179 }
1180
1181 /* EOF */