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
IPTimeoutDpcFn(PKDPC Dpc
,
67 PVOID DeferredContext
,
68 PVOID SystemArgument1
,
69 PVOID SystemArgument2
)
71 * FUNCTION: Timeout DPC
73 * Dpc = Pointer to our DPC object
74 * DeferredContext = Pointer to context information (unused)
75 * SystemArgument1 = Unused
76 * SystemArgument2 = Unused
78 * This routine is dispatched once in a while to do maintainance jobs
81 /* Check if datagram fragments have taken too long to assemble */
82 IPDatagramReassemblyTimeout();
84 /* Clean possible outdated cached neighbor addresses */
89 VOID
IPDispatchProtocol(
90 PIP_INTERFACE Interface
,
93 * FUNCTION: IP protocol dispatcher
95 * NTE = Pointer to net table entry which the packet was received on
96 * IPPacket = Pointer to an IP packet that was received
98 * This routine examines the IP header and passes the packet on to the
99 * right upper level protocol receive handler
103 IP_ADDRESS SrcAddress
;
105 switch (IPPacket
->Type
) {
107 Protocol
= ((PIPv4_HEADER
)(IPPacket
->Header
))->Protocol
;
108 AddrInitIPv4(&SrcAddress
, ((PIPv4_HEADER
)(IPPacket
->Header
))->SrcAddr
);
111 /* FIXME: IPv6 adresses not supported */
112 TI_DbgPrint(MIN_TRACE
, ("IPv6 datagram discarded.\n"));
115 TI_DbgPrint(MIN_TRACE
, ("Unrecognized datagram discarded.\n"));
119 NBResetNeighborTimeout(&SrcAddress
);
121 if (Protocol
< IP_PROTOCOL_TABLE_SIZE
)
123 /* Call the appropriate protocol handler */
124 (*ProtocolTable
[Protocol
])(Interface
, IPPacket
);
129 PIP_INTERFACE
IPCreateInterface(
130 PLLIP_BIND_INFO BindInfo
)
132 * FUNCTION: Creates an IP interface
134 * BindInfo = Pointer to link layer to IP binding information
136 * Pointer to IP_INTERFACE structure, NULL if there was
137 * not enough free resources
142 TI_DbgPrint(DEBUG_IP
, ("Called. BindInfo (0x%X).\n", BindInfo
));
145 if (BindInfo
->Address
) {
146 PUCHAR A
= BindInfo
->Address
;
147 TI_DbgPrint(DEBUG_IP
, ("Interface address (%02X %02X %02X %02X %02X %02X).\n",
148 A
[0], A
[1], A
[2], A
[3], A
[4], A
[5]));
152 IF
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(IP_INTERFACE
),
155 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
159 RtlZeroMemory(IF
, sizeof(IP_INTERFACE
));
162 IF
->Context
= BindInfo
->Context
;
163 IF
->HeaderSize
= BindInfo
->HeaderSize
;
164 IF
->MinFrameSize
= BindInfo
->MinFrameSize
;
165 IF
->MTU
= BindInfo
->MTU
;
166 IF
->Address
= BindInfo
->Address
;
167 IF
->AddressLength
= BindInfo
->AddressLength
;
168 IF
->Transmit
= BindInfo
->Transmit
;
170 IF
->Unicast
.Type
= IP_ADDRESS_V4
;
171 IF
->PointToPoint
.Type
= IP_ADDRESS_V4
;
172 IF
->Netmask
.Type
= IP_ADDRESS_V4
;
173 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
175 TcpipInitializeSpinLock(&IF
->Lock
);
177 IF
->TCPContext
= ExAllocatePoolWithTag
178 ( NonPagedPool
, sizeof(OSK_IFADDR
) + 3 * sizeof( struct sockaddr_in
),
179 OSKITTCP_CONTEXT_TAG
);
180 if (!IF
->TCPContext
) {
181 ExFreePoolWithTag(IF
, IP_INTERFACE_TAG
);
186 InsertTDIInterfaceEntity( IF
);
193 VOID
IPDestroyInterface(
196 * FUNCTION: Destroys an IP interface
198 * IF = Pointer to interface to destroy
201 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
204 RemoveTDIInterfaceEntity( IF
);
207 ExFreePoolWithTag(IF
->TCPContext
, OSKITTCP_CONTEXT_TAG
);
208 ExFreePoolWithTag(IF
, IP_INTERFACE_TAG
);
211 VOID
IPAddInterfaceRoute( PIP_INTERFACE IF
) {
212 PNEIGHBOR_CACHE_ENTRY NCE
;
213 IP_ADDRESS NetworkAddress
;
215 /* Add a permanent neighbor for this NTE */
216 NCE
= NBAddNeighbor(IF
, &IF
->Unicast
,
217 IF
->Address
, IF
->AddressLength
,
220 TI_DbgPrint(MIN_TRACE
, ("Could not create NCE.\n"));
224 AddrWidenAddress( &NetworkAddress
, &IF
->Unicast
, &IF
->Netmask
);
226 if (!RouterAddRoute(&NetworkAddress
, &IF
->Netmask
, NCE
, 1)) {
227 TI_DbgPrint(MIN_TRACE
, ("Could not add route due to insufficient resources.\n"));
230 /* Send a gratuitous ARP packet to update the route caches of
233 ARPTransmit(NULL
, NULL
, IF
);
236 BOOLEAN
IPRegisterInterface(
239 * FUNCTION: Registers an IP interface with IP layer
241 * IF = Pointer to interface to register
243 * TRUE if interface was successfully registered, FALSE if not
247 UINT ChosenIndex
= 0;
248 BOOLEAN IndexHasBeenChosen
;
249 IF_LIST_ITER(Interface
);
251 TI_DbgPrint(MID_TRACE
, ("Called. IF (0x%X).\n", IF
));
253 TcpipAcquireSpinLock(&IF
->Lock
, &OldIrql
);
255 /* Choose an index */
257 IndexHasBeenChosen
= TRUE
;
258 ForEachInterface(Interface
) {
259 if( Interface
->Index
== ChosenIndex
) {
261 IndexHasBeenChosen
= FALSE
;
264 } while( !IndexHasBeenChosen
);
266 IF
->Index
= ChosenIndex
;
268 /* Add interface to the global interface list */
269 TcpipInterlockedInsertTailList(&InterfaceListHead
,
273 TcpipReleaseSpinLock(&IF
->Lock
, OldIrql
);
278 VOID
IPRemoveInterfaceRoute( PIP_INTERFACE IF
) {
279 PNEIGHBOR_CACHE_ENTRY NCE
;
280 IP_ADDRESS GeneralRoute
;
282 NCE
= NBLocateNeighbor(&IF
->Unicast
);
285 TI_DbgPrint(DEBUG_IP
,("Removing interface Addr %s\n", A2S(&IF
->Unicast
)));
286 TI_DbgPrint(DEBUG_IP
,(" Mask %s\n", A2S(&IF
->Netmask
)));
288 AddrWidenAddress(&GeneralRoute
,&IF
->Unicast
,&IF
->Netmask
);
290 RouterRemoveRoute(&GeneralRoute
, &IF
->Unicast
);
292 NBRemoveNeighbor(NCE
);
296 VOID
IPUnregisterInterface(
299 * FUNCTION: Unregisters an IP interface with IP layer
301 * IF = Pointer to interface to unregister
306 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
308 IPRemoveInterfaceRoute( IF
);
310 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql3
);
311 RemoveEntryList(&IF
->ListEntry
);
312 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql3
);
316 VOID
DefaultProtocolHandler(
317 PIP_INTERFACE Interface
,
320 * FUNCTION: Default handler for Internet protocols
322 * NTE = Pointer to net table entry which the packet was received on
323 * IPPacket = Pointer to an IP packet that was received
326 TI_DbgPrint(MID_TRACE
, ("[IF %x] Packet of unknown Internet protocol "
327 "discarded.\n", Interface
));
329 Interface
->Stats
.InDiscardedUnknownProto
++;
333 VOID
IPRegisterProtocol(
335 IP_PROTOCOL_HANDLER Handler
)
337 * FUNCTION: Registers a handler for an IP protocol number
339 * ProtocolNumber = Internet Protocol number for which to register handler
340 * Handler = Pointer to handler to be called when a packet is received
342 * To unregister a protocol handler, call this function with Handler = NULL
345 if (ProtocolNumber
>= IP_PROTOCOL_TABLE_SIZE
) {
346 TI_DbgPrint(MIN_TRACE
, ("Protocol number is out of range (%d).\n", ProtocolNumber
));
350 ProtocolTable
[ProtocolNumber
] = Handler
? Handler
: DefaultProtocolHandler
;
354 NTSTATUS
IPStartup(PUNICODE_STRING RegistryPath
)
356 * FUNCTION: Initializes the IP subsystem
358 * RegistryPath = Our registry node for configuration parameters
360 * Status of operation
365 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
367 /* Initialize lookaside lists */
368 ExInitializeNPagedLookasideList(
369 &IPDRList
, /* Lookaside list */
370 NULL
, /* Allocate routine */
371 NULL
, /* Free routine */
373 sizeof(IPDATAGRAM_REASSEMBLY
), /* Size of each entry */
374 DATAGRAM_REASSEMBLY_TAG
, /* Tag */
377 ExInitializeNPagedLookasideList(
378 &IPFragmentList
, /* Lookaside list */
379 NULL
, /* Allocate routine */
380 NULL
, /* Free routine */
382 sizeof(IP_FRAGMENT
), /* Size of each entry */
383 DATAGRAM_FRAGMENT_TAG
, /* Tag */
386 ExInitializeNPagedLookasideList(
387 &IPHoleList
, /* Lookaside list */
388 NULL
, /* Allocate routine */
389 NULL
, /* Free routine */
391 sizeof(IPDATAGRAM_HOLE
), /* Size of each entry */
392 DATAGRAM_HOLE_TAG
, /* Tag */
395 /* Start routing subsystem */
398 /* Start neighbor cache subsystem */
401 /* Fill the protocol dispatch table with pointers
402 to the default protocol handler */
403 for (i
= 0; i
< IP_PROTOCOL_TABLE_SIZE
; i
++)
404 IPRegisterProtocol(i
, DefaultProtocolHandler
);
406 /* Initialize NTE list and protecting lock */
407 InitializeListHead(&NetTableListHead
);
408 TcpipInitializeSpinLock(&NetTableListLock
);
410 /* Initialize reassembly list and protecting lock */
411 InitializeListHead(&ReassemblyListHead
);
412 TcpipInitializeSpinLock(&ReassemblyListLock
);
414 IPInitialized
= TRUE
;
416 return STATUS_SUCCESS
;
423 * FUNCTION: Shuts down the IP subsystem
425 * Status of operation
428 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
431 return STATUS_SUCCESS
;
433 /* Shutdown neighbor cache subsystem */
436 /* Shutdown routing subsystem */
439 IPFreeReassemblyList();
441 /* Destroy lookaside lists */
442 ExDeleteNPagedLookasideList(&IPHoleList
);
443 ExDeleteNPagedLookasideList(&IPDRList
);
444 ExDeleteNPagedLookasideList(&IPFragmentList
);
446 IPInitialized
= FALSE
;
448 return STATUS_SUCCESS
;