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
101 PNEIGHBOR_CACHE_ENTRY
*PrevNCE
;
102 PNEIGHBOR_CACHE_ENTRY NCE
;
104 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
105 TcpipAcquireSpinLock(&NeighborCache
[i
].Lock
, &OldIrql
);
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
->EventCount
> ARP_RATE
&&
114 NCE
->EventCount
% ARP_TIMEOUT_RETRANSMISSION
== 0) ||
115 (NCE
->EventCount
== ARP_RATE
))
117 /* We haven't gotten a packet from them in
118 * EventCount seconds so we mark them as stale
120 NCE
->State
|= NUD_STALE
;
123 if (NCE
->EventTimer
- NCE
->EventCount
== 0) {
124 /* Solicit one last time */
127 /* Unlink and destroy the NCE */
128 *PrevNCE
= NCE
->Next
;
130 NBFlushPacketQueue(NCE
, NDIS_STATUS_REQUEST_ABORTED
);
131 ExFreePoolWithTag(NCE
, NCE_TAG
);
136 PrevNCE
= &NCE
->Next
;
139 TcpipReleaseSpinLock(&NeighborCache
[i
].Lock
, OldIrql
);
145 * FUNCTION: Starts the neighbor cache
150 TI_DbgPrint(DEBUG_NCACHE
, ("Called.\n"));
152 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
153 NeighborCache
[i
].Cache
= NULL
;
154 TcpipInitializeSpinLock(&NeighborCache
[i
].Lock
);
158 VOID
NBShutdown(VOID
)
160 * FUNCTION: Shuts down the neighbor cache
163 PNEIGHBOR_CACHE_ENTRY NextNCE
;
164 PNEIGHBOR_CACHE_ENTRY CurNCE
;
168 TI_DbgPrint(DEBUG_NCACHE
, ("Called.\n"));
170 /* Remove possible entries from the cache */
171 for (i
= 0; i
<= NB_HASHMASK
; i
++)
173 TcpipAcquireSpinLock(&NeighborCache
[i
].Lock
, &OldIrql
);
175 CurNCE
= NeighborCache
[i
].Cache
;
177 NextNCE
= CurNCE
->Next
;
179 /* Flush wait queue */
180 NBFlushPacketQueue( CurNCE
, NDIS_STATUS_NOT_ACCEPTED
);
182 ExFreePoolWithTag(CurNCE
, NCE_TAG
);
187 NeighborCache
[i
].Cache
= NULL
;
189 TcpipReleaseSpinLock(&NeighborCache
[i
].Lock
, OldIrql
);
192 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
195 VOID
NBSendSolicit(PNEIGHBOR_CACHE_ENTRY NCE
)
197 * FUNCTION: Sends a neighbor solicitation message
199 * NCE = Pointer to NCE of neighbor to solicit
201 * May be called with lock held on NCE's table
204 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X).\n", NCE
));
206 ARPTransmit(&NCE
->Address
,
207 (NCE
->State
& NUD_INCOMPLETE
) ? NULL
: NCE
->LinkAddress
,
211 PNEIGHBOR_CACHE_ENTRY
NBAddNeighbor(
212 PIP_INTERFACE Interface
,
215 UINT LinkAddressLength
,
219 * FUNCTION: Adds a neighbor to the neighbor cache
221 * Interface = Pointer to interface
222 * Address = Pointer to IP address
223 * LinkAddress = Pointer to link address (may be NULL)
224 * LinkAddressLength = Length of link address
225 * State = State of NCE
227 * Pointer to NCE, NULL there is not enough free resources
229 * The NCE if referenced for the caller if created. The NCE retains
230 * a reference to the IP address if it is created, the caller is
231 * responsible for providing this reference
234 PNEIGHBOR_CACHE_ENTRY NCE
;
240 ("Called. Interface (0x%X) Address (0x%X) "
241 "LinkAddress (0x%X) LinkAddressLength (%d) State (0x%X)\n",
242 Interface
, Address
, LinkAddress
, LinkAddressLength
, State
));
244 NCE
= ExAllocatePoolWithTag
245 (NonPagedPool
, sizeof(NEIGHBOR_CACHE_ENTRY
) + LinkAddressLength
,
249 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
253 NCE
->Interface
= Interface
;
254 NCE
->Address
= *Address
;
255 NCE
->LinkAddressLength
= LinkAddressLength
;
256 NCE
->LinkAddress
= (PVOID
)&NCE
[1];
258 RtlCopyMemory(NCE
->LinkAddress
, LinkAddress
, LinkAddressLength
);
260 memset(NCE
->LinkAddress
, 0xff, LinkAddressLength
);
262 NCE
->EventTimer
= EventTimer
;
264 InitializeListHead( &NCE
->PacketQueue
);
266 TI_DbgPrint(MID_TRACE
,("NCE: %x\n", NCE
));
268 HashValue
= *(PULONG
)&Address
->Address
;
269 HashValue
^= HashValue
>> 16;
270 HashValue
^= HashValue
>> 8;
271 HashValue
^= HashValue
>> 4;
272 HashValue
&= NB_HASHMASK
;
274 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
276 NCE
->Next
= NeighborCache
[HashValue
].Cache
;
277 NeighborCache
[HashValue
].Cache
= NCE
;
279 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
284 VOID
NBUpdateNeighbor(
285 PNEIGHBOR_CACHE_ENTRY NCE
,
289 * FUNCTION: Update link address information in NCE
291 * NCE = Pointer to NCE to update
292 * LinkAddress = Pointer to link address
293 * State = State of NCE
295 * The link address and state is updated. Any waiting packets are sent
301 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X) LinkAddress (0x%X) State (0x%X).\n", NCE
, LinkAddress
, State
));
303 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
304 HashValue
^= HashValue
>> 16;
305 HashValue
^= HashValue
>> 8;
306 HashValue
^= HashValue
>> 4;
307 HashValue
&= NB_HASHMASK
;
309 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
311 RtlCopyMemory(NCE
->LinkAddress
, LinkAddress
, NCE
->LinkAddressLength
);
315 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
317 if( !(NCE
->State
& NUD_INCOMPLETE
) )
318 NBSendPackets( NCE
);
322 NBResetNeighborTimeout(PIP_ADDRESS Address
)
326 PNEIGHBOR_CACHE_ENTRY NCE
;
328 TI_DbgPrint(DEBUG_NCACHE
, ("Resetting NCE timout for 0x%s\n", A2S(Address
)));
330 HashValue
= *(PULONG
)(&Address
->Address
);
331 HashValue
^= HashValue
>> 16;
332 HashValue
^= HashValue
>> 8;
333 HashValue
^= HashValue
>> 4;
334 HashValue
&= NB_HASHMASK
;
336 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
338 for (NCE
= NeighborCache
[HashValue
].Cache
;
342 if (AddrIsEqual(Address
, &NCE
->Address
))
349 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
352 PNEIGHBOR_CACHE_ENTRY
NBLocateNeighbor(
355 * FUNCTION: Locates a neighbor in the neighbor cache
357 * Address = Pointer to IP address
359 * Pointer to NCE, NULL if not found
361 * If the NCE is found, it is referenced. The caller is
362 * responsible for dereferencing it again after use
365 PNEIGHBOR_CACHE_ENTRY NCE
;
369 TI_DbgPrint(DEBUG_NCACHE
, ("Called. Address (0x%X).\n", Address
));
371 HashValue
= *(PULONG
)&Address
->Address
;
372 HashValue
^= HashValue
>> 16;
373 HashValue
^= HashValue
>> 8;
374 HashValue
^= HashValue
>> 4;
375 HashValue
&= NB_HASHMASK
;
377 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
379 NCE
= NeighborCache
[HashValue
].Cache
;
381 while ((NCE
) && (!AddrIsEqual(Address
, &NCE
->Address
)))
386 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
388 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
393 PNEIGHBOR_CACHE_ENTRY
NBFindOrCreateNeighbor(
394 PIP_INTERFACE Interface
,
397 * FUNCTION: Tries to find a neighbor and if unsuccesful, creates a new NCE
399 * Interface = Pointer to interface to use (in case NCE is not found)
400 * Address = Pointer to IP address
402 * Pointer to NCE, NULL if there is not enough free resources
404 * The NCE is referenced if found or created. The caller is
405 * responsible for dereferencing it again after use
408 PNEIGHBOR_CACHE_ENTRY NCE
;
410 TI_DbgPrint(DEBUG_NCACHE
, ("Called. Interface (0x%X) Address (0x%X).\n", Interface
, Address
));
412 NCE
= NBLocateNeighbor(Address
);
415 TI_DbgPrint(MID_TRACE
,("BCAST: %s\n", A2S(&Interface
->Broadcast
)));
416 if( AddrIsEqual(Address
, &Interface
->Broadcast
) ||
417 AddrIsUnspecified(Address
) ) {
418 TI_DbgPrint(MID_TRACE
,("Packet targeted at broadcast addr\n"));
419 NCE
= NBAddNeighbor(Interface
, Address
, NULL
,
420 Interface
->AddressLength
, NUD_PERMANENT
, 0);
422 NCE
= NBAddNeighbor(Interface
, Address
, NULL
,
423 Interface
->AddressLength
, NUD_INCOMPLETE
, ARP_TIMEOUT
);
424 if (!NCE
) return NULL
;
432 BOOLEAN
NBQueuePacket(
433 PNEIGHBOR_CACHE_ENTRY NCE
,
434 PNDIS_PACKET NdisPacket
,
435 PNEIGHBOR_PACKET_COMPLETE PacketComplete
,
438 * FUNCTION: Queues a packet on an NCE for later transmission
440 * NCE = Pointer to NCE to queue packet on
441 * NdisPacket = Pointer to NDIS packet to queue
443 * TRUE if the packet was successfully queued, FALSE if not
447 PNEIGHBOR_PACKET Packet
;
452 ("Called. NCE (0x%X) NdisPacket (0x%X).\n", NCE
, NdisPacket
));
454 Packet
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(NEIGHBOR_PACKET
),
455 NEIGHBOR_PACKET_TAG
);
456 if( !Packet
) return FALSE
;
458 /* FIXME: Should we limit the number of queued packets? */
460 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
461 HashValue
^= HashValue
>> 16;
462 HashValue
^= HashValue
>> 8;
463 HashValue
^= HashValue
>> 4;
464 HashValue
&= NB_HASHMASK
;
466 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
468 Packet
->Complete
= PacketComplete
;
469 Packet
->Context
= PacketContext
;
470 Packet
->Packet
= NdisPacket
;
471 InsertTailList( &NCE
->PacketQueue
, &Packet
->Next
);
473 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
475 if( !(NCE
->State
& NUD_INCOMPLETE
) )
476 NBSendPackets( NCE
);
481 VOID
NBRemoveNeighbor(
482 PNEIGHBOR_CACHE_ENTRY NCE
)
484 * FUNCTION: Removes a neighbor from the neighbor cache
486 * NCE = Pointer to NCE to remove from cache
488 * The NCE must be in a safe state
491 PNEIGHBOR_CACHE_ENTRY
*PrevNCE
;
492 PNEIGHBOR_CACHE_ENTRY CurNCE
;
496 TI_DbgPrint(DEBUG_NCACHE
, ("Called. NCE (0x%X).\n", NCE
));
498 HashValue
= *(PULONG
)(&NCE
->Address
.Address
);
499 HashValue
^= HashValue
>> 16;
500 HashValue
^= HashValue
>> 8;
501 HashValue
^= HashValue
>> 4;
502 HashValue
&= NB_HASHMASK
;
504 TcpipAcquireSpinLock(&NeighborCache
[HashValue
].Lock
, &OldIrql
);
506 /* Search the list and remove the NCE from the list if found */
507 for (PrevNCE
= &NeighborCache
[HashValue
].Cache
;
508 (CurNCE
= *PrevNCE
) != NULL
;
509 PrevNCE
= &CurNCE
->Next
)
513 /* Found it, now unlink it from the list */
514 *PrevNCE
= CurNCE
->Next
;
516 NBFlushPacketQueue( CurNCE
, NDIS_STATUS_REQUEST_ABORTED
);
517 ExFreePoolWithTag(CurNCE
, NCE_TAG
);
523 TcpipReleaseSpinLock(&NeighborCache
[HashValue
].Lock
, OldIrql
);
526 ULONG NBCopyNeighbors
527 (PIP_INTERFACE Interface
,
528 PIPARP_ENTRY ArpTable
)
530 PNEIGHBOR_CACHE_ENTRY CurNCE
;
534 for (i
= 0; i
<= NB_HASHMASK
; i
++) {
535 TcpipAcquireSpinLock(&NeighborCache
[i
].Lock
, &OldIrql
);
536 for( CurNCE
= NeighborCache
[i
].Cache
;
538 CurNCE
= CurNCE
->Next
) {
539 if( CurNCE
->Interface
== Interface
&&
540 !AddrIsEqual( &CurNCE
->Address
, &CurNCE
->Interface
->Unicast
) ) {
542 ArpTable
[Size
].Index
= Interface
->Index
;
543 ArpTable
[Size
].AddrSize
= CurNCE
->LinkAddressLength
;
545 (ArpTable
[Size
].PhysAddr
,
547 CurNCE
->LinkAddressLength
);
548 ArpTable
[Size
].LogAddr
= CurNCE
->Address
.Address
.IPv4Address
;
549 if( CurNCE
->State
& NUD_PERMANENT
)
550 ArpTable
[Size
].Type
= ARP_ENTRY_STATIC
;
551 else if( CurNCE
->State
& NUD_INCOMPLETE
)
552 ArpTable
[Size
].Type
= ARP_ENTRY_INVALID
;
554 ArpTable
[Size
].Type
= ARP_ENTRY_DYNAMIC
;
559 TcpipReleaseSpinLock(&NeighborCache
[i
].Lock
, OldIrql
);