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 = IPTablesV4_TestChain("OUTPUT",
79 (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 = IPTablesV4_TestChain("INPUT",
182 &hdr->Source, &hdr->Destination,
189 ret = IPTablesV4_TestChain("FORWARD",
190 &hdr->Source, &hdr->Destination,
201 Log_Debug("IPv4", "Silently dropping packet");
203 // Unknown, silent drop
211 //Log_Debug("IPv4", "Route the packet");
213 // TODO: Parse Routing tables and determine where to send it
219 if( gaIPv4_Callbacks[hdr->Protocol] )
220 gaIPv4_Callbacks[hdr->Protocol]( iface, &hdr->Source, dataLength, data );
222 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
226 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
227 * \brief Searches an adapter for a matching address
228 * \param Adapter Incoming Adapter
229 * \param Address Destination Address
230 * \param Broadcast Allow broadcast packets
232 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
234 tInterface *iface = NULL;
238 addr = ntohl( Address.L );
240 for( iface = gIP_Interfaces; iface; iface = iface->Next)
242 if( iface->Adapter != Adapter ) continue;
243 if( iface->Type != 4 ) continue;
244 //Log_Debug("IPv4", "%x == %x?\n", addr, ntohl(((tIPv4*)iface->Address)->L));
245 if( IP4_EQU(Address, *(tIPv4*)iface->Address) )
248 if( !Broadcast ) continue;
250 this = ntohl( ((tIPv4*)iface->Address)->L );
252 // Check for broadcast
253 netmask = IPv4_Netmask(iface->SubnetBits);
255 if( (addr & netmask) == (this & netmask)
256 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
263 * \brief Convert a network prefix to a netmask
264 * \param FixedBits Netmask size (/n)
266 * For example /24 will become 255.255.255.0
268 Uint32 IPv4_Netmask(int FixedBits)
270 Uint32 ret = 0xFFFFFFFF;
271 ret >>= (32-FixedBits);
272 ret <<= (32-FixedBits);
273 // Returns a native endian netmask
278 * \brief Calculate the IPv4 Checksum
279 * \param Buf Input buffer
280 * \param Size Size of input
282 * One's complement sum of all 16-bit words (bitwise inverted)
284 Uint16 IPv4_Checksum(const void *Buf, int Size)
287 const Uint16 *arr = Buf;
290 Size = (Size + 1) >> 1; // 16-bit word count
291 for(i = 0; i < Size; i++ )
293 Uint16 val = ntohs(arr[i]);
297 // Apply one's complement
299 sum = (sum & 0xFFFF) + (sum >> 16);
305 * \brief Sends an ICMP Echo and waits for a reply
306 * \param IFace Interface
307 * \param Addr Destination address
309 int IPv4_Ping(tInterface *IFace, tIPv4 Addr)
311 return ICMP_Ping(IFace, Addr);