3 * - IPv4 Protcol Handling
10 #define DEFAULT_TTL 32
13 extern tInterface *gIP_Interfaces;
14 extern void ICMP_Initialise();
15 extern int ICMP_Ping(tInterface *Interface, tIPv4 Addr);
16 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
19 int IPv4_Initialise();
20 int IPv4_RegisterCallback(int ID, tIPCallback Callback);
21 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
22 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
23 Uint32 IPv4_Netmask(int FixedBits);
24 Uint16 IPv4_Checksum(const void *Buf, int Size);
25 int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
28 tIPCallback gaIPv4_Callbacks[256];
32 * \brief Initialise the IPv4 Code
37 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
42 * \brief Registers a callback
43 * \param ID 8-bit packet type ID
44 * \param Callback Callback function
46 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
48 if( ID < 0 || ID > 255 ) return 0;
49 if( gaIPv4_Callbacks[ID] ) return 0;
50 gaIPv4_Callbacks[ID] = Callback;
55 * \brief Creates and sends an IPv4 Packet
56 * \param Iface Interface
57 * \param Address Destination IP
58 * \param Protocol Protocol ID
59 * \param ID Some random ID number
60 * \param Length Data Length
61 * \param Data Packet Data
62 * \return Boolean Success
64 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data)
66 tMacAddr to = ARP_Resolve4(Iface, Address);
67 const tMacAddr zero = {{0,0,0,0,0,0}};
68 int bufSize = sizeof(tIPv4Header) + Length;
70 tIPv4Header *hdr = (void*)buf;
73 if( MAC_EQU(to, zero) ) {
77 // OUTPUT Firewall rule go here
78 ret = IPTables_TestChain("OUTPUT",
79 4, (tIPv4*)Iface->Address, &Address,
83 // Just drop it (with an error)
87 memcpy(&hdr->Options[0], Data, Length);
89 hdr->HeaderLength = sizeof(tIPv4Header)/4;
90 hdr->DiffServices = 0; // TODO: Check
93 hdr->DontFragment = 0;
94 hdr->MoreFragments = 0;
98 hdr->TotalLength = htons( bufSize );
99 hdr->Identifcation = htons( ID ); // TODO: Check
100 hdr->TTL = DEFAULT_TTL;
101 hdr->Protocol = Protocol;
102 hdr->HeaderChecksum = 0; // Will be set later
103 hdr->Source = *(tIPv4*)Iface->Address;
104 hdr->Destination = Address;
105 hdr->HeaderChecksum = htons(IPv4_Checksum(hdr, sizeof(tIPv4Header)));
107 Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
108 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
109 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
114 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
115 * \brief Process an IPv4 Packet
117 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
119 tIPv4Header *hdr = Buffer;
125 if(Length < sizeof(tIPv4Header)) return;
128 //Log_Log("IPv4", "Version = %i", hdr->Version);
129 //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
130 //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
131 Log_Debug("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
132 //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
133 //Log_Log("IPv4", "TTL = %i", hdr->TTL );
134 Log_Debug("IPv4", "Protocol = %i", hdr->Protocol );
135 //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
136 Log_Debug("IPv4", "Source = %i.%i.%i.%i",
137 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
138 Log_Debug("IPv4", "Destination = %i.%i.%i.%i",
139 hdr->Destination.B[0], hdr->Destination.B[1],
140 hdr->Destination.B[2], hdr->Destination.B[3] );
143 // Check that the version IS IPv4
144 if(hdr->Version != 4) {
145 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
149 // Check Header checksum
151 Uint16 hdrVal, compVal;
152 hdrVal = ntohs(hdr->HeaderChecksum);
153 hdr->HeaderChecksum = 0;
154 compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4);
155 if(hdrVal != compVal) {
156 Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
159 hdr->HeaderChecksum = hdrVal;
162 // Check Packet length
163 if( ntohs(hdr->TotalLength) > Length) {
164 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
168 // TODO: Handle packet fragmentation
171 // Get Data and Data Length
172 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
173 data = &hdr->Options[0];
175 // Get Interface (allowing broadcasts)
176 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
181 ret = IPTables_TestChain("INPUT",
182 4, &hdr->Source, &hdr->Destination,
189 ret = IPTables_TestChain("FORWARD",
190 4, &hdr->Source, &hdr->Destination,
201 Log_Debug("IPv4", "Silently dropping packet");
203 // Unknown, silent drop
214 Log_Debug("IPv4", "Route the packet");
215 // Drop the packet if the TTL is zero
216 if( hdr->TTL == 0 ) {
217 Log_Warning("IPv4", "TODO: Sent ICMP-Timeout when TTL exceeded");
223 rt = IPStack_FindRoute(4, NULL, &hdr->Destination); // Get the route (gets the interface)
224 to = ARP_Resolve4(rt->Interface, hdr->Destination); // Resolve address
227 Log_Log("IPv4", "Forwarding packet to %i.%i.%i.%i (via %i.%i.%i.%i)",
228 hdr->Destination.B[0], hdr->Destination.B[1],
229 hdr->Destination.B[2], hdr->Destination.B[3],
230 ((tIPv4*)rt->NextHop)->B[0], ((tIPv4*)rt->NextHop)->B[1],
231 ((tIPv4*)rt->NextHop)->B[2], ((tIPv4*)rt->NextHop)->B[3]);
232 Link_SendPacket(rt->Interface->Adapter, IPV4_ETHERNET_ID, to, Length, Buffer);
239 if( !gaIPv4_Callbacks[hdr->Protocol] ) {
240 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
244 gaIPv4_Callbacks[hdr->Protocol]( iface, &hdr->Source, dataLength, data );
248 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
249 * \brief Searches an adapter for a matching address
250 * \param Adapter Incoming Adapter
251 * \param Address Destination Address
252 * \param Broadcast Allow broadcast packets
254 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
256 tInterface *iface = NULL;
260 addr = ntohl( Address.L );
262 for( iface = gIP_Interfaces; iface; iface = iface->Next)
264 if( iface->Adapter != Adapter ) continue;
265 if( iface->Type != 4 ) continue;
266 if( IP4_EQU(Address, *(tIPv4*)iface->Address) )
269 if( !Broadcast ) continue;
271 this = ntohl( ((tIPv4*)iface->Address)->L );
273 // Check for broadcast
274 netmask = IPv4_Netmask(iface->SubnetBits);
276 if( (addr & netmask) == (this & netmask)
277 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
284 * \brief Convert a network prefix to a netmask
285 * \param FixedBits Netmask size (/n)
287 * For example /24 will become 255.255.255.0 (0xFFFFFF00)
289 Uint32 IPv4_Netmask(int FixedBits)
291 Uint32 ret = 0xFFFFFFFF;
292 ret >>= (32-FixedBits);
293 ret <<= (32-FixedBits);
294 // Returns a native endian netmask
299 * \brief Calculate the IPv4 Checksum
300 * \param Buf Input buffer
301 * \param Size Size of input
303 * One's complement sum of all 16-bit words (bitwise inverted)
305 Uint16 IPv4_Checksum(const void *Buf, int Size)
308 const Uint16 *arr = Buf;
311 Size = (Size + 1) >> 1; // 16-bit word count
312 for(i = 0; i < Size; i++ )
314 Uint16 val = ntohs(arr[i]);
318 // Apply one's complement
320 sum = (sum & 0xFFFF) + (sum >> 16);
326 * \brief Sends an ICMP Echo and waits for a reply
327 * \param IFace Interface
328 * \param Addr Destination address
330 int IPv4_Ping(tInterface *IFace, tIPv4 Addr)
332 return ICMP_Ping(IFace, Addr);