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 int bufSize = sizeof(tIPv4Header) + Length;
69 tIPv4Header *hdr = (void*)buf;
72 // OUTPUT Firewall rule go here
73 ret = IPTablesV4_TestChain("OUTPUT",
74 (tIPv4*)Iface->Address, &Address,
78 // Just drop it (with an error)
82 memcpy(&hdr->Options[0], Data, Length);
84 hdr->HeaderLength = sizeof(tIPv4Header)/4;
85 hdr->DiffServices = 0; // TODO: Check
88 hdr->DontFragment = 0;
89 hdr->MoreFragments = 0;
93 hdr->TotalLength = htons( bufSize );
94 hdr->Identifcation = htons( ID ); // TODO: Check
95 hdr->TTL = DEFAULT_TTL;
96 hdr->Protocol = Protocol;
97 hdr->HeaderChecksum = 0; // Will be set later
98 hdr->Source = *(tIPv4*)Iface->Address;
99 hdr->Destination = Address;
100 hdr->HeaderChecksum = IPv4_Checksum(hdr, sizeof(tIPv4Header));
102 Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
103 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
104 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
109 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
110 * \brief Process an IPv4 Packet
112 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
114 tIPv4Header *hdr = Buffer;
120 if(Length < sizeof(tIPv4Header)) return;
123 //Log_Log("IPv4", "Version = %i", hdr->Version);
124 //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
125 //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
126 Log_Debug("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
127 //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
128 //Log_Log("IPv4", "TTL = %i", hdr->TTL );
129 Log_Debug("IPv4", "Protocol = %i", hdr->Protocol );
130 //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
131 Log_Debug("IPv4", "Source = %i.%i.%i.%i",
132 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
133 Log_Debug("IPv4", "Destination = %i.%i.%i.%i",
134 hdr->Destination.B[0], hdr->Destination.B[1],
135 hdr->Destination.B[2], hdr->Destination.B[3] );
138 // Check that the version IS IPv4
139 if(hdr->Version != 4) {
140 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
144 // Check Header checksum
146 Uint16 hdrVal, compVal;
147 hdrVal = hdr->HeaderChecksum;
148 hdr->HeaderChecksum = 0;
149 compVal = IPv4_Checksum(hdr, hdr->HeaderLength);
150 if(hdrVal != compVal) {
151 Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
154 hdr->HeaderChecksum = hdrVal;
157 // Check Packet length
158 if( ntohs(hdr->TotalLength) > Length) {
159 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
163 // TODO: Handle packet fragmentation
166 // Get Data and Data Length
167 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
168 data = &hdr->Options[0];
170 // Get Interface (allowing broadcasts)
171 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
176 ret = IPTablesV4_TestChain("INPUT",
177 &hdr->Source, &hdr->Destination,
184 ret = IPTablesV4_TestChain("FORWARD",
185 &hdr->Source, &hdr->Destination,
196 Log_Debug("IPv4", "Silently dropping packet");
198 // Unknown, silent drop
206 Log_Debug("IPv4", "Route the packet");
208 // TODO: Parse Routing tables and determine where to send it
214 if( gaIPv4_Callbacks[hdr->Protocol] )
215 gaIPv4_Callbacks[hdr->Protocol]( iface, &hdr->Source, dataLength, data );
217 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
221 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
222 * \brief Searches an adapter for a matching address
223 * \param Adapter Incoming Adapter
224 * \param Address Destination Address
225 * \param Broadcast Allow broadcast packets
227 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
229 tInterface *iface = NULL;
233 addr = ntohl( Address.L );
235 for( iface = gIP_Interfaces; iface; iface = iface->Next)
237 if( iface->Adapter != Adapter ) continue;
238 if( iface->Type != 4 ) continue;
239 if( IP4_EQU(Address, *(tIPv4*)iface->Address) )
242 if( !Broadcast ) continue;
244 this = ntohl( ((tIPv4*)iface->Address)->L );
246 // Check for broadcast
247 netmask = IPv4_Netmask(iface->SubnetBits);
249 if( (addr & netmask) == (this & netmask)
250 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
257 * \brief Convert a network prefix to a netmask
258 * \param FixedBits Netmask size (/n)
260 * For example /24 will become 255.255.255.0
262 Uint32 IPv4_Netmask(int FixedBits)
264 Uint32 ret = 0xFFFFFFFF;
265 ret >>= (32-FixedBits);
266 ret <<= (32-FixedBits);
267 // Returns a native endian netmask
272 * \brief Calculate the IPv4 Checksum
273 * \param Buf Input buffer
274 * \param Size Size of input
276 * One's complement sum of all 16-bit words (bitwise inverted)
278 Uint16 IPv4_Checksum(const void *Buf, int Size)
281 const Uint16 *arr = Buf;
284 Size = (Size + 1) >> 1; // 16-bit word count
285 for(i = 0; i < Size; i++ )
287 if((int)sum + arr[i] > 0xFFFF)
288 sum ++; // Simulate 1's complement
295 * \brief Sends an ICMP Echo and waits for a reply
296 * \param IFace Interface
297 * \param Addr Destination address
299 int IPv4_Ping(tInterface *IFace, tIPv4 Addr)
301 return ICMP_Ping(IFace, Addr);