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 */
77 /* Call upper layer timeout routines */
82 VOID
IPDispatchProtocol(
83 PIP_INTERFACE Interface
,
86 * FUNCTION: IP protocol dispatcher
88 * NTE = Pointer to net table entry which the packet was received on
89 * IPPacket = Pointer to an IP packet that was received
91 * This routine examines the IP header and passes the packet on to the
92 * right upper level protocol receive handler
96 IP_ADDRESS SrcAddress
;
98 switch (IPPacket
->Type
) {
100 Protocol
= ((PIPv4_HEADER
)(IPPacket
->Header
))->Protocol
;
101 AddrInitIPv4(&SrcAddress
, ((PIPv4_HEADER
)(IPPacket
->Header
))->SrcAddr
);
104 /* FIXME: IPv6 adresses not supported */
105 TI_DbgPrint(MIN_TRACE
, ("IPv6 datagram discarded.\n"));
108 TI_DbgPrint(MIN_TRACE
, ("Unrecognized datagram discarded.\n"));
112 NBResetNeighborTimeout(&SrcAddress
);
114 if (Protocol
< IP_PROTOCOL_TABLE_SIZE
)
116 /* Call the appropriate protocol handler */
117 (*ProtocolTable
[Protocol
])(Interface
, IPPacket
);
122 PIP_INTERFACE
IPCreateInterface(
123 PLLIP_BIND_INFO BindInfo
)
125 * FUNCTION: Creates an IP interface
127 * BindInfo = Pointer to link layer to IP binding information
129 * Pointer to IP_INTERFACE structure, NULL if there was
130 * not enough free resources
135 TI_DbgPrint(DEBUG_IP
, ("Called. BindInfo (0x%X).\n", BindInfo
));
138 if (BindInfo
->Address
) {
139 PUCHAR A
= BindInfo
->Address
;
140 TI_DbgPrint(DEBUG_IP
, ("Interface address (%02X %02X %02X %02X %02X %02X).\n",
141 A
[0], A
[1], A
[2], A
[3], A
[4], A
[5]));
145 IF
= exAllocatePool(NonPagedPool
, sizeof(IP_INTERFACE
));
147 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
151 INIT_TAG(IF
, 'ECAF');
153 RtlZeroMemory(IF
, sizeof(IP_INTERFACE
));
156 IF
->Context
= BindInfo
->Context
;
157 IF
->HeaderSize
= BindInfo
->HeaderSize
;
158 IF
->MinFrameSize
= BindInfo
->MinFrameSize
;
159 IF
->MTU
= BindInfo
->MTU
;
160 IF
->Address
= BindInfo
->Address
;
161 IF
->AddressLength
= BindInfo
->AddressLength
;
162 IF
->Transmit
= BindInfo
->Transmit
;
164 IF
->Unicast
.Type
= IP_ADDRESS_V4
;
165 IF
->PointToPoint
.Type
= IP_ADDRESS_V4
;
166 IF
->Netmask
.Type
= IP_ADDRESS_V4
;
167 IF
->Broadcast
.Type
= IP_ADDRESS_V4
;
169 TcpipInitializeSpinLock(&IF
->Lock
);
171 IF
->TCPContext
= exAllocatePool
172 ( NonPagedPool
, sizeof(OSK_IFADDR
) + 2 * sizeof( struct sockaddr_in
) );
173 if (!IF
->TCPContext
) {
179 InsertTDIInterfaceEntity( IF
);
186 VOID
IPDestroyInterface(
189 * FUNCTION: Destroys an IP interface
191 * IF = Pointer to interface to destroy
194 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
197 RemoveTDIInterfaceEntity( IF
);
200 exFreePool(IF
->TCPContext
);
204 VOID
IPAddInterfaceRoute( PIP_INTERFACE IF
) {
205 PNEIGHBOR_CACHE_ENTRY NCE
;
206 IP_ADDRESS NetworkAddress
;
208 /* Add a permanent neighbor for this NTE */
209 NCE
= NBAddNeighbor(IF
, &IF
->Unicast
,
210 IF
->Address
, IF
->AddressLength
,
213 TI_DbgPrint(MIN_TRACE
, ("Could not create NCE.\n"));
217 AddrWidenAddress( &NetworkAddress
, &IF
->Unicast
, &IF
->Netmask
);
219 if (!RouterAddRoute(&NetworkAddress
, &IF
->Netmask
, NCE
, 1)) {
220 TI_DbgPrint(MIN_TRACE
, ("Could not add route due to insufficient resources.\n"));
223 /* Send a gratuitous ARP packet to update the route caches of
226 ARPTransmit(NULL
, NULL
, IF
);
229 BOOLEAN
IPRegisterInterface(
232 * FUNCTION: Registers an IP interface with IP layer
234 * IF = Pointer to interface to register
236 * TRUE if interface was successfully registered, FALSE if not
240 UINT ChosenIndex
= 0;
241 BOOLEAN IndexHasBeenChosen
;
242 IF_LIST_ITER(Interface
);
244 TI_DbgPrint(MID_TRACE
, ("Called. IF (0x%X).\n", IF
));
246 TcpipAcquireSpinLock(&IF
->Lock
, &OldIrql
);
248 /* Choose an index */
250 IndexHasBeenChosen
= TRUE
;
251 ForEachInterface(Interface
) {
252 if( Interface
->Index
== ChosenIndex
) {
254 IndexHasBeenChosen
= FALSE
;
257 } while( !IndexHasBeenChosen
);
259 IF
->Index
= ChosenIndex
;
261 /* Add interface to the global interface list */
262 TcpipInterlockedInsertTailList(&InterfaceListHead
,
266 TcpipReleaseSpinLock(&IF
->Lock
, OldIrql
);
271 VOID
IPRemoveInterfaceRoute( PIP_INTERFACE IF
) {
272 PNEIGHBOR_CACHE_ENTRY NCE
;
273 IP_ADDRESS GeneralRoute
;
275 NCE
= NBLocateNeighbor(&IF
->Unicast
);
278 TI_DbgPrint(DEBUG_IP
,("Removing interface Addr %s\n", A2S(&IF
->Unicast
)));
279 TI_DbgPrint(DEBUG_IP
,(" Mask %s\n", A2S(&IF
->Netmask
)));
281 AddrWidenAddress(&GeneralRoute
,&IF
->Unicast
,&IF
->Netmask
);
283 RouterRemoveRoute(&GeneralRoute
, &IF
->Unicast
);
285 NBRemoveNeighbor(NCE
);
289 VOID
IPUnregisterInterface(
292 * FUNCTION: Unregisters an IP interface with IP layer
294 * IF = Pointer to interface to unregister
299 TI_DbgPrint(DEBUG_IP
, ("Called. IF (0x%X).\n", IF
));
301 IPRemoveInterfaceRoute( IF
);
303 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql3
);
304 RemoveEntryList(&IF
->ListEntry
);
305 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql3
);
309 VOID
DefaultProtocolHandler(
310 PIP_INTERFACE Interface
,
313 * FUNCTION: Default handler for Internet protocols
315 * NTE = Pointer to net table entry which the packet was received on
316 * IPPacket = Pointer to an IP packet that was received
319 TI_DbgPrint(MID_TRACE
, ("[IF %x] Packet of unknown Internet protocol "
320 "discarded.\n", Interface
));
322 Interface
->Stats
.InDiscardedUnknownProto
++;
326 VOID
IPRegisterProtocol(
328 IP_PROTOCOL_HANDLER Handler
)
330 * FUNCTION: Registers a handler for an IP protocol number
332 * ProtocolNumber = Internet Protocol number for which to register handler
333 * Handler = Pointer to handler to be called when a packet is received
335 * To unregister a protocol handler, call this function with Handler = NULL
338 if (ProtocolNumber
>= IP_PROTOCOL_TABLE_SIZE
) {
339 TI_DbgPrint(MIN_TRACE
, ("Protocol number is out of range (%d).\n", ProtocolNumber
));
343 ProtocolTable
[ProtocolNumber
] = Handler
? Handler
: DefaultProtocolHandler
;
347 NTSTATUS
IPStartup(PUNICODE_STRING RegistryPath
)
349 * FUNCTION: Initializes the IP subsystem
351 * RegistryPath = Our registry node for configuration parameters
353 * Status of operation
358 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
360 /* Initialize lookaside lists */
361 ExInitializeNPagedLookasideList(
362 &IPDRList
, /* Lookaside list */
363 NULL
, /* Allocate routine */
364 NULL
, /* Free routine */
366 sizeof(IPDATAGRAM_REASSEMBLY
), /* Size of each entry */
370 ExInitializeNPagedLookasideList(
371 &IPFragmentList
, /* Lookaside list */
372 NULL
, /* Allocate routine */
373 NULL
, /* Free routine */
375 sizeof(IP_FRAGMENT
), /* Size of each entry */
379 ExInitializeNPagedLookasideList(
380 &IPHoleList
, /* Lookaside list */
381 NULL
, /* Allocate routine */
382 NULL
, /* Free routine */
384 sizeof(IPDATAGRAM_HOLE
), /* Size of each entry */
388 /* Start routing subsystem */
391 /* Start neighbor cache subsystem */
394 /* Fill the protocol dispatch table with pointers
395 to the default protocol handler */
396 for (i
= 0; i
< IP_PROTOCOL_TABLE_SIZE
; i
++)
397 IPRegisterProtocol(i
, DefaultProtocolHandler
);
399 /* Initialize NTE list and protecting lock */
400 InitializeListHead(&NetTableListHead
);
401 TcpipInitializeSpinLock(&NetTableListLock
);
403 /* Initialize reassembly list and protecting lock */
404 InitializeListHead(&ReassemblyListHead
);
405 TcpipInitializeSpinLock(&ReassemblyListLock
);
407 IPInitialized
= TRUE
;
409 return STATUS_SUCCESS
;
416 * FUNCTION: Shuts down the IP subsystem
418 * Status of operation
421 TI_DbgPrint(MAX_TRACE
, ("Called.\n"));
424 return STATUS_SUCCESS
;
426 /* Shutdown neighbor cache subsystem */
429 /* Shutdown routing subsystem */
432 IPFreeReassemblyList();
434 /* Destroy lookaside lists */
435 ExDeleteNPagedLookasideList(&IPHoleList
);
436 ExDeleteNPagedLookasideList(&IPDRList
);
437 ExDeleteNPagedLookasideList(&IPFragmentList
);
439 IPInitialized
= FALSE
;
441 return STATUS_SUCCESS
;