2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
5 * PURPOSE: Internet Protocol module
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/08-2000 Created
14 LIST_ENTRY InterfaceListHead
;
15 KSPIN_LOCK InterfaceListLock
;
16 LIST_ENTRY NetTableListHead
;
17 KSPIN_LOCK NetTableListLock
;
18 BOOLEAN IPInitialized
= FALSE
;
19 BOOLEAN IpWorkItemQueued
= FALSE
;
20 /* Work around calling timer at Dpc level */
22 IP_PROTOCOL_HANDLER ProtocolTable
[IP_PROTOCOL_TABLE_SIZE
];
27 * FUNCTION: Do nothing for when the IPPacket struct is part of another
29 * Object = Pointer to an IP packet structure
37 * FUNCTION: Frees an interface object
39 * Object = Pointer to an interface structure
42 ExFreePoolWithTag(Object
, IP_INTERFACE_TAG
);
45 PIP_PACKET
IPInitializePacket(
49 * FUNCTION: Creates an IP packet object
51 * Type = Type of IP packet
53 * Pointer to the created IP packet. NULL if there was not enough free resources.
56 /* FIXME: Is this needed? */
57 RtlZeroMemory(IPPacket
, sizeof(IP_PACKET
));
59 IPPacket
->Free
= DontFreePacket
;
60 IPPacket
->Type
= Type
;
66 void NTAPI
IPTimeout( PVOID Context
) {
67 IpWorkItemQueued
= FALSE
;
69 /* Check if datagram fragments have taken too long to assemble */
70 IPDatagramReassemblyTimeout();
72 /* Clean possible outdated cached neighbor addresses */
77 VOID
IPDispatchProtocol(
78 PIP_INTERFACE Interface
,
81 * FUNCTION: IP protocol dispatcher
83 * NTE = Pointer to net table entry which the packet was received on
84 * IPPacket = Pointer to an IP packet that was received
86 * This routine examines the IP header and passes the packet on to the
87 * right upper level protocol receive handler
91 IP_ADDRESS SrcAddress
;
93 switch (IPPacket
->Type
) {
95 Protocol
= ((PIPv4_HEADER
)(IPPacket
->Header
))->Protocol
;
96 AddrInitIPv4(&SrcAddress
, ((PIPv4_HEADER
)(IPPacket
->Header
))->SrcAddr
);
99 /* FIXME: IPv6 adresses not supported */
100 TI_DbgPrint(MIN_TRACE
, ("IPv6 datagram discarded.\n"));
103 TI_DbgPrint(MIN_TRACE
, ("Unrecognized datagram discarded.\n"));
107 NBResetNeighborTimeout(&SrcAddress
);
109 if (Protocol
< IP_PROTOCOL_TABLE_SIZE
)
111 /* Call the appropriate protocol handler */
112 (*ProtocolTable
[Protocol
])(Interface
, IPPacket
);
117 PIP_INTERFACE
IPCreateInterface(
118 PLLIP_BIND_INFO BindInfo
)
120 * FUNCTION: Creates an IP interface
122 * BindInfo = Pointer to link layer to IP binding information
124 * Pointer to IP_INTERFACE structure, NULL if there was
125 * not enough free resources
130 TI_DbgPrint(DEBUG_IP
, ("Called. BindInfo (0x%X).\n", BindInfo
));
133 if (BindInfo
->Address
) {
134 PUCHAR A
= BindInfo
->Address
;
135 TI_DbgPrint(DEBUG_IP
, ("Interface address (%02X %02X %02X %02X %02X %02X).\n",
136 A
[0], A
[1], A
[2], A
[3], A
[4], A
[5]));
140 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(IP_INTERFACE
),
143 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
147 RtlZeroMemory(IF
, sizeof(IP_INTERFACE
));
150 IF
->Context
= BindInfo
->Context
;
151 IF
->HeaderSize
= BindInfo
->HeaderSize
;
152 IF
->MinFrameSize
= BindInfo
->MinFrameSize
;
153 IF
->MTU
= BindInfo
->MTU
;
154 IF
->Address
= BindInfo
->Address
;
155 IF
->AddressLength
= BindInfo
->AddressLength
;
156 IF
->Transmit
= BindInfo
->Transmit
;
158 IF
->Unicast
.Type
= IP_ADDRESS_V4
;
159 IF
->PointToPoint
.Type
= IP_ADDRESS_V4
;
160 IF
->Netmask
.Type
= IP_ADDRESS_V4
;
161 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
163 TcpipInitializeSpinLock(&IF
->Lock
);
165 IF
->TCPContext
= ExAllocatePoolWithTag
166 ( NonPagedPool
, sizeof(OSK_IFADDR
) + 2 * sizeof( struct sockaddr_in
),
167 OSKITTCP_CONTEXT_TAG
);
168 if (!IF
->TCPContext
) {
169 ExFreePoolWithTag(IF
, IP_INTERFACE_TAG
);
174 InsertTDIInterfaceEntity( IF
);
181 VOID
IPDestroyInterface(
184 * FUNCTION: Destroys an IP interface
186 * IF = Pointer to interface to destroy
189 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
192 RemoveTDIInterfaceEntity( IF
);
195 ExFreePoolWithTag(IF
->TCPContext
, OSKITTCP_CONTEXT_TAG
);
196 ExFreePoolWithTag(IF
, IP_INTERFACE_TAG
);
199 VOID
IPAddInterfaceRoute( PIP_INTERFACE IF
) {
200 PNEIGHBOR_CACHE_ENTRY NCE
;
201 IP_ADDRESS NetworkAddress
;
203 /* Add a permanent neighbor for this NTE */
204 NCE
= NBAddNeighbor(IF
, &IF
->Unicast
,
205 IF
->Address
, IF
->AddressLength
,
208 TI_DbgPrint(MIN_TRACE
, ("Could not create NCE.\n"));
212 AddrWidenAddress( &NetworkAddress
, &IF
->Unicast
, &IF
->Netmask
);
214 if (!RouterAddRoute(&NetworkAddress
, &IF
->Netmask
, NCE
, 1)) {
215 TI_DbgPrint(MIN_TRACE
, ("Could not add route due to insufficient resources.\n"));
218 /* Send a gratuitous ARP packet to update the route caches of
221 ARPTransmit(NULL
, NULL
, IF
);
224 BOOLEAN
IPRegisterInterface(
227 * FUNCTION: Registers an IP interface with IP layer
229 * IF = Pointer to interface to register
231 * TRUE if interface was successfully registered, FALSE if not
235 UINT ChosenIndex
= 0;
236 BOOLEAN IndexHasBeenChosen
;
237 IF_LIST_ITER(Interface
);
239 TI_DbgPrint(MID_TRACE
, ("Called. IF (0x%X).\n", IF
));
241 TcpipAcquireSpinLock(&IF
->Lock
, &OldIrql
);
243 /* Choose an index */
245 IndexHasBeenChosen
= TRUE
;
246 ForEachInterface(Interface
) {
247 if( Interface
->Index
== ChosenIndex
) {
249 IndexHasBeenChosen
= FALSE
;
252 } while( !IndexHasBeenChosen
);
254 IF
->Index
= ChosenIndex
;
256 /* Add interface to the global interface list */
257 TcpipInterlockedInsertTailList(&InterfaceListHead
,
261 TcpipReleaseSpinLock(&IF
->Lock
, OldIrql
);
266 VOID
IPRemoveInterfaceRoute( PIP_INTERFACE IF
) {
267 PNEIGHBOR_CACHE_ENTRY NCE
;
268 IP_ADDRESS GeneralRoute
;
270 NCE
= NBLocateNeighbor(&IF
->Unicast
);
273 TI_DbgPrint(DEBUG_IP
,("Removing interface Addr %s\n", A2S(&IF
->Unicast
)));
274 TI_DbgPrint(DEBUG_IP
,(" Mask %s\n", A2S(&IF
->Netmask
)));
276 AddrWidenAddress(&GeneralRoute
,&IF
->Unicast
,&IF
->Netmask
);
278 RouterRemoveRoute(&GeneralRoute
, &IF
->Unicast
);
280 NBRemoveNeighbor(NCE
);
284 VOID
IPUnregisterInterface(
287 * FUNCTION: Unregisters an IP interface with IP layer
289 * IF = Pointer to interface to unregister
294 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
296 IPRemoveInterfaceRoute( IF
);
298 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql3
);
299 RemoveEntryList(&IF
->ListEntry
);
300 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql3
);
304 VOID
DefaultProtocolHandler(
305 PIP_INTERFACE Interface
,
308 * FUNCTION: Default handler for Internet protocols
310 * NTE = Pointer to net table entry which the packet was received on
311 * IPPacket = Pointer to an IP packet that was received
314 TI_DbgPrint(MID_TRACE
, ("[IF %x] Packet of unknown Internet protocol "
315 "discarded.\n", Interface
));
317 Interface
->Stats
.InDiscardedUnknownProto
++;
321 VOID
IPRegisterProtocol(
323 IP_PROTOCOL_HANDLER Handler
)
325 * FUNCTION: Registers a handler for an IP protocol number
327 * ProtocolNumber = Internet Protocol number for which to register handler
328 * Handler = Pointer to handler to be called when a packet is received
330 * To unregister a protocol handler, call this function with Handler = NULL
333 if (ProtocolNumber
>= IP_PROTOCOL_TABLE_SIZE
) {
334 TI_DbgPrint(MIN_TRACE
, ("Protocol number is out of range (%d).\n", ProtocolNumber
));
338 ProtocolTable
[ProtocolNumber
] = Handler
? Handler
: DefaultProtocolHandler
;
342 NTSTATUS
IPStartup(PUNICODE_STRING RegistryPath
)
344 * FUNCTION: Initializes the IP subsystem
346 * RegistryPath = Our registry node for configuration parameters
348 * Status of operation
353 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
355 /* Initialize lookaside lists */
356 ExInitializeNPagedLookasideList(
357 &IPDRList
, /* Lookaside list */
358 NULL
, /* Allocate routine */
359 NULL
, /* Free routine */
361 sizeof(IPDATAGRAM_REASSEMBLY
), /* Size of each entry */
362 DATAGRAM_REASSEMBLY_TAG
, /* Tag */
365 ExInitializeNPagedLookasideList(
366 &IPFragmentList
, /* Lookaside list */
367 NULL
, /* Allocate routine */
368 NULL
, /* Free routine */
370 sizeof(IP_FRAGMENT
), /* Size of each entry */
371 DATAGRAM_FRAGMENT_TAG
, /* Tag */
374 ExInitializeNPagedLookasideList(
375 &IPHoleList
, /* Lookaside list */
376 NULL
, /* Allocate routine */
377 NULL
, /* Free routine */
379 sizeof(IPDATAGRAM_HOLE
), /* Size of each entry */
380 DATAGRAM_HOLE_TAG
, /* Tag */
383 /* Start routing subsystem */
386 /* Start neighbor cache subsystem */
389 /* Fill the protocol dispatch table with pointers
390 to the default protocol handler */
391 for (i
= 0; i
< IP_PROTOCOL_TABLE_SIZE
; i
++)
392 IPRegisterProtocol(i
, DefaultProtocolHandler
);
394 /* Initialize NTE list and protecting lock */
395 InitializeListHead(&NetTableListHead
);
396 TcpipInitializeSpinLock(&NetTableListLock
);
398 /* Initialize reassembly list and protecting lock */
399 InitializeListHead(&ReassemblyListHead
);
400 TcpipInitializeSpinLock(&ReassemblyListLock
);
402 IPInitialized
= TRUE
;
404 return STATUS_SUCCESS
;
411 * FUNCTION: Shuts down the IP subsystem
413 * Status of operation
416 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
419 return STATUS_SUCCESS
;
421 /* Shutdown neighbor cache subsystem */
424 /* Shutdown routing subsystem */
427 IPFreeReassemblyList();
429 /* Destroy lookaside lists */
430 ExDeleteNPagedLookasideList(&IPHoleList
);
431 ExDeleteNPagedLookasideList(&IPDRList
);
432 ExDeleteNPagedLookasideList(&IPFragmentList
);
434 IPInitialized
= FALSE
;
436 return STATUS_SUCCESS
;