3 * - Address Resolution Protocol
4 * - Part of the IPv4 protocol
10 #include "ipv4.h" // For IPv4_Netmask
11 #include "include/adapters_int.h" // for MAC addr
14 #define ARP_CACHE_SIZE 64
15 #define ARP_MAX_AGE (60*60*1000) // 1Hr
18 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
20 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
25 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
26 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
35 int giARP_Cache4Space;
44 int giARP_Cache6Space;
47 volatile int giARP_LastUpdateID = 0;
51 * \fn int ARP_Initialise()
52 * \brief Initalise the ARP section
56 gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
57 memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
58 giARP_Cache4Space = ARP_CACHE_SIZE;
61 gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
62 memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
63 giARP_Cache6Space = ARP_CACHE_SIZE;
66 Link_RegisterType(0x0806, ARP_int_GetPacket);
71 * \brief Resolves a MAC address from an IPv4 address
73 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
77 struct sArpRequest4 req;
80 ENTER("pInterface xAddress", Interface, Address);
82 // Check for broadcast
87 return cMAC_BROADCAST;
90 // Check routing tables if not on this subnet
91 if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
93 tRoute *route = IPStack_FindRoute(4, Interface, &Address);
94 // If the next hop is defined, use it
95 // - 0.0.0.0 as the next hop means "no next hop / direct"
96 if( route && ((tIPv4*)route->NextHop)->L != 0 )
98 // Recursion: see /Recursion/
99 LOG("Recursing with %s", IPStack_PrintAddress(4, route->NextHop));
101 return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
103 // No route, fall though
108 // Check for broadcast
109 netmask = IPv4_Netmask(Interface->SubnetBits);
110 if( (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
112 LOG("Local Broadcast");
114 return cMAC_BROADCAST;
119 Mutex_Acquire( &glARP_Cache4 );
120 for( i = 0; i < giARP_Cache4Space; i++ )
122 if(gaARP_Cache4[i].IP.L != Address.L) continue;
124 // Check if the entry needs to be refreshed
125 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
127 Mutex_Release( &glARP_Cache4 );
128 LOG("Return %x:%x:%x:%x:%x:%x",
129 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
130 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
131 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
134 return gaARP_Cache4[i].MAC;
136 Mutex_Release( &glARP_Cache4 );
138 lastID = giARP_LastUpdateID;
141 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
142 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
144 req.HWType = htons(0x0001); // Ethernet
145 req.Type = htons(0x0800);
148 req.Request = htons(1);
149 memcpy(&req.SourceMac, Interface->Adapter->HWAddr, 6); // TODO: Remove hard size
150 req.SourceIP = *(tIPv4*)Interface->Address;
151 req.DestMac = cMAC_BROADCAST;
152 req.DestIP = Address;
154 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(3); // Assumes only a header and footer at link layer
155 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, &req, NULL, NULL);
158 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, buffer);
160 IPStack_Buffer_DestroyBuffer(buffer);
162 timeout = now() + Interface->TimeoutDelay;
167 while(lastID == giARP_LastUpdateID && now() < timeout) {
171 if( now() >= timeout ) {
172 Log_Log("ARP4", "Timeout");
176 lastID = giARP_LastUpdateID;
178 Mutex_Acquire( &glARP_Cache4 );
179 for( i = 0; i < giARP_Cache4Space; i++ )
181 if(gaARP_Cache4[i].IP.L != Address.L) continue;
183 Mutex_Release( &glARP_Cache4 );
184 Log_Debug("ARP4", "Return %02x:%02x:%02x:%02x:%02x:%02x",
185 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
186 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
187 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]);
188 return gaARP_Cache4[i].MAC;
190 Mutex_Release( &glARP_Cache4 );
193 tMacAddr ret = {{0,0,0,0,0,0}};
199 * \brief Updates the ARP Cache entry for an IPv4 Address
201 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
207 // Find an entry for the IP address in the cache
208 Mutex_Acquire(&glARP_Cache4);
209 for( i = giARP_Cache4Space; i--; )
211 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
214 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
215 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
217 // If there was no match, we need to make one
225 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
226 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
227 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
231 gaARP_Cache4[i].IP = SWAddr;
232 gaARP_Cache4[i].MAC = HWAddr;
233 gaARP_Cache4[i].LastUpdate = now();
234 giARP_LastUpdateID ++;
235 Mutex_Release(&glARP_Cache4);
240 * \brief Updates the ARP Cache entry for an IPv6 Address
242 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
248 // Find an entry for the MAC address in the cache
249 Mutex_Acquire(&glARP_Cache6);
250 for( i = giARP_Cache6Space; i--; )
252 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
255 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
256 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
258 // If there was no match, we need to make one
264 gaARP_Cache6[i].MAC = HWAddr;
267 gaARP_Cache6[i].IP = SWAddr;
268 gaARP_Cache6[i].LastUpdate = now();
269 giARP_LastUpdateID ++;
270 Mutex_Release(&glARP_Cache6);
275 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
276 * \brief Called when an ARP packet is recieved
278 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
280 tArpRequest4 *req4 = Buffer;
282 tArpRequest6 *req6 = Buffer;
286 // Sanity Check Packet
287 if( Length < (int)sizeof(tArpRequest4) ) {
288 Log_Log("ARP", "Recieved undersized packet");
291 if( ntohs(req4->Type) != 0x0800 ) {
292 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
295 if( req4->HWSize != 6 ) {
296 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
299 #if ARP_DETECT_SPOOFS
300 if( !MAC_EQU(req4->SourceMac, From) ) {
301 Log_Log("ARP", "ARP spoofing detected "
302 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
303 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
304 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
305 From.B[0], From.B[1], From.B[2],
306 From.B[3], From.B[4], From.B[5]
312 switch( ntohs(req4->Request) )
314 case 1: // You want my IP?
315 // Check what type of IP it is
316 switch( req4->SWSize )
319 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
320 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
322 req4->SourceIP.B[0], req4->SourceIP.B[1],
323 req4->SourceIP.B[2], req4->SourceIP.B[3]);
324 Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
325 req4->SourceMac.B[0], req4->SourceMac.B[1],
326 req4->SourceMac.B[2], req4->SourceMac.B[3],
327 req4->SourceMac.B[4], req4->SourceMac.B[5]);
328 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
331 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
333 req4->DestIP = req4->SourceIP;
334 req4->DestMac = req4->SourceMac;
335 req4->SourceIP = *(tIPv4*)iface->Address;;
336 memcpy(&req4->SourceMac, Adapter->HWAddr, 6); // TODO: Remove hard size
337 req4->Request = htons(2);
338 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
339 req4->SourceMac.B[0], req4->SourceMac.B[1],
340 req4->SourceMac.B[2], req4->SourceMac.B[3],
341 req4->SourceMac.B[4], req4->SourceMac.B[5]);
343 // Assumes only a header and footer at link layer
344 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(3);
345 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, req4, NULL, NULL);
346 Link_SendPacket(Adapter, 0x0806, req4->DestMac, buffer);
347 IPStack_Buffer_DestroyBuffer(buffer);
352 if( Length < (int)sizeof(tArpRequest6) ) {
353 Log_Log("ARP", "Recieved undersized packet (IPv6)");
356 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
357 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
358 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
360 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
363 req6->DestIP = req6->SourceIP;
364 req6->DestMac = req6->SourceMac;
365 req6->SourceIP = *(tIPv6*)iface->Address;
366 req6->SourceMac = Adapter->MacAddr;
367 req6->Request = htons(2);
368 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
369 req4->SourceMac.B[0], req4->SourceMac.B[1],
370 req4->SourceMac.B[2], req4->SourceMac.B[3],
371 req4->SourceMac.B[4], req4->SourceMac.B[5]);
372 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
377 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
383 case 2: // Ooh! A response!
384 // Check what type of IP it is
385 switch( req4->SWSize )
388 ARP_UpdateCache4( req4->SourceIP, From );
392 if( Length < (int)sizeof(tArpRequest6) ) {
393 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
396 ARP_UpdateCache6( req6->SourceIP, From );
400 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
407 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));