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 * \fn int IPv4_Initialise()
37 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
42 * \fn int IPv4_RegisterCallback( int ID, tIPCallback Callback )
43 * \brief Registers a callback
45 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
47 if( ID < 0 || ID > 255 ) return 0;
48 if( gaIPv4_Callbacks[ID] ) return 0;
49 gaIPv4_Callbacks[ID] = Callback;
54 * \brief Creates and sends an IPv4 Packet
55 * \param Iface Interface
56 * \param Address Destination IP
57 * \param Protocol Protocol ID
58 * \param ID Some random ID number
59 * \param Length Data Length
60 * \param Data Packet Data
62 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data)
64 tMacAddr to = ARP_Resolve4(Iface, Address);
65 int bufSize = sizeof(tIPv4Header) + Length;
67 tIPv4Header *hdr = (void*)buf;
70 // TODO: OUTPUT Firewall rule go here
71 ret = IPTablesV4_TestChain("OUTPUT",
72 &Iface->IP4.Address, &Address,
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 = Iface->IP4.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_Log("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_Log("IPv4", "Protocol = %i", hdr->Protocol );
130 //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
131 Log_Log("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_Log("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
147 // Check Packet length
148 if( ntohs(hdr->TotalLength) > Length) {
149 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
154 // TODO: Handle packet fragmentation
156 // Get Data and Data Length
157 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
158 data = &hdr->Options[0];
160 // Get Interface (allowing broadcasts)
161 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
163 // TODO: INPUT Firewall Rule
165 ret = IPTablesV4_TestChain("INPUT",
166 &hdr->Source, &hdr->Destination,
172 ret = IPTablesV4_TestChain("FORWARD",
173 &hdr->Source, &hdr->Destination,
184 // Unknown, silent drop
192 Log_Log("IPv4", "Route the packet");
194 // TODO: Parse Routing tables and determine where to send it
200 if( gaIPv4_Callbacks[hdr->Protocol] )
201 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
203 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
207 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
208 * \brief Searches an adapter for a matching address
210 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
212 tInterface *iface = NULL;
216 addr = ntohl( Address.L );
218 for( iface = gIP_Interfaces; iface; iface = iface->Next)
220 if( iface->Adapter != Adapter ) continue;
221 if( iface->Type != 4 ) continue;
222 if( IP4_EQU(Address, iface->IP4.Address) )
225 if( !Broadcast ) continue;
227 this = ntohl( iface->IP4.Address.L );
229 // Check for broadcast
230 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
232 if( (addr & netmask) == (this & netmask)
233 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
240 * \brief Convert a network prefix to a netmask
242 * For example /24 will become 255.255.255.0
244 Uint32 IPv4_Netmask(int FixedBits)
246 Uint32 ret = 0xFFFFFFFF;
247 ret >>= (32-FixedBits);
248 ret <<= (32-FixedBits);
249 // Returns a native endian netmask
254 * \brief Calculate the IPv4 Checksum
256 Uint16 IPv4_Checksum(const void *Buf, int Size)
259 const Uint16 *arr = Buf;
262 Size = (Size + 1) >> 1; // 16-bit word count
263 for(i = 0; i < Size; i++ )
265 if((int)sum + arr[i] > 0xFFFF)
266 sum ++; // Simulate 1's complement
273 * \brief Sends an ICMP Echo and waits for a reply
275 int IPv4_Ping(tInterface *Iface, tIPv4 Addr)
277 return ICMP_Ping(Iface, Addr);