3 * - IPv4 Protcol Handling
11 #define DEFAULT_TTL 32
14 extern tInterface *gIP_Interfaces;
15 extern void ICMP_Initialise();
16 extern int ICMP_Ping(tInterface *Interface, tIPv4 Addr);
17 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
20 int IPv4_Initialise();
21 int IPv4_RegisterCallback(int ID, tIPCallback Callback);
22 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
23 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
24 Uint32 IPv4_Netmask(int FixedBits);
25 Uint16 IPv4_Checksum(const void *Buf, size_t Length);
26 int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
29 tIPCallback gaIPv4_Callbacks[256];
33 * \brief Initialise the IPv4 Code
38 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
43 * \brief Registers a callback
44 * \param ID 8-bit packet type ID
45 * \param Callback Callback function
47 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
49 if( ID < 0 || ID > 255 ) return 0;
50 if( gaIPv4_Callbacks[ID] ) return 0;
51 gaIPv4_Callbacks[ID] = Callback;
56 * \brief Creates and sends an IPv4 Packet
57 * \param Iface Interface
58 * \param Address Destination IP
59 * \param Protocol Protocol ID
60 * \param ID Some random ID number
61 * \param Length Data Length
62 * \param Data Packet Data
63 * \return Boolean Success
65 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, tIPStackBuffer *Buffer)
71 length = IPStack_Buffer_GetLength(Buffer);
73 // --- Resolve destination MAC address
74 to = ARP_Resolve4(Iface, Address);
75 if( MAC_EQU(to, cMAC_ZERO) ) {
77 Log_Notice("IPv4", "No route to host %i.%i.%i.%i",
78 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
82 // --- Handle OUTPUT firewall rules
83 // TODO: Update firewall rules for tIPStackBuffer
85 int ret = IPTables_TestChain("OUTPUT",
86 4, (tIPv4*)Iface->Address, &Address,
90 // Just drop it (with an error)
91 Log_Notice("IPv4", "Firewall dropped packet");
96 // --- Initialise header
98 hdr.HeaderLength = sizeof(tIPv4Header)/4;
99 hdr.DiffServices = 0; // TODO: Check
102 hdr.DontFragment = 0;
103 hdr.MoreFragments = 0;
107 hdr.TotalLength = htons( sizeof(tIPv4Header) + length );
108 hdr.Identifcation = htons( ID ); // TODO: Check
109 hdr.TTL = DEFAULT_TTL;
110 hdr.Protocol = Protocol;
111 hdr.HeaderChecksum = 0; // Will be set later
112 hdr.Source = *(tIPv4*)Iface->Address;
113 hdr.Destination = Address;
115 // Actually set checksum (zeroed above)
116 hdr.HeaderChecksum = htons( IPv4_Checksum(&hdr, sizeof(tIPv4Header)) );
118 IPStack_Buffer_AppendSubBuffer(Buffer, sizeof(tIPv4Header), 0, &hdr, NULL, NULL);
120 Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
121 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
122 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, Buffer);
127 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
128 * \brief Process an IPv4 Packet
130 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
132 tIPv4Header *hdr = Buffer;
138 if(Length < sizeof(tIPv4Header)) return;
141 //Log_Log("IPv4", "Version = %i", hdr->Version);
142 //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
143 //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
144 Log_Debug("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
145 //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
146 //Log_Log("IPv4", "TTL = %i", hdr->TTL );
147 Log_Debug("IPv4", "Protocol = %i", hdr->Protocol );
148 //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
149 Log_Debug("IPv4", "Source = %i.%i.%i.%i",
150 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
151 Log_Debug("IPv4", "Destination = %i.%i.%i.%i",
152 hdr->Destination.B[0], hdr->Destination.B[1],
153 hdr->Destination.B[2], hdr->Destination.B[3] );
156 // Check that the version IS IPv4
157 if(hdr->Version != 4) {
158 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
162 // Check Header checksum
164 Uint16 hdrVal, compVal;
165 hdrVal = ntohs(hdr->HeaderChecksum);
166 hdr->HeaderChecksum = 0;
167 compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4);
168 if(hdrVal != compVal) {
169 Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
172 hdr->HeaderChecksum = hdrVal;
175 // Check Packet length
176 if( ntohs(hdr->TotalLength) > Length) {
177 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
181 // TODO: Handle packet fragmentation
184 Log_Debug("IPv4", " From %i.%i.%i.%i to %i.%i.%i.%i",
185 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3],
186 hdr->Destination.B[0], hdr->Destination.B[1], hdr->Destination.B[2], hdr->Destination.B[3]
189 // Get Data and Data Length
190 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
191 data = &hdr->Options[0];
193 // Get Interface (allowing broadcasts)
194 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
199 ret = IPTables_TestChain("INPUT",
200 4, &hdr->Source, &hdr->Destination,
207 ret = IPTables_TestChain("FORWARD",
208 4, &hdr->Source, &hdr->Destination,
219 Log_Debug("IPv4", "Silently dropping packet");
224 // Unknown, silent drop
226 Log_Warning("IPv4", "Unknown firewall rule");
236 Log_Debug("IPv4", "Route the packet");
237 // Drop the packet if the TTL is zero
238 if( hdr->TTL == 0 ) {
239 Log_Warning("IPv4", "TODO: Send ICMP-Timeout when TTL exceeded");
245 rt = IPStack_FindRoute(4, NULL, &hdr->Destination); // Get the route (gets the interface)
246 if( !rt || !rt->Interface )
248 to = ARP_Resolve4(rt->Interface, hdr->Destination); // Resolve address
249 if( MAC_EQU(to, cMAC_ZERO) )
253 Log_Log("IPv4", "Forwarding packet to %i.%i.%i.%i (via %i.%i.%i.%i)",
254 hdr->Destination.B[0], hdr->Destination.B[1],
255 hdr->Destination.B[2], hdr->Destination.B[3],
256 ((tIPv4*)rt->NextHop)->B[0], ((tIPv4*)rt->NextHop)->B[1],
257 ((tIPv4*)rt->NextHop)->B[2], ((tIPv4*)rt->NextHop)->B[3]);
258 Log_Warning("IPv4", "TODO: Implement forwarding with tIPStackBuffer");
259 // Link_SendPacket(rt->Interface->Adapter, IPV4_ETHERNET_ID, to, Length, Buffer);
265 if( !gaIPv4_Callbacks[hdr->Protocol] ) {
266 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
270 gaIPv4_Callbacks[hdr->Protocol]( iface, &hdr->Source, dataLength, data );
274 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
275 * \brief Searches an adapter for a matching address
276 * \param Adapter Incoming Adapter
277 * \param Address Destination Address
278 * \param Broadcast Allow broadcast packets
280 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
282 tInterface *iface = NULL;
286 ENTER("pAdapter xAddress bBroadcast", Adapter, Address, Broadcast);
288 addr = ntohl( Address.L );
289 LOG("addr = 0x%x", addr);
291 for( iface = gIP_Interfaces; iface; iface = iface->Next)
293 if( iface->Adapter != Adapter ) continue;
294 if( iface->Type != 4 ) continue;
295 if( IP4_EQU(Address, *(tIPv4*)iface->Address) ) {
301 if( !Broadcast ) continue;
303 // Check for broadcast
304 this = ntohl( ((tIPv4*)iface->Address)->L );
305 netmask = IPv4_Netmask(iface->SubnetBits);
306 LOG("iface addr = 0x%x, netmask = 0x%x (bits = %i)", this, netmask, iface->SubnetBits);
308 if( (addr & netmask) == (this & netmask) && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
310 LOG("Broadcast match");
320 * \brief Convert a network prefix to a netmask
321 * \param FixedBits Netmask size (/n)
323 * For example /24 will become 255.255.255.0 (0xFFFFFF00)
325 Uint32 IPv4_Netmask(int FixedBits)
327 Uint32 ret = 0xFFFFFFFF;
332 ret >>= (32-FixedBits);
333 ret <<= (32-FixedBits);
335 // Returns a native endian netmask
340 * \brief Calculate the IPv4 Checksum
341 * \param Buf Input buffer
342 * \param Size Size of input
344 * One's complement sum of all 16-bit words (bitwise inverted)
346 Uint16 IPv4_Checksum(const void *Buf, size_t Length)
348 const Uint16 *words = Buf;
352 // Sum all whole words
353 for(i = 0; i < Length/2; i++ )
355 sum += ntohs(words[i]);
358 sum += ntohs( words[i] & 0xFF );
360 // Apply one's complement
362 sum = (sum & 0xFFFF) + (sum >> 16);
368 * \brief Sends an ICMP Echo and waits for a reply
369 * \param IFace Interface
370 * \param Addr Destination address
372 int IPv4_Ping(tInterface *IFace, tIPv4 Addr)
374 return ICMP_Ping(IFace, Addr);