3 * - IPv4 Protcol Handling
9 #include "hwaddr_cache.h"
13 #define DEFAULT_TTL 32
14 #define IPV4_TRACE 1 // set to 1 to enable packet tracing
17 extern tInterface *gIP_Interfaces;
18 extern void ICMP_Initialise();
19 extern int ICMP_Ping(tInterface *Interface, tIPv4 Addr);
22 int IPv4_Initialise();
23 int IPv4_RegisterCallback(int ID, tIPCallback Callback);
24 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
25 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
26 Uint32 IPv4_Netmask(int FixedBits);
27 Uint16 IPv4_Checksum(const void *Buf, size_t Length);
28 int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
31 tIPCallback gaIPv4_Callbacks[256];
35 * \brief Initialise the IPv4 Code
40 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
45 * \brief Registers a callback
46 * \param ID 8-bit packet type ID
47 * \param Callback Callback function
49 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
51 if( ID < 0 || ID > 255 ) return 0;
52 if( gaIPv4_Callbacks[ID] ) return 0;
53 gaIPv4_Callbacks[ID] = Callback;
58 * \brief Creates and sends an IPv4 Packet
59 * \param Iface Interface
60 * \param Address Destination IP
61 * \param Protocol Protocol ID
62 * \param ID Some random ID number
63 * \param Length Data Length
64 * \param Data Packet Data
65 * \return Boolean Success
67 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, tIPStackBuffer *Buffer)
73 length = IPStack_Buffer_GetLength(Buffer);
75 // --- Resolve destination MAC address
76 to = HWCache_Resolve(Iface, &Address);
77 if( MAC_EQU(to, cMAC_ZERO) ) {
79 Log_Notice("IPv4", "No route to host %i.%i.%i.%i",
80 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
84 // --- Handle OUTPUT firewall rules
85 // TODO: Update firewall rules for tIPStackBuffer
87 int ret = IPTables_TestChain("OUTPUT",
88 4, (tIPv4*)Iface->Address, &Address,
92 // Just drop it (with an error)
93 Log_Notice("IPv4", "Firewall dropped packet");
98 // --- Initialise header
100 hdr.HeaderLength = sizeof(tIPv4Header)/4;
101 hdr.DiffServices = 0; // TODO: Check
104 hdr.DontFragment = 0;
105 hdr.MoreFragments = 0;
109 hdr.TotalLength = htons( sizeof(tIPv4Header) + length );
110 hdr.Identifcation = htons( ID ); // TODO: Check
111 hdr.TTL = DEFAULT_TTL;
112 hdr.Protocol = Protocol;
113 hdr.HeaderChecksum = 0; // Will be set later
114 hdr.Source = *(tIPv4*)Iface->Address;
115 hdr.Destination = Address;
117 // Actually set checksum (zeroed above)
118 hdr.HeaderChecksum = htons( IPv4_Checksum(&hdr, sizeof(tIPv4Header)) );
120 IPStack_Buffer_AppendSubBuffer(Buffer, sizeof(tIPv4Header), 0, &hdr, NULL, NULL);
123 Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
124 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
126 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, Buffer);
131 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
132 * \brief Process an IPv4 Packet
134 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
136 tIPv4Header *hdr = Buffer;
142 if(Length < sizeof(tIPv4Header)) return;
145 //Log_Log("IPv4", "Version = %i", hdr->Version);
146 //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
147 //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
148 Log_Debug("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
149 //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
150 //Log_Log("IPv4", "TTL = %i", hdr->TTL );
151 Log_Debug("IPv4", "Protocol = %i", hdr->Protocol );
152 //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
153 Log_Debug("IPv4", "Source = %i.%i.%i.%i",
154 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
155 Log_Debug("IPv4", "Destination = %i.%i.%i.%i",
156 hdr->Destination.B[0], hdr->Destination.B[1],
157 hdr->Destination.B[2], hdr->Destination.B[3] );
160 // Check that the version IS IPv4
161 if(hdr->Version != 4) {
162 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
166 // Check Header checksum
168 Uint16 hdrVal, compVal;
169 hdrVal = ntohs(hdr->HeaderChecksum);
170 hdr->HeaderChecksum = 0;
171 compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4);
172 if(hdrVal != compVal) {
173 Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
176 hdr->HeaderChecksum = hdrVal;
179 // Check Packet length
180 if( ntohs(hdr->TotalLength) > Length) {
181 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
185 // TODO: Handle packet fragmentation
188 Log_Debug("IPv4", "Proto 0x%x From %i.%i.%i.%i to %i.%i.%i.%i",
190 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3],
191 hdr->Destination.B[0], hdr->Destination.B[1], hdr->Destination.B[2], hdr->Destination.B[3]
195 // Get Data and Data Length
196 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
197 data = &hdr->Options[0];
199 // Get Interface (allowing broadcasts)
200 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
205 ret = IPTables_TestChain("INPUT",
206 4, &hdr->Source, &hdr->Destination,
213 // Drop the packet if the TTL is zero
214 if( hdr->TTL == 0 ) {
215 Log_Warning("IPv4", "TODO: Send ICMP-Timeout when TTL exceeded");
220 ret = IPTables_TestChain("FORWARD",
221 4, &hdr->Source, &hdr->Destination,
232 Log_Debug("IPv4", "Silently dropping packet");
237 // Unknown, silent drop
239 Log_Warning("IPv4", "Unknown firewall rule");
246 //IPStack_RoutePacket(4, &hdr->Destination, Length, Buffer);
250 // Populate ARP cache from recieved packets
252 if( IPStack_CompareAddress(4, &hdr->Source, iface->Address, iface->SubnetBits) )
254 HWCache_Set(Adapter, 4, &hdr->Source, &From);
258 if( !gaIPv4_Callbacks[hdr->Protocol] ) {
259 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
263 gaIPv4_Callbacks[hdr->Protocol]( iface, &hdr->Source, dataLength, data );
267 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
268 * \brief Searches an adapter for a matching address
269 * \param Adapter Incoming Adapter
270 * \param Address Destination Address
271 * \param Broadcast Allow broadcast packets
273 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
275 tInterface *iface = NULL, *zero_iface = NULL;
279 ENTER("pAdapter xAddress bBroadcast", Adapter, Address, Broadcast);
281 addr = ntohl( Address.L );
282 LOG("addr = 0x%x", addr);
284 for( iface = gIP_Interfaces; iface; iface = iface->Next)
286 if( iface->Adapter != Adapter ) continue;
287 if( iface->Type != 4 ) continue;
288 if( IP4_EQU(Address, *(tIPv4*)iface->Address) ) {
294 LOG("iface->Address = 0x%x", *(Uint32*)iface->Address);
296 if( *(Uint32*)iface->Address == 0 ) {
298 Log_Notice("IPv4", "Multiple 0.0.0.0 interfaces on the same adapter, ignoring");
302 LOG("Zero IF %p", iface);
307 if( !Broadcast ) continue;
309 // Check for broadcast
310 this = ntohl( ((tIPv4*)iface->Address)->L );
311 netmask = IPv4_Netmask(iface->SubnetBits);
312 LOG("iface addr = 0x%x, netmask = 0x%x (bits = %i)", this, netmask, iface->SubnetBits);
314 if( (addr & netmask) == (this & netmask) && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
316 LOG("Broadcast match");
322 // Special case for intefaces that are being DHCP configured
323 // - If the interface address is 0.0.0.0, then if there is no match for the
324 // destination the packet is treated as if it was addressed to 0.0.0.0
325 if( zero_iface && Broadcast )
327 LOG("Using 0.0.0.0 interface with magic!");
328 LEAVE('p', zero_iface);
337 * \brief Convert a network prefix to a netmask
338 * \param FixedBits Netmask size (/n)
340 * For example /24 will become 255.255.255.0 (0xFFFFFF00)
342 Uint32 IPv4_Netmask(int FixedBits)
344 Uint32 ret = 0xFFFFFFFF;
349 ret >>= (32-FixedBits);
350 ret <<= (32-FixedBits);
352 // Returns a native endian netmask
357 * \brief Calculate the IPv4 Checksum
358 * \param Buf Input buffer
359 * \param Size Size of input
361 * One's complement sum of all 16-bit words (bitwise inverted)
363 Uint16 IPv4_Checksum(const void *Buf, size_t Length)
365 //Debug_HexDump("IPv4_Checksum", Buf, Length);
366 const Uint16 *words = Buf;
370 // Sum all whole words
371 for(i = 0; i < Length/2; i++ )
373 sum += ntohs(words[i]);
376 sum += ntohs( words[i] & 0xFF );
378 // Apply one's complement
380 sum = (sum & 0xFFFF) + (sum >> 16);
386 * \brief Sends an ICMP Echo and waits for a reply
387 * \param IFace Interface
388 * \param Addr Destination address
390 int IPv4_Ping(tInterface *IFace, tIPv4 Addr)
392 return ICMP_Ping(IFace, Addr);