3 * - Address Resolution Protocol
4 * - Part of the IPv4 protocol
10 #include "ipv4.h" // For IPv4_Netmask
13 #define ARP_CACHE_SIZE 64
14 #define ARP_MAX_AGE (60*60*1000) // 1Hr
17 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
19 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
24 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
25 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
34 int giARP_Cache4Space;
43 int giARP_Cache6Space;
46 volatile int giARP_LastUpdateID = 0;
50 * \fn int ARP_Initialise()
51 * \brief Initalise the ARP section
55 gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
56 memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
57 giARP_Cache4Space = ARP_CACHE_SIZE;
60 gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
61 memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
62 giARP_Cache6Space = ARP_CACHE_SIZE;
65 Link_RegisterType(0x0806, ARP_int_GetPacket);
70 * \brief Resolves a MAC address from an IPv4 address
72 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
76 struct sArpRequest4 req;
79 ENTER("pInterface xAddress", Interface, Address);
81 // Check for broadcast
86 return cMAC_BROADCAST;
89 // Check routing tables if not on this subnet
90 if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
92 tRoute *route = IPStack_FindRoute(4, Interface, &Address);
93 // If the next hop is defined, use it
94 // - 0.0.0.0 as the next hop means "no next hop / direct"
95 if( route && ((tIPv4*)route->NextHop)->L != 0 )
97 // Recursion: see /Recursion/
98 LOG("Recursing with %s", IPStack_PrintAddress(4, route->NextHop));
100 return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
102 // No route, fall though
107 // Check for broadcast
108 netmask = IPv4_Netmask(Interface->SubnetBits);
109 if( (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
111 LOG("Local Broadcast");
113 return cMAC_BROADCAST;
118 Mutex_Acquire( &glARP_Cache4 );
119 for( i = 0; i < giARP_Cache4Space; i++ )
121 if(gaARP_Cache4[i].IP.L != Address.L) continue;
123 // Check if the entry needs to be refreshed
124 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
126 Mutex_Release( &glARP_Cache4 );
127 LOG("Return %x:%x:%x:%x:%x:%x",
128 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
129 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
130 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
133 return gaARP_Cache4[i].MAC;
135 Mutex_Release( &glARP_Cache4 );
137 lastID = giARP_LastUpdateID;
140 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
141 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
143 req.HWType = htons(0x0001); // Ethernet
144 req.Type = htons(0x0800);
147 req.Request = htons(1);
148 req.SourceMac = Interface->Adapter->MacAddr;
149 req.SourceIP = *(tIPv4*)Interface->Address;
150 req.DestMac = cMAC_BROADCAST;
151 req.DestIP = Address;
153 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(3); // Assumes only a header and footer at link layer
154 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, &req, NULL, NULL);
157 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, buffer);
159 IPStack_Buffer_DestroyBuffer(buffer);
161 timeout = now() + Interface->TimeoutDelay;
166 while(lastID == giARP_LastUpdateID && now() < timeout) {
170 if( now() >= timeout ) {
171 Log_Log("ARP4", "Timeout");
175 lastID = giARP_LastUpdateID;
177 Mutex_Acquire( &glARP_Cache4 );
178 for( i = 0; i < giARP_Cache4Space; i++ )
180 if(gaARP_Cache4[i].IP.L != Address.L) continue;
182 Mutex_Release( &glARP_Cache4 );
183 Log_Debug("ARP4", "Return %02x:%02x:%02x:%02x:%02x:%02x",
184 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
185 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
186 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]);
187 return gaARP_Cache4[i].MAC;
189 Mutex_Release( &glARP_Cache4 );
192 tMacAddr ret = {{0,0,0,0,0,0}};
198 * \brief Updates the ARP Cache entry for an IPv4 Address
200 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
206 // Find an entry for the IP address in the cache
207 Mutex_Acquire(&glARP_Cache4);
208 for( i = giARP_Cache4Space; i--; )
210 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
213 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
214 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
216 // If there was no match, we need to make one
224 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
225 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
226 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
230 gaARP_Cache4[i].IP = SWAddr;
231 gaARP_Cache4[i].MAC = HWAddr;
232 gaARP_Cache4[i].LastUpdate = now();
233 giARP_LastUpdateID ++;
234 Mutex_Release(&glARP_Cache4);
239 * \brief Updates the ARP Cache entry for an IPv6 Address
241 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
247 // Find an entry for the MAC address in the cache
248 Mutex_Acquire(&glARP_Cache6);
249 for( i = giARP_Cache6Space; i--; )
251 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
254 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
255 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
257 // If there was no match, we need to make one
263 gaARP_Cache6[i].MAC = HWAddr;
266 gaARP_Cache6[i].IP = SWAddr;
267 gaARP_Cache6[i].LastUpdate = now();
268 giARP_LastUpdateID ++;
269 Mutex_Release(&glARP_Cache6);
274 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
275 * \brief Called when an ARP packet is recieved
277 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
279 tArpRequest4 *req4 = Buffer;
281 tArpRequest6 *req6 = Buffer;
285 // Sanity Check Packet
286 if( Length < (int)sizeof(tArpRequest4) ) {
287 Log_Log("ARP", "Recieved undersized packet");
290 if( ntohs(req4->Type) != 0x0800 ) {
291 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
294 if( req4->HWSize != 6 ) {
295 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
298 #if ARP_DETECT_SPOOFS
299 if( !MAC_EQU(req4->SourceMac, From) ) {
300 Log_Log("ARP", "ARP spoofing detected "
301 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
302 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
303 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
304 From.B[0], From.B[1], From.B[2],
305 From.B[3], From.B[4], From.B[5]
311 switch( ntohs(req4->Request) )
313 case 1: // You want my IP?
314 // Check what type of IP it is
315 switch( req4->SWSize )
318 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
319 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
321 req4->SourceIP.B[0], req4->SourceIP.B[1],
322 req4->SourceIP.B[2], req4->SourceIP.B[3]);
323 Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
324 req4->SourceMac.B[0], req4->SourceMac.B[1],
325 req4->SourceMac.B[2], req4->SourceMac.B[3],
326 req4->SourceMac.B[4], req4->SourceMac.B[5]);
327 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
330 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
332 req4->DestIP = req4->SourceIP;
333 req4->DestMac = req4->SourceMac;
334 req4->SourceIP = *(tIPv4*)iface->Address;;
335 req4->SourceMac = Adapter->MacAddr;
336 req4->Request = htons(2);
337 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
338 req4->SourceMac.B[0], req4->SourceMac.B[1],
339 req4->SourceMac.B[2], req4->SourceMac.B[3],
340 req4->SourceMac.B[4], req4->SourceMac.B[5]);
342 // Assumes only a header and footer at link layer
343 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(3);
344 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, &req4, NULL, NULL);
345 Link_SendPacket(Adapter, 0x0806, req4->DestMac, buffer);
346 IPStack_Buffer_DestroyBuffer(buffer);
351 if( Length < (int)sizeof(tArpRequest6) ) {
352 Log_Log("ARP", "Recieved undersized packet (IPv6)");
355 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
356 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
357 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
359 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
362 req6->DestIP = req6->SourceIP;
363 req6->DestMac = req6->SourceMac;
364 req6->SourceIP = *(tIPv6*)iface->Address;
365 req6->SourceMac = Adapter->MacAddr;
366 req6->Request = htons(2);
367 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
368 req4->SourceMac.B[0], req4->SourceMac.B[1],
369 req4->SourceMac.B[2], req4->SourceMac.B[3],
370 req4->SourceMac.B[4], req4->SourceMac.B[5]);
371 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
376 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
382 case 2: // Ooh! A response!
383 // Check what type of IP it is
384 switch( req4->SWSize )
387 ARP_UpdateCache4( req4->SourceIP, From );
391 if( Length < (int)sizeof(tArpRequest6) ) {
392 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
395 ARP_UpdateCache6( req6->SourceIP, From );
399 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
406 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));