328337cdb381e4876d09813b2eddb5716ad4747c
[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 KeAcquireSpinLock(&AddrFile->Lock, &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 KeReleaseSpinLock(&AddrFile->Lock, OldIrql);
216 return STATUS_UNSUCCESSFUL;
217 }
218
219 TI_DbgPrint(MID_TRACE,("About to get route to destination\n"));
220
221 if(!(NCE = RouteGetRouteToDestination( &RemoteAddress )))
222 {
223 KeReleaseSpinLock(&AddrFile->Lock, OldIrql);
224 return STATUS_NETWORK_UNREACHABLE;
225 }
226
227 LocalAddress = AddrFile->Address;
228 if (AddrIsUnspecified(&LocalAddress))
229 {
230 /* If the local address is unspecified (0),
231 * then use the unicast address of the
232 * interface we're sending over
233 */
234 LocalAddress = NCE->Interface->Unicast;
235 }
236
237 Status = BuildRawIpPacket( AddrFile,
238 &Packet,
239 &RemoteAddress,
240 RemotePort,
241 &LocalAddress,
242 AddrFile->Port,
243 BufferData,
244 DataSize );
245
246 if( !NT_SUCCESS(Status) )
247 {
248 KeReleaseSpinLock(&AddrFile->Lock, OldIrql);
249 return Status;
250 }
251
252 TI_DbgPrint(MID_TRACE,("About to send datagram\n"));
253
254 if (!NT_SUCCESS(Status = IPSendDatagram( &Packet, NCE, RawIpSendPacketComplete, NULL )))
255 {
256 KeReleaseSpinLock(&AddrFile->Lock, OldIrql);
257 FreeNdisPacket(Packet.NdisPacket);
258 return Status;
259 }
260
261 TI_DbgPrint(MID_TRACE,("Leaving\n"));
262
263 KeReleaseSpinLock(&AddrFile->Lock, OldIrql);
264
265 return STATUS_SUCCESS;
266 }
267
268
269 VOID RawIpReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
270 /*
271 * FUNCTION: Receives and queues a RawIp datagram
272 * ARGUMENTS:
273 * NTE = Pointer to net table entry which the packet was received on
274 * IPPacket = Pointer to an IP packet that was received
275 * NOTES:
276 * This is the low level interface for receiving RawIp datagrams. It strips
277 * the RawIp header from a packet and delivers the data to anyone that wants it
278 */
279 {
280 AF_SEARCH SearchContext;
281 PIPv4_HEADER IPv4Header;
282 PADDRESS_FILE AddrFile;
283 PIP_ADDRESS DstAddress, SrcAddress;
284 UINT DataSize;
285
286 TI_DbgPrint(MAX_TRACE, ("Called.\n"));
287
288 switch (IPPacket->Type) {
289 /* IPv4 packet */
290 case IP_ADDRESS_V4:
291 IPv4Header = IPPacket->Header;
292 DstAddress = &IPPacket->DstAddr;
293 SrcAddress = &IPPacket->SrcAddr;
294 DataSize = IPPacket->TotalSize;
295 break;
296
297 /* IPv6 packet */
298 case IP_ADDRESS_V6:
299 TI_DbgPrint(MIN_TRACE, ("Discarded IPv6 datagram (%i bytes).\n", IPPacket->TotalSize));
300
301 /* FIXME: IPv6 is not supported */
302 return;
303
304 default:
305 return;
306 }
307
308 /* Locate a receive request on destination address file object
309 and deliver the packet if one is found. If there is no receive
310 request on the address file object, call the associated receive
311 handler. If no receive handler is registered, drop the packet */
312
313 AddrFile = AddrSearchFirst(DstAddress,
314 0,
315 IPv4Header->Protocol,
316 &SearchContext);
317 if (AddrFile) {
318 do {
319 DGDeliverData(AddrFile,
320 SrcAddress,
321 DstAddress,
322 0,
323 0,
324 IPPacket,
325 DataSize);
326 } while ((AddrFile = AddrSearchNext(&SearchContext)) != NULL);
327 } else {
328 /* There are no open address files that will take this datagram */
329 /* FIXME: IPv4 only */
330 TI_DbgPrint(MID_TRACE, ("Cannot deliver IPv4 raw datagram to address (0x%X).\n",
331 DN2H(DstAddress->Address.IPv4Address)));
332
333 /* FIXME: Send ICMP reply */
334 }
335 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
336 }
337
338
339 NTSTATUS RawIPStartup(VOID)
340 /*
341 * FUNCTION: Initializes the UDP subsystem
342 * RETURNS:
343 * Status of operation
344 */
345 {
346 #ifdef __NTDRIVER__
347 RtlZeroMemory(&UDPStats, sizeof(UDP_STATISTICS));
348 #endif
349
350 /* Register this protocol with IP layer */
351 IPRegisterProtocol(IPPROTO_RAW, RawIpReceive);
352
353 return STATUS_SUCCESS;
354 }
355
356
357 NTSTATUS RawIPShutdown(VOID)
358 /*
359 * FUNCTION: Shuts down the UDP subsystem
360 * RETURNS:
361 * Status of operation
362 */
363 {
364 /* Deregister this protocol with IP layer */
365 IPRegisterProtocol(IPPROTO_RAW, NULL);
366
367 return STATUS_SUCCESS;
368 }
369
370 /* EOF */