3 * - IPv4 Protcol Handling
12 extern tInterface *gIP_Interfaces;
13 extern void ICMP_Initialise();
14 extern int ICMP_Ping(tInterface *Interface, tIPv4 Addr);
15 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
18 int IPv4_Initialise();
19 int IPv4_RegisterCallback(int ID, tIPCallback Callback);
20 void IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
21 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
22 Uint32 IPv4_Netmask(int FixedBits);
23 Uint16 IPv4_Checksum(void *Buf, int Size);
24 int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
27 tIPCallback gaIPv4_Callbacks[256];
31 * \fn int IPv4_Initialise()
36 Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
41 * \fn int IPv4_RegisterCallback( int ID, tIPCallback Callback )
42 * \brief Registers a callback
44 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
46 if( ID < 0 || ID > 255 ) return 0;
47 if( gaIPv4_Callbacks[ID] ) return 0;
48 gaIPv4_Callbacks[ID] = Callback;
53 * \brief Creates and sends an IPv4 Packet
54 * \param Iface Interface
55 * \param Address Destination IP
56 * \param Protocol Protocol ID
57 * \param ID Some random ID number
58 * \param Length Data Length
59 * \param Data Packet Data
61 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, void *Data)
63 tMacAddr to = ARP_Resolve4(Iface, Address);
64 int bufSize = sizeof(tIPv4Header) + Length;
66 tIPv4Header *hdr = (void*)buf;
68 memcpy(&hdr->Options[0], Data, Length);
70 hdr->HeaderLength = sizeof(tIPv4Header)/4;
71 hdr->DiffServices = 0; // TODO: Check
72 hdr->TotalLength = htons( bufSize );
73 hdr->Identifcation = htons( ID ); // TODO: Check
74 hdr->TTL = DEFAULT_TTL;
75 hdr->Protocol = Protocol;
76 hdr->HeaderChecksum = 0; // Will be set later
77 hdr->Source = Iface->IP4.Address;
78 hdr->Destination = Address;
79 hdr->HeaderChecksum = IPv4_Checksum(hdr, sizeof(tIPv4Header));
81 Log("[IPv4 ] Sending packet to %i.%i.%i.%i",
82 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
83 Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
88 * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
89 * \brief Process an IPv4 Packet
91 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
93 tIPv4Header *hdr = Buffer;
97 if(Length < sizeof(tIPv4Header)) return;
99 //Log("[IPv4 ] Version = %i", hdr->Version);
100 Log("[IPv4 ] HeaderLength = %i", hdr->HeaderLength);
101 Log("[IPv4 ] DiffServices = %i", hdr->DiffServices);
102 Log("[IPv4 ] TotalLength = %i", ntohs(hdr->TotalLength) );
103 //Log("[IPv4 ] Identifcation = %i", ntohs(hdr->Identifcation) );
104 //Log("[IPv4 ] TTL = %i", hdr->TTL );
105 Log("[IPv4 ] Protocol = %i", hdr->Protocol );
106 //Log("[IPv4 ] HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
107 Log("[IPv4 ] Source = %i.%i.%i.%i",
108 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
109 Log("[IPv4 ] Destination = %i.%i.%i.%i",
110 hdr->Destination.B[0], hdr->Destination.B[1],
111 hdr->Destination.B[2], hdr->Destination.B[3] );
113 // Check that the version IS IPv4
114 if(hdr->Version != 4) {
115 Log("[IPv4 ] hdr->Version(%i) != 4", hdr->Version);
119 // Check Header checksum
122 // Check Packet length
123 if( ntohs(hdr->TotalLength) > Length) {
124 Log("[IPv4 ] hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
128 // Get Interface (allowing broadcasts)
129 iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
131 Log("[IPv4 ] Ignoring Packet (Not for us)");
132 return; // Not for us? Well, let's ignore it
138 dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
139 data = &hdr->Options[0];
142 if( gaIPv4_Callbacks[hdr->Protocol] )
143 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
145 Log("[IPv4 ] Unknown Protocol %i", hdr->Protocol);
149 * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
150 * \brief Searches an adapter for a matching address
152 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
154 tInterface *iface = NULL;
158 addr = ntohl( Address.L );
160 for( iface = gIP_Interfaces; iface; iface = iface->Next)
162 if( iface->Adapter != Adapter ) continue;
163 if( iface->Type != 4 ) continue;
164 if( IP4_EQU(Address, iface->IP4.Address) )
167 if( !Broadcast ) continue;
169 this = ntohl( iface->IP4.Address.L );
171 // Check for broadcast
172 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
174 //Log("netmask = 0x%08x", netmask);
175 //Log("addr = 0x%08x", addr);
176 //Log("this = 0x%08x", this);
177 //Log("%08x == %08x && %08x == %08x",
178 // (addr & netmask), (this & netmask),
179 // (addr & ~netmask), (0xFFFFFFFF & ~netmask)
181 if( (addr & netmask) == (this & netmask)
182 && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
189 * \brief Convert a network prefix to a netmask
191 * For example /24 will become 255.255.255.0
193 Uint32 IPv4_Netmask(int FixedBits)
195 Uint32 ret = 0xFFFFFFFF;
196 ret >>= (32-FixedBits);
197 ret <<= (32-FixedBits);
198 // Returs a little endian netmask
203 * \brief Calculate the IPv4 Checksum
205 Uint16 IPv4_Checksum(void *Buf, int Size)
211 Log("IPv4_Checksum: (%p, %i)", Buf, Size);
213 Size = (Size + 1) >> 1;
214 for(i = 0; i < Size; i++ )
216 if((int)sum + arr[i] > 0xFFFF)
217 sum ++; // Simulate 1's complement
224 * \brief Sends an ICMP Echo and waits for a reply
226 int IPv4_Ping(tInterface *Iface, tIPv4 Addr)
228 return ICMP_Ping(Iface, Addr);