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
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 INIT_TAG(IPPacket
, 'TKPI');
61 IPPacket
->Free
= DontFreePacket
;
62 IPPacket
->Type
= Type
;
68 void NTAPI
IPTimeout( PVOID Context
) {
69 IpWorkItemQueued
= FALSE
;
71 /* Check if datagram fragments have taken too long to assemble */
72 IPDatagramReassemblyTimeout();
74 /* Clean possible outdated cached neighbor addresses */
79 VOID
IPDispatchProtocol(
80 PIP_INTERFACE Interface
,
83 * FUNCTION: IP protocol dispatcher
85 * NTE = Pointer to net table entry which the packet was received on
86 * IPPacket = Pointer to an IP packet that was received
88 * This routine examines the IP header and passes the packet on to the
89 * right upper level protocol receive handler
93 IP_ADDRESS SrcAddress
;
95 switch (IPPacket
->Type
) {
97 Protocol
= ((PIPv4_HEADER
)(IPPacket
->Header
))->Protocol
;
98 AddrInitIPv4(&SrcAddress
, ((PIPv4_HEADER
)(IPPacket
->Header
))->SrcAddr
);
101 /* FIXME: IPv6 adresses not supported */
102 TI_DbgPrint(MIN_TRACE
, ("IPv6 datagram discarded.\n"));
105 TI_DbgPrint(MIN_TRACE
, ("Unrecognized datagram discarded.\n"));
109 NBResetNeighborTimeout(&SrcAddress
);
111 if (Protocol
< IP_PROTOCOL_TABLE_SIZE
)
113 /* Call the appropriate protocol handler */
114 (*ProtocolTable
[Protocol
])(Interface
, IPPacket
);
119 PIP_INTERFACE
IPCreateInterface(
120 PLLIP_BIND_INFO BindInfo
)
122 * FUNCTION: Creates an IP interface
124 * BindInfo = Pointer to link layer to IP binding information
126 * Pointer to IP_INTERFACE structure, NULL if there was
127 * not enough free resources
132 TI_DbgPrint(DEBUG_IP
, ("Called. BindInfo (0x%X).\n", BindInfo
));
135 if (BindInfo
->Address
) {
136 PUCHAR A
= BindInfo
->Address
;
137 TI_DbgPrint(DEBUG_IP
, ("Interface address (%02X %02X %02X %02X %02X %02X).\n",
138 A
[0], A
[1], A
[2], A
[3], A
[4], A
[5]));
142 IF
= exAllocatePool(NonPagedPool
, sizeof(IP_INTERFACE
));
144 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
148 INIT_TAG(IF
, 'ECAF');
150 RtlZeroMemory(IF
, sizeof(IP_INTERFACE
));
153 IF
->Context
= BindInfo
->Context
;
154 IF
->HeaderSize
= BindInfo
->HeaderSize
;
155 IF
->MinFrameSize
= BindInfo
->MinFrameSize
;
156 IF
->MTU
= BindInfo
->MTU
;
157 IF
->Address
= BindInfo
->Address
;
158 IF
->AddressLength
= BindInfo
->AddressLength
;
159 IF
->Transmit
= BindInfo
->Transmit
;
161 IF
->Unicast
.Type
= IP_ADDRESS_V4
;
162 IF
->PointToPoint
.Type
= IP_ADDRESS_V4
;
163 IF
->Netmask
.Type
= IP_ADDRESS_V4
;
164 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
166 TcpipInitializeSpinLock(&IF
->Lock
);
168 IF
->TCPContext
= exAllocatePool
169 ( NonPagedPool
, sizeof(OSK_IFADDR
) + 2 * sizeof( struct sockaddr_in
) );
170 if (!IF
->TCPContext
) {
176 InsertTDIInterfaceEntity( IF
);
183 VOID
IPDestroyInterface(
186 * FUNCTION: Destroys an IP interface
188 * IF = Pointer to interface to destroy
191 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
194 RemoveTDIInterfaceEntity( IF
);
197 exFreePool(IF
->TCPContext
);
201 VOID
IPAddInterfaceRoute( PIP_INTERFACE IF
) {
202 PNEIGHBOR_CACHE_ENTRY NCE
;
203 IP_ADDRESS NetworkAddress
;
205 /* Add a permanent neighbor for this NTE */
206 NCE
= NBAddNeighbor(IF
, &IF
->Unicast
,
207 IF
->Address
, IF
->AddressLength
,
210 TI_DbgPrint(MIN_TRACE
, ("Could not create NCE.\n"));
214 AddrWidenAddress( &NetworkAddress
, &IF
->Unicast
, &IF
->Netmask
);
216 if (!RouterAddRoute(&NetworkAddress
, &IF
->Netmask
, NCE
, 1)) {
217 TI_DbgPrint(MIN_TRACE
, ("Could not add route due to insufficient resources.\n"));
220 /* Send a gratuitous ARP packet to update the route caches of
223 ARPTransmit(NULL
, NULL
, IF
);
226 BOOLEAN
IPRegisterInterface(
229 * FUNCTION: Registers an IP interface with IP layer
231 * IF = Pointer to interface to register
233 * TRUE if interface was successfully registered, FALSE if not
237 UINT ChosenIndex
= 0;
238 BOOLEAN IndexHasBeenChosen
;
239 IF_LIST_ITER(Interface
);
241 TI_DbgPrint(MID_TRACE
, ("Called. IF (0x%X).\n", IF
));
243 TcpipAcquireSpinLock(&IF
->Lock
, &OldIrql
);
245 /* Choose an index */
247 IndexHasBeenChosen
= TRUE
;
248 ForEachInterface(Interface
) {
249 if( Interface
->Index
== ChosenIndex
) {
251 IndexHasBeenChosen
= FALSE
;
254 } while( !IndexHasBeenChosen
);
256 IF
->Index
= ChosenIndex
;
258 /* Add interface to the global interface list */
259 TcpipInterlockedInsertTailList(&InterfaceListHead
,
263 TcpipReleaseSpinLock(&IF
->Lock
, OldIrql
);
268 VOID
IPRemoveInterfaceRoute( PIP_INTERFACE IF
) {
269 PNEIGHBOR_CACHE_ENTRY NCE
;
270 IP_ADDRESS GeneralRoute
;
272 NCE
= NBLocateNeighbor(&IF
->Unicast
);
275 TI_DbgPrint(DEBUG_IP
,("Removing interface Addr %s\n", A2S(&IF
->Unicast
)));
276 TI_DbgPrint(DEBUG_IP
,(" Mask %s\n", A2S(&IF
->Netmask
)));
278 AddrWidenAddress(&GeneralRoute
,&IF
->Unicast
,&IF
->Netmask
);
280 RouterRemoveRoute(&GeneralRoute
, &IF
->Unicast
);
282 NBRemoveNeighbor(NCE
);
286 VOID
IPUnregisterInterface(
289 * FUNCTION: Unregisters an IP interface with IP layer
291 * IF = Pointer to interface to unregister
296 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
298 IPRemoveInterfaceRoute( IF
);
300 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql3
);
301 RemoveEntryList(&IF
->ListEntry
);
302 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql3
);
306 VOID
DefaultProtocolHandler(
307 PIP_INTERFACE Interface
,
310 * FUNCTION: Default handler for Internet protocols
312 * NTE = Pointer to net table entry which the packet was received on
313 * IPPacket = Pointer to an IP packet that was received
316 TI_DbgPrint(MID_TRACE
, ("[IF %x] Packet of unknown Internet protocol "
317 "discarded.\n", Interface
));
319 Interface
->Stats
.InDiscardedUnknownProto
++;
323 VOID
IPRegisterProtocol(
325 IP_PROTOCOL_HANDLER Handler
)
327 * FUNCTION: Registers a handler for an IP protocol number
329 * ProtocolNumber = Internet Protocol number for which to register handler
330 * Handler = Pointer to handler to be called when a packet is received
332 * To unregister a protocol handler, call this function with Handler = NULL
335 if (ProtocolNumber
>= IP_PROTOCOL_TABLE_SIZE
) {
336 TI_DbgPrint(MIN_TRACE
, ("Protocol number is out of range (%d).\n", ProtocolNumber
));
340 ProtocolTable
[ProtocolNumber
] = Handler
? Handler
: DefaultProtocolHandler
;
344 NTSTATUS
IPStartup(PUNICODE_STRING RegistryPath
)
346 * FUNCTION: Initializes the IP subsystem
348 * RegistryPath = Our registry node for configuration parameters
350 * Status of operation
355 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
357 /* Initialize lookaside lists */
358 ExInitializeNPagedLookasideList(
359 &IPDRList
, /* Lookaside list */
360 NULL
, /* Allocate routine */
361 NULL
, /* Free routine */
363 sizeof(IPDATAGRAM_REASSEMBLY
), /* Size of each entry */
367 ExInitializeNPagedLookasideList(
368 &IPFragmentList
, /* Lookaside list */
369 NULL
, /* Allocate routine */
370 NULL
, /* Free routine */
372 sizeof(IP_FRAGMENT
), /* Size of each entry */
376 ExInitializeNPagedLookasideList(
377 &IPHoleList
, /* Lookaside list */
378 NULL
, /* Allocate routine */
379 NULL
, /* Free routine */
381 sizeof(IPDATAGRAM_HOLE
), /* Size of each entry */
385 /* Start routing subsystem */
388 /* Start neighbor cache subsystem */
391 /* Fill the protocol dispatch table with pointers
392 to the default protocol handler */
393 for (i
= 0; i
< IP_PROTOCOL_TABLE_SIZE
; i
++)
394 IPRegisterProtocol(i
, DefaultProtocolHandler
);
396 /* Initialize NTE list and protecting lock */
397 InitializeListHead(&NetTableListHead
);
398 TcpipInitializeSpinLock(&NetTableListLock
);
400 /* Initialize reassembly list and protecting lock */
401 InitializeListHead(&ReassemblyListHead
);
402 TcpipInitializeSpinLock(&ReassemblyListLock
);
404 IPInitialized
= TRUE
;
406 return STATUS_SUCCESS
;
413 * FUNCTION: Shuts down the IP subsystem
415 * Status of operation
418 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
421 return STATUS_SUCCESS
;
423 /* Shutdown neighbor cache subsystem */
426 /* Shutdown routing subsystem */
429 IPFreeReassemblyList();
431 /* Destroy lookaside lists */
432 ExDeleteNPagedLookasideList(&IPHoleList
);
433 ExDeleteNPagedLookasideList(&IPDRList
);
434 ExDeleteNPagedLookasideList(&IPFragmentList
);
436 IPInitialized
= FALSE
;
438 return STATUS_SUCCESS
;