3 * - Address Resolution Protocol
11 #define ARP_CACHE_SIZE 64
12 #define ARP_MAX_AGE (60*60*1000) // 1Hr
15 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
17 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
22 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
23 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
32 int giARP_Cache4Space;
41 int giARP_Cache6Space;
44 int giARP_LastUpdateID = 0;
48 * \fn int ARP_Initialise()
49 * \brief Initalise the ARP section
53 gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
54 memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
55 giARP_Cache4Space = ARP_CACHE_SIZE;
58 gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
59 memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
60 giARP_Cache6Space = ARP_CACHE_SIZE;
63 Link_RegisterType(0x0806, ARP_int_GetPacket);
68 * \brief Resolves a MAC address from an IPv4 address
70 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
74 struct sArpRequest4 req;
76 ENTER("pInterface xAddress", Interface, Address);
78 // Check routing tables
79 // Replace address with gateway if needed
81 Mutex_Acquire( &glARP_Cache4 );
82 for( i = 0; i < giARP_Cache4Space; i++ )
84 if(gaARP_Cache4[i].IP.L != Address.L) continue;
86 // Check if the entry needs to be refreshed
87 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
89 Mutex_Release( &glARP_Cache4 );
90 LOG("Return %x:%x:%x:%x:%x:%x",
91 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
92 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
93 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
96 return gaARP_Cache4[i].MAC;
98 Mutex_Release( &glARP_Cache4 );
100 lastID = giARP_LastUpdateID;
103 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
104 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
106 req.HWType = htons(0x0001); // Ethernet
107 req.Type = htons(0x0800);
110 req.Request = htons(1);
111 req.SourceMac = Interface->Adapter->MacAddr;
112 req.SourceIP = *(tIPv4*)Interface->Address;
113 req.DestMac = cMAC_BROADCAST;
114 req.DestIP = Address;
117 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
122 while(lastID == giARP_LastUpdateID) Threads_Yield();
123 lastID = giARP_LastUpdateID;
125 Mutex_Acquire( &glARP_Cache4 );
126 for( i = 0; i < giARP_Cache4Space; i++ )
128 if(gaARP_Cache4[i].IP.L != Address.L) continue;
130 Mutex_Release( &glARP_Cache4 );
131 return gaARP_Cache4[i].MAC;
133 Mutex_Release( &glARP_Cache4 );
138 * \brief Updates the ARP Cache entry for an IPv4 Address
140 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
146 // Find an entry for the IP address in the cache
147 Mutex_Acquire(&glARP_Cache4);
148 for( i = giARP_Cache4Space; i--; )
150 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
153 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
154 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
156 // If there was no match, we need to make one
162 gaARP_Cache4[i].IP = SWAddr;
165 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
166 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
167 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
171 gaARP_Cache4[i].MAC = HWAddr;
172 gaARP_Cache4[i].LastUpdate = now();
173 giARP_LastUpdateID ++;
174 Mutex_Release(&glARP_Cache4);
179 * \brief Updates the ARP Cache entry for an IPv6 Address
181 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
187 // Find an entry for the MAC address in the cache
188 Mutex_Acquire(&glARP_Cache6);
189 for( i = giARP_Cache6Space; i--; )
191 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
194 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
195 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
197 // If there was no match, we need to make one
203 gaARP_Cache6[i].MAC = HWAddr;
206 gaARP_Cache6[i].IP = SWAddr;
207 gaARP_Cache6[i].LastUpdate = now();
208 giARP_LastUpdateID ++;
209 Mutex_Release(&glARP_Cache6);
214 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
215 * \brief Called when an ARP packet is recieved
217 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
219 tArpRequest4 *req4 = Buffer;
221 tArpRequest6 *req6 = Buffer;
225 // Sanity Check Packet
226 if( Length < (int)sizeof(tArpRequest4) ) {
227 Log_Log("ARP", "Recieved undersized packet");
230 if( ntohs(req4->Type) != 0x0800 ) {
231 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
234 if( req4->HWSize != 6 ) {
235 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
238 #if ARP_DETECT_SPOOFS
239 if( !MAC_EQU(req4->SourceMac, From) ) {
240 Log_Log("ARP", "ARP spoofing detected "
241 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
242 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
243 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
244 From.B[0], From.B[1], From.B[2],
245 From.B[3], From.B[4], From.B[5]
251 Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
253 switch( ntohs(req4->Request) )
255 case 1: // You want my IP?
256 Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
257 // Check what type of IP it is
258 switch( req4->SWSize )
261 Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
262 req4->SourceMac.B[0], req4->SourceMac.B[1],
263 req4->SourceMac.B[2], req4->SourceMac.B[3],
264 req4->SourceMac.B[4], req4->SourceMac.B[5]);
265 //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
266 // req4->DestMac.B[0], req4->DestMac.B[1],
267 // req4->DestMac.B[2], req4->DestMac.B[3],
268 // req4->DestMac.B[4], req4->DestMac.B[5]);
269 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
270 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
272 req4->SourceIP.B[0], req4->SourceIP.B[1],
273 req4->SourceIP.B[2], req4->SourceIP.B[3]);
274 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
277 Log_Debug("ARP", "Caching sender's IP Address");
278 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
280 req4->DestIP = req4->SourceIP;
281 req4->DestMac = req4->SourceMac;
282 req4->SourceIP = *(tIPv4*)iface->Address;;
283 req4->SourceMac = Adapter->MacAddr;
284 req4->Request = htons(2);
285 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
286 req4->SourceMac.B[0], req4->SourceMac.B[1],
287 req4->SourceMac.B[2], req4->SourceMac.B[3],
288 req4->SourceMac.B[4], req4->SourceMac.B[5]);
289 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
294 if( Length < (int)sizeof(tArpRequest6) ) {
295 Log_Log("ARP", "Recieved undersized packet (IPv6)");
298 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
299 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
300 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
302 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
305 req6->DestIP = req6->SourceIP;
306 req6->DestMac = req6->SourceMac;
307 req6->SourceIP = *(tIPv6*)iface->Address;
308 req6->SourceMac = Adapter->MacAddr;
309 req6->Request = htons(2);
310 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
311 req4->SourceMac.B[0], req4->SourceMac.B[1],
312 req4->SourceMac.B[2], req4->SourceMac.B[3],
313 req4->SourceMac.B[4], req4->SourceMac.B[5]);
314 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
319 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
325 case 2: // Ooh! A response!
326 // Check what type of IP it is
327 switch( req4->SWSize )
330 ARP_UpdateCache4( req4->SourceIP, From );
334 if( Length < (int)sizeof(tArpRequest6) ) {
335 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
338 ARP_UpdateCache6( req6->SourceIP, From );
342 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
349 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));