2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: network/neighbor.c
5 * PURPOSE: Neighbor address cache
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/08-2000 Created
13 NEIGHBOR_CACHE_TABLE NeighborCache
[NB_HASHMASK
+ 1];
15 VOID
NBCompleteSend( PVOID Context
,
16 PNDIS_PACKET NdisPacket
,
17 NDIS_STATUS Status
) {
18 PNEIGHBOR_PACKET Packet
= (PNEIGHBOR_PACKET
)Context
;
19 TI_DbgPrint(MID_TRACE
, ("Called\n"));
20 ASSERT_KM_POINTER(Packet
);
21 ASSERT_KM_POINTER(Packet
->Complete
);
22 Packet
->Complete( Packet
->Context
, Packet
->Packet
, Status
);
23 TI_DbgPrint(MID_TRACE
, ("Completed\n"));
24 ExFreePoolWithTag( Packet
, NEIGHBOR_PACKET_TAG
);
25 TI_DbgPrint(MID_TRACE
, ("Freed\n"));
28 VOID
NBSendPackets( PNEIGHBOR_CACHE_ENTRY NCE
) {
29 PLIST_ENTRY PacketEntry
;
30 PNEIGHBOR_PACKET Packet
;
33 ASSERT(!(NCE
->State
& NUD_INCOMPLETE
));
35 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
36 HashValue
^= HashValue
>> 16;
37 HashValue
^= HashValue
>> 8;
38 HashValue
^= HashValue
>> 4;
39 HashValue
&= NB_HASHMASK
;
41 /* Send any waiting packets */
42 while ((PacketEntry
= ExInterlockedRemoveHeadList(&NCE
->PacketQueue
,
43 &NeighborCache
[HashValue
].Lock
)) != NULL
)
45 Packet
= CONTAINING_RECORD( PacketEntry
, NEIGHBOR_PACKET
, Next
);
49 ("PacketEntry: %x, NdisPacket %x\n",
50 PacketEntry
, Packet
->Packet
));
52 PC(Packet
->Packet
)->DLComplete
= NBCompleteSend
;
53 PC(Packet
->Packet
)->Context
= Packet
;
55 NCE
->Interface
->Transmit
56 ( NCE
->Interface
->Context
,
64 /* Must be called with table lock acquired */
65 VOID
NBFlushPacketQueue( PNEIGHBOR_CACHE_ENTRY NCE
,
66 NTSTATUS ErrorCode
) {
67 PLIST_ENTRY PacketEntry
;
68 PNEIGHBOR_PACKET Packet
;
70 while( !IsListEmpty(&NCE
->PacketQueue
) ) {
71 PacketEntry
= RemoveHeadList(&NCE
->PacketQueue
);
72 Packet
= CONTAINING_RECORD
73 ( PacketEntry
, NEIGHBOR_PACKET
, Next
);
75 ASSERT_KM_POINTER(Packet
);
79 ("PacketEntry: %x, NdisPacket %x\n",
80 PacketEntry
, Packet
->Packet
));
82 ASSERT_KM_POINTER(Packet
->Complete
);
83 Packet
->Complete( Packet
->Context
,
87 ExFreePoolWithTag( Packet
, NEIGHBOR_PACKET_TAG
);
93 * FUNCTION: Neighbor address cache timeout handler
95 * This routine is called by IPTimeout to remove outdated cache
100 PNEIGHBOR_CACHE_ENTRY
*PrevNCE
;
101 PNEIGHBOR_CACHE_ENTRY NCE
;
104 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
105 TcpipAcquireSpinLockAtDpcLevel(&NeighborCache
[i
].Lock
);
107 for (PrevNCE
= &NeighborCache
[i
].Cache
;
108 (NCE
= *PrevNCE
) != NULL
;) {
109 /* Check if event timer is running */
110 if (NCE
->EventTimer
> 0) {
111 ASSERT(!(NCE
->State
& NUD_PERMANENT
));
113 if (NCE
->State
& NUD_INCOMPLETE
)
115 /* We desperately need an address in this state or
116 * we timeout in 5 seconds */
119 else if ((NCE
->EventCount
> ARP_RATE
&&
120 NCE
->EventCount
% ARP_TIMEOUT_RETRANSMISSION
== 0) ||
121 (NCE
->EventCount
== ARP_RATE
))
123 /* We haven't gotten a packet from them in
124 * EventCount seconds so we mark them as stale
126 NCE
->State
|= NUD_STALE
;
129 if (NCE
->EventTimer
- NCE
->EventCount
== 0) {
130 /* Unlink and destroy the NCE */
131 *PrevNCE
= NCE
->Next
;
133 /* Choose the proper failure status */
134 if (NCE
->State
& NUD_INCOMPLETE
)
136 /* We couldn't get an address to this IP at all */
137 Status
= NDIS_STATUS_NETWORK_UNREACHABLE
;
141 /* This guy was stale for way too long */
142 Status
= NDIS_STATUS_REQUEST_ABORTED
;
145 NBFlushPacketQueue(NCE
, Status
);
146 ExFreePoolWithTag(NCE
, NCE_TAG
);
151 PrevNCE
= &NCE
->Next
;
154 TcpipReleaseSpinLockFromDpcLevel(&NeighborCache
[i
].Lock
);
160 * FUNCTION: Starts the neighbor cache
165 TI_DbgPrint(DEBUG_NCACHE
, ("Called.\n"));
167 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
168 NeighborCache
[i
].Cache
= NULL
;
169 TcpipInitializeSpinLock(&NeighborCache
[i
].Lock
);
173 VOID
NBShutdown(VOID
)
175 * FUNCTION: Shuts down the neighbor cache
178 PNEIGHBOR_CACHE_ENTRY NextNCE
;
179 PNEIGHBOR_CACHE_ENTRY CurNCE
;
183 TI_DbgPrint(DEBUG_NCACHE
, ("Called.\n"));
185 /* Remove possible entries from the cache */
186 for (i
= 0; i
<= NB_HASHMASK
; i
++)
188 TcpipAcquireSpinLock(&NeighborCache
[i
].Lock
, &OldIrql
);
190 CurNCE
= NeighborCache
[i
].Cache
;
192 NextNCE
= CurNCE
->Next
;
194 /* Flush wait queue */
195 NBFlushPacketQueue( CurNCE
, NDIS_STATUS_NOT_ACCEPTED
);
197 ExFreePoolWithTag(CurNCE
, NCE_TAG
);
202 NeighborCache
[i
].Cache
= NULL
;
204 TcpipReleaseSpinLock(&NeighborCache
[i
].Lock
, OldIrql
);
207 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
210 VOID
NBSendSolicit(PNEIGHBOR_CACHE_ENTRY NCE
)
212 * FUNCTION: Sends a neighbor solicitation message
214 * NCE = Pointer to NCE of neighbor to solicit
216 * May be called with lock held on NCE's table
219 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X).\n", NCE
));
221 ARPTransmit(&NCE
->Address
,
222 (NCE
->State
& NUD_INCOMPLETE
) ? NULL
: NCE
->LinkAddress
,
226 PNEIGHBOR_CACHE_ENTRY
NBAddNeighbor(
227 PIP_INTERFACE Interface
,
230 UINT LinkAddressLength
,
234 * FUNCTION: Adds a neighbor to the neighbor cache
236 * Interface = Pointer to interface
237 * Address = Pointer to IP address
238 * LinkAddress = Pointer to link address (may be NULL)
239 * LinkAddressLength = Length of link address
240 * State = State of NCE
242 * Pointer to NCE, NULL there is not enough free resources
244 * The NCE if referenced for the caller if created. The NCE retains
245 * a reference to the IP address if it is created, the caller is
246 * responsible for providing this reference
249 PNEIGHBOR_CACHE_ENTRY NCE
;
255 ("Called. Interface (0x%X) Address (0x%X) "
256 "LinkAddress (0x%X) LinkAddressLength (%d) State (0x%X)\n",
257 Interface
, Address
, LinkAddress
, LinkAddressLength
, State
));
259 NCE
= ExAllocatePoolWithTag
260 (NonPagedPool
, sizeof(NEIGHBOR_CACHE_ENTRY
) + LinkAddressLength
,
264 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
268 NCE
->Interface
= Interface
;
269 NCE
->Address
= *Address
;
270 NCE
->LinkAddressLength
= LinkAddressLength
;
271 NCE
->LinkAddress
= (PVOID
)&NCE
[1];
273 RtlCopyMemory(NCE
->LinkAddress
, LinkAddress
, LinkAddressLength
);
275 memset(NCE
->LinkAddress
, 0xff, LinkAddressLength
);
277 NCE
->EventTimer
= EventTimer
;
279 InitializeListHead( &NCE
->PacketQueue
);
281 TI_DbgPrint(MID_TRACE
,("NCE: %x\n", NCE
));
283 HashValue
= *(PULONG
)&Address
->Address
;
284 HashValue
^= HashValue
>> 16;
285 HashValue
^= HashValue
>> 8;
286 HashValue
^= HashValue
>> 4;
287 HashValue
&= NB_HASHMASK
;
289 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
291 NCE
->Next
= NeighborCache
[HashValue
].Cache
;
292 NeighborCache
[HashValue
].Cache
= NCE
;
294 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
299 VOID
NBUpdateNeighbor(
300 PNEIGHBOR_CACHE_ENTRY NCE
,
304 * FUNCTION: Update link address information in NCE
306 * NCE = Pointer to NCE to update
307 * LinkAddress = Pointer to link address
308 * State = State of NCE
310 * The link address and state is updated. Any waiting packets are sent
316 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X) LinkAddress (0x%X) State (0x%X).\n", NCE
, LinkAddress
, State
));
318 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
319 HashValue
^= HashValue
>> 16;
320 HashValue
^= HashValue
>> 8;
321 HashValue
^= HashValue
>> 4;
322 HashValue
&= NB_HASHMASK
;
324 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
326 RtlCopyMemory(NCE
->LinkAddress
, LinkAddress
, NCE
->LinkAddressLength
);
330 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
332 if( !(NCE
->State
& NUD_INCOMPLETE
) )
334 NCE
->EventTimer
= ARP_COMPLETE_TIMEOUT
;
335 NBSendPackets( NCE
);
340 NBResetNeighborTimeout(PIP_ADDRESS Address
)
344 PNEIGHBOR_CACHE_ENTRY NCE
;
346 TI_DbgPrint(DEBUG_NCACHE
, ("Resetting NCE timout for 0x%s\n", A2S(Address
)));
348 HashValue
= *(PULONG
)(&Address
->Address
);
349 HashValue
^= HashValue
>> 16;
350 HashValue
^= HashValue
>> 8;
351 HashValue
^= HashValue
>> 4;
352 HashValue
&= NB_HASHMASK
;
354 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
356 for (NCE
= NeighborCache
[HashValue
].Cache
;
360 if (AddrIsEqual(Address
, &NCE
->Address
))
367 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
370 PNEIGHBOR_CACHE_ENTRY
NBLocateNeighbor(
373 * FUNCTION: Locates a neighbor in the neighbor cache
375 * Address = Pointer to IP address
377 * Pointer to NCE, NULL if not found
379 * If the NCE is found, it is referenced. The caller is
380 * responsible for dereferencing it again after use
383 PNEIGHBOR_CACHE_ENTRY NCE
;
387 TI_DbgPrint(DEBUG_NCACHE
, ("Called. Address (0x%X).\n", Address
));
389 HashValue
= *(PULONG
)&Address
->Address
;
390 HashValue
^= HashValue
>> 16;
391 HashValue
^= HashValue
>> 8;
392 HashValue
^= HashValue
>> 4;
393 HashValue
&= NB_HASHMASK
;
395 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
397 NCE
= NeighborCache
[HashValue
].Cache
;
399 while ((NCE
) && (!AddrIsEqual(Address
, &NCE
->Address
)))
404 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
406 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
411 PNEIGHBOR_CACHE_ENTRY
NBFindOrCreateNeighbor(
412 PIP_INTERFACE Interface
,
415 * FUNCTION: Tries to find a neighbor and if unsuccesful, creates a new NCE
417 * Interface = Pointer to interface to use (in case NCE is not found)
418 * Address = Pointer to IP address
420 * Pointer to NCE, NULL if there is not enough free resources
422 * The NCE is referenced if found or created. The caller is
423 * responsible for dereferencing it again after use
426 PNEIGHBOR_CACHE_ENTRY NCE
;
428 TI_DbgPrint(DEBUG_NCACHE
, ("Called. Interface (0x%X) Address (0x%X).\n", Interface
, Address
));
430 NCE
= NBLocateNeighbor(Address
);
433 TI_DbgPrint(MID_TRACE
,("BCAST: %s\n", A2S(&Interface
->Broadcast
)));
434 if( AddrIsEqual(Address
, &Interface
->Broadcast
) ||
435 AddrIsUnspecified(Address
) ) {
436 TI_DbgPrint(MID_TRACE
,("Packet targeted at broadcast addr\n"));
437 NCE
= NBAddNeighbor(Interface
, Address
, NULL
,
438 Interface
->AddressLength
, NUD_PERMANENT
, 0);
440 NCE
= NBAddNeighbor(Interface
, Address
, NULL
,
441 Interface
->AddressLength
, NUD_INCOMPLETE
, ARP_INCOMPLETE_TIMEOUT
);
442 if (!NCE
) return NULL
;
450 BOOLEAN
NBQueuePacket(
451 PNEIGHBOR_CACHE_ENTRY NCE
,
452 PNDIS_PACKET NdisPacket
,
453 PNEIGHBOR_PACKET_COMPLETE PacketComplete
,
456 * FUNCTION: Queues a packet on an NCE for later transmission
458 * NCE = Pointer to NCE to queue packet on
459 * NdisPacket = Pointer to NDIS packet to queue
461 * TRUE if the packet was successfully queued, FALSE if not
465 PNEIGHBOR_PACKET Packet
;
470 ("Called. NCE (0x%X) NdisPacket (0x%X).\n", NCE
, NdisPacket
));
472 Packet
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(NEIGHBOR_PACKET
),
473 NEIGHBOR_PACKET_TAG
);
474 if( !Packet
) return FALSE
;
476 /* FIXME: Should we limit the number of queued packets? */
478 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
479 HashValue
^= HashValue
>> 16;
480 HashValue
^= HashValue
>> 8;
481 HashValue
^= HashValue
>> 4;
482 HashValue
&= NB_HASHMASK
;
484 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
486 Packet
->Complete
= PacketComplete
;
487 Packet
->Context
= PacketContext
;
488 Packet
->Packet
= NdisPacket
;
489 InsertTailList( &NCE
->PacketQueue
, &Packet
->Next
);
491 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
493 if( !(NCE
->State
& NUD_INCOMPLETE
) )
494 NBSendPackets( NCE
);
499 VOID
NBRemoveNeighbor(
500 PNEIGHBOR_CACHE_ENTRY NCE
)
502 * FUNCTION: Removes a neighbor from the neighbor cache
504 * NCE = Pointer to NCE to remove from cache
506 * The NCE must be in a safe state
509 PNEIGHBOR_CACHE_ENTRY
*PrevNCE
;
510 PNEIGHBOR_CACHE_ENTRY CurNCE
;
514 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X).\n", NCE
));
516 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
517 HashValue
^= HashValue
>> 16;
518 HashValue
^= HashValue
>> 8;
519 HashValue
^= HashValue
>> 4;
520 HashValue
&= NB_HASHMASK
;
522 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
524 /* Search the list and remove the NCE from the list if found */
525 for (PrevNCE
= &NeighborCache
[HashValue
].Cache
;
526 (CurNCE
= *PrevNCE
) != NULL
;
527 PrevNCE
= &CurNCE
->Next
)
531 /* Found it, now unlink it from the list */
532 *PrevNCE
= CurNCE
->Next
;
534 NBFlushPacketQueue( CurNCE
, NDIS_STATUS_REQUEST_ABORTED
);
535 ExFreePoolWithTag(CurNCE
, NCE_TAG
);
541 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
544 ULONG NBCopyNeighbors
545 (PIP_INTERFACE Interface
,
546 PIPARP_ENTRY ArpTable
)
548 PNEIGHBOR_CACHE_ENTRY CurNCE
;
552 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
553 TcpipAcquireSpinLock(&NeighborCache
[i
].Lock
, &OldIrql
);
554 for( CurNCE
= NeighborCache
[i
].Cache
;
556 CurNCE
= CurNCE
->Next
) {
557 if( CurNCE
->Interface
== Interface
&&
558 !AddrIsEqual( &CurNCE
->Address
, &CurNCE
->Interface
->Unicast
) ) {
560 ArpTable
[Size
].Index
= Interface
->Index
;
561 ArpTable
[Size
].AddrSize
= CurNCE
->LinkAddressLength
;
563 (ArpTable
[Size
].PhysAddr
,
565 CurNCE
->LinkAddressLength
);
566 ArpTable
[Size
].LogAddr
= CurNCE
->Address
.Address
.IPv4Address
;
567 if( CurNCE
->State
& NUD_PERMANENT
)
568 ArpTable
[Size
].Type
= ARP_ENTRY_STATIC
;
569 else if( CurNCE
->State
& NUD_INCOMPLETE
)
570 ArpTable
[Size
].Type
= ARP_ENTRY_INVALID
;
572 ArpTable
[Size
].Type
= ARP_ENTRY_DYNAMIC
;
577 TcpipReleaseSpinLock(&NeighborCache
[i
].Lock
, OldIrql
);