[TCPIP, IP]
[reactos.git] / lib / drivers / ip / transport / rawip / rawip.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: transport/rawip/rawip.c
5 * PURPOSE: User Datagram Protocol routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 NTSTATUS AddGenericHeaderIPv4(
14 PADDRESS_FILE AddrFile,
15 PIP_ADDRESS RemoteAddress,
16 USHORT RemotePort,
17 PIP_ADDRESS LocalAddress,
18 USHORT LocalPort,
19 PIP_PACKET IPPacket,
20 UINT DataLength,
21 UINT Protocol,
22 UINT ExtraLength,
23 PVOID *NextHeader )
24 /*
25 * FUNCTION: Adds an IPv4 and RawIp header to an IP packet
26 * ARGUMENTS:
27 * SendRequest = Pointer to send request
28 * LocalAddress = Pointer to our local address
29 * LocalPort = The port we send this datagram from
30 * IPPacket = Pointer to IP packet
31 * RETURNS:
32 * Status of operation
33 */
34 {
35 PIPv4_HEADER IPHeader;
36 ULONG BufferSize;
37
38 TI_DbgPrint(MID_TRACE, ("Packet: %x NdisPacket %x\n",
39 IPPacket, IPPacket->NdisPacket));
40
41 BufferSize = sizeof(IPv4_HEADER) + ExtraLength;
42
43 GetDataPtr( IPPacket->NdisPacket,
44 0,
45 (PCHAR *)&IPPacket->Header,
46 &IPPacket->ContigSize );
47
48 IPPacket->HeaderSize = 20;
49
50 TI_DbgPrint(MAX_TRACE, ("Allocated %d bytes for headers at 0x%X.\n",
51 BufferSize, IPPacket->Header));
52 TI_DbgPrint(MAX_TRACE, ("Packet total length %d\n", IPPacket->TotalSize));
53
54 /* Build IPv4 header */
55 IPHeader = (PIPv4_HEADER)IPPacket->Header;
56 /* Version = 4, Length = 5 DWORDs */
57 IPHeader->VerIHL = 0x45;
58 /* Normal Type-of-Service */
59 IPHeader->Tos = 0;
60 /* Length of header and data */
61 IPHeader->TotalLength = WH2N((USHORT)IPPacket->TotalSize);
62 /* Identification */
63 IPHeader->Id = (USHORT)Random();
64 /* One fragment at offset 0 */
65 IPHeader->FlagsFragOfs = 0;
66 /* Time-to-Live */
67 IPHeader->Ttl = AddrFile->TTL;
68 /* Protocol */
69 IPHeader->Protocol = Protocol;
70 /* Checksum is 0 (for later calculation of this) */
71 IPHeader->Checksum = 0;
72 /* Source address */
73 IPHeader->SrcAddr = LocalAddress->Address.IPv4Address;
74 /* Destination address. FIXME: IPv4 only */
75 IPHeader->DstAddr = RemoteAddress->Address.IPv4Address;
76
77 /* Build RawIp header */
78 *NextHeader = (((PCHAR)IPHeader) + sizeof(IPv4_HEADER));
79 IPPacket->Data = ((PCHAR)*NextHeader) + ExtraLength;
80
81 return STATUS_SUCCESS;
82 }
83
84
85 NTSTATUS BuildRawIpPacket(
86 PADDRESS_FILE AddrFile,
87 PIP_PACKET Packet,
88 PIP_ADDRESS RemoteAddress,
89 USHORT RemotePort,
90 PIP_ADDRESS LocalAddress,
91 USHORT LocalPort,
92 PCHAR DataBuffer,
93 UINT DataLen )
94 /*
95 * FUNCTION: Builds an RawIp packet
96 * ARGUMENTS:
97 * Context = Pointer to context information (DATAGRAM_SEND_REQUEST)
98 * LocalAddress = Pointer to our local address
99 * LocalPort = The port we send this datagram from
100 * IPPacket = Address of pointer to IP packet
101 * RETURNS:
102 * Status of operation
103 */
104 {
105 NTSTATUS Status;
106 PCHAR Payload;
107
108 TI_DbgPrint(MAX_TRACE, ("Called.\n"));
109
110 /* FIXME: Assumes IPv4 */
111 IPInitializePacket(Packet, IP_ADDRESS_V4);
112
113 Packet->TotalSize = sizeof(IPv4_HEADER) + DataLen;
114
115 /* Prepare packet */
116 Status = AllocatePacketWithBuffer( &Packet->NdisPacket,
117 NULL,
118 Packet->TotalSize );
119
120 if( !NT_SUCCESS(Status) ) return Status;
121
122 TI_DbgPrint(MID_TRACE, ("Allocated packet: %x\n", Packet->NdisPacket));
123 TI_DbgPrint(MID_TRACE, ("Local Addr : %s\n", A2S(LocalAddress)));
124 TI_DbgPrint(MID_TRACE, ("Remote Addr: %s\n", A2S(RemoteAddress)));
125
126 switch (RemoteAddress->Type) {
127 case IP_ADDRESS_V4:
128 Status = AddGenericHeaderIPv4
129 (AddrFile, RemoteAddress, RemotePort,
130 LocalAddress, LocalPort, Packet, DataLen,
131 AddrFile->Protocol,
132 0, (PVOID *)&Payload );
133 break;
134 case IP_ADDRESS_V6:
135 /* FIXME: Support IPv6 */
136 Status = STATUS_UNSUCCESSFUL;
137 TI_DbgPrint(MIN_TRACE, ("IPv6 RawIp datagrams are not supported.\n"));
138 break;
139
140 default:
141 Status = STATUS_UNSUCCESSFUL;
142 TI_DbgPrint(MIN_TRACE, ("Bad Address Type %d\n", RemoteAddress->Type));
143 break;
144 }
145
146 if( !NT_SUCCESS(Status) ) {
147 TI_DbgPrint(MIN_TRACE, ("Cannot add header. Status = (0x%X)\n",
148 Status));
149 FreeNdisPacket(Packet->NdisPacket);
150 return Status;
151 }
152
153 TI_DbgPrint(MID_TRACE, ("Copying data (hdr %x data %x (%d))\n",
154 Packet->Header, Packet->Data,
155 (PCHAR)Packet->Data - (PCHAR)Packet->Header));
156
157 RtlCopyMemory( Packet->Data, DataBuffer, DataLen );
158
159 Packet->Flags |= IP_PACKET_FLAG_RAW;
160
161 TI_DbgPrint(MID_TRACE, ("Displaying packet\n"));
162
163 DISPLAY_IP_PACKET(Packet);
164
165 TI_DbgPrint(MID_TRACE, ("Leaving\n"));
166
167 return STATUS_SUCCESS;
168 }
169
170 VOID RawIpSendPacketComplete
171 ( PVOID Context, PNDIS_PACKET Packet, NDIS_STATUS Status ) {
172 FreeNdisPacket( Packet );
173 }
174
175 NTSTATUS RawIPSendDatagram(
176 PADDRESS_FILE AddrFile,
177 PTDI_CONNECTION_INFORMATION ConnInfo,
178 PCHAR BufferData,
179 ULONG DataSize,
180 PULONG DataUsed )
181 /*
182 * FUNCTION: Sends an RawIp datagram to a remote address
183 * ARGUMENTS:
184 * Request = Pointer to TDI request
185 * ConnInfo = Pointer to connection information
186 * Buffer = Pointer to NDIS buffer with data
187 * DataSize = Size in bytes of data to be sent
188 * RETURNS:
189 * Status of operation
190 */
191 {
192 IP_PACKET Packet;
193 PTA_IP_ADDRESS RemoteAddressTa = (PTA_IP_ADDRESS)ConnInfo->RemoteAddress;
194 IP_ADDRESS RemoteAddress, LocalAddress;
195 USHORT RemotePort;
196 NTSTATUS Status;
197 PNEIGHBOR_CACHE_ENTRY NCE;
198 KIRQL OldIrql;
199
200 LockObject(AddrFile, &OldIrql);
201
202 TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n",
203 AddrFile, ConnInfo, BufferData, DataSize));
204 TI_DbgPrint(MID_TRACE,("RemoteAddressTa: %x\n", RemoteAddressTa));
205
206 switch( RemoteAddressTa->Address[0].AddressType ) {
207 case TDI_ADDRESS_TYPE_IP:
208 RemoteAddress.Type = IP_ADDRESS_V4;
209 RemoteAddress.Address.IPv4Address =
210 RemoteAddressTa->Address[0].Address[0].in_addr;
211 RemotePort = RemoteAddressTa->Address[0].Address[0].sin_port;
212 break;
213
214 default:
215 UnlockObject(AddrFile, OldIrql);
216 return STATUS_UNSUCCESSFUL;
217 }
218
219 TI_DbgPrint(MID_TRACE,("About to get route to destination\n"));
220
221 LocalAddress = AddrFile->Address;
222 if (AddrIsUnspecified(&LocalAddress))
223 {
224 /* If the local address is unspecified (0),
225 * then use the unicast address of the
226 * interface we're sending over
227 */
228 if(!(NCE = RouteGetRouteToDestination( &RemoteAddress ))) {
229 UnlockObject(AddrFile, OldIrql);
230 return STATUS_NETWORK_UNREACHABLE;
231 }
232
233 LocalAddress = NCE->Interface->Unicast;
234 }
235 else
236 {
237 if(!(NCE = NBLocateNeighbor( &LocalAddress ))) {
238 UnlockObject(AddrFile, OldIrql);
239 return STATUS_INVALID_PARAMETER;
240 }
241 }
242
243 Status = BuildRawIpPacket( AddrFile,
244 &Packet,
245 &RemoteAddress,
246 RemotePort,
247 &LocalAddress,
248 AddrFile->Port,
249 BufferData,
250 DataSize );
251
252 if( !NT_SUCCESS(Status) )
253 {
254 UnlockObject(AddrFile, OldIrql);
255 return Status;
256 }
257
258 TI_DbgPrint(MID_TRACE,("About to send datagram\n"));
259
260 if (!NT_SUCCESS(Status = IPSendDatagram( &Packet, NCE, RawIpSendPacketComplete, NULL )))
261 {
262 UnlockObject(AddrFile, OldIrql);
263 FreeNdisPacket(Packet.NdisPacket);
264 return Status;
265 }
266
267 TI_DbgPrint(MID_TRACE,("Leaving\n"));
268
269 UnlockObject(AddrFile, OldIrql);
270
271 return STATUS_SUCCESS;
272 }
273
274
275 VOID RawIpReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
276 /*
277 * FUNCTION: Receives and queues a RawIp datagram
278 * ARGUMENTS:
279 * NTE = Pointer to net table entry which the packet was received on
280 * IPPacket = Pointer to an IP packet that was received
281 * NOTES:
282 * This is the low level interface for receiving RawIp datagrams. It strips
283 * the RawIp header from a packet and delivers the data to anyone that wants it
284 */
285 {
286 AF_SEARCH SearchContext;
287 PIPv4_HEADER IPv4Header;
288 PADDRESS_FILE AddrFile;
289 PIP_ADDRESS DstAddress, SrcAddress;
290 UINT DataSize;
291
292 TI_DbgPrint(MAX_TRACE, ("Called.\n"));
293
294 switch (IPPacket->Type) {
295 /* IPv4 packet */
296 case IP_ADDRESS_V4:
297 IPv4Header = IPPacket->Header;
298 DstAddress = &IPPacket->DstAddr;
299 SrcAddress = &IPPacket->SrcAddr;
300 DataSize = IPPacket->TotalSize;
301 break;
302
303 /* IPv6 packet */
304 case IP_ADDRESS_V6:
305 TI_DbgPrint(MIN_TRACE, ("Discarded IPv6 datagram (%i bytes).\n", IPPacket->TotalSize));
306
307 /* FIXME: IPv6 is not supported */
308 return;
309
310 default:
311 return;
312 }
313
314 /* Locate a receive request on destination address file object
315 and deliver the packet if one is found. If there is no receive
316 request on the address file object, call the associated receive
317 handler. If no receive handler is registered, drop the packet */
318
319 AddrFile = AddrSearchFirst(DstAddress,
320 0,
321 IPv4Header->Protocol,
322 &SearchContext);
323 if (AddrFile) {
324 do {
325 DGDeliverData(AddrFile,
326 SrcAddress,
327 DstAddress,
328 0,
329 0,
330 IPPacket,
331 DataSize);
332 } while ((AddrFile = AddrSearchNext(&SearchContext)) != NULL);
333 } else {
334 /* There are no open address files that will take this datagram */
335 /* FIXME: IPv4 only */
336 TI_DbgPrint(MID_TRACE, ("Cannot deliver IPv4 raw datagram to address (0x%X).\n",
337 DN2H(DstAddress->Address.IPv4Address)));
338
339 /* FIXME: Send ICMP reply */
340 }
341 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
342 }
343
344
345 NTSTATUS RawIPStartup(VOID)
346 /*
347 * FUNCTION: Initializes the UDP subsystem
348 * RETURNS:
349 * Status of operation
350 */
351 {
352 #ifdef __NTDRIVER__
353 RtlZeroMemory(&UDPStats, sizeof(UDP_STATISTICS));
354 #endif
355
356 /* Register this protocol with IP layer */
357 IPRegisterProtocol(IPPROTO_RAW, RawIpReceive);
358
359 return STATUS_SUCCESS;
360 }
361
362
363 NTSTATUS RawIPShutdown(VOID)
364 /*
365 * FUNCTION: Shuts down the UDP subsystem
366 * RETURNS:
367 * Status of operation
368 */
369 {
370 /* Deregister this protocol with IP layer */
371 IPRegisterProtocol(IPPROTO_RAW, NULL);
372
373 return STATUS_SUCCESS;
374 }
375
376 /* EOF */